tree.pyx 30.2 KB
Newer Older
1 2
# distutils:language=c++
# cython:language_level=3
HansBug's avatar
HansBug 已提交
3

4
import os
HansBug's avatar
HansBug 已提交
5
from collections.abc import Sized, Container, Reversible, Mapping
6
from operator import itemgetter
7

8
import cython
9
from hbutils.design import SingletonMark
HansBug's avatar
HansBug 已提交
10

HansBug's avatar
HansBug 已提交
11
from ..common.delay cimport undelay, _c_delayed_partial, DelayedProxy
12
from ..common.storage cimport TreeStorage, create_storage, _c_undelay_data
13
from ...utils import format_tree
HansBug's avatar
HansBug 已提交
14

15 16 17
cdef class _CObject:
    pass

18 19 20 21 22 23 24
try:
    reversed({'a': 1}.keys())
except TypeError:
    _reversible = False
else:
    _reversible = True

25 26 27 28 29 30
cdef inline object _item_unwrap(object v):
    if isinstance(v, list) and len(v) == 1:
        return v[0]
    else:
        return v

31 32
_GET_NO_DEFAULT = SingletonMark('get_no_default')

33 34 35 36
cdef inline TreeStorage _dict_unpack(dict d):
    cdef str k
    cdef object v
    cdef dict result = {}
HansBug's avatar
HansBug 已提交
37

38 39 40 41 42
    for k, v in d.items():
        if isinstance(v, dict):
            result[k] = _dict_unpack(v)
        elif isinstance(v, TreeValue):
            result[k] = v._detach()
HansBug's avatar
HansBug 已提交
43
        else:
44
            result[k] = v
HansBug's avatar
HansBug 已提交
45

46
    return create_storage(result)
HansBug's avatar
HansBug 已提交
47

48 49
_DEFAULT_STORAGE = create_storage({})

50 51
cdef class TreeValue:
    r"""
52 53 54 55 56 57 58
    Overview:
        Base framework of tree value. \
        And if the fast functions and operators are what you need, \
        please use `FastTreeValue` in `treevalue.tree.general`. \
        The `TreeValue` class is a light-weight framework just for DIY.
    """

59 60 61 62
    def __cinit__(self, object data):
        self._st = _DEFAULT_STORAGE
        self._type = type(self)

63
    @cython.binding(True)
64
    def __init__(self, object data):
65 66 67 68 69
        """
        Overview:
            Constructor of `TreeValue`.

        Arguments:
70 71
            - data: (:obj:`Union[TreeStorage, 'TreeValue', dict]`): Original data to init a tree value, \
                can be a `TreeStorage`, `TreeValue` or dict.
72 73 74 75 76 77 78 79 80 81

        Example:
            >>> TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
            >>> # this is the tree:
            >>> # <root> -+--> a (1)
            >>> #         +--> b (2)
            >>> #         +--> x
            >>> #              +--> c (3)
            >>> #              +--> d (4)
        """
82 83 84 85 86 87 88 89 90 91
        if isinstance(data, TreeStorage):
            self._st = data
        elif isinstance(data, TreeValue):
            self._st = data._detach()
        elif isinstance(data, dict):
            self._st = _dict_unpack(data)
        else:
            raise TypeError(
                "Unknown initialization type for tree value - {type}.".format(
                    type=repr(type(data).__name__)))
HansBug's avatar
HansBug 已提交
92

93 94 95 96 97 98
    def __getnewargs_ex__(self):  # for __cinit__, when pickle.loads
        return ({},), {}

    cpdef TreeStorage _detach(self):
        return self._st

HansBug's avatar
HansBug 已提交
99
    cdef inline object _unraw(self, object obj):
100 101 102 103 104
        if isinstance(obj, TreeStorage):
            return self._type(obj)
        else:
            return obj

HansBug's avatar
HansBug 已提交
105
    cdef inline object _raw(self, object obj):
106 107 108 109
        if isinstance(obj, TreeValue):
            return obj._detach()
        else:
            return obj
HansBug's avatar
HansBug 已提交
110

111
    @cython.binding(True)
112
    cpdef get(self, str key, object default=None):
