Skip to content

Commit 0883e39

Browse files
committed
-
1 parent 7a463ed commit 0883e39

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

misc/IDE files/Wing/python_toolbox_py3.wpr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ debug.launch-configs = (1,
1616
'pypath': ('default',
1717
''),
1818
'pyrunargs': ('project',
19-
''),
20-
'runargs': u'--processes=3',
21-
'rundir': ('custom',
22-
u'${WING:PROJECT_HOME}')})})
19+
u''),
20+
'runargs': u'',
21+
'rundir': ('default',
22+
u'')})})
2323
proj.directory-list = [{'dirloc': loc('../../..'),
2424
'excludes': [u'dist',
2525
u'source_py2',

source_py3/python_toolbox/nifty_collections/bagging.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .lazy_tuple import LazyTuple
1414
from .ordered_dict import OrderedDict
1515
from .frozen_dict_and_frozen_ordered_dict import FrozenDict, FrozenOrderedDict
16-
from .abstract import Ordered
16+
from .abstract import Ordered, DefinitelyUnordered
1717

1818
try: # Load C helper function if available
1919
from _collections import _count_elements
@@ -363,43 +363,43 @@ def __repr__(self):
363363
)
364364

365365

366-
class _OrderedDictDelegator(Ordered, collections.MutableMapping):
366+
class _BaseDictDelegator(collections.MutableMapping):
367367

368368
# Start by filling-out the abstract methods
369369
def __init__(self, dict=None, **kwargs):
370-
self.data = OrderedDict()
370+
self._dict = self._dict_type()
371371
if dict is not None:
372372
self.update(dict)
373373
if len(kwargs):
374374
self.update(kwargs)
375-
def __len__(self): return len(self.data)
375+
def __len__(self): return len(self._dict)
376376
def __getitem__(self, key):
377-
if key in self.data:
378-
return self.data[key]
377+
if key in self._dict:
378+
return self._dict[key]
379379
if hasattr(self.__class__, "__missing__"):
380380
return self.__class__.__missing__(self, key)
381381
raise KeyError(key)
382-
def __setitem__(self, key, item): self.data[key] = item
383-
def __delitem__(self, key): del self.data[key]
382+
def __setitem__(self, key, item): self._dict[key] = item
383+
def __delitem__(self, key): del self._dict[key]
384384
def __iter__(self):
385-
return iter(self.data)
385+
return iter(self._dict)
386386

387387
# Modify __contains__ to work correctly when __missing__ is present
388388
def __contains__(self, key):
389-
return key in self.data
389+
return key in self._dict
390390

391391
# Now, add the methods in dicts but not in MutableMapping
392-
def __repr__(self): return repr(self.data)
392+
def __repr__(self): return repr(self._dict)
393393
def copy(self):
394394
if self.__class__ is _OrderedDictDelegator:
395-
return _OrderedDictDelegator(self.data.copy())
395+
return _OrderedDictDelegator(self._dict.copy())
396396
import copy
397-
data = self.data
397+
data = self._dict
398398
try:
399-
self.data = OrderedDict
399+
self._dict = self._dict_type()
400400
c = copy.copy(self)
401401
finally:
402-
self.data = data
402+
self._dict = data
403403
c.update(self)
404404
return c
405405
@classmethod
@@ -409,9 +409,14 @@ def fromkeys(cls, iterable, value=None):
409409
d[key] = value
410410
return d
411411

412+
class _OrderedDictDelegator(Ordered, _BaseDictDelegator):
413+
_dict_type = OrderedDict
414+
415+
class _DictDelegator(DefinitelyUnordered, _BaseDictDelegator):
416+
_dict_type = dict
412417

413418

414-
class Bag(_MutableBagMixin, collections.UserDict):
419+
class Bag(_MutableBagMixin, _DictDelegator):
415420
'''
416421
A bag that counts items.
417422
@@ -430,7 +435,6 @@ class Bag(_MutableBagMixin, collections.UserDict):
430435
that follow. Only positive integers are allowed as counts.
431436
432437
'''
433-
_dict = property(lambda self: self.data)
434438

435439

436440
class OrderedBag(_OrderedBagMixin, _MutableBagMixin, _OrderedDictDelegator):
@@ -455,8 +459,6 @@ class OrderedBag(_OrderedBagMixin, _MutableBagMixin, _OrderedDictDelegator):
455459
`collections.OrderedDict`.)
456460
457461
'''
458-
_dict = property(lambda self: self.data)
459-
460462

461463

462464
class FrozenBag(_BaseBagMixin, FrozenDict):

source_py3/test_python_toolbox/test_nifty_collections/test_bagging.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ def test_common(self):
6969
assert self.bag_type({'a': 0, 'b': 1,}) == \
7070
self.bag_type({'c': 0, 'b': 1,})
7171

72+
def test_no_visible_dict(self):
73+
bag = self.bag_type('abc')
74+
with cute_testing.RaiseAssertor(AttributeError):
75+
bag.data
76+
with cute_testing.RaiseAssertor(AttributeError):
77+
bag.dict
78+
79+
80+
7281
def test_repr(self):
7382
assert re.match(
7483
self._repr_result_pattern,

0 commit comments

Comments
 (0)