Module idict.core.iempty

Expand source code
#  Copyright (c) 2021. Davi Pereira dos Santos
#  This file is part of the idict project.
#  Please respect the license - more about this in the section (*) below.
#
#  idict is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  idict is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with idict.  If not, see <http://www.gnu.org/licenses/>.
#
#  (*) Removing authorship by any means, e.g. by distribution of derived
#  works or verbatim, obfuscated, compiled or rewritten versions of any
#  part of this work is illegal and unethical regarding the effort and
#  time spent here.
from typing import Callable, Dict, Union

from ldict.core.base import AbstractLazyDict
from ldict.exception import DependenceException, UndefinedSeed

from idict.core.frozenidentifieddict import FrozenIdentifiedDict
from idict.core.idict_ import Idict
from idict.parameter.ilet import iLet


class iEmpty(FrozenIdentifiedDict):
    def __init__(self):
        super().__init__()

    def __rrshift__(self, left: Union[Dict, Callable, iLet]):
        if isinstance(left, dict) and not isinstance(left, AbstractLazyDict):
            return Idict(left, identity=self.identity)
        if callable(left):
            return iLet(left)
        if isinstance(left, iLet):
            return left
        return NotImplemented  # pragma: no cover

    def __rshift__(self, other: Union[Dict, Callable]):
        if isinstance(other, AbstractLazyDict):
            return other
        if isinstance(other, dict):
            return Idict(other, identity=self.identity)
        if callable(other):
            try:
                return Idict(identity=self.identity) >> other
            except (DependenceException, UndefinedSeed):
                return iLet(other)
        return NotImplemented  # pragma: no cover

    __mul__ = __rshift__

Classes

class iEmpty

Immutable lazy universally identified dict for serializable (picklable) pairs str->value

Usage:

