如何在公共位置而不是每个 doc_string 中记录整个 Python3 class 中使用的字典模式?
How to document a dict schema used throughout Python3 class in a common place instead of each doc_string?
在下面给出的示例代码中,class 对多个函数使用相同的字典架构,每个函数的 doc_strings 到 冗余地记录字典的模式。
是否有任何已知约定 在 class 中的公共位置记录字典模式并因此移出个人 doc_strings?
class X:
def create(data_dict):
"""
:param <dict> create_handler: format below
a: <string - REQUIRED> ...
b: <boolean> ...
..
..
..
:return: ...
"""
def search(data_dict):
"""
:param <dict> create_handler: format below
a: <string - REQUIRED> ...
b: <boolean> ...
..
..
..
:return: ...
"""
def update(data_dict):
"""
:param < dict > create_handler: format
below
a: < string - REQUIRED > ...
b: < boolean > ...
..
..
..
:return: ...
"""
使用inspect
In [752]: docstrings = {method: method_name.__doc__ for method, method_name in inspect.getmembers(pd.DataFrame, predicate=inspect.ismethod)}
In [754]: print(docstrings['from_csv'])
Read CSV file.
.. deprecated:: 0.21.0
Use :func:`pandas.read_csv` instead.
It is preferable to use the more powerful :func:`pandas.read_csv`
for most general purposes, but ``from_csv`` makes for an easy
roundtrip to and from a file (the exact counterpart of
``to_csv``), especially with a DataFrame of time series data.
This method only differs from the preferred :func:`pandas.read_csv`
in some defaults:
- `index_col` is ``0`` instead of ``None`` (take first column as index
by default)
- `parse_dates` is ``True`` instead of ``False`` (try parsing the index
as datetime by default)
So a ``pd.DataFrame.from_csv(path)`` can be replaced by
``pd.read_csv(path, index_col=0, parse_dates=True)``.
Parameters
----------
path : string file path or file handle / StringIO
header : int, default 0
Row to use as header (skip prior rows)
sep : string, default ','
Field delimiter
index_col : int or sequence, default 0
Column to use for index. If a sequence is given, a MultiIndex
is used. Different default from read_table
parse_dates : boolean, default True
Parse dates. Different default from read_table
tupleize_cols : boolean, default False
write multi_index columns as a list of tuples (if True)
or new (expanded format) if False)
infer_datetime_format: boolean, default False
If True and `parse_dates` is True for a column, try to infer the
datetime format based on the first datetime string. If the format
can be inferred, there often will be a large parsing speed-up.
See also
--------
pandas.read_csv
Returns
-------
y : DataFrame
在下面给出的示例代码中,class 对多个函数使用相同的字典架构,每个函数的 doc_strings 到 冗余地记录字典的模式。
是否有任何已知约定 在 class 中的公共位置记录字典模式并因此移出个人 doc_strings?
class X:
def create(data_dict):
"""
:param <dict> create_handler: format below
a: <string - REQUIRED> ...
b: <boolean> ...
..
..
..
:return: ...
"""
def search(data_dict):
"""
:param <dict> create_handler: format below
a: <string - REQUIRED> ...
b: <boolean> ...
..
..
..
:return: ...
"""
def update(data_dict):
"""
:param < dict > create_handler: format
below
a: < string - REQUIRED > ...
b: < boolean > ...
..
..
..
:return: ...
"""
使用inspect
In [752]: docstrings = {method: method_name.__doc__ for method, method_name in inspect.getmembers(pd.DataFrame, predicate=inspect.ismethod)}
In [754]: print(docstrings['from_csv'])
Read CSV file.
.. deprecated:: 0.21.0
Use :func:`pandas.read_csv` instead.
It is preferable to use the more powerful :func:`pandas.read_csv`
for most general purposes, but ``from_csv`` makes for an easy
roundtrip to and from a file (the exact counterpart of
``to_csv``), especially with a DataFrame of time series data.
This method only differs from the preferred :func:`pandas.read_csv`
in some defaults:
- `index_col` is ``0`` instead of ``None`` (take first column as index
by default)
- `parse_dates` is ``True`` instead of ``False`` (try parsing the index
as datetime by default)
So a ``pd.DataFrame.from_csv(path)`` can be replaced by
``pd.read_csv(path, index_col=0, parse_dates=True)``.
Parameters
----------
path : string file path or file handle / StringIO
header : int, default 0
Row to use as header (skip prior rows)
sep : string, default ','
Field delimiter
index_col : int or sequence, default 0
Column to use for index. If a sequence is given, a MultiIndex
is used. Different default from read_table
parse_dates : boolean, default True
Parse dates. Different default from read_table
tupleize_cols : boolean, default False
write multi_index columns as a list of tuples (if True)
or new (expanded format) if False)
infer_datetime_format: boolean, default False
If True and `parse_dates` is True for a column, try to infer the
datetime format based on the first datetime string. If the format
can be inferred, there often will be a large parsing speed-up.
See also
--------
pandas.read_csv
Returns
-------
y : DataFrame