Skip to content

Commit

Permalink
add pickle ability
Browse files Browse the repository at this point in the history
  • Loading branch information
pohmelie committed Jan 14, 2018
1 parent debe417 commit 6d7762f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 9 additions & 4 deletions skin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


__all__ = ("Skin",)
__version__ = "0.0.5"
__version__ = "0.0.6"
version = tuple(map(int, __version__.split(".")))


Expand All @@ -14,7 +14,6 @@ class SkinValueError(ValueError):
DEFAULT_VALUE = object()
ANY = object()
FORBIDDEN = (str, bytes, bytearray, memoryview, range)
TRANSPARENT_ATTRIBUTES = {"value", "__class__", "__deepcopy__"}


def _wrapper_or_value(self, value=DEFAULT_VALUE, *, parent=None, parent_name=None):
Expand All @@ -27,6 +26,10 @@ def _wrapper_or_value(self, value=DEFAULT_VALUE, *, parent=None, parent_name=Non
return value


def _special_method(name):
return name.startswith("__") and name.endswith("__") or name == "value"


class Skin:

def __init__(self, value=DEFAULT_VALUE, *, allowed=ANY, forbidden=FORBIDDEN, _parent=None, _parent_name=None):
Expand All @@ -49,11 +52,13 @@ def __init__(self, value=DEFAULT_VALUE, *, allowed=ANY, forbidden=FORBIDDEN, _pa
setter("parent_name", _parent_name)

def __getattribute__(self, name):
if name not in TRANSPARENT_ATTRIBUTES:
raise AttributeError
if _special_method(name):
return super().__getattribute__(name)
return super().__getattribute__(name)

def __getattr__(self, name):
if _special_method(name):
raise AttributeError
if hasattr(self.value, name):
return getattr(self.value, name)
return self[name]
Expand Down
6 changes: 6 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import collections
import copy
import pickle

import pytest

Expand Down Expand Up @@ -188,3 +189,8 @@ def test_config_inheritance():
s1 = Skin(forbidden=(set,))
s2 = s1.foo.bar.baz
assert super(Skin, s1).__getattribute__("forbidden") is super(Skin, s2).__getattribute__("forbidden")


def test_pickle(s):
x = pickle.loads(pickle.dumps(s))
assert x.value == s.value

0 comments on commit 6d7762f

Please sign in to comment.