Module xtuples

Expand source code
# ---------------------------------------------------------------

import json

import abc
import typing
import dataclasses
import collections

import operator
import itertools
import functools

# ---------------------------------------------------------------

# NOTE: at worst, not worse, than an un-optimised canonical solution

# where I cribbed from the itertools recipes (and other python docs), all credit to the original authors.

# where i didn't, i probably should have.

# ---------------------------------------------------------------

REGISTRY = {}

# ---------------------------------------------------------------

# TODO: some kind of validation placeholder?
# called in init, eg. quarter in [1 .. 4]

class nTuple(abc.ABC):

    @abc.abstractmethod
    def __abstract__(self):
        # NOTE: here to prevent initialise instances of this
        # but rather use the decorator and typing.NamedTuple
        return

    @staticmethod
    def pipe(obj, f, *args, **kwargs):
        return f(obj, *args, **kwargs)

    @staticmethod
    def partial(obj, f, *args, **kwargs):
        return functools.partial(f, obj, *args, **kwargs)

    @classmethod
    def is_subclass(cls, t):
        """
        >>> nTuple.is_subclass(tuple)
        False
        >>> nTuple.is_subclass(Example(1, "a"))
        False
        >>> nTuple.is_subclass(Example)
        True
        """
        try:
            is_sub = issubclass(t, tuple)
        except:
            is_sub = False
        return (
            is_sub and
            hasattr(t, "cls") and
            hasattr(t, "pipe") and
            hasattr(t, "partial")
        )

    @classmethod
    def is_instance(cls, obj):
        """
        >>> nTuple.is_instance(tuple)
        False
        >>> nTuple.is_instance(Example)
        False
        >>> nTuple.is_instance(Example(1, "a"))
        True
        """
        return (
            cls.is_subclass(type(obj)) and
            hasattr(obj, '_asdict') and
            hasattr(obj, '_fields')
        )


    @staticmethod
    def annotations(obj):
        """
        >>> ex = Example(1, "a")
        >>> ex.pipe(ex.cls.annotations)
        {'x': <class 'int'>, 's': <class 'str'>, 'it': <class 'xtuples.xtuples.iTuple'>}
        """
        return fDict(obj.__annotations__)

    @classmethod
    def as_dict(cls, obj):
        """
        >>> ex = Example(1, "a")
        >>> ex.pipe(ex.cls.as_dict)
        {'x': 1, 's': 'a', 'it': iTuple()}
        """
        return fDict(obj._asdict())

    @classmethod
    def cast_json(cls, obj):
        """
        >>> ex = Example(1, "a")
        >>> ex.pipe(ex.cls.cast_json)
        {'x': 1, 's': 'a', 'it': {'__t__': 'iTuple', 'data': []}, '__t__': 'Example'}
        """
        d = {
            k: cast_json(v)
            for k, v in obj._asdict().items()
            #
        }
        d["__t__"] = type(obj).__name__
        return d

    @classmethod
    def uncast_json(meta, obj):
        """
        >>> ex = Example(1, "a")
        >>> ex.cls.uncast_json(ex.pipe(ex.cls.cast_json))
        Example(x=1, s='a', it=iTuple())
        """
        cls = REGISTRY[obj["__t__"]]
        return cls(
            *(
                uncast_json(v)
                for k, v in obj.items() if k != "__t__"
            )
        )

    @classmethod
    def decorate(meta, cls):
        assert cls.__name__ not in REGISTRY
        cls.pipe = meta.pipe
        cls.partial = meta.partial
        cls.cls = meta
        REGISTRY[cls.__name__] = cls
        return cls

# ---------------------------------------------------------------

class fDict(collections.UserDict):
    __slots__ = ()

    data: dict

    def pipe(self, f, *args, **kwargs):
        """
        >>> fDict({0: 1}).pipe(lambda d: d.map_values(
        ...     lambda v: v + 1
        ... ))
        {0: 2}
        """
        res = f(self, *args, **kwargs)
        if isinstance(res, dict):
            return fDict(res)
        return res

    def partial(self, f, *args, **kwargs):
        """
        >>> f = fDict({0: 1}).partial(
        ...     lambda d, n: d.map_values(lambda v: v + n)
        ... )
        >>> f(1)
        {0: 2}
        >>> f(2)
        {0: 3}
        """
        return functools.partial(f, self, *args, **kwargs)

    def keys_tuple(self):
        """
        >>> fDict({0: 1}).keys_tuple()
        iTuple(0)
        """
        return iTuple.from_keys(self)

    def values_tuple(self):
        """
        >>> fDict({0: 1}).values_tuple()
        iTuple(1)
        """
        return iTuple.from_values(self)
    
    def items_tuple(self):
        """
        >>> fDict({0: 1}).items_tuple()
        iTuple((0, 1))
        """
        return iTuple.from_items(self)

    # NOTE: we have separate map implementations 
    # as they are constant size, dict to dict
    # other iterator functions should use iTuple (from the above)

    def map_keys(self, f, *args, **kwargs):
        """
        >>> fDict({0: 1}).map_keys(lambda v: v + 1)
        {1: 1}
        """
        return fDict(
            (f(k, *args, **kwargs), v) for k, v in self.items()
        )

    def map_values(self, f, *args, **kwargs):
        """
        >>> fDict({0: 1}).map_values(lambda v: v + 1)
        {0: 2}
        """
        return fDict(
            (k, f(v, *args, **kwargs)) for k, v in self.items()
        )

    def map_items(self, f, *args, **kwargs):
        """
        >>> fDict({0: 1}).map_items(lambda k, v: (v, k))
        {1: 0}
        """
        return fDict(
            f(k, v, *args, **kwargs) for k, v in self.items()
        )

    def invert(self):
        """
        >>> fDict({0: 1}).invert()
        {1: 0}
        """
        return fDict((v, k) for k, v in self.items())

# ---------------------------------------------------------------

