本文共 5161 字,大约阅读时间需要 17 分钟。
In the process of making the v1.0.0 release of , I had one primary goal: to simplify and solidify the interface to git-pandas objects (the ProjectDirectory and the Repository). At the end of the day, the usefulness of a project like git-pandas versus one off analysis or rolling your own interface is consistent and predictable interfaces to commonly used functions.
在制作 v1.0.0版本的过程中,我有一个主要目标:简化和巩固git-pandas对象(ProjectDirectory和Repository)的接口。 归根结底,像git-pandas之类的项目与一次性分析或滚动自己的界面相比,其有用之处是与常用功能保持一致且可预测的界面。
So with that in mind, I was interested in the various input parameters for the functions. What I wanted to avoid was something like:
因此,我对函数的各种输入参数感兴趣。 我想避免的是这样的:
df = repo.functionA(file_extension='py')df = repo.functionB(file_ext='py')
So to quickly get an idea of where things stood, I looked to the in the standard python library. With this, we can load git-pandas into memory, find all of the classes in it, and get a dictionary of the arguments to each function. I’ve actually left this script in the git-pandas repo , if you’d like to use it, but let’s dig into it here:
因此,为了快速了解情况,我查看了标准python库中的 。 这样,我们可以将git-pandas加载到内存中,找到其中的所有类,并获取每个函数的参数字典。 其实我已经在混帐大熊猫离开了这个脚本回购 ,如果你想使用它,但让我们钻进去在这里:
The first step is to extract the objects (classes and functions are really what we are interested in here) from the module. In our case, we are just looking at those objects directly importable via “from gitpandas import foo”.
第一步是从模块中提取对象(类和函数确实是我们感兴趣的对象)。 在我们的例子中,我们只是在看那些可以通过“ from gitpandas import foo”直接导入的对象。
def extract_objects(m, classes=True, functions=False): # add in the classes at this level out = {} if classes: m_dict = {k: v for k, v in m.__dict__.items() if inspect.isclass(v)} out.update(m_dict) if functions: m_dict = {k: v for k, v in m.__dict__.items() if inspect.isfunction(v)} out.update(m_dict) return out
Here we use the dict attribute of the module to iterate through the objects stored in it, checking if they are classes or functions, and shoving them into a dictionary for further analysis.
在这里,我们使用模块的dict属性来遍历存储在模块中的对象,检查它们是类还是函数,并将它们放入字典中以进行进一步分析。
Next we need to find the arguments for each function, or function in a class:
接下来,我们需要找到每个函数或类中函数的参数:
def get_signatures(m, remove_self=True): if remove_self: excludes = ['self'] else: excludes = [] out = {} for key in m.keys(): try: for k, v in m[key].__dict__.items(): try: out[str(key) + '.' + k] = [x for x in list(inspect.getargspec(v).args) if x not in excludes] except: pass except: out[key] = [x for x in list(inspect.getargspec(m[key]).args) if x not in excludes] return out
To denote class methods, we use class.method notation. Optionally, we can exclude the ‘self’ parameter which is convention to use as the instance variable in class methods.
为了表示类方法,我们使用class.method表示法。 可选地,我们可以排除“ self”参数,该参数习惯上用作类方法中的实例变量。
Finally, we can take this dictionary of functions and arguments, and find the unique set of arguments for the module.
最后,我们可以使用此函数和参数的字典,并为模块找到唯一的参数集。
def get_distinct_params(m): out = set() for k in m.keys(): out.update(m[k]) return out
So pulling these three together, we can just do:
因此,将这三个结合在一起,我们可以做:
sigs = get_signatures(extract_objects(module))print(get_distinct_params(sigs))
And find out that git-pandas has only a handful of possible arguments:
并发现git-pandas只有少数可能的参数:
Going forward the aim will continue to be keeping this list short and logical while growing the functionality.
向前迈进的目标将是在增加功能的同时,使这份清单简短合理。
So check out the new release on PyPI, or at the source:
因此,请在PyPI上或在源代码处查看新版本:
翻译自:
转载地址:http://rrqwd.baihongyu.com/