python - 无论如何要向预先存在的函数添加关键字参数?
问题描述
我正在使用 get 函数从 Airtable 导入数据,但我需要对记录进行排序,所以无论如何要创建一个可以在 python 中对结果进行排序的 kwarg?这个想法是在github中提出的,但我无法理解
Github代码:
class SortParam(_BaseObjectArrayParam):
"""
Sort Param
Kwargs:
``sort=``
Specifies how the records will be ordered. If you set the view
parameter, the returned records in that view will be sorted by these
fields.
If sorting by multiple columns, column names can be passed as a list.
Sorting Direction is ascending by default, but can be reversed by
prefixing the column name with a minus sign ``-``, or passing
``COLUMN_NAME, DIRECTION`` tuples. Direction options
are ``asc`` and ``desc``.
Usage:
>>> airtable.get(sort='ColumnA')
Multiple Columns:
>>> airtable.get(sort=['ColumnA', '-ColumnB'])
Explicit Directions:
>>> airtable.get(sort=[('ColumnA', 'asc'), ('ColumnB', 'desc')])
Args:
fields (``str``, ``list``): Name of columns and directions.
"""
# Class Input > Output
# >>> filter = SortParam([{'field': 'col', 'direction': 'asc'}])
# >>> filter.to_param_dict()
# {'sort[0]['field']: 'col', sort[0]['direction']: 'asc'}
param_name = "sort"
kwarg = param_name
def __init__(self, value):
# Wraps string into list to avoid string iteration
if hasattr(value, "startswith"):
value = [value]
self.value = []
direction = "asc"
for item in value:
if not hasattr(item, "startswith"):
field_name, direction = item
else:
if item.startswith("-"):
direction = "desc"
field_name = item[1:]
else:
field_name = item
sort_param = {"field": field_name, "direction": direction}
self.value.append(sort_param)
解决方案
推荐阅读
- rdf - RDF 可以对带有边缘属性的标记属性图进行建模吗?
- javascript - querySelectorAll 返回的节点问题
- javascript - ASP.NET onClientClick 返回 false 不起作用
- react-native - 将 base64 图像 uri 发送到 ImageManipulator
- nativescript - 如何在 Nativescript 中为 TabViewItem 赋予宽度?
- java - 如何获取任意整数数组并返回具有相同整数但没有重复的数组
- stopwatch - 我需要帮助为反应时间测试仪创建秒表
- .htaccess - .htaccess 出现 | 但它不存在
- racket - 用合约实现算术模 N
- c# - 为什么'value'可以用作变量,当它是C#中的关键字时?