AutoSlots
(class from pyomo.common.autoslots
)
- class pyomo.common.autoslots.AutoSlots(name, bases, classdict)[source]
Bases:
type
Metaclass to automatically collect __slots__ for generic pickling
The class __slots__ are collected in reverse MRO order.
Any fields that require special handling are handled through callbacks specified through the __autoslot_mappers__ class attribute. __autoslot_mappers__ should be a dict that maps the field name (either __slot__ or regular __dict__ entry) to a function with the signature:
mapper(encode: bool, val: Any) -> Any
The value from the object field (or state) is passed to the mapper function, and the function returns the corrected value. __getstate__ calls the mapper with encode=True, and __setstate__ calls the mapper with encode=False. __autoslot_mappers__ class attributes are collected and combined in reverse MRO order (so duplicate mappers in more derived classes will replace mappers defined in base classes).
AutoSlots
defines several common mapper functions, including:Result
This metaclass will add a __auto_slots__ class attribute to the class (and all derived classes). This attribute is an instance of a
_autoslot_info
named 4-tuple:(has_dict, slots, slot_mappers, field_mappers)
- has_dict: bool
True if this class has a __dict__ attribute (that would need to be pickled in addition to the __slots__)
- slots: tuple
Tuple of all slots declared for this class (the union of any slots declared locally with all slots declared on any base class)
- slot_mappers: dict
Dict mapping index in slots to a function with signature mapper(encode: bool, val: Any) that can be used to encode or decode that slot
- field_mappers: dict
Dict mapping field name in __dict__ to a function with signature mapper(encode: bool, val: Any) that can be used to encode or decode that field value.
Methods
__init__
(name, bases, classdict)collect_autoslots
(cls)encode_as_none
(encode, val)__autoslot_mappers__ mapper that will replace fields with None
mro
()Return a type's method resolution order.
weakref_mapper
(encode, val)__autoslot_mappers__ mapper for fields that contain weakrefs
weakref_sequence_mapper
(encode, val)__autoslot_mappers__ mapper for fields with sequences of weakrefs
Member Documentation
- class Mixin[source]
Bases:
object
Mixin class to configure a class hierarchy to use AutoSlots
Inheriting from this class will set up the automatic generation of the __auto_slots__ class attribute, and define the standard implementations for __deepcopy__, __getstate__, and __setstate__.
- static encode_as_none(encode, val)[source]
__autoslot_mappers__ mapper that will replace fields with None
This mapper will encode the field as None (regardless of the current field value). No mapping occurs when restoring a state.
- mro()
Return a type’s method resolution order.
- static weakref_mapper(encode, val)[source]
__autoslot_mappers__ mapper for fields that contain weakrefs
This mapper expects to be passed a field containing either a weakref or None. It will resolve the weakref to a hard reference when generating a state, and then convert the hard reference back to a weakref when restoring the state.
- static weakref_sequence_mapper(encode, val)[source]
__autoslot_mappers__ mapper for fields with sequences of weakrefs
This mapper expects to be passed a field that is a sequence of weakrefs. It will resolve all weakrefs when generating a state, and then convert the hard references back to a weakref when restoring the state.