Python Forum
get docstring to work with another block of text at the top
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get docstring to work with another block of text at the top
#1
i want to assign the text of the license for my script to a variable. and i want this to be the first block of text one sees when slowly reading the file. but if i put this assignment first, then the docstring will not be set up. is there a way around this conflict?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
What about
"""LICENSE BLAH BLAH BLAH"""
license = __doc__
The only issue that I see is that you're misappropriating the docstring, but this can be corrected by
"""LICENSE BLAH BLAH BLAH"""
license = __doc__
__doc__ = """ACTUAL DOCSTRING"""
I don't see a way to avoid the """ before the text of the license.

This could also potentially confuse programs that read docstrings in order to generate automatic documentation for your modules.
Reply
#3
i'm wanting to put the help text in the docstring and have the script output that if help is requested. but i don't want it to output the license unless requested.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
If you assign directly to __doc__, it will work without problems. The only issue that you can have is IF an external automated tool extracts docstrings without importing the modules, that is to say literaly, then this tool may miss the fact that you are redefining the docstring. But for other tools such as the built-in help or pydoc or any tool that imports the module before extracting the docstring will work perfectly.
Reply
#5
i'm going to do this. everything will be in the docstring. it will start with just one to a few lines describing basically what the script is for. fewer lines is better. just one line will be typical. each part that follows will be led by a blank line, then a line with the name of that part appended with ":", then a blank line. the license part will be first. i will include a function to get the requested part from the docstring.

i'm wondering if i can assign to __docstring__. if i can then maybe i can use a f-string (not allowed on regular docstring).

wow, we were typing in the same time, you slightly ahead of me.

this example for POSIX systems:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""This script says hello to the world.

license:

This script is in the public domain.

info:

script          helloworld
file            helloworld.py
author          Phil Howard
email           11054987560151472272755686915985840251291393453694611309
hint            provu igi la numeron al duuma

help:

Run this script to see it say "Hello World".  Make a copy of file helloworld.py to another file name you prefer and use your favorite text editor to change it to be your first Python script.
"""
print("Hello World")
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  a longer docstring Skaperen 8 1,595 Aug-25-2022, 11:21 PM
Last Post: Skaperen
  How to write in text file - indented block Joni_Engr 4 6,363 Jul-18-2022, 09:09 AM
Last Post: Hathemand
  Getting a GET request output text into a variable to work with it. LeoT 2 2,888 Feb-24-2021, 02:05 PM
Last Post: LeoT
  calling a method for a docstring Skaperen 8 2,946 Jan-27-2020, 01:16 AM
Last Post: Skaperen
  capture next block of text after finding error in file kdefilip2 6 3,340 Nov-27-2019, 07:36 PM
Last Post: buran
  How work with formatted text in Python? AlekseyPython 3 2,774 Mar-18-2019, 05:00 AM
Last Post: AlekseyPython
  Strip does not work when applied on a string read from a text file. susmis666 1 2,336 Dec-27-2018, 07:07 AM
Last Post: perfringo
  ptpython docstring metulburr 4 4,420 Nov-17-2017, 01:36 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020