hbutils.collection.recover

register_recovery

hbutils.collection.recover.register_recovery(cls: Type[hbutils.collection.recover.BaseRecovery])[source]
Overview:

Register recovery class.

Parameters:

cls – Recovery class.

Note

This API is used for customize recovery for other classes. For more details, you may take a look at the source code of BaseRecovery.

get_recovery_func

hbutils.collection.recover.get_recovery_func(origin: _OriginType, recursive: bool = True) → Callable[], _OriginType][source]
Overview:

Get recovery function for given object. Dict, list and tuple object are natively supported.

Parameters:
  • origin – Original object to recover.

  • recursive – Recursive or not. Default is True which means the child-level object contained in origin will be recovered as well.

Returns:

Recovery function.

Examples::
>>> from hbutils.collection import get_recovery_func
>>> l = [1, {'a': 1, 'b': 2}, 3, 4, 5]
>>> print(id(l), l)
140146367304720 [1, {'a': 1, 'b': 2}, 3, 4, 5]
>>> f = get_recovery_func(l)
>>> l[3] = 1
>>> l.pop()
>>> l.append('sdklfj')
>>> l.append('sdkfhjksd')
>>> l[1]['c'] = 2
>>> l[1]['a'] = 100
>>> print(id(l), l)
140146367304720 [1, {'a': 100, 'b': 2, 'c': 2}, 3, 1, 'sdklfj', 'sdkfhjksd']
>>> lx = f()
>>> print(id(lx), lx)  # lx is l
140146367304720 [1, {'a': 1, 'b': 2}, 3, 4, 5]
>>> print(id(l), l)  # the value is recovered
140146367304720 [1, {'a': 1, 'b': 2}, 3, 4, 5]

BaseRecovery

class hbutils.collection.recover.BaseRecovery(origin: _OriginType)[source]
__init__(origin: _OriginType)[source]

Constructor of BaseRecovery.

Parameters:

origin – Origin object to be recovered.

classmethod _create_child(child, recursive: bool = True)[source]

Create child-level object for storage usage.

Parameters:
  • child – Original child-level object.

  • recursive – Recursive or not. Default is True which means the child-level object contained in origin will be recovered as well.

Returns:

Object for storage.

_recover()[source]

Implementation for recovery.

classmethod _recover_child(child)[source]

Get recovered child-level object.

Parameters:

child – Child object, should be a BaseRecovery or native object.

Returns:

Recovered child-level object.

classmethod from_origin(origin: _OriginType, recursive: bool = True)hbutils.collection.recover.BaseRecovery[source]

Create a recovery object by the given original object.

Parameters:
  • origin – Original object to recover.

  • recursive – Recursive or not. Default is True which means the child-level object contained in origin will be recovered as well.

Returns:

Recovery object.

recover() → _OriginType[source]

Recover the given object.

Returns:

Recovered object.

DictRecovery

class hbutils.collection.recover.DictRecovery(origin: _DictType, mp: Dict)[source]
__init__(origin: _DictType, mp: Dict)[source]

Constructor of DictRecovery.

Parameters:

origin – Origin object to be recovered.

classmethod _create_child(child, recursive: bool = True)

Create child-level object for storage usage.

Parameters:
  • child – Original child-level object.

  • recursive – Recursive or not. Default is True which means the child-level object contained in origin will be recovered as well.

Returns:

Object for storage.

_recover()[source]

Implementation for recovery.

classmethod _recover_child(child)

Get recovered child-level object.

Parameters:

child – Child object, should be a BaseRecovery or native object.

Returns:

Recovered child-level object.

classmethod from_origin(origin: _DictType, recursive: bool = True)hbutils.collection.recover.DictRecovery[source]

Create a recovery object by the given original object.

Parameters:
  • origin – Original object to recover.

  • recursive – Recursive or not. Default is True which means the child-level object contained in origin will be recovered as well.

Returns:

Recovery object.

recover() → _OriginType

Recover the given object.

Returns:

Recovered object.

ListRecovery

class hbutils.collection.recover.ListRecovery(origin: _ListType, items: List)[source]
__init__(origin: _ListType, items: List)[source]

Constructor of ListRecovery.

Parameters:

origin – Origin object to be recovered.

classmethod _create_child(child, recursive: bool = True)

