diff --git a/README.md b/README.md index 55c04d6..2b9957c 100644 --- a/README.md +++ b/README.md @@ -175,8 +175,9 @@ with the documentation for each option: ### `value_type` -The value_type argument can be used to give a type or sequence of types that the option -is allowed to have. Trying to set an option with a non-allowed type raises a +The `value_type` argument can be used to give a type or sequence of types that +the option is allowed to have. Trying to set an option with a non-allowed type +raises a `ValueError`: ```python >>> d2 = D(d=-2) @@ -387,6 +388,12 @@ or all values including defaults, by passing `True` to the `with_defaults` argum ``` +### Pickling (with dill) + +`Options` objects can be pickled using `dill`. This is tested. Pickling of +`MutableOptions` objects is not currently supported. + + Examples -------- diff --git a/optionsfactory/optionsfactory.py b/optionsfactory/optionsfactory.py index acb92b8..98df319 100644 --- a/optionsfactory/optionsfactory.py +++ b/optionsfactory/optionsfactory.py @@ -546,6 +546,14 @@ def __setattr__(self, key, value): raise TypeError("Options does not allow assigning to attributes") super(OptionsFactory.Options, self).__setattr__(key, value) + def __getstate__(self): + # Need to define this so that pickling with dill works + return vars(self) + + def __setstate__(self, state): + # Need to define this so that pickling with dill works + vars(self).update(state) + def is_default(self, key): try: return self.__is_default[key]