113 114 115 116
        r"""
        Overview:
            Get item from the tree node.

117 118 119
        :param key: Item's name.
        :param default: Default value when this item is not found, default is ``None``.
        :return: Item's value.
120

121 122 123
        .. note::
            The method :meth:`get` will never raise ``KeyError``, like the behaviour in \
            `dict.get <https://docs.python.org/3/library/stdtypes.html#dict.get>`_.
124
        """
125
        return self._unraw(self._st.get_or_default(key, default))
126

HansBug's avatar
HansBug 已提交
127 128 129 130 131 132
    @cython.binding(True)
    cpdef pop(self, str key, object default=_GET_NO_DEFAULT):
        """
        Overview:
            Pop item from the tree node.

133 134 135 136
        :param key: Item's name.
        :param default: Default value when this item is not found, default is ``_GET_NO_DEFAULT`` which means \
            just raise ``KeyError`` when not found.
        :return: Item's value.
HansBug's avatar
HansBug 已提交
137

138 139 140
        .. note::
            The method :meth:`pop` will raise ``KeyError`` when ``key`` is not found, like the behaviour in \
            `dict.pop <https://docs.python.org/3/library/stdtypes.html#dict.pop>`_.
HansBug's avatar
HansBug 已提交
141 142 143 144 145 146 147 148 149
        """
        cdef object value
        if default is _GET_NO_DEFAULT:
            value = self._st.pop(key)
        else:
            value = self._st.pop_or_default(key, default)

        return self._unraw(value)

HansBug's avatar
HansBug 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    @cython.binding(True)
    cpdef popitem(self):
        """
        Overview:
            Pop item (with a key and its value) from the tree node.

        :return: Popped item.
        :raise KeyError: When current treevalue is empty.

        .. note::
            The method :meth:`popitem` will raise ``KeyError`` when empty, like the behaviour in \
            `dict.popitem <https://docs.python.org/3/library/stdtypes.html#dict.popitem>`_.
        """
        cdef str k
        cdef object v
        try:
            k, v = self._st.popitem()
            return k, self._unraw(v)
        except KeyError:
            raise KeyError(f'popitem(): {self._type.__name__} is empty.')

171 172 173 174 175 176 177 178
    @cython.binding(True)
    cpdef void clear(self):
        """
        Overview:
            Clear all the items in this treevalue object.
        """
        self._st.clear()

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    @cython.binding(True)
    cpdef object setdefault(self, str key, object default=None):
        """
        Overview:
            Set the ``default`` to this treevalue and return it if the ``key`` is not exist, \
            otherwise just return the existing value of ``key``.

        :param key: Items' name.
        :param default: Default value of the ``key``, ``None`` will be used when not given.
        :return: The newest value of the ``key``.

        .. note::
            The behaviour of method :meth:`setdefault` is similar to \
            `dict.setdefault <https://docs.python.org/3/library/stdtypes.html#dict.setdefault>`_.

        Examples::
            >>> from treevalue import TreeValue, delayed
            >>> t = TreeValue({'a': 1, 'b': 3, 'c': '233'})
            >>> t.setdefault('d', 'dsflgj')  # set new value
            'dsflgj'
            >>> t
            <TreeValue 0x7efe31576048>
            ├── 'a' --> 1
            ├── 'b' --> 3
            ├── 'c' --> '233'
            └── 'd' --> 'dsflgj'
            >>>
            >>> t.setdefault('ff')  # default value - None
            >>> t
            <TreeValue 0x7efe31576048>
            ├── 'a' --> 1
            ├── 'b' --> 3
            ├── 'c' --> '233'
            ├── 'd' --> 'dsflgj'
            └── 'ff' --> None
            >>>
            >>> t.setdefault('a', 1000)  # existing key
            1
            >>> t
            <TreeValue 0x7efe31576048>
            ├── 'a' --> 1
            ├── 'b' --> 3
            ├── 'c' --> '233'
            ├── 'd' --> 'dsflgj'
            └── 'ff' --> None
            >>>
            >>> t.setdefault('g', delayed(lambda: 1))  # delayed value
            1
            >>> t
            <TreeValue 0x7efe31576048>
            ├── 'a' --> 1
            ├── 'b' --> 3
            ├── 'c' --> '233'
            ├── 'd' --> 'dsflgj'
            ├── 'ff' --> None
            └── 'g' --> 1
        """
        return self._unraw(self._st.setdefault(key, self._raw(default)))

    cdef inline void _update(self, object d, dict kwargs) except *:
