Skip to main content

data wrangling for lists of tuples and dictionaries

Project description

Pytups


.. image:: https://img.shields.io/pypi/v/pytups.svg :target: https://pypi.org/project/pytups/ .. image:: https://img.shields.io/pypi/l/pytups.svg :target: https://pypi.org/project/pytups/ .. image:: https://img.shields.io/pypi/pyversions/pytups.svg :target: https://pypi.org/project/pytups/ .. image:: https://travis-ci.org/pchtsp/pytups.svg?branch=master :target: https://travis-ci.org/pchtsp/pytups

.. highlight:: python

What and why

I grew used to the chained operations in R's tidyverse <https://www.tidyverse.org/>_ packages or, although not a great fan myself, python's pandas <https://pandas.pydata.org/>_ . 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 <https://github.com/coin-or/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: dictionaries, tuple lists and ordered sets.

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

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.4.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

pytups-0.4-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytups-0.4.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for pytups-0.4.tar.gz
Algorithm Hash digest
SHA256 39855dec3ae74e06ed927ce9e27756f0285df325f755b4449698fab6ce19af3c
MD5 b35d7e5b319cc974a48dd37f16c18e7d
BLAKE2b-256 23f2c4ab6c37222d5b8fbe1dc0d7bc8b0224182183b710e1c3d81aba74da8f9b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytups-0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for pytups-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9fcb270e29abd5bf169cca71103d991f9e49c066bab7a364fe694e369016c9d1
MD5 febd15eca4923fc1fa21f333a3a62ae5
BLAKE2b-256 25f6570c6dc639aa5411be2046a919823f4738cfd3d1b4a086a48fb360815d79

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