记录命名元组的标准方法是什么?

时间:2016-08-23 23:22:09

标签: python code-documentation

希望通过使用namedtuple来保存一些代码以保存多个变量以通过许多函数。下面是一个简化的例子(我实际上还有一些额外的参数)。

在:

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.
"""

后:

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.
"""

对于文档字符串,我一直在关注Google样式指南,添加了类型(受this post启发),我非常喜欢它,因为它可以更容易地跟踪哪些类型进来了。

我的问题是,在这种情况下你将如何记录一个命名元组?显然,正如它当前设置的那样,你没有关于namedtuple中类型的信息。是否有一种可接受的方法来扩展文档字符串,或者记录它定义的名称元组(未显示)?

我知道你可以在这个庄园里记录一个班级,但是我试图远离使用课程,因为除了掌握变量之外我没有任何其他目的。

2 个答案:

答案 0 :(得分:3)

namedtuple的Python3文档显示,可以通过将自己的字符串附加到StartService字段来自定义namedtuple的文档字符串。对于您的问题,您可以写:

__doc__

然后执行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.' 输出:

help(Sessions)

,然后在其他几行文档之后出现:

Help on class Sessions in module MyModule:

class Sessions(builtins.tuple)
|  Sessions(cass, solr, mysql): All database sessions.
|  
|  Method resolution order:
|      Sessions

|---------------------------------------------------------------------- | 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. | | ---------------------------------------------------------------------- 上自动显示的文档文本数量之多,可能很难找到您添加的特定文档。

答案 1 :(得分:1)

我不熟悉Google风格指南,但是如何:

对于一个namedtuple或元组或列表或任何可以互换的东西,我会选择这样的东西

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.
    """    

另一方面,如果我使用namedtuple的属性,那么可能是这样的

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.
    """    

对于字典,这个怎么样

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.
    """    

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.
    """   

在每个实例中,只要求函数需要正常工作的最小功能