@dataclasses.dataclass(init = False, repr=True)
class iTuple(collections.UserList, tuple): # type: ignore
    __slots__ = ()

    data: tuple # type: ignore

    # -----

    @staticmethod
    def __new__(cls, data = None):
        # NOTE: we use cls not array
        # so sub-classing *does* change identity
        if isinstance(data, cls):
            return data
        return super().__new__(cls, data=data)

    @staticmethod
    def wrap_tuple(data):
        return data if isinstance(data, tuple) else tuple(data)
    
    def __init__(self, data = None):
        # TODO: option for lazy init?
        self.data = (
            tuple() if data is None
            else self.wrap_tuple(data)
        )

    def __repr__(self):
        s = super().__repr__()
        return "{}({})".format(
            type(self).__name__,
            s[1:-2 if s[-2] == "," else -1],
        )

    def __hash__(self):
        return hash(self.data)

    @classmethod
    def decorate(meta, cls):
        assert cls.__name__ not in REGISTRY
        REGISTRY[cls.__name__] = cls
        return cls

    # -----

    def cast_json(self):
        """
        >>> iTuple.range(1).cast_json()
        {'__t__': 'iTuple', 'data': [0]}
        """
        return dict(
            __t__ = type(self).__name__,
            data = list(self.map(cast_json)),
        )

    @classmethod
    def uncast_json(cls, obj):
        """
        >>> iTuple.uncast_json(iTuple.range(1).cast_json())
        iTuple(0)
        """
        assert obj["__t__"] == cls.__name__
        return cls(data=obj["data"])

    # -----

    @classmethod
    def range(cls, *args, **kwargs):
        """
        >>> iTuple.range(3)
        iTuple(0, 1, 2)
        """
        return cls(range(*args, **kwargs))

    @classmethod
    def from_keys(cls, d):
        """
        >>> iTuple.from_keys({i: i + 1 for i in range(2)})
        iTuple(0, 1)
        """
        return cls(d.keys())
        
    @classmethod
    def from_values(cls, d):
        """
        >>> iTuple.from_values({i: i + 1 for i in range(2)})
        iTuple(1, 2)
        """
        return cls(d.values())
        
    @classmethod
    def from_items(cls, d):
        """
        >>> iTuple.from_items({i: i + 1 for i in range(2)})
        iTuple((0, 1), (1, 2))
        """
        return cls(d.items())

    # -----

    def pipe(self, f, *args, **kwargs):
        """
        >>> iTuple.range(2).pipe(lambda it: it)
        iTuple(0, 1)
        >>> iTuple.range(2).pipe(
        ...     lambda it, v: it.map(lambda x: x * v), 2
        ... )
        iTuple(0, 2)
        """
        return f(self, *args, **kwargs)

    def partial(self, f, *args, **kwargs):
        """
        >>> f = iTuple.range(2).partial(
        ...     lambda it, v: it.map(lambda x: x * v)
        ... )
        >>> f(2)
        iTuple(0, 2)
        >>> f(3)
        iTuple(0, 3)
        """
        return functools.partial(f, self, *args, **kwargs)

    # -----

    def len(self):
        """
        >>> iTuple.range(3).len()
        3
        """
        return len(self)

    def append(self, value, *values):
        """
        >>> iTuple.range(1).append(1)
        iTuple(0, 1)
        >>> iTuple.range(1).append(1, 2)
        iTuple(0, 1, 2)
        >>> iTuple.range(1).append(1, 2, 3)
        iTuple(0, 1, 2, 3)
        >>> iTuple.range(1).append(1, (2,))
        iTuple(0, 1, (2,))
        """
        return self + (value, *values)

    def prepend(self, value, *values):
        """
        >>> iTuple.range(1).prepend(1)
        iTuple(1, 0)
        >>> iTuple.range(1).prepend(1, 2)
        iTuple(1, 2, 0)
        >>> iTuple.range(1).prepend(1, 2, 3)
        iTuple(1, 2, 3, 0)
        >>> iTuple.range(1).prepend(1, (2,))
        iTuple(1, (2,), 0)
        """
        return (value, *values) + self

    def zip(self, *itrs, lazy = False):
        """
        >>> iTuple([[1, 1], [2, 2], [3, 3]]).zip()
        iTuple((1, 2, 3), (1, 2, 3))
        >>> iTuple([iTuple.range(3), iTuple.range(1, 4)]).zip()
        iTuple((0, 1), (1, 2), (2, 3))
        >>> iTuple.range(3).zip(iTuple.range(1, 4))
        iTuple((0, 1), (1, 2), (2, 3))
        """
        if len(itrs) == 0:
            res = zip(*self)
        else:
            res = zip(self, *itrs)
        return res if lazy else iTuple(data=res)

    def flatten(self):
        """
        >>> iTuple.range(3).map(lambda x: [x]).flatten()
        iTuple(0, 1, 2)
        """
        return iTuple(itertools.chain(*self))

    def extend(self, value, *values):
        """
        >>> iTuple.range(1).extend((1,))
        iTuple(0, 1)
        >>> iTuple.range(1).extend([1])
        iTuple(0, 1)
        >>> iTuple.range(1).extend([1], [2])
        iTuple(0, 1, 2)
        >>> iTuple.range(1).extend([1], [[2]])
        iTuple(0, 1, [2])
        >>> iTuple.range(1).extend([1], [[2]], [2])
        iTuple(0, 1, [2], 2)
        """
        return iTuple(itertools.chain.from_iterable(
            (self, value, *values)
        ))

    def pretend(self, value, *values):
        """
        >>> iTuple.range(1).pretend((1,))
        iTuple(1, 0)
        >>> iTuple.range(1).pretend([1])
        iTuple(1, 0)
        >>> iTuple.range(1).pretend([1], [2])
        iTuple(1, 2, 0)
        >>> iTuple.range(1).pretend([1], [[2]])
        iTuple(1, [2], 0)
        >>> iTuple.range(1).pretend([1], [[2]], [2])
        iTuple(1, [2], 2, 0)
        """
        return iTuple(itertools.chain.from_iterable(
            (value, *values, self)
        ))

    def filter_eq(self, v, f = None, eq = None, lazy = False):
        """
        >>> iTuple.range(3).filter_eq(1)
        iTuple(1)
        """
        if f is None and eq is None:
            res = filter(lambda x: x == v, self)
        elif f is not None:
            res = filter(lambda x: f(x) == v, self)
        elif eq is not None:
            res = filter(lambda x: eq(x, v), self)
        else:
            res = filter(lambda x: eq(f(x), v), self)
        return res if lazy else type(self)(data=res)

    def filter(self, f, eq = None, lazy = False):
        """
        >>> iTuple.range(3).filter(lambda x: x > 1)
        iTuple(2)
        """
        return self.filter_eq(True, f = f, eq = eq, lazy = lazy)

    def map(self, f, *iterables, lazy = False):
        """
        >>> iTuple.range(3).map(lambda x: x * 2)
        iTuple(0, 2, 4)
        """
        res = map(f, self, *iterables)
        return res if lazy else iTuple(data=res)

    def enumerate(self):
        """
        >>> iTuple.range(3).enumerate()
        iTuple((0, 0), (1, 1), (2, 2))
        """
        # TODO: allow lazy
        return iTuple(enumerate(self))

    def groupby(
        self, 
        f, 
        lazy = False, 
        keys = False,
        pipe= None,
    ):
        """
        >>> iTuple.range(3).groupby(lambda x: x < 2)
        iTuple((0, 1), (2,))
        >>> iTuple.range(3).groupby(
        ...    lambda x: x < 2, keys=True, pipe=fDict
        ... )
        {True: (0, 1), False: (2,)}
        """
        # TODO: lazy no keys
        res = itertools.groupby(self, key=f)
        if lazy and keys and pipe is None:
            return res
        if pipe is None:
            pipe = iTuple
        if keys:
            return pipe((k, tuple(g),) for k, g in res)
        else:
            return pipe(tuple(g) for k, g in res)

    def first(self):
        """
        >>> iTuple.range(3).first()
        0
        """
        return self[0]
    
    def last(self):
        """
        >>> iTuple.range(3).last()
        2
        """
        return self[-1]

    def first_where(self, f):
        """
        >>> iTuple.range(3).first_where(lambda v: v > 0)
        1
        """
        for v in self:
            if f(v):
                return v
        return None

    def last_where(self, f):
        """
        >>> iTuple.range(3).last_where(lambda v: v < 2)
        1
        """
        for v in reversed(self):
            if f(v):
                return v
        return None

    def take(self, n):
        """
        >>> iTuple.range(3).take(2)
        iTuple(0, 1)
        """
        return self[:n]

    def tail(self, n):
        """
        >>> iTuple.range(3).tail(2)
        iTuple(1, 2)
        """
        return self[-n:]

    def reverse(self, lazy = False):
        """
        >>> iTuple.range(3).reverse()
        iTuple(2, 1, 0)
        """
        if lazy:
            return reversed(self)
        return type(self)(data=reversed(self))

    def take_while(self, f, n = None, lazy = False):
        """
        >>> iTuple.range(3).take_while(lambda v: v < 1)
        iTuple(0)
        """
        def iter():
            i = 0
            for v in self:
                if f(v) and (n is None or i < n):
                    yield v
                    i += 1
                else:
                    return
        res = iter()
        return res if lazy else type(self)(data=res)

    def tail_while(self, f, n = None):
        """
        >>> iTuple.range(3).tail_while(lambda v: v > 1)
        iTuple(2)
        """
        i = 0
        for v in reversed(self):
            if f(v) and (n is None or i < n):
                i += 1
            else:
                break
        return self.tail(i)

    # NOTE: from as in, starting from first true
    # versus above, which is until first false
    def take_after(self, f, n = None, lazy = False):
        """
        >>> iTuple.range(3).take_after(lambda v: v < 1)
        iTuple(1, 2)
        >>> iTuple.range(3).take_after(lambda v: v < 1, n = 1)
        iTuple(1)
        """
        def iter():
            i = 0
            for v in self:
                if f(v):
                    pass
                elif n is None or i < n:
                    yield v
                    i += 1
                else:
                    return
        res = iter()
        return res if lazy else type(self)(data=res)

    def tail_after(self, f, n = None):
        """
        >>> iTuple.range(3).tail_after(lambda v: v < 2)
        iTuple(0, 1)
        >>> iTuple.range(3).tail_after(lambda v: v < 2, 1)
        iTuple(1)
        """
        l = 0
        r = 0
        for v in reversed(self):
            if not f(v):
                l += 1
            elif n is None or r < n:
                r += 1
            else:
                break
        return self.tail(l + r).take(r)

    def islice(self, left = None, right = None):
        """
        >>> iTuple.range(5).islice(1, 3)
        iTuple(1, 2)
        """
        return self[left:right]

    def unique(self):
        """
        >>> iTuple([1, 1, 3, 2, 4, 2, 3]).unique()
        iTuple(1, 3, 2, 4)
        """
        def iter():
            seen = set()
            seen_add = seen.add
            seen_contains = seen.__contains__
            for v in itertools.filterfalse(seen_contains, self):
                seen_add(v)
                yield v
        return type(self)(data=iter())
    
    def sort(self, f = lambda v: v):
        """
        >>> iTuple.range(3).reverse().sort()
        iTuple(0, 1, 2)
        >>> iTuple.range(3).sort()
        iTuple(0, 1, 2)
        """
        return type(self)(data=sorted(self, key = f))

    def accumulate(self, f, initial = None, lazy = False):
        """
        >>> iTuple.range(3).accumulate(lambda acc, v: v)
        iTuple(0, 1, 2)
        >>> iTuple.range(3).accumulate(lambda acc, v: v, initial=0)
        iTuple(0, 0, 1, 2)
        >>> iTuple.range(3).accumulate(operator.add)
        iTuple(0, 1, 3)
        """
        res = itertools.accumulate(self, func=f, initial=initial)
        return res if lazy else iTuple(data=res)

    def foldcum(self, *args, **kwargs):
        """
        >>> iTuple.range(3).foldcum(lambda acc, v: v)
        iTuple(0, 1, 2)
        >>> iTuple.range(3).foldcum(operator.add)
        iTuple(0, 1, 3)
        """
        return self.accumulate(*args, **kwargs)

    def fold(self, f, initial=None):
        """
        >>> iTuple.range(3).fold(lambda acc, v: v)
        2
        >>> iTuple.range(3).fold(lambda acc, v: v, initial=0)
        2
        >>> iTuple.range(3).fold(operator.add)
        3
        """
        if initial is not None:
            res = functools.reduce(f, self, initial)
        else:
            res = functools.reduce(f, self)
        return res

    # -----

    # combinatorics

    # -----

