Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_wildcarddict(self):
wd = WildcardDict()
wd[('authors', '*')] = True
self.assertRaises(KeyError, wd.__getitem__, ('authors',))
self.assertTrue(wd[('authors', 1)])
self.assertTrue(wd[('authors', 1, 'name')])
self.assertTrue(wd[('authors', 1, 'affiliation')])
del wd[('authors', '*')]
wd[('authors', '+')] = True
self.assertRaises(KeyError, wd.__getitem__, ('authors',))
self.assertTrue(wd[('authors', 1)])
self.assertRaises(KeyError, wd.__getitem__, ('authors', 1, 'name'))
self.assertRaises(KeyError, wd.__getitem__, ('authors', 1,
self.assertRaises(KeyError, wd.__getitem__, ('authors',))
self.assertTrue(wd[('authors', 1)])
self.assertRaises(KeyError, wd.__getitem__, ('authors', 1, 'name'))
self.assertRaises(KeyError, wd.__getitem__, ('authors', 1,
'affiliation'))
del wd[('authors', '+')]
wd[('foo', 'bar')] = True
self.assertRaises(KeyError, wd.__getitem__, ('foo',))
self.assertTrue(wd[('foo', 'bar')])
self.assertRaises(KeyError, wd.__getitem__, ('foo', 'bar', 'banana'))
# query_path part
wd = WildcardDict()
wd[('authors', '*')] = True
wd[('apple', '+')] = True
wd[('foo', 'bar')] = True
self.assertRaises(KeyError, wd.query_path, ('utz',))
self.assertRaises(KeyError, wd.query_path, ('foo',))
self.assertRaises(KeyError, wd.query_path, ('bar',))
self.assertRaises(KeyError, wd.query_path, ('apple', 'banana',
'mango'))
self.assertEqual(('authors', '*'), wd.query_path(('authors', 1)))
self.assertEqual(('authors', '*'), wd.query_path(('authors', 1, 1)))
self.assertEqual(('authors', '*'), wd.query_path(('authors', 1, 1, 1)))
self.assertEqual(('apple', '+'), wd.query_path(('apple', 'banana')))
self.assertEqual(('apple', '+'), wd.query_path(('apple', 'mango')))
self.assertEqual(('foo', 'bar'), wd.query_path(('foo', 'bar')))
>>> w[ ('foo', 'bar') ]
'* card'
>>> w[ ('foo', 'bar', 'baz') ]
'* card'
>>> w[ ('banana', 'apple') ]
'+ card'
>>> w[ ('banana', 'apple', 'mango') ]
Traceback (most recent call last):
...
KeyError
"""
try:
return super(WildcardDict, self).__getitem__(key)
except KeyError:
if key[:-1] in self.plus_keys:
return super(WildcardDict, self).__getitem__(key[:-1]+('+',))
for _key in [key[:-i] for i in range(1, len(key)+1)]:
if _key in self.star_keys:
return super(WildcardDict, self).__getitem__(_key+('*',))
raise KeyError