Skip to content

Commit bce8595

Browse files
committed
Remove SemiStrictDict to improve perfs
1 parent 4f59c7f commit bce8595

File tree

4 files changed

+4
-83
lines changed

4 files changed

+4
-83
lines changed

docs/changelog.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Changelog
44

55
Development
66
===========
7-
- (Fill this out as you fix issues and develop your features).
7+
- Improve performances by removing SemiStrictDict
88

99
Changes in 0.14.0
1010
=================

mongoengine/base/datastructures.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -445,42 +445,3 @@ def __repr__(self):
445445

446446
cls._classes[allowed_keys] = SpecificStrictDict
447447
return cls._classes[allowed_keys]
448-
449-
450-
class SemiStrictDict(StrictDict):
451-
__slots__ = ('_extras', )
452-
_classes = {}
453-
454-
def __getattr__(self, attr):
455-
try:
456-
super(SemiStrictDict, self).__getattr__(attr)
457-
except AttributeError:
458-
try:
459-
return self.__getattribute__('_extras')[attr]
460-
except KeyError as e:
461-
raise AttributeError(e)
462-
463-
def __setattr__(self, attr, value):
464-
try:
465-
super(SemiStrictDict, self).__setattr__(attr, value)
466-
except AttributeError:
467-
try:
468-
self._extras[attr] = value
469-
except AttributeError:
470-
self._extras = {attr: value}
471-
472-
def __delattr__(self, attr):
473-
try:
474-
super(SemiStrictDict, self).__delattr__(attr)
475-
except AttributeError:
476-
try:
477-
del self._extras[attr]
478-
except KeyError as e:
479-
raise AttributeError(e)
480-
481-
def __iter__(self):
482-
try:
483-
extras_iter = iter(self.__getattribute__('_extras'))
484-
except AttributeError:
485-
extras_iter = ()
486-
return itertools.chain(super(SemiStrictDict, self).__iter__(), extras_iter)

mongoengine/base/document.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from mongoengine.base.common import get_document
1414
from mongoengine.base.datastructures import (BaseDict, BaseList,
1515
EmbeddedDocumentList,
16-
SemiStrictDict, StrictDict)
16+
StrictDict)
1717
from mongoengine.base.fields import ComplexBaseField
1818
from mongoengine.common import _import_class
1919
from mongoengine.errors import (FieldDoesNotExist, InvalidDocumentError,
@@ -79,8 +79,7 @@ def __init__(self, *args, **values):
7979
if self.STRICT and not self._dynamic:
8080
self._data = StrictDict.create(allowed_keys=self._fields_ordered)()
8181
else:
82-
self._data = SemiStrictDict.create(
83-
allowed_keys=self._fields_ordered)()
82+
self._data = {}
8483

8584
self._dynamic_fields = SON()
8685

tests/test_datastructures.py

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from mongoengine.base.datastructures import StrictDict, SemiStrictDict
3+
from mongoengine.base.datastructures import StrictDict
44

55

66
class TestStrictDict(unittest.TestCase):
@@ -76,44 +76,5 @@ def test_mappings_protocol(self):
7676
assert dict(**d) == {'a': 1, 'b': 2}
7777

7878

79-
class TestSemiSrictDict(TestStrictDict):
80-
def strict_dict_class(self, *args, **kwargs):
81-
return SemiStrictDict.create(*args, **kwargs)
82-
83-
def test_init_fails_on_nonexisting_attrs(self):
84-
# disable irrelevant test
85-
pass
86-
87-
def test_setattr_raises_on_nonexisting_attr(self):
88-
# disable irrelevant test
89-
pass
90-
91-
def test_setattr_getattr_nonexisting_attr_succeeds(self):
92-
d = self.dtype()
93-
d.x = 1
94-
self.assertEqual(d.x, 1)
95-
96-
def test_init_succeeds_with_nonexisting_attrs(self):
97-
d = self.dtype(a=1, b=1, c=1, x=2)
98-
self.assertEqual((d.a, d.b, d.c, d.x), (1, 1, 1, 2))
99-
100-
def test_iter_with_nonexisting_attrs(self):
101-
d = self.dtype(a=1, b=1, c=1, x=2)
102-
self.assertEqual(list(d), ['a', 'b', 'c', 'x'])
103-
104-
def test_iteritems_with_nonexisting_attrs(self):
105-
d = self.dtype(a=1, b=1, c=1, x=2)
106-
self.assertEqual(list(d.iteritems()), [('a', 1), ('b', 1), ('c', 1), ('x', 2)])
107-
108-
def tets_cmp_with_strict_dicts(self):
109-
d = self.dtype(a=1, b=1, c=1)
110-
dd = StrictDict.create(("a", "b", "c"))(a=1, b=1, c=1)
111-
self.assertEqual(d, dd)
112-
113-
def test_cmp_with_strict_dict_with_nonexisting_attrs(self):
114-
d = self.dtype(a=1, b=1, c=1, x=2)
115-
dd = StrictDict.create(("a", "b", "c", "x"))(a=1, b=1, c=1, x=2)
116-
self.assertEqual(d, dd)
117-
11879
if __name__ == '__main__':
11980
unittest.main()

0 commit comments

Comments
 (0)