HansBug's avatar
HansBug 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
        cdef object dt
        if d is None:
            dt = {}
        elif isinstance(d, Mapping):
            dt = d
        elif isinstance(d, TreeValue):
            dt = d._detach().detach()
        elif isinstance(d, TreeStorage):
            dt = d.detach()
        else:
            raise TypeError(f'Invalid type of update dict - {type(d)!r}.')

        cdef str key
        cdef object value
        for key, value in dt.items():
            self._st.set(key, self._raw(value))
        for key, value in kwargs.items():
            self._st.set(key, self._raw(value))

    @cython.binding(True)
    def update(self, __update_dict=None, **kwargs):
        """
        Overview:
            Update items in current treevalue.

        :param __update_dict: Dictionary object for updating.
        :param kwargs: Arguments for updating.

        .. note::
            The method :meth:`update` is similar to \
            `dict.update <https://docs.python.org/3/library/stdtypes.html#dict.update>`_.

        Examples::
            >>> from treevalue import TreeValue
            >>>
            >>> t = TreeValue({'a': 1, 'b': 3, 'c': '233'})
            >>> t.update({'a': 10, 'f': 'sdkj'})  # with dict
            >>> t
            <TreeValue 0x7fa31f5ba048>
            ├── 'a' --> 10
            ├── 'b' --> 3
            ├── 'c' --> '233'
            └── 'f' --> 'sdkj'
            >>>
            >>> t.update(a=100, ft='fffff')  # with key-word arguments
            >>> t
            <TreeValue 0x7fa31f5ba048>
            ├── 'a' --> 100
            ├── 'b' --> 3
            ├── 'c' --> '233'
            ├── 'f' --> 'sdkj'
            └── 'ft' --> 'fffff'
            >>>
            >>> t.update(TreeValue({'f': {'x': 1}, 'b': 40}))  # with TreeValue
            >>> t
            <TreeValue 0x7fa31f5ba048>
            ├── 'a' --> 100
            ├── 'b' --> 40
            ├── 'c' --> '233'
            ├── 'f' --> <TreeValue 0x7fa31f5ba278>
            │   └── 'x' --> 1
            └── 'ft' --> 'fffff'
        """
        self._update(__update_dict, kwargs)

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    @cython.binding(True)
    cpdef _attr_extern(self, str key):
        r"""
        Overview:
            External protected function for support the unfounded attributes. \
            Default is raise a `KeyError`.

        Arguments:
            - key (:obj:`str`): Attribute name.

        Returns:
            - return (:obj:): Anything you like, \
                and if it is not able to validly return anything, \
                just raise an exception here.
        """
        raise AttributeError("Attribute {key} not found.".format(key=repr(key)))
320

321
    @cython.binding(True)
322
    def __getattribute__(self, str item):
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
        """
        Overview:
            Get item from this tree value.

        Arguments:
            - key (:obj:`str`): Attribute name.

        Returns:
            - attr (:obj:): Target attribute value.

        Example:
            >>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
            >>> t.a    # 1
            >>> t.b    # 2
            >>> t.x.c  # 3
        """
339 340 341 342

        # original order: __dict__, self._st, self._attr_extern
        # new order: self._st, __dict__, self._attr_extern
        # this may cause problem when pickle.loads, so __getnewargs_ex__ and __cinit__ is necessary
343
        if self._st.contains(item):
344
            return self._unraw(self._st.get(item))
345 346 347 348 349
        else:
            try:
                return object.__getattribute__(self, item)
            except AttributeError:
                return self._attr_extern(item)
HansBug's avatar
HansBug 已提交
350

351 352
    @cython.binding(True)
    def __setattr__(self, str key, object value):