# ---------------------------------------------------------------

@nTuple.decorate
class Example(typing.NamedTuple):
    """
    >>> ex = Example(1, "a")
    >>> ex
    Example(x=1, s='a', it=iTuple())
    >>> ex.cls
    <class 'xtuples.xtuples.nTuple'>
    >>> ex.pipe(lambda nt: nt.x)
    1
    >>> f = ex.partial(lambda nt, v: nt.x * v)
    >>> f(2)
    2
    >>> f(3)
    3
    """
    # NOTE: cls, pipe, partial are mandatory boilerplate

    x: int
    s: str
    it: iTuple = iTuple([])

    @property
    def cls(self):
        ...

    def pipe(self, f, *args, **kwargs):
        ...

    def partial(self, f, *args, **kwargs):
        ...

# ---------------------------------------------------------------

# TODO: context manager to control
# if we add the type information when writing to json or not

# TODO: context mananger to control
# lazy default behaviour (ie. default to lazy or not)

# ---------------------------------------------------------------

class JSONEncoder(json.JSONEncoder):

    def iterencode(self, o, *args, **kwargs):
        for chunk in super().iterencode(
            cast_json(o), *args, **kwargs
        ):
            yield chunk

    # def meta_default(self, obj):
    #     return json.JSONEncoder.default(self, obj)

    # def default(self, obj):
    #     if isinstance(obj, fDict):
    #         return self.meta_default(obj.data)
    #     return cast_json(obj, default=self.meta_default)

# -----

class JSONDecoder(json.JSONDecoder):

    def __init__(self, *args, **kwargs):
        json.JSONDecoder.__init__(
            self,
            object_hook=self.object_hook,
            *args,
            **kwargs
            #
        )

    @classmethod
    def xtuple_object_hook(cls, d):
        return uncast_json(d)

    def object_hook(self, d):
        return self.xtuple_object_hook(d)

# -----

def cast_json(obj, default = lambda obj: obj):
    if nTuple.is_instance(obj):
        return nTuple.cast_json(obj)
    try:
        return obj.cast_json()
    except:
        return default(obj)

def uncast_json(obj):
    if not isinstance(obj, dict):
        return obj
    __t__ = obj.get("__t__", None)
    if __t__ is None:
        return obj
    cls = iTuple if __t__ == "iTuple" else REGISTRY[__t__]
    if hasattr(cls, "uncast_json"):
        return cls.uncast_json(obj)
    return cls(
        *(v for k, v in obj.items() if k != "__t__")
    )

# -----

# TODO: fString so can do .pipe ?
def to_json(v, **kwargs):
    """
    >>> print(iTuple([Example(1, "a")]).pipe(to_json, indent=2))
    {
      "__t__": "iTuple",
      "data": [
        {
          "x": 1,
          "s": "a",
          "it": {
            "__t__": "iTuple",
            "data": []
          },
          "__t__": "Example"
        }
      ]
    }
    >>> print(iTuple([
    ...     iTuple([Example(1, "a")])
    ... ]).pipe(to_json, indent=2))
    {
      "__t__": "iTuple",
      "data": [
        {
          "__t__": "iTuple",
          "data": [
            {
              "x": 1,
              "s": "a",
              "it": {
                "__t__": "iTuple",
                "data": []
              },
              "__t__": "Example"
            }
          ]
        }
      ]
    }
    >>> print(Example(2, "b", iTuple([
    ...     iTuple([Example(1, "a")])
    ... ])).pipe(to_json, indent=2))
    {
      "x": 2,
      "s": "b",
      "it": {
        "__t__": "iTuple",
        "data": [
          {
            "__t__": "iTuple",
            "data": [
              {
                "x": 1,
                "s": "a",
                "it": {
                  "__t__": "iTuple",
                  "data": []
                },
                "__t__": "Example"
              }
            ]
          }
        ]
      },
      "__t__": "Example"
    }
    """
    return json.dumps(v, cls=JSONEncoder, **kwargs)

def from_json(v: str, **kwargs):
    """
    >>> ex = iTuple([Example(1, "a")])
    >>> from_json(ex.pipe(to_json))
    iTuple(Example(x=1, s='a', it=iTuple()))
    >>> from_json(
    ...     iTuple([iTuple([Example(1, "a")])]).pipe(to_json)
    ... )
    iTuple(iTuple(Example(x=1, s='a', it=iTuple())))
    >>> from_json(
    ...     Example(2, "b", iTuple([
    ...         iTuple([Example(1, "a")])
    ...     ])).pipe(to_json)
    ... )
    Example(x=2, s='b', it=iTuple(iTuple(Example(x=1, s='a', it=iTuple()))))
    """
    return json.loads(v, cls=JSONDecoder, **kwargs)

def load_json(f):
    return json.load(f, cls=JSONDecoder)

def dump_json(f, v):
    return json.dump(f, v, cls=JSONEncoder)

# ---------------------------------------------------------------

__all__ = [
    "iTuple",
    "nTuple",
    "fDict",
    "JSONDecoder",
    "JSONEncoder",
]

# ---------------------------------------------------------------

Classes

class JSONDecoder (*args, **kwargs)

Simple JSON http://json.org decoder

Performs the following translations in decoding by default:

+---------------+-------------------+ | JSON | Python | +===============+===================+ | object | dict | +---------------+-------------------+ | array | list | +---------------+-------------------+ | string | str | +---------------+-------------------+ | number (int) | int | +---------------+-------------------+ | number (real) | float | +---------------+-------------------+ | true | True | +---------------+-------------------+ | false | False | +---------------+-------------------+ | null | None | +---------------+-------------------+

It also understands NaN, Infinity, and -Infinity as their corresponding float values, which is outside the JSON spec.

object_hook, if specified, will be called with the result of every JSON object decoded and its return value will be used in place of the given dict. This can be used to provide custom deserializations (e.g. to support JSON-RPC class hinting).

object_pairs_hook, if specified will be called with the result of every JSON object decoded with an ordered list of pairs. The return value of object_pairs_hook will be used instead of the dict. This feature can be used to implement custom decoders. If object_hook is also defined, the object_pairs_hook takes priority.

parse_float, if specified, will be called with the string of every JSON float to be decoded. By default this is equivalent to float(num_str). This can be used to use another datatype or parser for JSON floats (e.g. decimal.Decimal).

parse_int, if specified, will be called with the string of every JSON int to be decoded. By default this is equivalent to int(num_str). This can be used to use another datatype or parser for JSON integers (e.g. float).

parse_constant, if specified, will be called with one of the following strings: -Infinity, Infinity, NaN. This can be used to raise an exception if invalid JSON numbers are encountered.

If strict is false (true is the default), then control characters will be allowed inside strings. Control characters in this context are those with character codes in the 0-31 range, including '\t' (tab), '\n', '\r' and '\0'.

Expand source code
class JSONDecoder(json.JSONDecoder):

    def __init__(self, *args, **kwargs):
        json.JSONDecoder.__init__(
            self,
            object_hook=self.object_hook,
            *args,
            **kwargs
            #
        )

    @classmethod
    def xtuple_object_hook(cls, d):
        return uncast_json(d)

    def object_hook(self, d):
        return self.xtuple_object_hook(d)

Ancestors

  • json.decoder.JSONDecoder

Static methods

def xtuple_object_hook(d)
Expand source code
@classmethod
def xtuple_object_hook(cls, d):
    return uncast_json(d)

Methods

def object_hook(self, d)
Expand source code
def object_hook(self, d):
    return self.xtuple_object_hook(d)
class JSONEncoder (*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Extensible JSON http://json.org encoder for Python data structures.

Supports the following objects and types by default:

+-------------------+---------------+ | Python | JSON | +===================+===============+ | dict | object | +-------------------+---------------+ | list, tuple | array | +-------------------+---------------+ | str | string | +-------------------+---------------+ | int, float | number | +-------------------+---------------+ | True | true | +-------------------+---------------+ | False | false | +-------------------+---------------+ | None | null | +-------------------+---------------+

To extend this to recognize other objects, subclass and implement a .default() method with another method that returns a serializable object for o if possible, otherwise it should call the superclass implementation (to raise TypeError).

Constructor for JSONEncoder, with sensible defaults.

If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.

If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.

If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an OverflowError). Otherwise, no such check takes place.

If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.

If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.

If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.

If specified, separators should be an (item_separator, key_separator) tuple. The default is (', ', ': ') if indent is None and (',', ': ') otherwise. To get the most compact JSON representation, you should specify (',', ':') to eliminate whitespace.

If specified, default is a function that gets called for objects that can't otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError.

Expand source code
class JSONEncoder(json.JSONEncoder):

    def iterencode(self, o, *args, **kwargs):
        for chunk in super().iterencode(
            cast_json(o), *args, **kwargs
        ):
            yield chunk

Ancestors

  • json.encoder.JSONEncoder

Methods

def iterencode(self, o, *args, **kwargs)

Encode the given object and yield each string representation as available.

For example::

