Skip to main content

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).

Metadata

Release files for pytups 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytups 0.1
File Size Uploaded
pytups-0.1.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytups 0.1
File Interpreter ABI Platform
pytups-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 15.3 kB

Release files / pytups-0.1.tar.gz

Download URL pytups-0.1.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
378fd49369ebb4b5c02b913ad284dba9bfc90af79b749c2c64842b59b2a2d202
BLAKE2b-256 checksum
How to use checksums
43c13107f8c8ecfa4df9ba33c2b2aceae401d30e928af1ca1c9f14aee88b481e
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / pytups-0.1-py3-none-any.whl

Download URL pytups-0.1-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d9359ad1cbf718415ca01d153004e51569436a15ee45bf479e2c2262bec1c3e9
BLAKE2b-256 checksum
How to use checksums
f26d0dc696398a02f413e8ec4d36e9203621486cc6555ae8269fddd2d0d73e79
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.0

2 release files

0.88.1

2 release files

0.87.3

2 release files

0.87.1

2 release files

0.86.2

2 release files

0.86.1

2 release files

0.85.0

2 release files

0.84.0

2 release files

0.83.0

2 release files

0.82.0

2 release files

0.80.0

2 release files

0.9.0

2 release files

0.7

2 release files

0.6

2 release files

0.5

2 release files

0.4

2 release files

0.3

2 release files

0.2

2 release files

This release

0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page