353 354 355 356 357 358 359 360 361 362 363 364 365
        """
        Overview:
            Set sub node to this tree value.

        Arguments:
            - key (:obj:`str`): Attribute name.
            - value (:obj:): Sub value.

        Example:
            >>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
            >>> t.a = 3                 # t will be TreeValue({'a': 3, 'b': 2, 'x': {'c': 3, 'd': 4}})
            >>> t.b = {'x': 1, 'y': 2}  # t will be TreeValue({'a': 3, 'b': {'x': 1, 'y': 2}, 'x': {'c': 3, 'd': 4}})
        """
366
        self._st.set(key, self._raw(value))
HansBug's avatar
HansBug 已提交
367

368 369
    @cython.binding(True)
    def __delattr__(self, str item):
370 371 372 373 374 375 376 377 378 379 380 381
        """
        Overview:
            Delete attribute from tree value.

        Arguments:
            - key (:obj:`str`): Attribute name.

        Example:
            >>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
            >>> del t.a    # t will be TreeValue({'b': 2, 'x': {'c': 3, 'd': 4}})
            >>> del t.x.c  # t will be TreeValue({'b': 2, 'x': {'d': 4}})
        """
382 383 384 385
        try:
            self._st.del_(item)
        except KeyError:
            raise AttributeError("Unable to delete attribute {attr}.".format(attr=repr(item)))
HansBug's avatar
HansBug 已提交
386

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
    @cython.binding(True)
    cpdef _getitem_extern(self, object key):
        r"""
        Overview:
            External protected function for support the getitem operation. \
            Default is raise a `KeyError`.

        Arguments:
            - key (:obj:`object`): Item object.

        Returns:
            - return (:obj:): Anything you like, \
                and if it is not able to validly return anything, \
                just raise an ``KeyError`` here.
        """
402
        raise KeyError(f'Key {repr(key)} not found.')
403 404 405 406 407 408 409 410 411 412 413 414 415 416

    @cython.binding(True)
    def __getitem__(self, object key):
        """
        Overview:
            Get item from this tree value.

        Arguments:
            - key (:obj:`str`): Item object.

        Returns:
            - value (:obj:): Target object value.

        Example:
417
            >>> from treevalue import TreeValue
418
            >>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
419 420 421 422 423 424
            >>> t['a']
            1
            >>> t['b']
            2
            >>> t['x']['c']
            3
425
        """
426
        if isinstance(key, str):
427 428
            return self._unraw(self._st.get(key))
        else:
429
            return self._getitem_extern(_item_unwrap(key))
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456

    @cython.binding(True)
    cpdef _setitem_extern(self, object key, object value):
        r"""
        Overview:
            External function for supporting ``__setitem__`` operation.
        
        Arguments:
            - key (:obj:`object`): Key object.
            - value (:obj:`object`): Value object.
        
        Raises:
            - NotImplementError: Just raise this when not implemented.
        """
        raise NotImplementedError

    @cython.binding(True)
    def __setitem__(self, object key, object value):
        """
        Overview:
            Set item to current :class:`TreeValue` object.

        Arguments:
            - key (:obj:`object`): Key object.
            - value (:obj:`object`): Value object.

        Examples::
457
            >>> from treevalue import TreeValue
458 459 460 461
            >>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
            >>> t['a'] = 11
            >>> t['x']['c'] = 30
            >>> t
462 463 464 465 466 467
            <TreeValue 0x7f11704c5358>
            ├── 'a' --> 11
            ├── 'b' --> 2
            └── 'x' --> <TreeValue 0x7f11704c52e8>
                ├── 'c' --> 30
                └── 'd' --> 4
468 469 470 471
        """
        if isinstance(key, str):
            self._st.set(key, self._raw(value))
        else:
472
            self._setitem_extern(_item_unwrap(key), value)
473 474 475 476 477 478 479 480 481 482 483 484 485

    @cython.binding(True)
    cpdef _delitem_extern(self, object key):
        r"""
        Overview:
            External function for supporting ``__delitem__`` operation.
        
        Arguments:
            - key (:obj:`object`): Key object.
        
        Raises:
            - KeyError: Just raise this in default case.
        """