for chunk in JSONEncoder().iterencode(bigobject):
    mysocket.write(chunk)
Expand source code
def iterencode(self, o, *args, **kwargs):
    for chunk in super().iterencode(
        cast_json(o), *args, **kwargs
    ):
        yield chunk
class fDict (dict=None, /, **kwargs)
Expand source code
class fDict(collections.UserDict):
    __slots__ = ()

    data: dict

    def pipe(self, f, *args, **kwargs):
        """
        >>> fDict({0: 1}).pipe(lambda d: d.map_values(
        ...     lambda v: v + 1
        ... ))
        {0: 2}
        """
        res = f(self, *args, **kwargs)
        if isinstance(res, dict):
            return fDict(res)
        return res

    def partial(self, f, *args, **kwargs):
        """
        >>> f = fDict({0: 1}).partial(
        ...     lambda d, n: d.map_values(lambda v: v + n)
        ... )
        >>> f(1)
        {0: 2}
        >>> f(2)
        {0: 3}
        """
        return functools.partial(f, self, *args, **kwargs)

    def keys_tuple(self):
        """
        >>> fDict({0: 1}).keys_tuple()
        iTuple(0)
        """
        return iTuple.from_keys(self)

    def values_tuple(self):
        """
        >>> fDict({0: 1}).values_tuple()
        iTuple(1)
        """
        return iTuple.from_values(self)
    
    def items_tuple(self):
        """
        >>> fDict({0: 1}).items_tuple()
        iTuple((0, 1))
        """
        return iTuple.from_items(self)

    # NOTE: we have separate map implementations 
    # as they are constant size, dict to dict
    # other iterator functions should use iTuple (from the above)

    def map_keys(self, f, *args, **kwargs):
        """
        >>> fDict({0: 1}).map_keys(lambda v: v + 1)
        {1: 1}
        """
        return fDict(
            (f(k, *args, **kwargs), v) for k, v in self.items()
        )

    def map_values(self, f, *args, **kwargs):
        """
        >>> fDict({0: 1}).map_values(lambda v: v + 1)
        {0: 2}
        """
        return fDict(
            (k, f(v, *args, **kwargs)) for k, v in self.items()
        )

    def map_items(self, f, *args, **kwargs):
        """
        >>> fDict({0: 1}).map_items(lambda k, v: (v, k))
        {1: 0}
        """
        return fDict(
            f(k, v, *args, **kwargs) for k, v in self.items()
        )

    def invert(self):
        """
        >>> fDict({0: 1}).invert()
        {1: 0}
        """
        return fDict((v, k) for k, v in self.items())

Ancestors

  • collections.UserDict
  • collections.abc.MutableMapping
  • collections.abc.Mapping
  • collections.abc.Collection
  • collections.abc.Sized
  • collections.abc.Iterable
  • collections.abc.Container

Class variables

var data : dict

Methods

def invert(self)
>>> fDict({0: 1}).invert()
{1: 0}
Expand source code
def invert(self):
    """
    >>> fDict({0: 1}).invert()
    {1: 0}
    """
    return fDict((v, k) for k, v in self.items())
def items_tuple(self)
>>> fDict({0: 1}).items_tuple()
iTuple((0, 1))
Expand source code
def items_tuple(self):
    """
    >>> fDict({0: 1}).items_tuple()
    iTuple((0, 1))
    """
    return iTuple.from_items(self)
def keys_tuple(self)
>>> fDict({0: 1}).keys_tuple()
iTuple(0)
Expand source code
def keys_tuple(self):
    """
    >>> fDict({0: 1}).keys_tuple()
    iTuple(0)
    """
    return iTuple.from_keys(self)
def map_items(self, f, *args, **kwargs)
>>> fDict({0: 1}).map_items(lambda k, v: (v, k))
{1: 0}
Expand source code
def map_items(self, f, *args, **kwargs):
    """
    >>> fDict({0: 1}).map_items(lambda k, v: (v, k))
    {1: 0}
    """
    return fDict(
        f(k, v, *args, **kwargs) for k, v in self.items()
    )
def map_keys(self, f, *args, **kwargs)
>>> fDict({0: 1}).map_keys(lambda v: v + 1)
{1: 1}
Expand source code
def map_keys(self, f, *args, **kwargs):
    """
    >>> fDict({0: 1}).map_keys(lambda v: v + 1)
    {1: 1}
    """
    return fDict(
        (f(k, *args, **kwargs), v) for k, v in self.items()
    )
def map_values(self, f, *args, **kwargs)
>>> fDict({0: 1}).map_values(lambda v: v + 1)
{0: 2}
Expand source code
def map_values(self, f, *args, **kwargs):
    """
    >>> fDict({0: 1}).map_values(lambda v: v + 1)
    {0: 2}
    """
    return fDict(
        (k, f(v, *args, **kwargs)) for k, v in self.items()
    )
def partial(self, f, *args, **kwargs)
>>> f = fDict({0: 1}).partial(
...     lambda d, n: d.map_values(lambda v: v + n)
... )
>>> f(1)
{0: 2}
>>> f(2)
{0: 3}
Expand source code
def partial(self, f, *args, **kwargs):
    """
    >>> f = fDict({0: 1}).partial(
    ...     lambda d, n: d.map_values(lambda v: v + n)
    ... )
    >>> f(1)
    {0: 2}
    >>> f(2)
    {0: 3}
    """
    return functools.partial(f, self, *args, **kwargs)
def pipe(self, f, *args, **kwargs)
>>> fDict({0: 1}).pipe(lambda d: d.map_values(
...     lambda v: v + 1
... ))
{0: 2}
Expand source code
def pipe(self, f, *args, **kwargs):
    """
    >>> fDict({0: 1}).pipe(lambda d: d.map_values(
    ...     lambda v: v + 1
    ... ))
    {0: 2}
    """
    res = f(self, *args, **kwargs)
    if isinstance(res, dict):
        return fDict(res)
    return res
def values_tuple(self)
>>> fDict({0: 1}).values_tuple()
iTuple(1)
Expand source code
def values_tuple(self):
    """
    >>> fDict({0: 1}).values_tuple()
    iTuple(1)
    """
    return iTuple.from_values(self)
class iTuple (data=None)

iTuple(data=None)

