https://github.com/limix/pickle-mixin
Makes un-pickle-able objects pick-able.
https://github.com/limix/pickle-mixin
Last synced: 8 months ago
JSON representation
Makes un-pickle-able objects pick-able.
- Host: GitHub
- URL: https://github.com/limix/pickle-mixin
- Owner: limix
- License: mit
- Created: 2017-02-14T15:33:15.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-04-09T22:27:16.000Z (about 9 years ago)
- Last Synced: 2025-07-01T00:52:31.655Z (11 months ago)
- Language: Python
- Size: 17.6 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pickle-mixin
[](https://pypi.python.org/pypi/pickle-mixin/)
[](https://pypi.python.org/pypi/pickle-mixin/)
Makes un-pickle-able objects pick-able.
## Install
You can install it via pip
```
pip install pickle-mixin
```
## Running the tests
After installation, you can test it
```
python -c "import pickle_mixin; pickle_mixin.test()"
```
as long as you have [pytest](http://docs.pytest.org/en/latest/).
## Usage
### Pickle by initialisation
Suppose that you have a class whose objects are un-pickle-able or that would
demand a large amount of disk space or memory to be pickle-able.
``PickleByInit`` class lets you pickle object attributes via object
initialization.
Consider the following classes:
```python
class Foo(PickleByInit):
def __init__(self, obj):
super(Foo, self).__init__()
self.obj = obj
class Bar(object):
def __init__(self, filename):
self.filename = filename
def __getstate__(self):
raise PicklingError
def init_dict(self):
return dict(filename=self.filename)
```
Trying to pickle as follows
```python
f = Foo(Bar('file.txt'))
pickle.dumps(f)
```
would raise a ``PicklingError``.
The following on the other hand would work:
```python
f = Foo(Bar('file.txt'))
f.set_signature_only_attr('obj')
pickle.dumps(f)
```
The un-pickling process of ``f.obj`` attribute happens via object
initialisation, passing the returned dictionary from ``init_dict``
as keyword arguments to ``Bar.__init__``.
### Mixing classes with and without slots
Pickling does not save attributes defined via ``__slots__`` in the following
case:
```python
class Foo(object):
__slots__ = ['a']
def __init__(self):
self.a = 4
class Bar(Foo):
def __init__(self):
pass
```
``SlotPickleMixin`` fixes it:
```python
class FooMixin(object):
__slots__ = ['a']
def __init__(self):
self.a = 4
class BarMixin(FooMixin, SlotPickleMixin):
def __init__(self):
FooMixin.__init__(self)
SlotPickleMixin.__init__(self)
f = BarMixin()
o = pickle.dumps(f)
f = pickle.loads(o)
assert hasattr(f, 'a')
```
## Authors
* **Danilo Horta** - [https://github.com/Horta](https://github.com/Horta)
## License
This project is licensed under the MIT License - see the
[LICENSE](LICENSE) file for details