ft library

Dealing with Lists of Dictionaries in Python


The ft library is for dealing with lists of dictionaries in Python. It builds indices of lists of dictionaries easily, and includes functions for exploiting those indices. It's written in pure Python, so installation is painless.

Getting ft

ft is available on pip:

pip install ft

Examples

Loading a CSV with semi-colon separated values:

  >>> import ft
  >>> csv_dialect = ft.dialect_dex["basic"][0]
  >>> csv_dialect
  {'pipe': , 'dialect': 'basic', 'cell_delimiter': ',', 
    'text_delimiter': '"', 'row_delimiter': '\n'}
  >>> csv_dialect["cell_delimiter"] = ";"
  >>> data = ft.load_csv(open("dans-records_2016-05-20.csv"), 
                         dialect = csv_dialect)
    

That part of the lib isn't glamorous. Let's look at the cool parts. Most central to everything is indexBy:

  >>> artistdex = ft.indexBy("Artist", data)
  >>> artistdex["Joy Division"]
  >>> pprint(artistdex["Joy Division"])
  [{'Album': 'Closer',
    'Artist': 'Joy Division',
    'Condition': 'NM',
    'Genre': 'Joy Division',
    'Loaned To': '',
    'Notes': '',
    'Sale': '',
    'Year': '1980*',
    'oqid': '11'},
    (etc...)

What about counting stuff? If you want to count everything, you can use ft.histo with a dex you've already made, e.g.:

  >>> artist_counts = ft.histo(artistdex)
>>> pprint(artist_counts)
{'(weird Russian artist)': 1,
    '2-Pac': 1,
    'A Certain Ratio': 1,
    'A Flock of Seagulls': 3,
    'A-ha': 1,
    'Adam Ant': 1,
    'Adam and the Ants': 1,
    'Al Hirt': 1,
    'America': 1,
    'Bach, Johann Sebastian': 2,
    'Bachman Turner Overdrive': 1,
    'Bad Company': 1,
    'Basil, Toni': 1,
    (etc...)

The CSV reader is pretty dopey, like I mentioned. However, we can tweak the data using ft.tag. This lets you define functions in terms of their arguments, not in terms of the dictionary you're manipulating.

>>> def year_int(year):
...     if year[-1] == "*": return int(year[:-1])
...     try:
...             return int(year)
...     except:
...             return None
... 
>>> data = ft.tag(data, "int_year", year_int, args=["Year"])
>>> data[0]["int_year"]
2010

Just a taste of what you can do with ft.