hbutils.collection.recover
Recovery utilities for restoring Python objects to their original state.
This module implements a flexible recovery system that captures the state of objects and provides a callable to restore them later. It supports nested structures and common built-in containers, and it can be extended for custom classes via registration.
The module contains the following main components:
BaseRecovery- Abstract base class for recovery implementations.DictRecovery- Recovery for dictionaries.ListRecovery- Recovery for lists.TupleRecovery- Recovery for tuples (recovers mutable children).NullRecovery- No-op recovery for immutable primitives.GenericObjectRecovery- Recovery for generic objects via__dict__.register_recovery()- Register custom recovery classes.get_recovery_func()- Create a recovery callable for an object.
Note
The recovery mechanism stores references to the original objects and restores them in-place. For mutable containers, this means the identity of the original object is preserved after recovery.
Example:
>>> from hbutils.collection import get_recovery_func
>>> l = [1, {'a': 1, 'b': 2}, 3, 4, 5]
>>> f = get_recovery_func(l)
>>> l[3] = 1
>>> l.pop()
>>> recovered = f()
>>> recovered is l
True
>>> l
[1, {'a': 1, 'b': 2}, 3, 4, 5]
__all__
- hbutils.collection.recover.__all__ = ['BaseRecovery', 'DictRecovery', 'ListRecovery', 'TupleRecovery', 'NullRecovery', 'GenericObjectRecovery', 'register_recovery', 'get_recovery_func']
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.
BaseRecovery
- class hbutils.collection.recover.BaseRecovery(origin: _OriginType)[source]
Base class for all recovery implementations.
This abstract class defines the interface for recovery objects that can restore Python objects to their original state. Subclasses implement the
_recover()andfrom_origin()methods to provide specific logic for different types.- Variables:
__rtype__ – The type(s) that this recovery class can handle.
origin – The original object to be recovered.
- __init__(origin: _OriginType)[source]
Constructor of
BaseRecovery.- Parameters:
origin (_OriginType) – Origin object to be recovered.
- __rtype__
alias of
object
- classmethod from_origin(origin: _OriginType, recursive: bool = True) BaseRecovery[source]
Create a recovery object by the given original object.
This method should be overridden by subclasses to create an appropriate recovery object for the given original object.
- Parameters:
origin (_OriginType) – Original object to recover.
recursive (bool) – Recursive or not. Default is
Truewhich means the child-level object contained inoriginwill be recovered as well.
- Returns:
Recovery object.
- Return type:
- Raises:
NotImplementedError – This method must be implemented by subclasses.
DictRecovery
- class hbutils.collection.recover.DictRecovery(origin: _DictType, mp: Dict)[source]
Recovery class for dictionary objects.
This class handles the recovery of dictionary objects by storing their key-value pairs and restoring them when recovery is triggered.
- Variables:
mapping – Dictionary mapping of keys to values (or recovery objects).
- __init__(origin: _DictType, mp: Dict)[source]
Constructor of
DictRecovery.- Parameters:
origin (_DictType) – Origin object to be recovered.
mp (Dict) – Dictionary mapping of keys to values or recovery objects.
- __rtype__
alias of
dict
- classmethod from_origin(origin: _DictType, recursive: bool = True) DictRecovery[source]
Create a
DictRecoveryobject from a dictionary.- Parameters:
origin (_DictType) – Original dictionary to recover.
recursive (bool) – Whether to recursively create recovery objects for values.
- Returns:
DictRecovery object.
- Return type:
TupleRecovery
- class hbutils.collection.recover.TupleRecovery(origin: _TupleType, items: List[Any])[source]
Recovery class for tuple objects.
Since tuples are immutable, this class primarily handles recovery of mutable objects contained within the tuple.
- Variables:
items – List of items (or recovery objects) in the tuple.
- __init__(origin: _TupleType, items: List[Any])[source]
Constructor of
TupleRecovery.- Parameters:
origin (_TupleType) – Origin object to be recovered.
items (List[Any]) – List of items or recovery objects from the tuple.
- __rtype__
alias of
tuple
- classmethod from_origin(origin: _TupleType, recursive: bool = True) TupleRecovery[source]
Create a
TupleRecoveryobject from a tuple.- Parameters:
origin (_TupleType) – Original tuple to recover.
recursive (bool) – Whether to recursively create recovery objects for items.
- Returns:
TupleRecovery object.
- Return type:
ListRecovery
- class hbutils.collection.recover.ListRecovery(origin: _ListType, items: List)[source]
Recovery class for list objects.
This class handles the recovery of list objects by storing their elements and restoring them when recovery is triggered.
- Variables:
items – List of items (or recovery objects) from the original list.
- __init__(origin: _ListType, items: List)[source]
Constructor of
ListRecovery.- Parameters:
origin (_ListType) – Origin object to be recovered.
items (List) – List of items or recovery objects from the original list.
- __rtype__
alias of
list
- classmethod from_origin(origin: _ListType, recursive: bool = True) ListRecovery[source]
Create a
ListRecoveryobject from a list.- Parameters:
origin (_ListType) – Original list to recover.
recursive (bool) – Whether to recursively create recovery objects for items.
- Returns:
ListRecovery object.
- Return type:
NullRecovery
- class hbutils.collection.recover.NullRecovery(origin: _OriginType)[source]
Empty recovery class for builtin immutable types.
This class is used for immutable types that cannot be modified and therefore do not need any recovery logic. It simply stores a reference to the original object.
- classmethod from_origin(origin: _OriginType, recursive: bool = True) NullRecovery[source]
Just do nothing.
Create a
NullRecoveryobject for an immutable type.- Parameters:
origin (_OriginType) – Original immutable object.
recursive (bool) – Ignored for immutable types.
- Returns:
NullRecovery object.
- Return type:
GenericObjectRecovery
- class hbutils.collection.recover.GenericObjectRecovery(origin: _OriginType, dict_: DictRecovery | None)[source]
Recovery class for generic objects.
The
__dict__will be recovered.This class provides recovery for arbitrary Python objects by storing and restoring their
__dict__attribute. This works for most custom classes but may not be sufficient for objects with special state storage.- Variables:
dict – Recovery object for the object’s
__dict__, or None if no__dict__exists.
Note
If what you need to recover is not only
__dict__, you may need to create a custom recovery class by inheritingBaseRecovery, and register it byregister_recovery().- __init__(origin: _OriginType, dict_: DictRecovery | None)[source]
Constructor of
GenericObjectRecovery.- Parameters:
origin (_OriginType) – Original object to recover.
dict (Optional[DictRecovery]) – Recovery object of
__dict__,Nonewhenorigindoes not have__dict__.
- __rtype__
alias of
object
- classmethod from_origin(origin: _OriginType, recursive: bool = True) BaseRecovery[source]
Create recovery object.
Creates a
GenericObjectRecoveryby storing the object’s__dict__if it exists.- Parameters:
origin (_OriginType) – Original object to recover.
recursive (bool) – Whether to recursively create recovery objects for
__dict__values.
- Returns:
GenericObjectRecovery object.
- Return type:
register_recovery
- hbutils.collection.recover.register_recovery(cls: Type[BaseRecovery]) None[source]
Register recovery class.
This function registers a custom recovery class to the global registry, allowing it to be used for recovering objects of the types specified in the class’s
__rtype__attribute.- Parameters:
cls (Type[BaseRecovery]) – Recovery class to register.
Note
This API is used for custom 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]
Get recovery function for given object.
Dict, list and tuple object are natively supported.
This function creates a recovery object for the given object and returns a callable that will restore the object to its original state when invoked.
- Parameters:
origin (_OriginType) – Original object to recover.
recursive (bool) – Recursive or not. Default is
Truewhich means the child-level object contained inoriginwill be recovered as well.
- Returns:
Recovery function that restores the object when called.
- Return type:
Callable[[], _OriginType]
- 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]