486
        raise KeyError(f'Key {repr(key)} not found.')
487 488 489 490 491 492 493 494 495

    @cython.binding(True)
    def __delitem__(self, object key):
        """
        Overview:
            Delete item from current :class:`TreeValue`.

        Arguments:
            - key (:obj:`object`): Key object.
496 497 498 499 500 501 502 503 504 505 506

        Examples::
            >>> from treevalue import TreeValue
            >>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
            >>> del t['a']
            >>> del t['x']['c']
            >>> t
            <TreeValue 0x7f11704c53c8>
            ├── 'b' --> 2
            └── 'x' --> <TreeValue 0x7f11704c5438>
                └── 'd' --> 4
507
        """
508
        if isinstance(key, str):
509 510
            self._st.del_(key)
        else:
511
            self._delitem_extern(_item_unwrap(key))
512

513 514
    @cython.binding(True)
    def __contains__(self, str item):
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
        """
        Overview:
            Check if attribute is in this tree value.

        Arguments:
            - key (:obj:`str`): Attribute name.

        Returns:
            - exist (:obj:`bool`): If attribute is in this tree value.

        Example:
            >>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
            >>> 'a' in t  # True
            >>> 'b' in t  # True
            >>> 'c' in t  # False
        """
531
        return self._st.contains(item)
HansBug's avatar
HansBug 已提交
532

533
    @cython.binding(True)
534
    def __iter__(self):
535 536 537 538
        """
        Overview:
            Get iterator of this tree value.

539
        :return: An iterator for the keys.
540

541 542 543
        Examples:
            >>> from treevalue import TreeValue
            >>>
544
            >>> t = TreeValue({'a': 1, 'b': 2, 'x': 3})
545 546 547 548 549
            >>> for key in t:
            ...     print(key)
            a
            b
            x
550

551 552 553 554 555
        .. note::
            The method :meth:`__iter__`'s bahaviour should be similar to \
            `dict.__iter__ <https://docs.python.org/3/library/stdtypes.html#dict.update>`_.
        """
        yield from self._st.iter_keys()
556

557 558
    @cython.binding(True)
    def __reversed__(self):
559
        """
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
        Overview:
            Get the reversed iterator of tree value.

        :return: A reversed iterator for the keys.

        Examples:
            >>> from treevalue import TreeValue
            >>>
            >>> t = TreeValue({'a': 1, 'b': 2, 'x': 3})
            >>> for key in reversed(t):
            ...     print(key)
            x
            b
            a

        .. note::
            Only available in python 3.8 or higher version.
        """
        if _reversible:
            return self._st.iter_rev_keys()
        else:
            raise TypeError(f'{self._type.__name__!r} object is not reversible')
582

583
    @cython.binding(True)
HansBug's avatar
HansBug 已提交
584
    def __len__(self):
585 586 587 588 589 590 591 592 593 594 595 596
        """
        Overview:
            Get count of the keys.

        Returns:
            - length (:obj:`int`): Count of the keys.

        Example:
            >>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
            >>> len(t)    # 3
            >>> len(t.x)  # 2
        """
597
        return self._st.size()
HansBug's avatar
HansBug 已提交
598

599 600
    @cython.binding(True)
    def __bool__(self):
601 602 603 604 605 606 607 608 609 610 611 612 613
        """
        Overview:
            Check if the tree value is not empty.

        Returns:
            - non_empty (:obj:`bool`): Not empty or do.

        Example:
            >>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}, 'e': {}})
            >>> not not t    # True
            >>> not not t.x  # True
            >>> not not t.e  # False
        """
614 615 616 617
        return not self._st.empty()

    @cython.binding(True)
    def __repr__(self):
618 619 620 621 622 623 624 625 626
        """
        Overview:
            Get representation format of tree value.

        Returns:
            - repr (:obj:`str`): Representation string.

        Example:
            >>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
627 628 629 630 631 632 633
            >>> t
            <TreeValue 0x7f672fc53320>
            ├── 'a' --> 1
            ├── 'b' --> 2
            └── 'x' --> <TreeValue 0x7f672fc53390>
                ├── 'c' --> 3
                └── 'd' --> 4
634
        """
