Skip to content

Instantly share code, notes, and snippets.

@franciscoafonsoo
Created November 22, 2022 16:57
Show Gist options
  • Save franciscoafonsoo/e2809cb6a6098132437c3521281bde8e to your computer and use it in GitHub Desktop.
Save franciscoafonsoo/e2809cb6a6098132437c3521281bde8e to your computer and use it in GitHub Desktop.
import toolz.dicttoolz as dz
class Dictz(dict):
"""Wrapper class of dict injected with dicttoolz
## Rational ##
Using several dicttoolz funcs in a row can be cumbersome
For Example:
>>> kv = {
"coordinates": {"latitude": 38.63999, "longitude": -9.10195},
"vehicleDeltaWeight": 3.0,
"vehicleDeltaChargePct": 90.0,
"stopTime": 30000,
},
>>> k = assoc(kv, new, kv[old])
>>> k = dissoc(k, old)
This is just an example, but basically you have to keep a intermediate state
(variable) for each operation and it's impossible to chain calls unless you
curry them before, making the code even more complex. and that's not the
functional way.
With this class you can chain commands easily:
>>> kv = {
"coordinates": {"latitude": 38.63999, "longitude": -9.10195},
"vehicleDeltaWeight": 3.0,
"vehicleDeltaChargePct": 90.0,
"stopTime": 30000,
},
>>> k = Dictz(kv).assoc(new, kv[old]).dissoc(old)
In the end you can always call to_dict() if you don't want the wrapper class
>>> k = Dictz(kv).assoc(new, kv[old]).dissoc(old).to_dict()
{
"coordinates": {"latitude": 38.63999, "longitude": -9.10195},
"vehicleDeltaWeight": 3.0,
"vehicleDeltaCharge": 90.0,
"stopTime": 30000,
},
## Installation ##
This will be converted into a package, but until then follow this steps:
1. install toolz (pip install toolz)
2. copy this file to your project
## Usage ##
This is a one to one replica of
https://toolz.readthedocs.io/en/latest/api.html#dicttoolz
The difference is that you should pass the dict to this wrapper class and in
the end call to_dict(). Example:
>>> Dictz(your_dict_here).<any_dictoolz_function>.to_dict()
to_dict() call is optional, since Dictz behaves exacltly like a dict
"""
def to_dict(self):
return dict(self)
def merge(self, *dicts, **kwargs):
return Dictz(dz.merge(self, *dicts, **kwargs))
def merge_with(self, func, *dicts, **kwargs):
return Dictz(dz.merge_with(func, self, *dicts, **kwargs))
def valmap(self, func, factory=dict):
return Dictz(dz.valmap(func, self, factory=factory))
def keymap(self, func, factory=dict):
return Dictz(dz.keymap(func, self, factory=factory))
def itemmap(self, func, factory=dict):
return Dictz(dz.itemmap(func, self, factory=factory))
def valfilter(self, predicate, factory=dict):
return Dictz(dz.valfilter(predicate, self, factory=factory))
def keyfilter(self, predicate, factory=dict):
return Dictz(dz.keyfilter(predicate, self, factory=factory))
def itemfilter(self, predicate, factory=dict):
return Dictz(dz.itemfilter(predicate, self, factory=factory))
def assoc(self, key, value):
return Dictz(dz.assoc(self, key, value))
def dissoc(self, *keys, **values):
return Dictz(dz.dissoc(self, *keys, **values))
def assoc_in(self, keys, value, factory=dict):
return Dictz(dz.assoc_in(self, keys, value, factory=factory))
def update_in(self, keys, func, default=None, factory=dict):
return Dictz(dz.update_in(self, keys, func, default=default, factory=factory))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment