diff --git a/treevalue/tree/tree/tree.pxd b/treevalue/tree/tree/tree.pxd index c034b1e06577c05d103ad6590a19db15fd181719..53e9ec5d846a0ea19389e876a34de680d05bbc38 100644 --- a/treevalue/tree/tree/tree.pxd +++ b/treevalue/tree/tree/tree.pxd @@ -3,6 +3,7 @@ from libcpp cimport bool +from .constraint cimport Constraint from ..common.delay cimport DelayedProxy from ..common.storage cimport TreeStorage @@ -11,6 +12,7 @@ cdef class _CObject: cdef class TreeValue: cdef readonly TreeStorage _st + cdef readonly Constraint _constraint cdef readonly type _type cpdef TreeStorage _detach(self) @@ -20,7 +22,7 @@ cdef class TreeValue: cpdef _getitem_extern(self, object key) cpdef _setitem_extern(self, object key, object value) cpdef _delitem_extern(self, object key) - cdef void _update(self, object d, dict kwargs) except * + cdef void _update(self, object d, dict kwargs) except* cpdef public get(self, str key, object default= *) cpdef public pop(self, str key, object default= *) cpdef public popitem(self) diff --git a/treevalue/tree/tree/tree.pyx b/treevalue/tree/tree/tree.pyx index 148fac32d3a3507ae89cdfd1700ef3dac7f75a4d..e63e6c1df0e198f4bbe48a6784e09719f7e5a366 100644 --- a/treevalue/tree/tree/tree.pyx +++ b/treevalue/tree/tree/tree.pyx @@ -8,6 +8,7 @@ from operator import itemgetter import cython from hbutils.design import SingletonMark +from .constraint cimport Constraint, to_constraint, EmptyConstraint from ..common.delay cimport undelay, _c_delayed_partial, DelayedProxy from ..common.storage cimport TreeStorage, create_storage, _c_undelay_data from ...utils import format_tree @@ -56,12 +57,13 @@ cdef class TreeValue: The `TreeValue` class is a light-weight framework just for DIY. """ - def __cinit__(self, object data): + def __cinit__(self, object data, Constraint constraint=None): self._st = _DEFAULT_STORAGE + self._constraint = EmptyConstraint() if constraint is None else constraint self._type = type(self) @cython.binding(True) - def __init__(self, object data): + def __init__(self, object data, object constraint=None): """ Constructor of :class:`TreeValue`. @@ -100,6 +102,8 @@ cdef class TreeValue: "Unknown initialization type for tree value - {type}.".format( type=repr(type(data).__name__))) + self._constraint = to_constraint(constraint) + def __getnewargs_ex__(self): # for __cinit__, when pickle.loads return ({},), {}