635 636 637 638
        return format_tree(
            _build_tree(self._detach(), self._type, '', {}, ()),
            itemgetter(0), itemgetter(1),
        )
HansBug's avatar
HansBug 已提交
639

640
    @cython.binding(True)
HansBug's avatar
HansBug 已提交
641
    def __hash__(self):
642 643 644 645 646 647 648
        """
        Overview:
            Hash value of current object.

        Returns:
            - hash (:obj:`int`): Hash code of current object.
        """
649
        return hash(self._st)
HansBug's avatar
HansBug 已提交
650

651 652
    @cython.binding(True)
    def __eq__(self, object other):
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
        """
        Overview:
            Check the equality of two tree values.

        Arguments:
            - other (:obj:`TreeValue`): Another tree value.

        Returns:
            - equal (:obj:`bool`): Equality.

        Example:
            >>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
            >>> clone(t) == t                                                # True
            >>> t == TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 5}})      # False
            >>> t == TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})      # True
            >>> t == FastTreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})  # False (type not match)
        """
HansBug's avatar
HansBug 已提交
670 671
        if self is other:
            return True
672 673
        elif type(other) == self._type:
            return self._st == other._detach()
HansBug's avatar
HansBug 已提交
674 675 676
        else:
            return False

677 678
    @cython.binding(True)
    def __setstate__(self, TreeStorage state):
HansBug's avatar
HansBug 已提交
679 680 681 682 683 684
        """
        Overview:
            Deserialize operation, can support `pickle.loads`.

        Arguments:
            - tree (:obj:`Tree`): Deserialize tree.
HansBug's avatar
HansBug 已提交
685 686 687 688 689 690 691 692

        Examples:
            >>> import pickle
            >>> from treevalue import TreeValue
            >>>
            >>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3}})
            >>> bin_ = pickle.dumps(t)  # dump it to binary
            >>> pickle.loads(bin_)      #  TreeValue({'a': 1, 'b': 2, 'x': {'c': 3}})
HansBug's avatar
HansBug 已提交
693
        """
694
        self._st = state
HansBug's avatar
HansBug 已提交
695

696
    @cython.binding(True)
HansBug's avatar
HansBug 已提交
697 698 699 700
    def __getstate__(self):
        """
        Overview:
            Serialize operation, can support `pickle.dumps`.
HansBug's avatar
HansBug 已提交
701 702 703 704 705 706 707 708

        Examples:
            >>> import pickle
            >>> from treevalue import TreeValue
            >>>
            >>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3}})
            >>> bin_ = pickle.dumps(t)  # dump it to binary
            >>> pickle.loads(bin_)      #  TreeValue({'a': 1, 'b': 2, 'x': {'c': 3}})
HansBug's avatar
HansBug 已提交
709
        """
710
        return self._st
711

712
    @cython.binding(True)
713
    cpdef treevalue_keys keys(self):
714 715 716 717 718 719
        """
        Overview:
            Get keys of this treevalue object, like the :class:`dict`.

        Returns:
            - keys: A generator of all the keys.
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739

        Examples::
            >>> from treevalue import TreeValue
            >>> 
            >>> t = TreeValue({'a': 1, 'b': 3, 'c': '233'})
            >>> t.keys()
            treevalue_keys(['a', 'b', 'c'])
            >>> len(t.keys())
            3
            >>> list(t.keys())
            ['a', 'b', 'c']
            >>> list(reversed(t.keys()))  # only available in python3.8+
            ['c', 'b', 'a']
            >>> 'a' in t.keys()
            True
            >>> 'f' in t.keys()
            False

        .. note::
            :func:`reversed` is only available in python 3.8 or higher versions.
740
        """
741
        return treevalue_keys(self._st, self._type)
742 743

    @cython.binding(True)
744
    cpdef treevalue_values values(self):
