Package: hickleable
A simple decorator to make your classes hickle-able.What is this?hickleableprovides a simple decorator for your classes that will almost always
make them serilalize well using the excellenthicklepackage. By default, custom classes are
not supported byhickle– instead, they are written to the HDF5 file as a binary
dataset that is serialized using the standard Pythonpickle. This obviously negates
much of the benefit ofhickle, for example, the fact that pickle-serialized data is
only readable using Python.hickleprovides a way to serialize your custom classes using the HDF5 format, via
defining a few hooks for loading/dumping. However, it can be a little tricky to
implement these hooks, as they are quite general.hickleableprovides a “default implementation” of these hooks that should satisfy
the requirements of most custom classes, and can be applied as a simple decorator.
This makes it a one-liner to transform your class into a well-supporteddata format.Check out the docs atReadTheDocs.InstallationSimplypip install hickleable. Conda-installable dependencies includeh5py.UsageSimply:from hickleable import hickleable

@hickleable()
class MyClass:
   def __init__(self, a=1, b='foo', c={'a': 'dict'}):
       self.a = a
       self.b = b
       self.c = cNow,MyClasscan be hickled without any pickling:import hickle

my_obj = MyClass()
hickle.dump(my_obj, 'temporary_file.h5')  # Note: no warnings about having to pickle
new_obj = hickle.load('temporary_file.h5')One super cool thing is that@cached_propertyattributes are respected, and
dataclasses are also supported:from dataclass import dataclass
from functools import cached_property

@hickleable()
@dataclass
class CachedClass:
    foo: str
    bar: int

    @cached_property
    def foobar(self) -> str:
        print("Obtaining foobar...")
        return foo*bar

c = CachedClass('baz', 50000)

foobar = c.foobar  # prints "Obtaining foobar..."
foobar = c.foobar  # prints nothing, since it's returning cached value.

hickle.dump(c, 'foobar.h5')
d = hickle.load('foobar.h5')

d_foobar = d.foobar  # prints nothing! The value is cached in the hickle file.One thing to note is that the cached properties are only saved in the hickle file if
they have already been evaluated. To forcehickleto write out all cached
properties, use theevaluate_cached_properties=Trueparameter in the call tohickleable().Customizing Dumping/LoadingWhilehickleablewill automatically render most classes hickle-able, there are bound
to be corner cases in which constituent attributes are not themselves hickleable, or
other concerns that you will want to customize. While all of this is of course
totally customizable by using the dumping/loading hooks fromhickle, thehickleabledecorator also respects the magic methods__gethstate__and__sethstate__, which act exactly like__getstate__and__setstate__do for
pickling. In fact, if the latter exist and the former don’t, the latter will be used to
serialize the object inhickle. For instance, let’s say you have a class that keeps
track of the number of times it is called in its lifecycle:@hickleable()
class Counter:
    def __init__(self, a):
        self.a = a
        self._counts = 0

    def __call__(self, b):
        self._counts += 1
        self.a *= bIf we make an instance and call it a few times, the_countsattribute is larger than
zero. If we save the object to a hickle file and load it back up somewhere else, it willstartwith_counts > 0. We can avoid this as follows:def ignore_counts(self, state: dict):
    state['_counts'] = 0
    self.__dict__.update(state)

Counter.__setstate__ = ignore_countsWe could also have removed_countsentirely from the hickle file:def remove_counts(self) -> dict:
    return {k: v for k, v in self.__dict__.items() if k != '_counts'}

Counter.__gethstate__ = remove_countsNote that since we setignore_countsto be the__setstate__method, it will be
respected both forhickleandpickle. We setremove_countsas the__gethstate__method, which means it will only be respected forhickle.