Documentation for the Code

SuperDict

SuperDict

class pytups.superdict.SuperDict

A dictionary with additional methods

clean(default_value=0, func: Callable | None = None, **kwargs) SuperDict

Filters elements by value

Parameters:
  • default_value – value of elements to take out

  • func (function) – function that evaluates to true if we take out the element

  • kwargs – optional arguments for func

Returns:

new SuperDict

Return type:

SuperDict

>>> SuperDict({'a': 1, 'b': 0, 'c': 1}).clean(0)
{'a': 1, 'c': 1}
copy_deep() SuperDict

Copies the complete object using python’s pickle

copy_deep2() SuperDict

Copies the complete object using json (or ujson if available)

copy_shallow() SuperDict

Copies the immediate keys only.

Returns:

new SuperDict

dicts_to_tup(keys: list, content) SuperDict

compacts nested dictionaries into one single dictionary with tuples as keys.

Parameters:
  • keys (list) – list of keys to use as new key

  • content

Returns:

modified SuperDict

Return type:

SuperDict

fill_with_default(keys: Iterable, default=0) SuperDict

guarantees dictionary will have specific keys

Parameters:
  • keys (Iterable) – dictionary will have at least these keys

  • default

Returns:

new SuperDict

filter(indices: Iterable, check: bool = True) SuperDict

takes out elements that are not in indices

Parameters:
  • indices – keys to keep in new dictionary

  • check (bool) – if True, only return valid ones

Returns:

new SuperDict

Return type:

SuperDict

>>> SuperDict({'a': 1, 'b': 0, 'c': 1}).filter(['a', 'b'])
{'a': 1, 'b': 0}
classmethod from_dict(data) SuperDict

Main initialization. Deals with nested dictionaries.

Parameters:

data (dict) – a (possibly nested) dictionary

Returns:

new SuperDict

get_m(*args, default=None) Any

Safe way to search for something in a nested dictionary

Parameters:

args – keys in nested dictionary

Returns:

content after traversing the nested dictionary. None if doesn’t exit

items_tl(pos: int = None) TupList

Shortcut to:

>>> tl.TupList(SuperDict().items())
Returns:

tuple list with keys

Parameters:

pos (int) – position to extract

Return type:

pytups.tuplist.TupList

kapply(func: Callable, *args, **kwargs) SuperDict

Same as apply but only on keys

Parameters:

func (callable) – function to apply.

Returns:

new SuperDict

keys_l(pos: int | None = None) list

Shortcut to:

>>> list(SuperDict().keys())
Returns:

list with keys

Parameters:

pos (int) – position to extract

Return type:

list or object

keys_tl(pos: int = None) TupList

Shortcut to:

>>> tl.TupList(SuperDict().keys())
Returns:

tuple list with keys

Parameters:

pos (int) – position to extract

Return type:

pytups.tuplist.TupList

kfilter(func: Callable, **kwargs) SuperDict

apply a filter over the dictionary keys :param function func: True for keys we want to filter :param kwargs: other arguments for func :return: new SuperDict :rtype: SuperDict

>>> SuperDict({'a': 2, 'b': 3, 'c': 1}).kfilter(lambda k: k > 'a')
{'b': 3, 'c': 1}
kvapply(func: Callable, *args, **kwargs) SuperDict

Applies a function to the dictionary and returns the result

Parameters:

func (callable) – function with two arguments: one for the key, another for the value

Returns:

new SuperDict

kvfilter(func: Callable, **kwargs) SuperDict

apply a filter over the dictionary values :param callable func: True for values we want to filter :param kwargs: other arguments for func :return: new SuperDict :rtype: SuperDict

>>> SuperDict({'a': 2, 'b': 3, 'c': 1}).kvfilter(lambda k, v: v > 1 and k=='a')
{'a': 2}
len() int

Shortcut to:

>>> len(SuperDict())
Returns:

length of dictionary

Return type:

int

list_reverse() SuperDict

transforms dictionary of lists to another dictionary of lists only indexed by the values.

Returns:

new SuperDict

sapply(func: Callable, other: dict, *args, **kwargs) SuperDict

Applies function to both dictionaries. Using keys of the self. It’s like applying a function over the left join.

Parameters:
  • func (callable) – function to apply.

  • other (dict) – dictionary to apply function to

Returns:

new SuperDict

set_m(*args, value=None) SuperDict

uses args as nested keys and then assigns value

Parameters:
  • args – keys to nest

  • value – value to assign to last dictionary

Returns:

modified SuperDict

>>> SuperDict({('a', 'b'): 1, ('b', 'c'): 0, 'c': 1}).set_m('c', 'd', 'a', value=1)
{'a': {'b': 1}, 'b': {'c': 0}, 'c': {'d': {'a': 1}}}
sorted(**kwargs) list

Applies sorted function to dictionary keys

Parameters:

kwargs – arguments for sorted

Returns:

to_dictdict() SuperDict

Expands tuple keys to nested dictionaries Useful to get json-compatible objects from the solution

Returns:

new (nested) SuperDict

>>> SuperDict({('a', 'b'): 1, ('b', 'c'): 0, 'c': 1}).to_dictdict()
{'a': {'b': 1}, 'b': {'c': 0}, 'c': 1}
to_dictup() SuperDict

Useful when reading a json and wanting to convert it to tuples. Opposite to to_dictdict

Returns:

new (flat) SuperDict

Return type:

SuperDict

to_lendict() SuperDict

get length of values in dictionary

Returns:

new SuperDict

to_tuplist() TupList

The last element of the returned tuple was the dict’s value. We try really hard to expand the tuples so it’s a flat tuple list.

Returns:

new pytups.tuplist.TupList

Return type:

pytups.tuplist.TupList

update(*args, **kwargs) SuperDict

updates a nested dictionary.

Parameters:
  • args – dictionary to update with

  • kwargs – specific keys and values to update

Returns:

the edited dictionary

values_l(pos: int | None = None) list

Shortcut to:

>>> list(SuperDict().values())
Returns:

list with values

Parameters:

pos (int) – position to extract

Return type:

list or object

values_tl(pos: int = None) TupList

Shortcut to:

>>> tl.TupList(SuperDict().values())
Returns:

tuple list with values

Parameters:

pos (int) – position to extract

Return type:

pytups.tuplist.TupList

vapply(func: Callable, *args, **kwargs) SuperDict

Same as apply but only on values

Parameters:

func (callable) – function to apply.

Returns:

new SuperDict

vfilter(func: Callable, **kwargs) SuperDict

apply a filter over the dictionary values :param function func: True for values we want to filter :param kwargs: other arguments for func :return: new SuperDict :rtype: SuperDict

>>> SuperDict({'a': 2, 'b': 3, 'c': 1}).vfilter(lambda v: v > 1)
{'a': 2, 'b': 3}

TupList

class pytups.tuplist.TupList(iterable=(), /)

A list of tuples or dictionaries

add(*args) None

this is just a shortcut for doing

>>> TupList().append((0, 1, 2))

by doing:

>>> TupList().add(0, 1, 2)

which is a little more friendly and short

Parameters:

args – any number of elements to append

Returns:

modified TupList

chain() TupList

Flattens a TupList by applying itertools chain method

copy_deep() TupList

Copies the complete object using python’s pickle

copy_shallow() TupList

Copies the list only. Not it’s contents

Returns:

new TupList

classmethod from_csv(path: str, func: Callable | None = None, **kwargs) TupList

Generates a new TupList by reading a csv file

Parameters:
  • path (str) – filename

  • func (callable) – function to apply to each row

  • kwargs – arguments to csv.reader

Returns:

new TupList

intersect(input_list: Iterable) TupList

Converts list and argument into sets and then intersects them.

Parameters:

input_list (list) – list to intersect

Returns:

new TupList

kapply(func: Callable, *args, **kwargs) TupList

maps function into each key of TupList

Parameters:

func (callable) – function to apply

Returns:

new TupList

kvapply(func: Callable, *args, **kwargs) TupList

maps function into each element of TupList with indexes

Parameters:

func (callable) – function to apply

Returns:

new TupList

len() int

Shortcut to:

>>> len(TupList())
Returns:

length of list

Return type:

int

set_diff(input_list: Iterable) TupList

Converts list and argument into sets and then subtracts one from the other.

Parameters:

input_list (list) – list to subtract

Returns:

new TupList

sorted(**kwargs) TupList

Applies sorted function to elements and returns a TupList

Parameters:

kwargs – arguments for sorted function main arguments for sorted are: - key - reverse

Returns:

new TupList

take_np(indices: Iterable | int) TupList

filters the tuple of each element of the list according to a list of positions

Parameters:

indices (int or list) – a list of positions

Returns:

a new TupList

to_csv(path: str) TupList

Exports the list to a csv file :param path: filename :return: the same TupList

to_dict(result_col: Iterable | int | None = 0, is_list: bool = True, indices: Iterable | int | None = None) SuperDict
This magic function converts a tuple list into a dictionary

by taking one or several of the columns as the result.

Parameters:
  • result_col (int or list or None) – a list of keys for the result (positions of the tuple or keys of the dict)

  • is_list (bool) – the value of the dictionary will be a TupList?

  • indices (list) – optional way of determining the indices instead of being the complement of result_col

Returns:

new pytups.superdict.SuperDict

to_dictlist(keys: list) TupList

Converts a list of tuples to a list of dictionaries.

Parameters:

keys (list) – a unique list of dictionary keys

Returns:

new TupList

to_list() list
Returns:

list

to_set() set
Returns:

set

to_start_finish(compare_tups: Callable, pp: int = 1, sort: bool = True, join_func: Callable | None = None) TupList

Takes a calendar tuple list of the form: (id, month) and returns a tuple list of the form (id, start_month, end_month) it works with a bigger tuple too.

Parameters:
  • compare_tups (callable) – returns True if tups are not consecutive. Takes 3 arguments

  • pp (int) – the position in the tuple where the period is

  • join_func (callable) – returns joined tuple from list of consecutive tuples. Takes 1 argument.

Returns:

new TupList

unique(**kwargs) TupList

Applies numpy.unique().

Parameters:

dtype – arguments to numpy.asarray()

Returns:

new TupList

unique2() TupList

Converts to set and then back to TupList.

Returns:

new TupList

vapply(func: Callable, *args, **kwargs) TupList

maps function into each element of TupList

Parameters:

func (callable) – function to apply

Returns:

new TupList

vapply_col(pos: int | str | None, func: Callable)

Like vapply, but it stores the result in one of the positions of the tuple (or dictionary) :param pos: int or str :param callable func: function to apply to create col

vfilter(function: Callable) TupList

returns new list with only tuples for which function returns True

Parameters:

function (callable) – function to apply to each element

Returns:

new TupList