Skip to main content

data wrangling for lists of tuples and dictionaries

Project description

Pytups

Build Status

What and why

I grew used to the chained operations in R's tidyverse packages or, although not a great fan myself, python's pandas. I find myself using dictionary and list comprehensions all the time to pass from one data format to the other efficiently. But after doing it for the Nth time, I thought of automaticing it.

In my case, it helps me construct optimisation models with PuLP. I see other possible uses not related to OR.

I've implemented some additional methods to regular dictionaries, lists and sets to come up with interesting methods that somewhat quickly pass from one to the other and help with data wrangling.

In order for the operations to make any sense, the assumption that is done is that whatever you are using has the same 'structure'. For example, if you a have a list of tuples: every element of the list is a tuple with the same size and the Nth element of the tuple has the same type, e.g. [(1, 'red', 'b', '2018-01'), (10, 'ccc', 'ttt', 'ff')]. Note that both tuples have four elements and the first one is a number, not a string. We do not check that this is consistent.

They're made to always return a new object, so no "in-place" editing, hopefully.

Right now there are three classes to use.

Quick example

We reverse a nested dictionary to take the deepest key outside while keeping the same final values. This chain of operations uses both superdict and tuplist at different points.

    import pytups as pt
    some_dict = {'a': {'b': {'c': 'A'}}, 'b': {'t': {'c' : 'B'}}}
    pt.SuperDict.from_dict(some_dict).\
        to_dictup().\
        to_tuplist().\
        filter([2, 0, 1, 3]).\
        to_dict(result_col=3, is_list=False).\
        to_dictdict()
    # {'c': {'a': {'b': 'A'}, 'b': {'t': 'B'}}}

Installing

pip install pytups

or, for the development version:

pip install https://github.com/pchtsp/pytups/archive/master.zip

Testing

Run the command

python -m unittest discover -s tests

if the output says OK, all tests were passed.

Reference

Superdicts

These are dictionaries with additional methods based on the contents.

Example:

    import pytups as pt
    indent_dict = {'a': {'b': {'c': 'A'}}, 'b': {'t': {'d' : 'B'}}}
    supdict = pl.Superdict.from_dict(indent_dict)
    supdict_dictup = supdict.to_dictup()
    # {('a', 'b', 'c'): 'A', ('b', 't', 'd'): 'B'}
    supdict_dictup.to_tup()
    # [('a', 'b', 'c', 'A'), ('b', 't', 'd', 'B')]
    supdict_dictup.apply(lambda k, v: v+'_1')
    # {('a', 'b', 'c'): 'A_1', ('b', 't', 'd'): 'B_1'}
    supdict_dictup.to_dictdict()
    # {'a': {'b': {'c': 'A'}}, 'b': {'t': {'d' : 'B'}}}

tuplists

Lists of tuples of any size. Example:

    _list = [('a', 'b', 'c', 1), ('a', 'b', 'c', 2), ('a', 'b', 'c', 3),
            ('r', 'b', 'c', 1), ('r', 'b', 'c', 2), ('r', 'b', 'c', 3)]
    tuplist = pt.TupList(_list)
    tuplist.filter([0, 2]).unique()
    # [('a', 'c'), ('r', 'c')]
    tuplist.to_dict(result_col=3, is_list=True)
    # {('a', 'b', 'c'): [1, 2, 3], ('r', 'b', 'c'): [1, 2, 3]}
    tuplist.test_filter_list_f(lambda x: x[0] <= 'a')
    # [('a', 'b', 'c', 1), ('a', 'b', 'c', 2), ('a', 'b', 'c', 3)]

ordered sets

We have implemented the most common list operations to use it as a list. The purpose is mainly to use it as a sequence of things in order to ask for the position, the next element and the previous one and X elements from it.

Specially useful for a list of dates, months, when you want fast lookup speeds.

As a set, it can only take as element hashable objects (lists are not ok: tuples are).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytups-0.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

pytups-0.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file pytups-0.1.tar.gz.

File metadata

  • Download URL: pytups-0.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for pytups-0.1.tar.gz
Algorithm Hash digest
SHA256 378fd49369ebb4b5c02b913ad284dba9bfc90af79b749c2c64842b59b2a2d202
MD5 4ab556a00c1e4db1b267fa03853b62b3
BLAKE2b-256 43c13107f8c8ecfa4df9ba33c2b2aceae401d30e928af1ca1c9f14aee88b481e

See more details on using hashes here.

File details

Details for the file pytups-0.1-py3-none-any.whl.

File metadata

  • Download URL: pytups-0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for pytups-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d9359ad1cbf718415ca01d153004e51569436a15ee45bf479e2c2262bec1c3e9
MD5 adcb67873fb5a8f934160860a7d5c53b
BLAKE2b-256 f26d0dc696398a02f413e8ec4d36e9203621486cc6555ae8269fddd2d0d73e79

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page