Create child-level object for storage usage.

Parameters:
  • child – Original child-level object.

  • recursive – Recursive or not. Default is True which means the child-level object contained in origin will be recovered as well.

Returns:

Object for storage.

_recover()[source]

Implementation for recovery.

classmethod _recover_child(child)

Get recovered child-level object.

Parameters:

child – Child object, should be a BaseRecovery or native object.

Returns:

Recovered child-level object.

classmethod from_origin(origin: _ListType, recursive: bool = True)hbutils.collection.recover.ListRecovery[source]

Create a recovery object by the given original object.

Parameters:
  • origin – Original object to recover.

  • recursive – Recursive or not. Default is True which means the child-level object contained in origin will be recovered as well.

Returns:

Recovery object.

recover() → _OriginType

Recover the given object.

Returns:

Recovered object.

TupleRecovery

class hbutils.collection.recover.TupleRecovery(origin: _TupleType, items: List[Any])[source]
__init__(origin: _TupleType, items: List[Any])[source]

Constructor of TupleRecovery.

Parameters:

origin – Origin object to be recovered.

classmethod _create_child(child, recursive: bool = True)

Create child-level object for storage usage.

Parameters:
  • child – Original child-level object.

  • recursive – Recursive or not. Default is True which means the child-level object contained in origin will be recovered as well.

Returns:

Object for storage.

_recover()[source]

Implementation for recovery.

classmethod _recover_child(child)

Get recovered child-level object.

Parameters:

child – Child object, should be a BaseRecovery or native object.

Returns:

Recovered child-level object.

classmethod from_origin(origin: _TupleType, recursive: bool = True)hbutils.collection.recover.TupleRecovery[source]

Create a recovery object by the given original object.

Parameters:
  • origin – Original object to recover.

  • recursive – Recursive or not. Default is True which means the child-level object contained in origin will be recovered as well.

Returns:

Recovery object.

recover() → _OriginType

Recover the given object.

Returns:

Recovered object.

NullRecovery

class hbutils.collection.recover.NullRecovery(origin: _OriginType)[source]
Overview:

Empty recovery class for builtin immutable types.

__init__(origin: _OriginType)

Constructor of BaseRecovery.

Parameters:

origin – Origin object to be recovered.

classmethod _create_child(child, recursive: bool = True)

Create child-level object for storage usage.

Parameters:
  • child – Original child-level object.

  • recursive – Recursive or not. Default is True which means the child-level object contained in origin will be recovered as well.

Returns:

Object for storage.

_recover()[source]

Just do nothing.

classmethod _recover_child(child)

Get recovered child-level object.

Parameters:

child – Child object, should be a BaseRecovery or native object.

Returns:

Recovered child-level object.

classmethod from_origin(origin: _OriginType, recursive: bool = True)hbutils.collection.recover.NullRecovery[source]

Just do nothing.

recover() → _OriginType

Recover the given object.

Returns:

Recovered object.

GenericObjectRecovery

class hbutils.collection.recover.GenericObjectRecovery(origin: _OriginType, dict_: Optional[DictRecovery])[source]
Overview:

Recovery class for generic objects. The __dict__ will be recovered.

Note

If what you need to recover is not only __dict__, may be you need to custom recovery class by inheriting BaseRecovery class, and register it by register_recovery() function.

__init__(origin: _OriginType, dict_: Optional[DictRecovery])[source]

Constructor of GenericObjectRecovery.

Parameters:
  • origin – Original object to recover.

  • dict – Recovery object of __dict__, None when origin do not have __dict__

classmethod _create_child(child, recursive: bool = True)

Create child-level object for storage usage.

Parameters:
  • child – Original child-level object.

  • recursive – Recursive or not. Default is True which means the child-level object contained in origin will be recovered as well.

Returns:

Object for storage.

_recover()[source]

Recover the __dict__.

classmethod _recover_child(child)

Get recovered child-level object.

Parameters:

child – Child object, should be a BaseRecovery or native object.

Returns:

Recovered child-level object.

classmethod from_origin(origin: _OriginType, recursive: bool = True)hbutils.collection.recover.BaseRecovery[source]

Create recovery object.

recover() → _OriginType

Recover the given object.

Returns:

Recovered object.