我正在使用 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)