Skip to content

Commit 36515e7

Browse files
committed
Clean up in reflex
1 parent b04d0e8 commit 36515e7

File tree

4 files changed

+15
-28
lines changed

4 files changed

+15
-28
lines changed

requirements_dev.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ invoke
66
twine
77
wheel
88
zest.releaser
9-

requirements_test.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ codecov>=2.0.0
66
gitpython
77
invoke
88
tox-venv
9+
10+
pytest
11+
pytest-django

sockpuppet/reflex.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,49 +27,37 @@ class Context(UserDict):
2727
2828
> context.my_data = 'hello'
2929
> context.my_data # 'hello'
30+
31+
The following property will contain all data of the dictionary
32+
> context.data
3033
"""
3134

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

37-
def __init__(self, *args, **kwargs):
38-
super().__init__(*args, **kwargs)
39-
self._attr_data = {}
40-
4140
def __getitem__(self, key):
4241
data = self.__dict__
43-
if (
44-
data["data"].get(key, KeyError) is KeyError
45-
and data["_attr_data"].get(key, KeyError) is KeyError
46-
):
42+
if data["data"].get(key, KeyError) is KeyError:
4743
raise KeyError(key)
48-
return self.data.get(key) or self._attr_data.get(key)
49-
50-
def __setitem__(self, key, item):
51-
if not self.__dict__.get("data"):
52-
self.__dict__["data"] = {}
53-
self.__dict__["data"][key] = item
44+
return self.data.get(key)
5445

5546
def __getattr__(self, key):
5647
if not self.__dict__.get("data"):
5748
self.__dict__["data"] = {}
58-
if not self.__dict__.get("_attr_data"):
59-
self.__dict__["_attr_data"] = {}
6049

61-
if (
62-
self.__dict__["data"].get(key, KeyError) is KeyError
63-
and self.__dict__["_attr_data"].get(key, KeyError) is KeyError
64-
):
50+
if self.__dict__["data"].get(key, KeyError) is KeyError:
6551
raise AttributeError(key)
66-
result = self.data.get(key) or self._attr_data.get(key)
52+
result = self.data.get(key)
6753
return result
6854

6955
def __setattr__(self, key, value):
70-
if not self.__dict__.get("_attr_data"):
71-
self.__dict__["_attr_data"] = {}
72-
self.__dict__["_attr_data"][key] = value
56+
if not self.__dict__.get("data"):
57+
self.__dict__["data"] = {}
58+
if key == "data" and value == {}:
59+
return
60+
self.__dict__["data"][key] = value
7361

7462

7563
class Reflex:

tests/test_reflex.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ def test_context_api_works_correctly(self):
2020
self.assertEqual(context.hello, 'hello')
2121
self.assertEqual(context['hello'], 'hello')
2222

23-
self.assertEqual(context.data.get('hello'), None)
24-
self.assertEqual(context._attr_data.get('hello'), 'hello')
25-
2623
with self.assertRaises(AttributeError):
2724
context.not_an_attribute
2825

0 commit comments

Comments
 (0)