sync to Atlas repo

This commit is contained in:
Luciano Ramalho
2014-12-06 15:38:22 -02:00
parent 2f495627fb
commit b38e6fc5f2
7 changed files with 72 additions and 43 deletions

View File

@@ -1,8 +1,13 @@
"""StrKeyDict always converts non-string keys to `str`
Test for initializer: keys are converted to `str`.
>>> d = StrKeyDict([(2, 'two'), ('4', 'four')])
>>> sorted(d.keys())
['2', '4']
Tests for item retrieval using `d[key]` notation::
>>> d = StrKeyDict([('2', 'two'), ('4', 'four')])
>>> d['2']
'two'
>>> d[4]
@@ -12,6 +17,15 @@ Tests for item retrieval using `d[key]` notation::
...
KeyError: '1'
Tests for item retrieval using `d.get(key)` notation::
>>> d.get('2')
'two'
>>> d.get(4)
'four'
>>> d.get(1, 'N/A')
'N/A'
Tests for the `in` operator::
>>> 2 in d

View File

@@ -14,6 +14,16 @@ Tests for item retrieval using `d[key]` notation::
...
KeyError: '1'
Tests for item retrieval using `d.get(key)` notation::
>>> d.get('2')
'two'
>>> d.get(4)
'four'
>>> d.get(1, 'N/A')
'N/A'
Tests for the `in` operator::
>>> 2 in d
@@ -24,8 +34,8 @@ Tests for the `in` operator::
# END STRKEYDICT0_TESTS
"""
# BEGIN STRKEYDICT0
# BEGIN STRKEYDICT0
class StrKeyDict0(dict): # <1>
def __missing__(self, key):
@@ -33,7 +43,13 @@ class StrKeyDict0(dict): # <1>
raise KeyError(key)
return self[str(key)] # <3>
def __contains__(self, key):
return key in self.keys() or str(key) in self.keys() # <4>
def get(self, key, default=None):
try:
return self[key] # <4>
except KeyError:
return default # <5>
# END STRKEYDICT0
def __contains__(self, key):
return key in self.keys() or str(key) in self.keys() # <6>
# END STRKEYDICT0