23
loading...
This website collects cookies to deliver better user experience
This is Day 19 of the #100DaysOfPython challenge.
pydoc
module from the standard library to to explore how we can navigate documentation as well as create our own.hello-pydoc
directory and set up the required files.# Make the `hello-pydoc` directory
$ mkdir hello-pydoc
$ cd hello-pydoc
# Create a folder to place our example files
$ mkdir src
# Create the required files
$ touch src/math.py main.py src/__init__.py
datetime
module from the command line.python -m pydoc [module]
and we should see the output based on the doc strings for each of the functions in the module.datetime
module. From the command line, we can run the following command:$ python -m pydoc datetime
Help on module datetime:
NAME
datetime - Fast implementation of the datetime type.
FILE
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/datetime.so
MODULE DOCS
https://docs.python.org/library/datetime
CLASSES
__builtin__.object
date
datetime
time
timedelta
tzinfo
class date(__builtin__.object)
| date(year, month, day) --> date object
|
| Methods defined here:
|
| __add__(...)
x.__add__(y) <==> x+y
|
| __eq__(...)
# ... reset omitted for brevity ...
datetime
module that can be traversed in the command-line.Heading | Description |
---|---|
NAME |
The name of the module. |
FILE |
The file that the module is defined in. |
MODULE DOCS |
The link to the documentation for the module. |
CLASSES |
The classes defined in the module. |
/
or ?
operator followed by a search term.Datetime in Python
, we heavily relied upon the datetime.datetime.now
function./
followed by now
and one of the search results will return the following:now(tz=None) from builtins.type
Returns new datetime object representing current time local to tz.
tz
Timezone object.
If no tz is specified, uses local timezone.
Tip: after searching, you can use the n
key to navigate to the next result.
now
function returns a datetime
object with the timezone's local time and date (with an optional argument of the timezone).from datetime import datetime, timezone
datetime.now() # datetime.datetime(2021, 8, 7, 7, 22, 37, 216899)
datetime.now(timezone.utc) # datetime.datetime(2021, 8, 6, 21, 22, 30, 841500, tzinfo=datetime.timezone.utc)
utcnow
, we can see the following:utcnow(...) from builtins.type
Return a new datetime representing UTC day and time.
datetime
object with the timezone of UTC.from datetime import datetime
datetime.utcnow() # datetime.datetime(2021, 8, 6, 21, 24, 2, 865402)
math
module.docstrings
.src/math.py
:def add(x, y):
"""add together two numbers
Args:
x (int): First number to add
y (int): Second number to add
Returns:
int: sum of x and y
"""
return x + y
docstring
is a string that is used to document the function and is denoted as the comment between lines 2 and 10 of the code above.Tip: if you use VSCode, check out the docstring generator
python -m pydoc src.math
and we should see the output of the docstring
:Help on module src.math in src:
NAME
src.math
FUNCTIONS
add(x, y)
add together two numbers
Args:
x (int): First number to add
y (int): Second number to add
Returns:
int: sum of x and y
FILE
/path/to/code/blog-projects/hello-pydoc/src/math.py
pydoc -h
, we see the following:-h
pydoc - the Python documentation tool
pydoc <name> ...
Show text documentation on something. <name> may be the name of a
Python keyword, topic, function, module, or package, or a dotted
reference to a class or function within a module or module in a
package. If <name> contains a '/', it is used as the path to a
Python source file to document. If name is 'keywords', 'topics',
or 'modules', a listing of these things is displayed.
pydoc -k <keyword>
Search for a keyword in the synopsis lines of all available modules.
pydoc -p <port>
Start an HTTP server on the given port on the local machine. Port
number 0 can be used to get an arbitrary unused port.
pydoc -g
Pop up a graphical interface for finding and serving documentation.
pydoc -w <name> ...
Write out the HTML documentation for a module to a file in the current
directory. If <name> contains a '/', it is treated as a filename; if
it names a directory, documentation is written for all the contents.
pydoc -p 4000
option to start a server on port 4000.http://localhost:4000
in your browser and explore the documentation. The interface isn't pretty, but the job gets done.-w
option to pydoc
.pydoc -w src.math
outputs HTML into src.math.html
which contains the following:<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Python: module src.math</title>
</head>
<body bgcolor="#f0f0f8">
<table
width="100%"
cellspacing="0"
cellpadding="2"
border="0"
summary="heading"
>
<tr bgcolor="#7799ee">
<td valign="bottom">
<font color="#ffffff" face="helvetica, arial"
> <big
><big
><strong
><a href="src.html"><font color="#ffffff">src</font></a
>.math</strong
></big
></big
></font
>
</td>
<td align="right" valign="bottom">
<font color="#ffffff" face="helvetica, arial"
><a
href="file:/path/to/code/blog-projects/hello-pydoc/src/math.py"
>/path/to/code/blog-projects/hello-pydoc/src/math.py</a
></font
>
</td>
</tr>
</table>
<p></p>
<p></p>
<table
width="100%"
cellspacing="0"
cellpadding="2"
border="0"
summary="section"
>
<tr bgcolor="#eeaa77">
<td colspan="3" valign="bottom">
<font color="#ffffff" face="helvetica, arial"
><big><strong>Functions</strong></big></font
>
</td>
</tr>
<tr>
<td bgcolor="#eeaa77"><tt> </tt></td>
<td> </td>
<td width="100%">
<dl>
<dt>
<a name="-add"><strong>add</strong></a
>(x, y)
</dt>
<dd>
<tt
>add together two numbers
Args:
x (int): First number to add
y (int): Second number to add
Returns:
int: sum of x and y</tt
>
</dd>
</dl>
</td>
</tr>
</table>
</body>
</html>
pydoc
package to explore built-in and custom documentation.milada_vigerova