Looking to clean up some code by using a namedtuple to hold multiple variables for passing through a number of functions. Below is a simplified example (I actually have a few more additional arguments).
Before:
def my_function(session_cass, session_solr, session_mysql, some_var, another):
"""Blah blah.
Args:
session_cass (Session): Cassandra session to execute queries with.
session_solr (SolrConnection): Solr connection to execute requests with.
session_mysql (connection): MySQL connection to execute queries with.
some_var (str): Yada yada.
another (int): Yada yada.
"""
After:
def my_function(sessions, some_var, another):
"""Blah blah.
Args:
sessions (namedtuple): Holds all the database sessions.
some_var (str): Yada yada.
another (int): Yada yada.
"""
For docstrings, I've been following the Google style guide, with the addition of types (inspired by this post), which I really like because it makes it a lot easier to keep track of what types are coming in.
My question is, how would you go about documenting a namedtuple in this scenario? Obviously as it's currently set up, you have no information about the types within the namedtuple. Is there an accepted way to extend the docstring here, or document the namedtuple where it's defined (not shown)?
I know you could document a class in this manor, but I'm trying to stay away from using a class as I don't really have any purpose for it other than to hold the variables.
To create a named tuple, import the namedtuple class from the collections module. The constructor takes the name of the named tuple (which is what type() will report), and a string containing the fields names, separated by whitespace. It returns a new namedtuple class for the specified fields.
A quite common use case for named tuples is to use them to store database records. You can define namedtuple classes using the column names as field names and retrieve the data from the rows in the database to named tuples. You can also do something similar with CSV files.
NamedTuple: The NamedTuple is a class that contains the data like a dictionary format stored under the 'collections' module. It stored the data in a key-value format where each key having mapped to more values. So, we access those data using a specific key or can use the indexes.
Named tuple container datatype is an alternative to the built-in tuple . This extension type enhances standard tuples so that their elements can be accessed by both their attribute name and the positional index. Named tuples are available in Python's standard library collections module under the namedtuple utility.
The Python3 documentation for namedtuple shows that a namedtuple's docstrings can be customized by appending your own strings to the __doc__
fields. For your question, you could write:
Sessions = namedtuple('Sessions', ['cass', 'solr', 'mysql'])
Sessions.__doc__ += ': All database sessions.'
Sessions.cass.__doc__ += ': Cassandra session to execute queries with.'
Sessions.solr.__doc__ += ': Solr connection to execute requests with.'
Sessions.mysql.__doc__ += ': MySQL connection to execute requests with.'
Then executing help(Sessions)
outputs:
Help on class Sessions in module MyModule:
class Sessions(builtins.tuple)
| Sessions(cass, solr, mysql): All database sessions.
|
| Method resolution order:
| Sessions
and then after few other rows of documentation comes:
|----------------------------------------------------------------------
| Data descriptors defined here:
|
| cass
| Alias for field number 0: Cassandra session to execute queries with.
|
| solr
| Alias for field number 1: Solr connection to execute requests with.
|
| mysql
| Alias for field number 2: MySQL connection to execute requests with.
|
| ----------------------------------------------------------------------
Admittedly the amount of automatically documentation text for Sessions
can make it difficult to find specific documentation you added.
I am not familiar with Google style guide, but how about this:
for a namedtuple or tuple or list or whatever that is interchangeable I would go for something like this
def my_function(sessions, some_var, another):
"""Blah blah.
Args:
sessions (sequence): A sequence of length n that
holds all the database sessions.
In position 0 need bla bla
In position 1 need ble ble
...
In position n-1 need blu blu
some_var (str): Yada yada.
another (int): Yada yada.
"""
on the other hand if I use the attributes of the namedtuple, then maybe something like this
def my_function(sessions, some_var, another):
"""Blah blah.
Args:
sessions (object): A object that holds all the database sessions.
It need the following attributes
bla_bla is ...
ble_ble is ...
...
blu_blu is ...
some_var (str): Yada yada.
another (int): Yada yada.
"""
for a dictionary, how about this
def my_function(sessions, some_var, another):
"""Blah blah.
Args:
sessions (map): A dictionary-like object that holds all the
database sessions, it need the following keys
bla_bla is ...
ble_ble is ...
...
blu_blu is ...
some_var (str): Yada yada.
another (int): Yada yada.
"""
or
def my_function(sessions, some_var, another):
"""Blah blah.
Args:
sessions (customclass): Holds all the database sessions.
some_var (str): Yada yada.
another (int): Yada yada.
"""
In each instance just ask for the minimum functionality that the function need to work correctly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With