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)[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.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

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)[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.