745 746 747 748 749 750
        """
        Overview:
            Get value of this treevalue object, like the :class:`dict`.

        Returns:
            - values: A generator of all the values
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770

        Examples::
            >>> from treevalue import TreeValue
            >>> 
            >>> t = TreeValue({'a': 1, 'b': 3, 'c': '233'})
            >>> t.values()
            treevalue_values([1, 3, '233'])
            >>> len(t.values())
            3
            >>> list(t.values())
            [1, 3, '233']
            >>> list(reversed(t.values()))  # only supported on python3.8+
            ['233', 3, 1]
            >>> 1 in t.values()
            True
            >>> 'fff' in t.values()
            False

        .. note::
            :func:`reversed` is only available in python 3.8 or higher versions.
771
        """
772
        return treevalue_values(self._st, self._type)
773 774

    @cython.binding(True)
775
    cpdef treevalue_items items(self):
776 777 778 779 780 781
        """
        Overview:
            Get pairs of keys and values of this treevalue object, like the :class:`items`.

        Returns:
            - items: A generator of pairs of keys and values.
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801

        Examples::
            >>> from treevalue import TreeValue
            >>> 
            >>> t = TreeValue({'a': 1, 'b': 3, 'c': '233'})
            >>> t.items()
            treevalue_items([('a', 1), ('b', 3), ('c', '233')])
            >>> len(t.items())
            3
            >>> list(t.items())
            [('a', 1), ('b', 3), ('c', '233')]
            >>> list(reversed(t.items()))  # only supported on python3.8+
            [('c', '233'), ('b', 3), ('a', 1)]
            >>> ('a', 1) in t.items()
            True
            >>> ('c', '234') in t.values()
            False

        .. note::
            :func:`reversed` is only available in python 3.8 or higher versions.
802
        """
803
        return treevalue_items(self._st, self._type)
804

805 806 807 808 809 810 811 812 813 814
cdef str _prefix_fix(object text, object prefix):
    cdef list lines = []
    cdef int i
    cdef str line
    cdef str white = ' ' * len(prefix)
    for i, line in enumerate(text.splitlines()):
        lines.append((prefix if i == 0 else white) + line)

    return os.linesep.join(lines)

815 816 817
cdef inline str _title_repr(TreeStorage st, object type_):
    return f'<{type_.__name__} {hex(id(st))}>'

818 819
cdef object _build_tree(TreeStorage st, object type_, str prefix, dict id_pool, tuple path):
    cdef object nid = id(st)
820
    cdef str self_repr = _title_repr(st, type_)
821 822 823
    cdef list children = []

    cdef str k, _t_prefix
HansBug's avatar
HansBug 已提交
824
    cdef object v, nv
825 826 827 828 829 830 831 832 833
    cdef dict data
    cdef tuple curpath
    if nid in id_pool:
        self_repr = os.linesep.join([
            self_repr, f'(The same address as {".".join(("<root>", *id_pool[nid]))})'])
    else:
        id_pool[nid] = path
        data = st.detach()
        for k, v in sorted(data.items()):
834
            v = _c_undelay_data(data, k, v)
835
            curpath = path + (k,)
836
            _t_prefix = f'{repr(k)} --> '
837 838 839 840 841 842 843
            if isinstance(v, TreeStorage):
                children.append(_build_tree(v, type_, _t_prefix, id_pool, curpath))
            else:
                children.append((_prefix_fix(repr(v), _t_prefix), []))

    self_repr = _prefix_fix(self_repr, prefix)
    return self_repr, children
HansBug's avatar
HansBug 已提交
844

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
# noinspection PyPep8Naming
cdef class treevalue_keys(_CObject, Sized, Container, Reversible):
    def __cinit__(self, TreeStorage storage, type _type):
        self._st = storage
        self._type = _type

    def __len__(self):
        return self._st.size()

    def __contains__(self, item):
        return self._st.contains(item)

    def _iter(self):
        for k in self._st.iter_keys():
            yield k

    def __iter__(self):
        return self._iter()

    def _rev_iter(self):
        for k in self._st.iter_rev_keys():
            yield k

    def __reversed__(self):
        if _reversible:
            return self._rev_iter()
        else:
            raise TypeError(f'{type(self).__name__!r} object is not reversible')

    def __repr__(self):
        return f'{type(self).__name__}({list(self)!r})'