>>> idict = FrozenIdentifiedDict
>>> idict().show(colored=False)
{
    "_id": "0000000000000000000000000000000000000000",
    "_ids": {}
}
>>> d = idict(x=5, y=3)
>>> d.show(colored=False)
{
    "x": 5,
    "y": 3,
    "_id": "pl_8763f6625970707cdef7dfd31096db4c63c91",
    "_ids": {
        "x": "GS_cb0fda15eac732cb08351e71fc359058b93bd (content: Mj_3bcd9aefb5020343384ae8ccb88fbd872cd8f)",
        "y": "Ku_f738128d6e27a03ec6c2e76d23514b4e998e3 (content: S5_331b7e710abd1443cd82d6b5cdafb9f04d5ab)"
    }
}
>>> d["y"]
3
>>> idict(y=88, x=123123).show(colored=False)
{
    "y": 88,
    "x": 123123,
    "_id": "BZ_2ef6b00691651b6f815ba62b93416aa9b3e80",
    "_ids": {
        "y": "-j_34f477bff0a6dac76fe447d0e95003e273c62 (content: 6X_ceb4a9cfad6b3b5a56b49c3484be71dff692a)",
        "x": "CF_13e3494690e330a722667f4ab9e067c64022e (content: I6_bcc6fa41b424962f427be985853b8416b2cff)"
    }
}
>>> d = idict(x=123123, y=88)
>>> d2 = d >> (lambda x: {"z": x**2})
>>> d2.hosh == d2.identity * d2.ids["z"] * d2.ids["x"] * d2.ids["y"]
True
>>> e = d2 >> (lambda x,y: {"w": x/y})
>>> e.show(colored=False)  # doctest:+ELLIPSIS
{
    "w": "→(x y)",
    "z": "→(x)",
    "x": 123123,
    "y": 88,
    "_id": "...",
    "_ids": {
        "w": "...",
        "z": "...",
        "x": "CF_13e3494690e330a722667f4ab9e067c64022e (content: I6_bcc6fa41b424962f427be985853b8416b2cff)",
        "y": "-j_34f477bff0a6dac76fe447d0e95003e273c62 (content: 6X_ceb4a9cfad6b3b5a56b49c3484be71dff692a)"
    }
}
>>> a = d >> (lambda x: {"z": x**2}) >> (lambda x, y: {"w": x/y})
>>> b = d >> (lambda x, y: {"w": x/y}) >> (lambda x: {"z": x**2})
>>> dic = d.asdict  # Converting to dict
>>> dic
{'x': 123123, 'y': 88, '_id': 'BZ_2ef6b00691651b6f815ba62b93416aa9b3e80', '_ids': {'x': 'CF_13e3494690e330a722667f4ab9e067c64022e', 'y': '-j_34f477bff0a6dac76fe447d0e95003e273c62'}}
>>> d2 = idict(dic)  # Reconstructing from a dict
>>> d2.show(colored=False)
{
    "x": 123123,
    "y": 88,
    "_id": "BZ_2ef6b00691651b6f815ba62b93416aa9b3e80",
    "_ids": {
        "x": "CF_13e3494690e330a722667f4ab9e067c64022e",
        "y": "-j_34f477bff0a6dac76fe447d0e95003e273c62"
    }
}
>>> d == d2
True
>>> from idict import Ø, setup
>>> d = idict() >> {"x": "more content"}
>>> d.show(colored=False)
{
    "x": "more content",
    "_id": "zd_6a3f5ec407f6c5c24b89b35ff7cc1793969fd",
    "_ids": {
        "x": "zd_6a3f5ec407f6c5c24b89b35ff7cc1793969fd"
    }
}
>>> f = lambda x,y: {"z":x+y}
>>> d = idict(x=5, y=7)
>>> d2 = d >> f
>>> d2.show(colored=False)  # doctest:+ELLIPSIS
{
    "z": "→(x y)",
    "x": 5,
    "y": 7,
    "_id": "...",
    "_ids": {
        "z": "...",
        "x": "GS_cb0fda15eac732cb08351e71fc359058b93bd (content: Mj_3bcd9aefb5020343384ae8ccb88fbd872cd8f)",
        "y": "WK_6ba95267cec724067d58b3186ecbcaa4253ad (content: 3m_131910d18a892d1b64285250092a4967c8065)"
    }
}
>>> c = {}
>>> d3 = d2 >> [c]
>>> d3.show(colored=False)  # doctest:+ELLIPSIS
{
    "z": "→(↑ x y)",
    "x": 5,
    "y": 7,
    "_id": "...",
    "_ids": {
        "z": "...",
        "x": "GS_cb0fda15eac732cb08351e71fc359058b93bd (content: Mj_3bcd9aefb5020343384ae8ccb88fbd872cd8f)",
        "y": "WK_6ba95267cec724067d58b3186ecbcaa4253ad (content: 3m_131910d18a892d1b64285250092a4967c8065)"
    }
}
>>> c
{}
>>> d3.z
12
>>> c  # doctest:+ELLIPSIS
{'...': 12, 'GS_cb0fda15eac732cb08351e71fc359058b93bd': 5, 'WK_6ba95267cec724067d58b3186ecbcaa4253ad': 7, '...': {'_id': '...', '_ids': {'z': '...', 'x': 'GS_cb0fda15eac732cb08351e71fc359058b93bd', 'y': 'WK_6ba95267cec724067d58b3186ecbcaa4253ad'}}}
>>> d3.show(colored=False)  # doctest:+ELLIPSIS
{
    "z": 12,
    "x": 5,
    "y": 7,
    "_id": "...",
    "_ids": {
        "z": "...",
        "x": "GS_cb0fda15eac732cb08351e71fc359058b93bd (content: Mj_3bcd9aefb5020343384ae8ccb88fbd872cd8f)",
        "y": "WK_6ba95267cec724067d58b3186ecbcaa4253ad (content: 3m_131910d18a892d1b64285250092a4967c8065)"
    }
}
>>> c = {}
>>> setup(cache=c)
>>> d3 = d >> f ^ Ø
>>> d3.show(colored=False)  # doctest:+ELLIPSIS
{
    "z": "→(↑ x y)",
    "x": 5,
    "y": 7,
    "_id": "...",
    "_ids": {
        "z": "...",
        "x": "GS_cb0fda15eac732cb08351e71fc359058b93bd (content: Mj_3bcd9aefb5020343384ae8ccb88fbd872cd8f)",
        "y": "WK_6ba95267cec724067d58b3186ecbcaa4253ad (content: 3m_131910d18a892d1b64285250092a4967c8065)"
    }
}
>>> c
{}
>>> d3.z
12
>>> c  # doctest:+ELLIPSIS
{'...': 12, 'GS_cb0fda15eac732cb08351e71fc359058b93bd': 5, 'WK_6ba95267cec724067d58b3186ecbcaa4253ad': 7, '...': {'_id': '...', '_ids': {'z': '...', 'x': 'GS_cb0fda15eac732cb08351e71fc359058b93bd', 'y': 'WK_6ba95267cec724067d58b3186ecbcaa4253ad'}}}
>>> d3.show(colored=False)  # doctest:+ELLIPSIS
{
    "z": 12,
    "x": 5,
    "y": 7,
    "_id": "...",
    "_ids": {
        "z": "...",
        "x": "GS_cb0fda15eac732cb08351e71fc359058b93bd (content: Mj_3bcd9aefb5020343384ae8ccb88fbd872cd8f)",
        "y": "WK_6ba95267cec724067d58b3186ecbcaa4253ad (content: 3m_131910d18a892d1b64285250092a4967c8065)"
    }
}
Expand source code
class iEmpty(FrozenIdentifiedDict):
    def __init__(self):
        super().__init__()

    def __rrshift__(self, left: Union[Dict, Callable, iLet]):
        if isinstance(left, dict) and not isinstance(left, AbstractLazyDict):
            return Idict(left, identity=self.identity)
        if callable(left):
            return iLet(left)
        if isinstance(left, iLet):
            return left
        return NotImplemented  # pragma: no cover

    def __rshift__(self, other: Union[Dict, Callable]):
        if isinstance(other, AbstractLazyDict):
            return other
        if isinstance(other, dict):
            return Idict(other, identity=self.identity)
        if callable(other):
            try:
                return Idict(identity=self.identity) >> other
            except (DependenceException, UndefinedSeed):
                return iLet(other)
        return NotImplemented  # pragma: no cover

    __mul__ = __rshift__

Ancestors

  • FrozenIdentifiedDict
  • ldict.core.base.AbstractLazyDict
  • collections.UserDict
  • collections.abc.MutableMapping
  • collections.abc.Mapping
  • collections.abc.Collection
  • collections.abc.Sized
  • collections.abc.Iterable
  • collections.abc.Container
  • builtins.dict
  • typing.Generic

Class variables

var hosh : garoupa.hosh.Hosh

Inherited members