Expand source code
@dataclasses.dataclass(init = False, repr=True)
class iTuple(collections.UserList, tuple): # type: ignore
    __slots__ = ()

    data: tuple # type: ignore

    # -----

    @staticmethod
    def __new__(cls, data = None):
        # NOTE: we use cls not array
        # so sub-classing *does* change identity
        if isinstance(data, cls):
            return data
        return super().__new__(cls, data=data)

    @staticmethod
    def wrap_tuple(data):
        return data if isinstance(data, tuple) else tuple(data)
    
    def __init__(self, data = None):
        # TODO: option for lazy init?
        self.data = (
            tuple() if data is None
            else self.wrap_tuple(data)
        )

    def __repr__(self):
        s = super().__repr__()
        return "{}({})".format(
            type(self).__name__,
            s[1:-2 if s[-2] == "," else -1],
        )

    def __hash__(self):
        return hash(self.data)

    @classmethod
    def decorate(meta, cls):
        assert cls.__name__ not in REGISTRY
        REGISTRY[cls.__name__] = cls
        return cls

    # -----

    def cast_json(self):
        """
        >>> iTuple.range(1).cast_json()
        {'__t__': 'iTuple', 'data': [0]}
        """
        return dict(
            __t__ = type(self).__name__,
            data = list(self.map(cast_json)),
        )

    @classmethod
    def uncast_json(cls, obj):
        """
        >>> iTuple.uncast_json(iTuple.range(1).cast_json())
        iTuple(0)
        """
        assert obj["__t__"] == cls.__name__
        return cls(data=obj["data"])

    # -----

    @classmethod
    def range(cls, *args, **kwargs):
        """
        >>> iTuple.range(3)
        iTuple(0, 1, 2)
        """
        return cls(range(*args, **kwargs))

    @classmethod
    def from_keys(cls, d):
        """
        >>> iTuple.from_keys({i: i + 1 for i in range(2)})
        iTuple(0, 1)
        """
        return cls(d.keys())
        
    @classmethod
    def from_values(cls, d):
        """
        >>> iTuple.from_values({i: i + 1 for i in range(2)})
        iTuple(1, 2)
        """
        return cls(d.values())
        
    @classmethod
    def from_items(cls, d):
        """
        >>> iTuple.from_items({i: i + 1 for i in range(2)})
        iTuple((0, 1), (1, 2))
        """
        return cls(d.items())

    # -----

    def pipe(self, f, *args, **kwargs):
        """
        >>> iTuple.range(2).pipe(lambda it: it)
        iTuple(0, 1)
        >>> iTuple.range(2).pipe(
        ...     lambda it, v: it.map(lambda x: x * v), 2
        ... )
        iTuple(0, 2)
        """
        return f(self, *args, **kwargs)

    def partial(self, f, *args, **kwargs):
        """
        >>> f = iTuple.range(2).partial(
        ...     lambda it, v: it.map(lambda x: x * v)
        ... )
        >>> f(2)
        iTuple(0, 2)
        >>> f(3)
        iTuple(0, 3)
        """
        return functools.partial(f, self, *args, **kwargs)

    # -----

    def len(self):
        """
        >>> iTuple.range(3).len()
        3
        """
        return len(self)

    def append(self, value, *values):
        """
        >>> iTuple.range(1).append(1)
        iTuple(0, 1)
        >>> iTuple.range(1).append(1, 2)
        iTuple(0, 1, 2)
        >>> iTuple.range(1).append(1, 2, 3)
        iTuple(0, 1, 2, 3)
        >>> iTuple.range(1).append(1, (2,))
        iTuple(0, 1, (2,))
        """
        return self + (value, *values)

    def prepend(self, value, *values):
        """
        >>> iTuple.range(1).prepend(1)
        iTuple(1, 0)
        >>> iTuple.range(1).prepend(1, 2)
        iTuple(1, 2, 0)
        >>> iTuple.range(1).prepend(1, 2, 3)
        iTuple(1, 2, 3, 0)
        >>> iTuple.range(1).prepend(1, (2,))
        iTuple(1, (2,), 0)
        """
        return (value, *values) + self

    def zip(self, *itrs, lazy = False):
        """
        >>> iTuple([[1, 1], [2, 2], [3, 3]]).zip()
        iTuple((1, 2, 3), (1, 2, 3))
        >>> iTuple([iTuple.range(3), iTuple.range(1, 4)]).zip()
        iTuple((0, 1), (1, 2), (2, 3))
        >>> iTuple.range(3).zip(iTuple.range(1, 4))
        iTuple((0, 1), (1, 2), (2, 3))
        """
        if len(itrs) == 0:
            res = zip(*self)
        else:
            res = zip(self, *itrs)
        return res if lazy else iTuple(data=res)

    def flatten(self):
        """
        >>> iTuple.range(3).map(lambda x: [x]).flatten()
        iTuple(0, 1, 2)
        """
        return iTuple(itertools.chain(*self))

    def extend(self, value, *values):
        """
        >>> iTuple.range(1).extend((1,))
        iTuple(0, 1)
        >>> iTuple.range(1).extend([1])
        iTuple(0, 1)
        >>> iTuple.range(1).extend([1], [2])
        iTuple(0, 1, 2)
        >>> iTuple.range(1).extend([1], [[2]])
        iTuple(0, 1, [2])
        >>> iTuple.range(1).extend([1], [[2]], [2])
        iTuple(0, 1, [2], 2)
        """
        return iTuple(itertools.chain.from_iterable(
            (self, value, *values)
        ))

    def pretend(self, value, *values):
        """
        >>> iTuple.range(1).pretend((1,))
        iTuple(1, 0)
        >>> iTuple.range(1).pretend([1])
        iTuple(1, 0)
        >>> iTuple.range(1).pretend([1], [2])
        iTuple(1, 2, 0)
        >>> iTuple.range(1).pretend([1], [[2]])
        iTuple(1, [2], 0)
        >>> iTuple.range(1).pretend([1], [[2]], [2])
        iTuple(1, [2], 2, 0)
        """
        return iTuple(itertools.chain.from_iterable(
            (value, *values, self)
        ))

    def filter_eq(self, v, f = None, eq = None, lazy = False):
        """
        >>> iTuple.range(3).filter_eq(1)
        iTuple(1)
        """
        if f is None and eq is None:
            res = filter(lambda x: x == v, self)
        elif f is not None:
            res = filter(lambda x: f(x) == v, self)
        elif eq is not None:
            res = filter(lambda x: eq(x, v), self)
        else:
            res = filter(lambda x: eq(f(x), v), self)
        return res if lazy else type(self)(data=res)

    def filter(self, f, eq = None, lazy = False):
        """
        >>> iTuple.range(3).filter(lambda x: x > 1)
        iTuple(2)
        """
        return self.filter_eq(True, f = f, eq = eq, lazy = lazy)

    def map(self, f, *iterables, lazy = False):
        """
        >>> iTuple.range(3).map(lambda x: x * 2)
        iTuple(0, 2, 4)
        """
        res = map(f, self, *iterables)
        return res if lazy else iTuple(data=res)

    def enumerate(self):
        """
        >>> iTuple.range(3).enumerate()
        iTuple((0, 0), (1, 1), (2, 2))
        """
        # TODO: allow lazy
        return iTuple(enumerate(self))

    def groupby(
        self, 
        f, 
        lazy = False, 
        keys = False,
        pipe= None,
    ):
        """
        >>> iTuple.range(3).groupby(lambda x: x < 2)
        iTuple((0, 1), (2,))
        >>> iTuple.range(3).groupby(
        ...    lambda x: x < 2, keys=True, pipe=fDict
        ... )
        {True: (0, 1), False: (2,)}
        """
        # TODO: lazy no keys
        res = itertools.groupby(self, key=f)
        if lazy and keys and pipe is None:
            return res
        if pipe is None:
            pipe = iTuple
        if keys:
            return pipe((k, tuple(g),) for k, g in res)
        else:
            return pipe(tuple(g) for k, g in res)

    def first(self):
        """
        >>> iTuple.range(3).first()
        0
        """
        return self[0]
    
    def last(self):
        """
        >>> iTuple.range(3).last()
        2
        """
        return self[-1]

    def first_where(self, f):
        """
        >>> iTuple.range(3).first_where(lambda v: v > 0)
        1
        """
        for v in self:
            if f(v):
                return v
        return None

    def last_where(self, f):
        """
        >>> iTuple.range(3).last_where(lambda v: v < 2)
        1
        """
        for v in reversed(self):
            if f(v):
                return v
        return None

    def take(self, n):
        """
        >>> iTuple.range(3).take(2)
        iTuple(0, 1)
        """
        return self[:n]

    def tail(self, n):
        """
        >>> iTuple.range(3).tail(2)
        iTuple(1, 2)
        """
        return self[-n:]

    def reverse(self, lazy = False):
        """
        >>> iTuple.range(3).reverse()
        iTuple(2, 1, 0)
        """
        if lazy:
            return reversed(self)
        return type(self)(data=reversed(self))

    def take_while(self, f, n = None, lazy = False):
        """
        >>> iTuple.range(3).take_while(lambda v: v < 1)
        iTuple(0)
        """
        def iter():
            i = 0
            for v in self:
                if f(v) and (n is None or i < n):
                    yield v
                    i += 1
                else:
                    return
        res = iter()
        return res if lazy else type(self)(data=res)

    def tail_while(self, f, n = None):
        """
        >>> iTuple.range(3).tail_while(lambda v: v > 1)
        iTuple(2)
        """
        i = 0
        for v in reversed(self):
            if f(v) and (n is None or i < n):
                i += 1
            else:
                break
        return self.tail(i)

    # NOTE: from as in, starting from first true
    # versus above, which is until first false
    def take_after(self, f, n = None, lazy = False):
        """
        >>> iTuple.range(3).take_after(lambda v: v < 1)
        iTuple(1, 2)
        >>> iTuple.range(3).take_after(lambda v: v < 1, n = 1)
        iTuple(1)
        """
        def iter():
            i = 0
            for v in self:
                if f(v):
                    pass
                elif n is None or i < n:
                    yield v
                    i += 1
                else:
                    return
        res = iter()
        return res if lazy else type(self)(data=res)

    def tail_after(self, f, n = None):
        """
        >>> iTuple.range(3).tail_after(lambda v: v < 2)
        iTuple(0, 1)
        >>> iTuple.range(3).tail_after(lambda v: v < 2, 1)
        iTuple(1)
        """
        l = 0
        r = 0
        for v in reversed(self):
            if not f(v):
                l += 1
            elif n is None or r < n:
                r += 1
            else:
                break
        return self.tail(l + r).take(r)

    def islice(self, left = None, right = None):
        """
        >>> iTuple.range(5).islice(1, 3)
        iTuple(1, 2)
        """
        return self[left:right]

    def unique(self):
        """
        >>> iTuple([1, 1, 3, 2, 4, 2, 3]).unique()
        iTuple(1, 3, 2, 4)
        """
        def iter():
            seen = set()
            seen_add = seen.add
            seen_contains = seen.__contains__
            for v in itertools.filterfalse(seen_contains, self):
                seen_add(v)
                yield v
        return type(self)(data=iter())
    
    def sort(self, f = lambda v: v):
        """
        >>> iTuple.range(3).reverse().sort()
        iTuple(0, 1, 2)
        >>> iTuple.range(3).sort()
        iTuple(0, 1, 2)
        """
        return type(self)(data=sorted(self, key = f))

    def accumulate(self, f, initial = None, lazy = False):
        """
        >>> iTuple.range(3).accumulate(lambda acc, v: v)
        iTuple(0, 1, 2)
        >>> iTuple.range(3).accumulate(lambda acc, v: v, initial=0)
        iTuple(0, 0, 1, 2)
        >>> iTuple.range(3).accumulate(operator.add)
        iTuple(0, 1, 3)
        """
        res = itertools.accumulate(self, func=f, initial=initial)
        return res if lazy else iTuple(data=res)

    def foldcum(self, *args, **kwargs):
        """
        >>> iTuple.range(3).foldcum(lambda acc, v: v)
        iTuple(0, 1, 2)
        >>> iTuple.range(3).foldcum(operator.add)
        iTuple(0, 1, 3)
        """
        return self.accumulate(*args, **kwargs)

    def fold(self, f, initial=None):
        """
        >>> iTuple.range(3).fold(lambda acc, v: v)
        2
        >>> iTuple.range(3).fold(lambda acc, v: v, initial=0)
        2
        >>> iTuple.range(3).fold(operator.add)
        3
        """
        if initial is not None:
            res = functools.reduce(f, self, initial)
        else:
            res = functools.reduce(f, self)
        return res

