1

I was using the get function to import data from Airtable but I need to make the records I get sorted so is there anyway to create a kwarg that can sort the results in python? The idea was made in github but I couldn't understand it

Github code:

  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)
  • 1
    Do you have an example of what you want to do? – 101 Mar 02 '20 at 00:25
  • @101 When I use the get request for airtable it gives me the all the records in the table I chose but it's ordered randomly so I wanted to add an argument that sort the records for me N.B: I edited the Q and added the example from github but I can't understand it – Kareem Alaa Mar 02 '20 at 00:32

0 Answers0