# noinspection PyPep8Naming
cdef class treevalue_values(_CObject, Sized, Container, Reversible):
    def __cinit__(self, TreeStorage storage, type _type):
        self._st = storage
        self._type = _type

    def __len__(self):
        return self._st.size()

    def __contains__(self, item):
        for v in self:
            if item == v:
                return True

        return False

    def _iter(self):
        for v in self._st.iter_values():
            if isinstance(v, TreeStorage):
                yield self._type(v)
            else:
                yield v

    def __iter__(self):
        return self._iter()

    def _rev_iter(self):
        for v in self._st.iter_rev_values():
            if isinstance(v, TreeStorage):
                yield self._type(v)
            else:
                yield v

    def __reversed__(self):
        if _reversible:
            return self._rev_iter()
        else:
            raise TypeError(f'{type(self).__name__!r} object is not reversible')

    def __repr__(self):
        return f'{type(self).__name__}({list(self)!r})'

# noinspection PyPep8Naming
cdef class treevalue_items(_CObject, Sized, Container, Reversible):
    def __cinit__(self, TreeStorage storage, type _type):
        self._st = storage
        self._type = _type

    def __len__(self):
        return self._st.size()

    def __contains__(self, item):
        for k, v in self:
            if item == (k, v):
                return True

        return False

    def _iter(self):
        for k, v in self._st.iter_items():
            if isinstance(v, TreeStorage):
                yield k, self._type(v)
            else:
                yield k, v

    def __iter__(self):
        return self._iter()

    def _rev_iter(self):
        for k, v in self._st.iter_rev_items():
            if isinstance(v, TreeStorage):
                yield k, self._type(v)
            else:
                yield k, v

    def __reversed__(self):
        if _reversible:
            return self._rev_iter()
        else:
            raise TypeError(f'{type(self).__name__!r} object is not reversible')

    def __repr__(self):
        return f'{type(self).__name__}({list(self)!r})'

HansBug's avatar
HansBug 已提交
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
cdef class DetachedDelayedProxy(DelayedProxy):
    def __init__(self, DelayedProxy proxy):
        self.proxy = proxy
        self.calculated = False
        self.val = None

    cpdef object value(self):
        if not self.calculated:
            self.val = undelay(self.proxy, False)
            self.calculated = True

        return self.val

    cpdef object fvalue(self):
        cdef object v = self.value()
        if isinstance(v, TreeValue):
            v = v._detach()
        return v

@cython.binding(True)
def delayed(func, *args, **kwargs):
    r"""
    Overview:
        Use delayed function in treevalue.
HansBug's avatar
HansBug 已提交
985 986
        The given ``func`` will not be called until its value is accessed, and \
        it will be only called once, after that the delayed node will be replaced by the actual value.
HansBug's avatar
HansBug 已提交
987 988 989 990 991

    Arguments:
        - func: Delayed function.
        - args: Positional arguments.
        - kwargs: Key-word arguments.
HansBug's avatar
HansBug 已提交
992 993 994 995

    Examples::
        >>> from treevalue import TreeValue, delayed
        >>> def f(x):
996 997 998
        ...     print('f is called, x is', x)
        ...     return x ** x
        ...
HansBug's avatar
HansBug 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
        >>> t = TreeValue({'a': delayed(f, 2), 'x': delayed(f, 3)})
        >>> t.a
        f is called, x is 2
        4
        >>> t.x
        f is called, x is 3
        27
        >>> t.a
        4
        >>> t.x
        27
        >>> t = TreeValue({'a': delayed(f, 2), 'x': delayed(f, 3)})
        >>> print(t)
        f is called, x is 2
        f is called, x is 3
1014 1015 1016
        <TreeValue 0x7f672fc53550>
        ├── 'a' --> 4
        └── 'x' --> 27
HansBug's avatar
HansBug 已提交
1017
        >>> print(t)
1018 1019 1020
        <TreeValue 0x7f672fc53550>
        ├── 'a' --> 4
        └── 'x' --> 27
HansBug's avatar
HansBug 已提交
1021 1022
    """
    return DetachedDelayedProxy(_c_delayed_partial(func, args, kwargs))