Skip to content

Commit

Permalink
Clean up in reflex
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-s committed Jul 31, 2021
1 parent b04d0e8 commit 36515e7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 28 deletions.
1 change: 0 additions & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ invoke
twine
wheel
zest.releaser

3 changes: 3 additions & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ codecov>=2.0.0
gitpython
invoke
tox-venv

pytest
pytest-django
36 changes: 12 additions & 24 deletions sockpuppet/reflex.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,49 +27,37 @@ class Context(UserDict):
> context.my_data = 'hello'
> context.my_data # 'hello'
The following property will contain all data of the dictionary
> context.data
"""

# NOTE for maintainer
# A dictionary that keeps track of whether it's been used as dictionary
# or if values has been set with dot notation. We expect things to be set
# in dot notation so a warning is issued until next major version (1.0)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._attr_data = {}

def __getitem__(self, key):
data = self.__dict__
if (
data["data"].get(key, KeyError) is KeyError
and data["_attr_data"].get(key, KeyError) is KeyError
):
if data["data"].get(key, KeyError) is KeyError:
raise KeyError(key)
return self.data.get(key) or self._attr_data.get(key)

def __setitem__(self, key, item):
if not self.__dict__.get("data"):
self.__dict__["data"] = {}
self.__dict__["data"][key] = item
return self.data.get(key)

def __getattr__(self, key):
if not self.__dict__.get("data"):
self.__dict__["data"] = {}
if not self.__dict__.get("_attr_data"):
self.__dict__["_attr_data"] = {}

if (
self.__dict__["data"].get(key, KeyError) is KeyError
and self.__dict__["_attr_data"].get(key, KeyError) is KeyError
):
if self.__dict__["data"].get(key, KeyError) is KeyError:
raise AttributeError(key)
result = self.data.get(key) or self._attr_data.get(key)
result = self.data.get(key)
return result

def __setattr__(self, key, value):
if not self.__dict__.get("_attr_data"):
self.__dict__["_attr_data"] = {}
self.__dict__["_attr_data"][key] = value
if not self.__dict__.get("data"):
self.__dict__["data"] = {}
if key == "data" and value == {}:
return
self.__dict__["data"][key] = value


class Reflex:
Expand Down
3 changes: 0 additions & 3 deletions tests/test_reflex.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ def test_context_api_works_correctly(self):
self.assertEqual(context.hello, 'hello')
self.assertEqual(context['hello'], 'hello')

self.assertEqual(context.data.get('hello'), None)
self.assertEqual(context._attr_data.get('hello'), 'hello')

with self.assertRaises(AttributeError):
context.not_an_attribute

Expand Down

0 comments on commit 36515e7

Please sign in to comment.