Lazy dict with deterministic universally unique identifier for values
Project description
idict
A lazy dict
with universally unique deterministic identifiers.
Latest release | Current code | API documentation
See also
- identification package used by
idict
: GaROUPa - only laziness, i.e., without the identification part: ldict
Overview
An idict
is an identified dict
with str
keys.
We consider that every value is generated by a process, starting from an empty
idict
. The process is a sequence of
transformation steps done through the operator >>
, which symbolizes the ordering of the steps.
There are two types of steps:
- value insertion - represented by dict-like objects
- function application - represented by ordinary Python functions
Functions, idict
s, and values have a deterministic UUID
(called hosh - operable hash).
Identifiers (hoshes) for idict
s and values are predictable through the
magic available here.
An idict
is completely defined by its key-value pairs so that
it can be converted from/to a built-in dict
.
Creating an idict
is not different from creating an ordinary dict
. Optionally it can be created through the >>
operator
used after empty
or Ø
(usually AltGr+Shift+o in most keyboards).
The resulting idict
always contains two extra entries id
and ids
:
Function application is done in the same way. The parameter names define the input fields, while the keys in the
returned dict
define the output fields:
After evaluated, the value will not be calculated again:
Functions can accept parameters:
Installation
...as a standalone lib
# Set up a virtualenv.
python3 -m venv venv
source venv/bin/activate
# Install from PyPI...
pip install --upgrade pip
pip install -U idict
pip install -U idict[full] # use the flag 'full' for extra functionality (recommended)
# ...or, install from updated source code.
pip install git+https://github.com/davips/idict
...from source
git clone https://github.com/davips/idict
cd idict
poetry install
poetry install -E full # use the flag 'full' for extra functionality (recommended)
Examples
Overview
# Creation by direct instantiation.
from idict import idict
d = idict(x=5, y=7, z=10)
# Creation from scratch.
# The expression 'v >> a >> b' means "Value 'v' will be processed by step 'a' then 'b'".
# A step can be a value insertion or a function application.
from idict import empty
d = empty >> {"x": 5} >> {"y": 7, "z": 10}
# Empty alias ('Ø') usage.
from idict import Ø
d = Ø >> {"x": 5} >> {"y": 7, "z": 10}
print(d)
"""
{
"x": 5,
"y": 7,
"z": 10,
"_id": "H8_d2866809b46b74333b8a1e77c2897466edc1b",
"_ids": {
"x": ".T_f0bb8da3062cc75365ae0446044f7b3270977",
"y": "mX_dc5a686049ceb1caf8778e34d26f5fd4cc8c8",
"z": "ll_4dd7bf5575f1c410dc6458230cda99c380bda"
}
}
"""
# Inverting color theme for a white background.
from garoupa import setup
setup(dark_theme=False)
d = idict(x=5, y=7, z=10)
print(d)
"""
{
"x": 5,
"y": 7,
"z": 10,
"_id": "H8_d2866809b46b74333b8a1e77c2897466edc1b",
"_ids": {
"x": ".T_f0bb8da3062cc75365ae0446044f7b3270977",
"y": "mX_dc5a686049ceb1caf8778e34d26f5fd4cc8c8",
"z": "ll_4dd7bf5575f1c410dc6458230cda99c380bda"
}
}
"""
# Function application.
# The input and output fields are detected from the function signature and returned dict.
def f(x):
return {"y": x ** 2}
d2 = d >> f
print(d2)
"""
{
"y": "→(x)",
"x": 5,
"z": 10,
"_id": "d6tEK5U0qq3sv0aCdSADNL3DiohLRgUAfdP7HEp4",
"_ids": {
"y": "S8kpf19fdmWw65QIdKajRI2i03eLRgUAfdP7HEp4",
"x": ".T_f0bb8da3062cc75365ae0446044f7b3270977",
"z": "ll_4dd7bf5575f1c410dc6458230cda99c380bda"
}
}
"""
# Anonymous function application.
d2 = d >> (lambda y: {"y": y / 5})
print(d)
"""
{
"x": 5,
"y": 7,
"z": 10,
"_id": "H8_d2866809b46b74333b8a1e77c2897466edc1b",
"_ids": {
"x": ".T_f0bb8da3062cc75365ae0446044f7b3270977",
"y": "mX_dc5a686049ceb1caf8778e34d26f5fd4cc8c8",
"z": "ll_4dd7bf5575f1c410dc6458230cda99c380bda"
}
}
"""
# Resulting values are evaluated lazily.
d >>= lambda y: {"y": y / 5}
print(d.y)
"""
1.4
"""
print(d)
"""
{
"y": 1.4,
"x": 5,
"z": 10,
"_id": "OfV9fKxwEnz.wbUBTPj8HiLDPnT04cq1Fa6MPeo7",
"_ids": {
"y": "LZHU.q4GvU4Y3YhITHVPLfKix2Q04cq1Fa6MPeo7",
"x": ".T_f0bb8da3062cc75365ae0446044f7b3270977",
"z": "ll_4dd7bf5575f1c410dc6458230cda99c380bda"
}
}
"""
# Parameterized function application.
# "Parameters" are distinguished from "fields" by having default values.
# When the default value is None, it means it will be explicitly defined later by 'let'.
from idict import let
def f(x, y, a=None, b=None):
return {"z": a * x ** b, "w": y ** b}
d2 = d >> let(f, a=7, b=2)
print(d2)
"""
{
"z": "→(a b x y)",
"w": "→(a b x y)",
"y": 1.4,
"x": 5,
"_id": "rBGX6-A6dn.ZvAjRUsoK2FLhMlGlSPVLUBztaeJJ",
"_ids": {
"z": "Auz4wZus04uj77lfU9xfNo-.RWKkODvKfrtJm.kD",
"w": "ofEb.nRSYsUsgAnnyp4KYFovZaUOV6000sv....-",
"y": "LZHU.q4GvU4Y3YhITHVPLfKix2Q04cq1Fa6MPeo7",
"x": ".T_f0bb8da3062cc75365ae0446044f7b3270977"
}
}
"""
# Parameterized function application with sampling.
# The default value is one of the following ranges,
# list, arithmetic progression, geometric progression.
# Each parameter value will be sampled later.
# A random number generator must be given.
from idict import let
from random import Random
def f(x, y, a=None, b=[1, 2, 3], ap=[1, 2, 3, ..., 10], gp=[1, 2, 4, ..., 16]):
return {"z": a * x ** b, "w": y ** ap * gp}
d2 = d >> Random(0) >> let(f, a=7)
print(d2)
"""
{
"z": "→(a b ap gp x y)",
"w": "→(a b ap gp x y)",
"y": 1.4,
"x": 5,
"_id": "yqpf1MHE-H93OFdGyZIlUm.Y--FH-Bd3TVgh3l2B",
"_ids": {
"z": "T5ExjZqH2uu442hSlvxo4um8xxEGWpP1eLaxf6Gu",
"w": "ofEb.nRSYsUsgAnnyp4KYFovZaUOV6000sv....-",
"y": "LZHU.q4GvU4Y3YhITHVPLfKix2Q04cq1Fa6MPeo7",
"x": ".T_f0bb8da3062cc75365ae0446044f7b3270977"
}
}
"""
print(d2.z)
"""
175
"""
print(d2)
"""
{
"z": 175,
"w": "10.541350399999995",
"y": 1.4,
"x": 5,
"_id": "yqpf1MHE-H93OFdGyZIlUm.Y--FH-Bd3TVgh3l2B",
"_ids": {
"z": "T5ExjZqH2uu442hSlvxo4um8xxEGWpP1eLaxf6Gu",
"w": "ofEb.nRSYsUsgAnnyp4KYFovZaUOV6000sv....-",
"y": "LZHU.q4GvU4Y3YhITHVPLfKix2Q04cq1Fa6MPeo7",
"x": ".T_f0bb8da3062cc75365ae0446044f7b3270977"
}
}
"""
Identity example
from idict import idict
a = idict(x=3)
print(a)
"""
{
"x": 3,
"_id": "WB_e55a47230d67db81bcc1aecde8f1b950282cd",
"_ids": {
"x": "WB_e55a47230d67db81bcc1aecde8f1b950282cd"
}
}
"""
b = idict(y=5)
print(b)
"""
{
"y": 5,
"_id": "0U_e2a86ff72e226d5365aea336044f7b4270977",
"_ids": {
"y": "0U_e2a86ff72e226d5365aea336044f7b4270977"
}
}
"""
print(a >> b)
"""
{
"x": 3,
"y": 5,
"_id": "Xt_a63010fa2b5b4c671270fbe8ec313568a8b35",
"_ids": {
"x": "WB_e55a47230d67db81bcc1aecde8f1b950282cd",
"y": "0U_e2a86ff72e226d5365aea336044f7b4270977"
}
}
"""
Merging two idicts
from idict import idict
a = idict(x=3)
print(a)
"""
{
"x": 3,
"_id": "WB_e55a47230d67db81bcc1aecde8f1b950282cd",
"_ids": {
"x": "WB_e55a47230d67db81bcc1aecde8f1b950282cd"
}
}
"""
b = idict(y=5)
print(b)
"""
{
"y": 5,
"_id": "0U_e2a86ff72e226d5365aea336044f7b4270977",
"_ids": {
"y": "0U_e2a86ff72e226d5365aea336044f7b4270977"
}
}
"""
print(a >> b)
"""
{
"x": 3,
"y": 5,
"_id": "Xt_a63010fa2b5b4c671270fbe8ec313568a8b35",
"_ids": {
"x": "WB_e55a47230d67db81bcc1aecde8f1b950282cd",
"y": "0U_e2a86ff72e226d5365aea336044f7b4270977"
}
}
"""
Lazily applying functions to idict
from idict import idict
a = idict(x=3)
print(a)
"""
{
"x": 3,
"_id": "WB_e55a47230d67db81bcc1aecde8f1b950282cd",
"_ids": {
"x": "WB_e55a47230d67db81bcc1aecde8f1b950282cd"
}
}
"""
a = a >> idict(y=5) >> {"z": 7} >> (lambda x, y, z: {"r": x ** y // z})
print(a)
"""
{
"r": "→(x y z)",
"x": 3,
"y": 5,
"z": 7,
"_id": "H8DftZZ4nH6d67WSvYYxh-KsdBqp9MQBdvkLxU2o",
"_ids": {
"r": "n57RGOgdv03kK4IqBkIf6oFrvgAp9MQBdvkLxU2o",
"x": "WB_e55a47230d67db81bcc1aecde8f1b950282cd",
"y": "0U_e2a86ff72e226d5365aea336044f7b4270977",
"z": "nX_da0e3a184cdeb1caf8778e34d26f5fd4cc8c8"
}
}
"""
print(a.r)
"""
34
"""
print(a)
"""
{
"r": 34,
"x": 3,
"y": 5,
"z": 7,
"_id": "H8DftZZ4nH6d67WSvYYxh-KsdBqp9MQBdvkLxU2o",
"_ids": {
"r": "n57RGOgdv03kK4IqBkIf6oFrvgAp9MQBdvkLxU2o",
"x": "WB_e55a47230d67db81bcc1aecde8f1b950282cd",
"y": "0U_e2a86ff72e226d5365aea336044f7b4270977",
"z": "nX_da0e3a184cdeb1caf8778e34d26f5fd4cc8c8"
}
}
"""
Parameterized functions and sampling
from random import Random
from idict import Ø, let
# A function provide input fields and, optionally, parameters.
# For instance:
# 'a' is sampled from an arithmetic progression
# 'b' is sampled from a geometric progression
# Here, the syntax for default parameter values is borrowed with a new meaning.
def fun(x, y, a=[-100, -99, -98, ..., 100], b=[0.0001, 0.001, 0.01, ..., 100000000]):
return {"z": a * x + b * y}
def simplefun(x, y):
return {"z": x * y}
# Creating an empty idict. Alternatively: d = idict().
d = Ø >> {}
d.show(colored=False)
"""
{
"_id": "0000000000000000000000000000000000000000",
"_ids": {}
}
"""
# Putting some values. Alternatively: d = idict(x=5, y=7).
d["x"] = 5
d["y"] = 7
d.show(colored=False)
"""
{
"x": 5,
"y": 7,
"_id": "mP_2d615fd34f97ac906e162c6fc6aedadc4d140",
"_ids": {
"x": ".T_f0bb8da3062cc75365ae0446044f7b3270977",
"y": "mX_dc5a686049ceb1caf8778e34d26f5fd4cc8c8"
}
}
"""
# Parameter values are uniformly sampled.
d1 = d >> simplefun
d1.show(colored=False)
print(d1.z)
"""
{
"z": "→(x y)",
"x": 5,
"y": 7,
"_id": "ZAasLu0lIEqhJyS1s8ML8WGeTnradBnjS7VNt6Mg",
"_ids": {
"z": "iE6rHiYYwfwOBqa4Luh4XCd-myeadBnjS7VNt6Mg",
"x": ".T_f0bb8da3062cc75365ae0446044f7b3270977",
"y": "mX_dc5a686049ceb1caf8778e34d26f5fd4cc8c8"
}
}
35
"""
d2 = d >> simplefun
d2.show(colored=False)
print(d2.z)
"""
{
"z": "→(x y)",
"x": 5,
"y": 7,
"_id": "ZAasLu0lIEqhJyS1s8ML8WGeTnradBnjS7VNt6Mg",
"_ids": {
"z": "iE6rHiYYwfwOBqa4Luh4XCd-myeadBnjS7VNt6Mg",
"x": ".T_f0bb8da3062cc75365ae0446044f7b3270977",
"y": "mX_dc5a686049ceb1caf8778e34d26f5fd4cc8c8"
}
}
35
"""
# Parameter values can also be manually set.
e = d >> let(fun, a=5, b=10)
print(e.z)
"""
95
"""
# Not all parameters need to be set.
e = d >> let(simplefun, a=5)
print(e.z)
"""
35
"""
# Each run will be a different sample for the missing parameters.
e = e >> let(simplefun, a=5)
print(e.z)
"""
35
"""
# We can define the initial state of the random sampler.
# It will be in effect from its location place onwards in the expression.
e = d >> Random(0) >> let(fun, a=5)
print(e.z)
"""
725.0
"""
# All runs will yield the same result,
# if starting from the same random number generator seed.
e = e >> Random(0) >> let(fun, a=[555, 777])
print("Let 'a' be a list:", e.z)
"""
Let 'a' be a list: 700003885.0
"""
# Reproducible different runs are achievable by using a single random number generator.
e = e >> Random(0) >> let(fun, a=[5, 25, 125, ..., 10000])
print("Let 'a' be a geometric progression:", e.z)
"""
Let 'a' be a geometric progression: 700003125.0
"""
rnd = Random(0)
e = d >> rnd >> let(fun, a=5)
print(e.z)
e = d >> rnd >> let(fun, a=5) # Alternative syntax.
print(e.z)
"""
725.0
700000025.0
"""
Composition of sets of functions
from random import Random
from idict import Ø
# A multistep process can be defined without applying its functions
def g(x, y, a=[1, 2, 3, ..., 10], b=[0.00001, 0.0001, 0.001, ..., 100000]):
return {"z": a * x + b * y}
def h(z, c=[1, 2, 3]):
return {"z": c * z}
# In the 'idict' framework 'data is function',
# so the alias Ø represents the 'empty data object' and the 'reflexive function' at the same time.
# In other words: 'inserting nothing' has the same effect as 'doing nothing'.
fun = Ø >> g >> h # 'empty' or 'Ø' enable the cartesian product of the subsequent sets of functions within the expression.
print(fun)
"""
«λ{} × λ»
"""
# Before a function is applied to a dict-like, the function free parameters remain unsampled.
# The result is an ordered set of composite functions.
d = {"x": 5, "y": 7} >> (Random(0) >> fun)
print(d)
"""
{
"z": "→(c z→(a b x y))",
"x": 5,
"y": 7,
"_id": "JGx.yapo8khQ2ugWugJBPhL0OA8r.6TiQwy0UcBi",
"_ids": {
"z": "KOkUw-xqAXPkjmAYNCeWB-hMhLXq.6TiQwy0UcBi",
"x": ".T_f0bb8da3062cc75365ae0446044f7b3270977",
"y": "mX_dc5a686049ceb1caf8778e34d26f5fd4cc8c8"
}
}
"""
print(d.z)
"""
105.0
"""
d = {"x": 5, "y": 7} >> (Random(0) >> fun)
print(d.z)
"""
105.0
"""
# Reproducible different runs by passing a stateful random number generator.
rnd = Random(0)
e = d >> rnd >> fun
print(e.z)
"""
105.0
"""
e = d >> rnd >> fun
print(e.z)
"""
14050.0
"""
# Repeating the same results.
rnd = Random(0)
e = d >> rnd >> fun
print(e.z)
"""
105.0
"""
e = d >> rnd >> fun
print(e.z)
"""
14050.0
"""
Concept
An idict
is like a common Python dict
, with extra functionality and lazy.
It is a mapping between string keys, called
fields, and any serializable (pickable) object.
Each idict
has two extra entries: id
(identifier) and ids
(value identifiers).
A custom 40-digit unique identifier (see GaROUPa)
can be provided as an attribute for each function.
Value objects can have custom identifiers as well, if provided whithin the entry ids
.
Otherwise, identifiers for functions and values will be calculated through blake3 hashing of their content. For functions, the bytecode is used as content. For this reason, such functions should be simple, with minimal external dependencies or with their import statements inside the function body. This decreases the odds of using two functions with identical local code (and, therefore, identical identifiers) performing different calculations.
Grants
This work was supported by Fapesp under supervision of Prof. André C. P. L. F. de Carvalho at CEPID-CeMEAI (Grants 2013/07375-0 – 2019/01735-0) until 2021-03-31.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file idict-1.211123.6.tar.gz
.
File metadata
- Download URL: idict-1.211123.6.tar.gz
- Upload date:
- Size: 43.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.8.10 Linux/5.4.0-89-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05ea210715f616e8bd23f8777478bd09ce073032b4fa3a2dc8a2d37231acfef4 |
|
MD5 | 11197619ba6e2a47b087a343e01c55fc |
|
BLAKE2b-256 | b0a6dc4bf67d2c213644570b806232216c02fed6715576febc0b2926999e8dfb |
File details
Details for the file idict-1.211123.6-py3-none-any.whl
.
File metadata
- Download URL: idict-1.211123.6-py3-none-any.whl
- Upload date:
- Size: 55.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.8.10 Linux/5.4.0-89-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e6eea2455d45017ca4adcf179e95519966f8dfce0681f36245408304a0bfc7a |
|
MD5 | e655ba397c6948f1b64aa19517f03bc6 |
|
BLAKE2b-256 | e40544d96f0eaccc4c04793558ab18faa530e6cca23956b9003acfe0bba25703 |