Ancestors

  • collections.UserList
  • collections.abc.MutableSequence
  • collections.abc.Sequence
  • collections.abc.Reversible
  • collections.abc.Collection
  • collections.abc.Sized
  • collections.abc.Iterable
  • collections.abc.Container
  • builtins.tuple

Class variables

var data : tuple

Static methods

def decorate(cls)
Expand source code
@classmethod
def decorate(meta, cls):
    assert cls.__name__ not in REGISTRY
    REGISTRY[cls.__name__] = cls
    return cls
def from_items(d)
>>> iTuple.from_items({i: i + 1 for i in range(2)})
iTuple((0, 1), (1, 2))
Expand source code
@classmethod
def from_items(cls, d):
    """
    >>> iTuple.from_items({i: i + 1 for i in range(2)})
    iTuple((0, 1), (1, 2))
    """
    return cls(d.items())
def from_keys(d)
>>> iTuple.from_keys({i: i + 1 for i in range(2)})
iTuple(0, 1)
Expand source code
@classmethod
def from_keys(cls, d):
    """
    >>> iTuple.from_keys({i: i + 1 for i in range(2)})
    iTuple(0, 1)
    """
    return cls(d.keys())
def from_values(d)
>>> iTuple.from_values({i: i + 1 for i in range(2)})
iTuple(1, 2)
Expand source code
@classmethod
def from_values(cls, d):
    """
    >>> iTuple.from_values({i: i + 1 for i in range(2)})
    iTuple(1, 2)
    """
    return cls(d.values())
def range(*args, **kwargs)
>>> iTuple.range(3)
iTuple(0, 1, 2)
Expand source code
@classmethod
def range(cls, *args, **kwargs):
    """
    >>> iTuple.range(3)
    iTuple(0, 1, 2)
    """
    return cls(range(*args, **kwargs))
def uncast_json(obj)
>>> iTuple.uncast_json(iTuple.range(1).cast_json())
iTuple(0)
Expand source code
@classmethod
def uncast_json(cls, obj):
    """
    >>> iTuple.uncast_json(iTuple.range(1).cast_json())
    iTuple(0)
    """
    assert obj["__t__"] == cls.__name__
    return cls(data=obj["data"])
def wrap_tuple(data)
Expand source code
@staticmethod
def wrap_tuple(data):
    return data if isinstance(data, tuple) else tuple(data)

Methods

def accumulate(self, f, initial=None, lazy=False)
>>> iTuple.range(3).accumulate(lambda acc, v: v)
iTuple(0, 1, 2)
>>> iTuple.range(3).accumulate(lambda acc, v: v, initial=0)
iTuple(0, 0, 1, 2)
>>> iTuple.range(3).accumulate(operator.add)
iTuple(0, 1, 3)
Expand source code
def accumulate(self, f, initial = None, lazy = False):
    """
    >>> iTuple.range(3).accumulate(lambda acc, v: v)
    iTuple(0, 1, 2)
    >>> iTuple.range(3).accumulate(lambda acc, v: v, initial=0)
    iTuple(0, 0, 1, 2)
    >>> iTuple.range(3).accumulate(operator.add)
    iTuple(0, 1, 3)
    """
    res = itertools.accumulate(self, func=f, initial=initial)
    return res if lazy else iTuple(data=res)
def append(self, value, *values)
>>> iTuple.range(1).append(1)
iTuple(0, 1)
>>> iTuple.range(1).append(1, 2)
iTuple(0, 1, 2)
>>> iTuple.range(1).append(1, 2, 3)
iTuple(0, 1, 2, 3)
>>> iTuple.range(1).append(1, (2,))
iTuple(0, 1, (2,))
Expand source code
def append(self, value, *values):
    """
    >>> iTuple.range(1).append(1)
    iTuple(0, 1)
    >>> iTuple.range(1).append(1, 2)
    iTuple(0, 1, 2)
    >>> iTuple.range(1).append(1, 2, 3)
    iTuple(0, 1, 2, 3)
    >>> iTuple.range(1).append(1, (2,))
    iTuple(0, 1, (2,))
    """
    return self + (value, *values)
def cast_json(self)
>>> iTuple.range(1).cast_json()
{'__t__': 'iTuple', 'data': [0]}
Expand source code
def cast_json(self):
    """
    >>> iTuple.range(1).cast_json()
    {'__t__': 'iTuple', 'data': [0]}
    """
    return dict(
        __t__ = type(self).__name__,
        data = list(self.map(cast_json)),
    )
def enumerate(self)
>>> iTuple.range(3).enumerate()
iTuple((0, 0), (1, 1), (2, 2))
Expand source code
def enumerate(self):
    """
    >>> iTuple.range(3).enumerate()
    iTuple((0, 0), (1, 1), (2, 2))
    """
    # TODO: allow lazy
    return iTuple(enumerate(self))
def extend(self, value, *values)
>>> iTuple.range(1).extend((1,))
iTuple(0, 1)
>>> iTuple.range(1).extend([1])
iTuple(0, 1)
>>> iTuple.range(1).extend([1], [2])
iTuple(0, 1, 2)
>>> iTuple.range(1).extend([1], [[2]])
iTuple(0, 1, [2])
>>> iTuple.range(1).extend([1], [[2]], [2])
iTuple(0, 1, [2], 2)
Expand source code
def extend(self, value, *values):
    """
    >>> iTuple.range(1).extend((1,))
    iTuple(0, 1)
    >>> iTuple.range(1).extend([1])
    iTuple(0, 1)
    >>> iTuple.range(1).extend([1], [2])
    iTuple(0, 1, 2)
    >>> iTuple.range(1).extend([1], [[2]])
    iTuple(0, 1, [2])
    >>> iTuple.range(1).extend([1], [[2]], [2])
    iTuple(0, 1, [2], 2)
    """
    return iTuple(itertools.chain.from_iterable(
        (self, value, *values)
    ))
def filter(self, f, eq=None, lazy=False)
>>> iTuple.range(3).filter(lambda x: x > 1)
iTuple(2)
Expand source code
def filter(self, f, eq = None, lazy = False):
    """
    >>> iTuple.range(3).filter(lambda x: x > 1)
    iTuple(2)
    """
    return self.filter_eq(True, f = f, eq = eq, lazy = lazy)
def filter_eq(self, v, f=None, eq=None, lazy=False)
>>> iTuple.range(3).filter_eq(1)
iTuple(1)
Expand source code
def filter_eq(self, v, f = None, eq = None, lazy = False):
    """
    >>> iTuple.range(3).filter_eq(1)
    iTuple(1)
    """
    if f is None and eq is None:
        res = filter(lambda x: x == v, self)
    elif f is not None:
        res = filter(lambda x: f(x) == v, self)
    elif eq is not None:
        res = filter(lambda x: eq(x, v), self)
    else:
        res = filter(lambda x: eq(f(x), v), self)
    return res if lazy else type(self)(data=res)
def first(self)
>>> iTuple.range(3).first()
0
Expand source code
def first(self):
    """
    >>> iTuple.range(3).first()
    0
    """
    return self[0]
def first_where(self, f)
>>> iTuple.range(3).first_where(lambda v: v > 0)
1
Expand source code
def first_where(self, f):
    """
    >>> iTuple.range(3).first_where(lambda v: v > 0)
    1
    """
    for v in self:
        if f(v):
            return v
    return None
def flatten(self)
>>> iTuple.range(3).map(lambda x: [x]).flatten()
iTuple(0, 1, 2)
Expand source code
def flatten(self):
    """
    >>> iTuple.range(3).map(lambda x: [x]).flatten()
    iTuple(0, 1, 2)
    """
    return iTuple(itertools.chain(*self))
def fold(self, f, initial=None)
>>> iTuple.range(3).fold(lambda acc, v: v)
2
>>> iTuple.range(3).fold(lambda acc, v: v, initial=0)
2
>>> iTuple.range(3).fold(operator.add)
3
Expand source code
def fold(self, f, initial=None):
    """
    >>> iTuple.range(3).fold(lambda acc, v: v)
    2
    >>> iTuple.range(3).fold(lambda acc, v: v, initial=0)
    2
    >>> iTuple.range(3).fold(operator.add)
    3
    """
    if initial is not None:
        res = functools.reduce(f, self, initial)
    else:
        res = functools.reduce(f, self)
    return res
def foldcum(self, *args, **kwargs)
>>> iTuple.range(3).foldcum(lambda acc, v: v)
iTuple(0, 1, 2)
>>> iTuple.range(3).foldcum(operator.add)
iTuple(0, 1, 3)
Expand source code
def foldcum(self, *args, **kwargs):
    """
    >>> iTuple.range(3).foldcum(lambda acc, v: v)
    iTuple(0, 1, 2)
    >>> iTuple.range(3).foldcum(operator.add)
    iTuple(0, 1, 3)
    """
    return self.accumulate(*args, **kwargs)
def groupby(self, f, lazy=False, keys=False, pipe=None)
>>> iTuple.range(3).groupby(lambda x: x < 2)
iTuple((0, 1), (2,))
>>> iTuple.range(3).groupby(
...    lambda x: x < 2, keys=True, pipe=fDict
... )
{True: (0, 1), False: (2,)}
Expand source code
def groupby(
    self, 
    f, 
    lazy = False, 
    keys = False,
    pipe= None,
):
    """
    >>> iTuple.range(3).groupby(lambda x: x < 2)
    iTuple((0, 1), (2,))
    >>> iTuple.range(3).groupby(
    ...    lambda x: x < 2, keys=True, pipe=fDict
    ... )
    {True: (0, 1), False: (2,)}
    """
    # TODO: lazy no keys
    res = itertools.groupby(self, key=f)
    if lazy and keys and pipe is None:
        return res
    if pipe is None:
        pipe = iTuple
    if keys:
        return pipe((k, tuple(g),) for k, g in res)
    else:
        return pipe(tuple(g) for k, g in res)
def islice(self, left=None, right=None)
>>> iTuple.range(5).islice(1, 3)
iTuple(1, 2)
Expand source code
def islice(self, left = None, right = None):
    """
    >>> iTuple.range(5).islice(1, 3)
    iTuple(1, 2)
    """
    return self[left:right]
def last(self)
>>> iTuple.range(3).last()
2
Expand source code
def last(self):
    """
    >>> iTuple.range(3).last()
    2
    """
    return self[-1]
def last_where(self, f)
>>> iTuple.range(3).last_where(lambda v: v < 2)
1
Expand source code
def last_where(self, f):
    """
    >>> iTuple.range(3).last_where(lambda v: v < 2)
    1
    """
    for v in reversed(self):
        if f(v):
            return v
    return None
def len(self)
>>> iTuple.range(3).len()
3
Expand source code
def len(self):
    """
    >>> iTuple.range(3).len()
    3
    """
    return len(self)
def map(self, f, *iterables, lazy=False)
>>> iTuple.range(3).map(lambda x: x * 2)
iTuple(0, 2, 4)
Expand source code
def map(self, f, *iterables, lazy = False):
    """
    >>> iTuple.range(3).map(lambda x: x * 2)
    iTuple(0, 2, 4)
    """
    res = map(f, self, *iterables)
    return res if lazy else iTuple(data=res)
def partial(self, f, *args, **kwargs)
>>> f = iTuple.range(2).partial(
...     lambda it, v: it.map(lambda x: x * v)
... )
>>> f(2)
iTuple(0, 2)
>>> f(3)
iTuple(0, 3)
Expand source code
def partial(self, f, *args, **kwargs):
    """
    >>> f = iTuple.range(2).partial(
    ...     lambda it, v: it.map(lambda x: x * v)
    ... )
    >>> f(2)
    iTuple(0, 2)
    >>> f(3)
    iTuple(0, 3)
    """
    return functools.partial(f, self, *args, **kwargs)
def pipe(self, f, *args, **kwargs)
>>> iTuple.range(2).pipe(lambda it: it)
iTuple(0, 1)
>>> iTuple.range(2).pipe(
...     lambda it, v: it.map(lambda x: x * v), 2
... )
iTuple(0, 2)
Expand source code
def pipe(self, f, *args, **kwargs):
    """
    >>> iTuple.range(2).pipe(lambda it: it)
    iTuple(0, 1)
    >>> iTuple.range(2).pipe(
    ...     lambda it, v: it.map(lambda x: x * v), 2
    ... )
    iTuple(0, 2)
    """
    return f(self, *args, **kwargs)
def prepend(self, value, *values)
>>> iTuple.range(1).prepend(1)
iTuple(1, 0)
>>> iTuple.range(1).prepend(1, 2)
iTuple(1, 2, 0)
>>> iTuple.range(1).prepend(1, 2, 3)
iTuple(1, 2, 3, 0)
>>> iTuple.range(1).prepend(1, (2,))
iTuple(1, (2,), 0)
Expand source code
def prepend(self, value, *values):
    """
    >>> iTuple.range(1).prepend(1)
    iTuple(1, 0)
    >>> iTuple.range(1).prepend(1, 2)
    iTuple(1, 2, 0)
    >>> iTuple.range(1).prepend(1, 2, 3)
    iTuple(1, 2, 3, 0)
    >>> iTuple.range(1).prepend(1, (2,))
    iTuple(1, (2,), 0)
    """
    return (value, *values) + self
def pretend(self, value, *values)
>>> iTuple.range(1).pretend((1,))
iTuple(1, 0)
>>> iTuple.range(1).pretend([1])
iTuple(1, 0)
>>> iTuple.range(1).pretend([1], [2])
iTuple(1, 2, 0)
>>> iTuple.range(1).pretend([1], [[2]])
iTuple(1, [2], 0)
>>> iTuple.range(1).pretend([1], [[2]], [2])
iTuple(1, [2], 2, 0)
Expand source code
def pretend(self, value, *values):
    """
    >>> iTuple.range(1).pretend((1,))
    iTuple(1, 0)
    >>> iTuple.range(1).pretend([1])
    iTuple(1, 0)
    >>> iTuple.range(1).pretend([1], [2])
    iTuple(1, 2, 0)
    >>> iTuple.range(1).pretend([1], [[2]])
    iTuple(1, [2], 0)
    >>> iTuple.range(1).pretend([1], [[2]], [2])
    iTuple(1, [2], 2, 0)
    """
    return iTuple(itertools.chain.from_iterable(
        (value, *values, self)
    ))
def reverse(self, lazy=False)
>>> iTuple.range(3).reverse()
iTuple(2, 1, 0)
Expand source code
def reverse(self, lazy = False):
    """
    >>> iTuple.range(3).reverse()
    iTuple(2, 1, 0)
    """
    if lazy:
        return reversed(self)
    return type(self)(data=reversed(self))
def sort(self, f=<function iTuple.<lambda>>)
>>> iTuple.range(3).reverse().sort()
iTuple(0, 1, 2)
>>> iTuple.range(3).sort()
iTuple(0, 1, 2)
Expand source code
def sort(self, f = lambda v: v):
    """
    >>> iTuple.range(3).reverse().sort()
    iTuple(0, 1, 2)
    >>> iTuple.range(3).sort()
    iTuple(0, 1, 2)
    """
    return type(self)(data=sorted(self, key = f))
def tail(self, n)
>>> iTuple.range(3).tail(2)
iTuple(1, 2)
Expand source code
def tail(self, n):
    """
    >>> iTuple.range(3).tail(2)
    iTuple(1, 2)
    """
    return self[-n:]
def tail_after(self, f, n=None)
>>> iTuple.range(3).tail_after(lambda v: v < 2)
iTuple(0, 1)
>>> iTuple.range(3).tail_after(lambda v: v < 2, 1)
iTuple(1)
Expand source code
def tail_after(self, f, n = None):
    """
    >>> iTuple.range(3).tail_after(lambda v: v < 2)
    iTuple(0, 1)
    >>> iTuple.range(3).tail_after(lambda v: v < 2, 1)
    iTuple(1)
    """
    l = 0
    r = 0
    for v in reversed(self):
        if not f(v):
            l += 1
        elif n is None or r < n:
            r += 1
        else:
            break
    return self.tail(l + r).take(r)
def tail_while(self, f, n=None)
>>> iTuple.range(3).tail_while(lambda v: v > 1)
iTuple(2)
Expand source code
def tail_while(self, f, n = None):
    """
    >>> iTuple.range(3).tail_while(lambda v: v > 1)
    iTuple(2)
    """
    i = 0
    for v in reversed(self):
        if f(v) and (n is None or i < n):
            i += 1
        else:
            break
    return self.tail(i)
def take(self, n)
>>> iTuple.range(3).take(2)
iTuple(0, 1)
Expand source code
def take(self, n):
    """
    >>> iTuple.range(3).take(2)
    iTuple(0, 1)
    """
    return self[:n]
def take_after(self, f, n=None, lazy=False)
>>> iTuple.range(3).take_after(lambda v: v < 1)
iTuple(1, 2)
>>> iTuple.range(3).take_after(lambda v: v < 1, n = 1)
iTuple(1)
Expand source code
def take_after(self, f, n = None, lazy = False):
    """
    >>> iTuple.range(3).take_after(lambda v: v < 1)
    iTuple(1, 2)
    >>> iTuple.range(3).take_after(lambda v: v < 1, n = 1)
    iTuple(1)
    """
    def iter():
        i = 0
        for v in self:
            if f(v):
                pass
            elif n is None or i < n:
                yield v
                i += 1
            else:
                return
    res = iter()
    return res if lazy else type(self)(data=res)
def take_while(self, f, n=None, lazy=False)
>>> iTuple.range(3).take_while(lambda v: v < 1)
iTuple(0)
Expand source code
def take_while(self, f, n = None, lazy = False):
    """
    >>> iTuple.range(3).take_while(lambda v: v < 1)
    iTuple(0)
    """
    def iter():
        i = 0
        for v in self:
            if f(v) and (n is None or i < n):
                yield v
                i += 1
            else:
                return
    res = iter()
    return res if lazy else type(self)(data=res)
def unique(self)
>>> iTuple([1, 1, 3, 2, 4, 2, 3]).unique()
iTuple(1, 3, 2, 4)
Expand source code
def unique(self):
    """
    >>> iTuple([1, 1, 3, 2, 4, 2, 3]).unique()
    iTuple(1, 3, 2, 4)
    """
    def iter():
        seen = set()
        seen_add = seen.add
        seen_contains = seen.__contains__
        for v in itertools.filterfalse(seen_contains, self):
            seen_add(v)
            yield v
    return type(self)(data=iter())
def zip(self, *itrs, lazy=False)
>>> iTuple([[1, 1], [2, 2], [3, 3]]).zip()
iTuple((1, 2, 3), (1, 2, 3))
>>> iTuple([iTuple.range(3), iTuple.range(1, 4)]).zip()
iTuple((0, 1), (1, 2), (2, 3))
>>> iTuple.range(3).zip(iTuple.range(1, 4))
iTuple((0, 1), (1, 2), (2, 3))
Expand source code
def zip(self, *itrs, lazy = False):
    """
    >>> iTuple([[1, 1], [2, 2], [3, 3]]).zip()
    iTuple((1, 2, 3), (1, 2, 3))
    >>> iTuple([iTuple.range(3), iTuple.range(1, 4)]).zip()
    iTuple((0, 1), (1, 2), (2, 3))
    >>> iTuple.range(3).zip(iTuple.range(1, 4))
    iTuple((0, 1), (1, 2), (2, 3))
    """
    if len(itrs) == 0:
        res = zip(*self)
    else:
        res = zip(self, *itrs)
    return res if lazy else iTuple(data=res)
class nTuple

Helper class that provides a standard way to create an ABC using inheritance.

Expand source code
class nTuple(abc.ABC):

    @abc.abstractmethod
    def __abstract__(self):
        # NOTE: here to prevent initialise instances of this
        # but rather use the decorator and typing.NamedTuple
        return

    @staticmethod
    def pipe(obj, f, *args, **kwargs):
        return f(obj, *args, **kwargs)

    @staticmethod
    def partial(obj, f, *args, **kwargs):
        return functools.partial(f, obj, *args, **kwargs)

    @classmethod
    def is_subclass(cls, t):
        """
        >>> nTuple.is_subclass(tuple)
        False
        >>> nTuple.is_subclass(Example(1, "a"))
        False
        >>> nTuple.is_subclass(Example)
        True
        """
        try:
            is_sub = issubclass(t, tuple)
        except:
            is_sub = False
        return (
            is_sub and
            hasattr(t, "cls") and
            hasattr(t, "pipe") and
            hasattr(t, "partial")
        )

    @classmethod
    def is_instance(cls, obj):
        """
        >>> nTuple.is_instance(tuple)
        False
        >>> nTuple.is_instance(Example)
        False
        >>> nTuple.is_instance(Example(1, "a"))
        True
        """
        return (
            cls.is_subclass(type(obj)) and
            hasattr(obj, '_asdict') and
            hasattr(obj, '_fields')
        )


    @staticmethod
    def annotations(obj):
        """
        >>> ex = Example(1, "a")
        >>> ex.pipe(ex.cls.annotations)
        {'x': <class 'int'>, 's': <class 'str'>, 'it': <class 'xtuples.xtuples.iTuple'>}
        """
        return fDict(obj.__annotations__)

    @classmethod
    def as_dict(cls, obj):
        """
        >>> ex = Example(1, "a")
        >>> ex.pipe(ex.cls.as_dict)
        {'x': 1, 's': 'a', 'it': iTuple()}
        """
        return fDict(obj._asdict())

    @classmethod
    def cast_json(cls, obj):
        """
        >>> ex = Example(1, "a")
        >>> ex.pipe(ex.cls.cast_json)
        {'x': 1, 's': 'a', 'it': {'__t__': 'iTuple', 'data': []}, '__t__': 'Example'}
        """
        d = {
            k: cast_json(v)
            for k, v in obj._asdict().items()
            #
        }
        d["__t__"] = type(obj).__name__
        return d

    @classmethod
    def uncast_json(meta, obj):
        """
        >>> ex = Example(1, "a")
        >>> ex.cls.uncast_json(ex.pipe(ex.cls.cast_json))
        Example(x=1, s='a', it=iTuple())
        """
        cls = REGISTRY[obj["__t__"]]
        return cls(
            *(
                uncast_json(v)
                for k, v in obj.items() if k != "__t__"
            )
        )

    @classmethod
    def decorate(meta, cls):
        assert cls.__name__ not in REGISTRY
        cls.pipe = meta.pipe
        cls.partial = meta.partial
        cls.cls = meta
        REGISTRY[cls.__name__] = cls
        return cls

Ancestors

  • abc.ABC

Static methods

def annotations(obj)
>>> ex = Example(1, "a")
>>> ex.pipe(ex.cls.annotations)
{'x': <class 'int'>, 's': <class 'str'>, 'it': <class 'xtuples.xtuples.iTuple'>}
Expand source code
@staticmethod
def annotations(obj):
    """
    >>> ex = Example(1, "a")
    >>> ex.pipe(ex.cls.annotations)
    {'x': <class 'int'>, 's': <class 'str'>, 'it': <class 'xtuples.xtuples.iTuple'>}
    """
    return fDict(obj.__annotations__)
def as_dict(obj)
>>> ex = Example(1, "a")
>>> ex.pipe(ex.cls.as_dict)
{'x': 1, 's': 'a', 'it': iTuple()}
Expand source code
@classmethod
def as_dict(cls, obj):
    """
    >>> ex = Example(1, "a")
    >>> ex.pipe(ex.cls.as_dict)
    {'x': 1, 's': 'a', 'it': iTuple()}
    """
    return fDict(obj._asdict())
def cast_json(obj)
>>> ex = Example(1, "a")
>>> ex.pipe(ex.cls.cast_json)
{'x': 1, 's': 'a', 'it': {'__t__': 'iTuple', 'data': []}, '__t__': 'Example'}
Expand source code
@classmethod
def cast_json(cls, obj):
    """
    >>> ex = Example(1, "a")
    >>> ex.pipe(ex.cls.cast_json)
    {'x': 1, 's': 'a', 'it': {'__t__': 'iTuple', 'data': []}, '__t__': 'Example'}
    """
    d = {
        k: cast_json(v)
        for k, v in obj._asdict().items()
        #
    }
    d["__t__"] = type(obj).__name__
    return d
def decorate(cls)
Expand source code
@classmethod
def decorate(meta, cls):
    assert cls.__name__ not in REGISTRY
    cls.pipe = meta.pipe
    cls.partial = meta.partial
    cls.cls = meta
    REGISTRY[cls.__name__] = cls
    return cls
def is_instance(obj)
>>> nTuple.is_instance(tuple)
False
>>> nTuple.is_instance(Example)
False
>>> nTuple.is_instance(Example(1, "a"))
True
Expand source code
@classmethod
def is_instance(cls, obj):
    """
    >>> nTuple.is_instance(tuple)
    False
    >>> nTuple.is_instance(Example)
    False
    >>> nTuple.is_instance(Example(1, "a"))
    True
    """
    return (
        cls.is_subclass(type(obj)) and
        hasattr(obj, '_asdict') and
        hasattr(obj, '_fields')
    )
def is_subclass(t)
>>> nTuple.is_subclass(tuple)
False
>>> nTuple.is_subclass(Example(1, "a"))
False
>>> nTuple.is_subclass(Example)
True
Expand source code
@classmethod
def is_subclass(cls, t):
    """
    >>> nTuple.is_subclass(tuple)
    False
    >>> nTuple.is_subclass(Example(1, "a"))
    False
    >>> nTuple.is_subclass(Example)
    True
    """
    try:
        is_sub = issubclass(t, tuple)
    except:
        is_sub = False
    return (
        is_sub and
        hasattr(t, "cls") and
        hasattr(t, "pipe") and
        hasattr(t, "partial")
    )
def partial(obj, f, *args, **kwargs)
Expand source code
@staticmethod
def partial(obj, f, *args, **kwargs):
    return functools.partial(f, obj, *args, **kwargs)
def pipe(obj, f, *args, **kwargs)
Expand source code
@staticmethod
def pipe(obj, f, *args, **kwargs):
    return f(obj, *args, **kwargs)
def uncast_json(obj)
>>> ex = Example(1, "a")
>>> ex.cls.uncast_json(ex.pipe(ex.cls.cast_json))
Example(x=1, s='a', it=iTuple())
Expand source code
@classmethod
def uncast_json(meta, obj):
    """
    >>> ex = Example(1, "a")
    >>> ex.cls.uncast_json(ex.pipe(ex.cls.cast_json))
    Example(x=1, s='a', it=iTuple())
    """
    cls = REGISTRY[obj["__t__"]]
    return cls(
        *(
            uncast_json(v)
            for k, v in obj.items() if k != "__t__"
        )
    )