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({'a': 1, 'b': 0, 'c': 1}).clean(0) {'a': 1, 'c': 1}
- dicts_to_tup(keys: list, content) SuperDict ¶
compacts nested dictionaries into one single dictionary with tuples as keys.
- 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({'a': 1, 'b': 0, 'c': 1}).filter(['a', 'b']) {'a': 1, 'b': 0}
- 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
- head() str ¶
Returns a string representation with the first pair of key values in the SuperDict, the last pair of key value and the number of elements on the SuperDict.
- Returns:
the head string representation of the SuperDict
- Return type:
- 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:
- kapply(func: Callable, *args, **kwargs) SuperDict ¶
Same as apply but only on keys
- Parameters:
func (callable) – function to apply.
- Returns:
new
SuperDict
- 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:
- 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}
- 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 | int | float | str, *args, **kwargs) SuperDict ¶
Applies function to both dictionaries. Using keys of the self. It’s like applying a function over the left join.
- 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
- 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:
- Return type:
- 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_tl(pos: int = None) TupList ¶
Shortcut to:
>>> tl.TupList(SuperDict().values())
- Returns:
tuple list with values
- Parameters:
pos (int) – position to extract
- Return type:
- 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
- classmethod from_csv(path: str, func: Callable | None = None, **kwargs) TupList ¶
Generates a new TupList by reading a csv file
- intersect(input_list: Iterable) TupList ¶
Converts list and argument into sets and then intersects them.
- 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
- set_diff(input_list: Iterable) TupList ¶
Converts list and argument into sets and then subtracts one from the other.
- 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
- 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:
- Returns:
- 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.
- unique(**kwargs) TupList ¶
Applies
numpy.unique()
.- Parameters:
dtype – arguments to
numpy.asarray()
- 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