Skip to content

Commit

Permalink
Remove SemiStrictDict to improve perfs
Browse files Browse the repository at this point in the history
  • Loading branch information
touilleMan committed Aug 24, 2017
1 parent 4f59c7f commit bce8595
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 83 deletions.
2 changes: 1 addition & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Changelog

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

Changes in 0.14.0
=================
Expand Down
39 changes: 0 additions & 39 deletions mongoengine/base/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,42 +445,3 @@ def __repr__(self):

cls._classes[allowed_keys] = SpecificStrictDict
return cls._classes[allowed_keys]


class SemiStrictDict(StrictDict):
__slots__ = ('_extras', )
_classes = {}

def __getattr__(self, attr):
try:
super(SemiStrictDict, self).__getattr__(attr)
except AttributeError:
try:
return self.__getattribute__('_extras')[attr]
except KeyError as e:
raise AttributeError(e)

def __setattr__(self, attr, value):
try:
super(SemiStrictDict, self).__setattr__(attr, value)
except AttributeError:
try:
self._extras[attr] = value
except AttributeError:
self._extras = {attr: value}

def __delattr__(self, attr):
try:
super(SemiStrictDict, self).__delattr__(attr)
except AttributeError:
try:
del self._extras[attr]
except KeyError as e:
raise AttributeError(e)

def __iter__(self):
try:
extras_iter = iter(self.__getattribute__('_extras'))
except AttributeError:
extras_iter = ()
return itertools.chain(super(SemiStrictDict, self).__iter__(), extras_iter)
5 changes: 2 additions & 3 deletions mongoengine/base/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from mongoengine.base.common import get_document
from mongoengine.base.datastructures import (BaseDict, BaseList,
EmbeddedDocumentList,
SemiStrictDict, StrictDict)
StrictDict)
from mongoengine.base.fields import ComplexBaseField
from mongoengine.common import _import_class
from mongoengine.errors import (FieldDoesNotExist, InvalidDocumentError,
Expand Down Expand Up @@ -79,8 +79,7 @@ def __init__(self, *args, **values):
if self.STRICT and not self._dynamic:
self._data = StrictDict.create(allowed_keys=self._fields_ordered)()
else:
self._data = SemiStrictDict.create(
allowed_keys=self._fields_ordered)()
self._data = {}

self._dynamic_fields = SON()

Expand Down
41 changes: 1 addition & 40 deletions tests/test_datastructures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from mongoengine.base.datastructures import StrictDict, SemiStrictDict
from mongoengine.base.datastructures import StrictDict


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


class TestSemiSrictDict(TestStrictDict):
def strict_dict_class(self, *args, **kwargs):
return SemiStrictDict.create(*args, **kwargs)

def test_init_fails_on_nonexisting_attrs(self):
# disable irrelevant test
pass

def test_setattr_raises_on_nonexisting_attr(self):
# disable irrelevant test
pass

def test_setattr_getattr_nonexisting_attr_succeeds(self):
d = self.dtype()
d.x = 1
self.assertEqual(d.x, 1)

def test_init_succeeds_with_nonexisting_attrs(self):
d = self.dtype(a=1, b=1, c=1, x=2)
self.assertEqual((d.a, d.b, d.c, d.x), (1, 1, 1, 2))

def test_iter_with_nonexisting_attrs(self):
d = self.dtype(a=1, b=1, c=1, x=2)
self.assertEqual(list(d), ['a', 'b', 'c', 'x'])

def test_iteritems_with_nonexisting_attrs(self):
d = self.dtype(a=1, b=1, c=1, x=2)
self.assertEqual(list(d.iteritems()), [('a', 1), ('b', 1), ('c', 1), ('x', 2)])

def tets_cmp_with_strict_dicts(self):
d = self.dtype(a=1, b=1, c=1)
dd = StrictDict.create(("a", "b", "c"))(a=1, b=1, c=1)
self.assertEqual(d, dd)

def test_cmp_with_strict_dict_with_nonexisting_attrs(self):
d = self.dtype(a=1, b=1, c=1, x=2)
dd = StrictDict.create(("a", "b", "c", "x"))(a=1, b=1, c=1, x=2)
self.assertEqual(d, dd)

if __name__ == '__main__':
unittest.main()

0 comments on commit bce8595

Please sign in to comment.