Skip to content

Commit

Permalink
New utlity function lookup.
Browse files Browse the repository at this point in the history
Combines `.`-style and `[]`-style lookups for the cases where we do
not actually know if we're passed a dictionary or an object/namespace.
  • Loading branch information
riccardomurri committed Aug 27, 2017
1 parent 8c8d17c commit a1a9ccd
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion gc3libs/utils.py
Expand Up @@ -990,6 +990,52 @@ def __str__(self):
for record in self._messages]) + '\n'


def lookup(obj, name):
"""
Return attribute or item with the given name in collection `obj`.
:raise LookupError:
If `obj` has no attribute nor item with the given name.
This is meant for cases when different versions of an API may
either return a dictionary (hence, key/`__getitem__`-based lookup)
or an object/namespace (hence, `.`/`getattr`-style lookup) and you
want to handle them in a uniform way.
The following examples demo it::
>>> D = {'a':1, 'b':2}
>>> lookup(D, 'a')
1
>>> lookup(D, 'c')
Traceback (most recent call last):
...
LookupError: Object ... has no attribute nor key named `c`
>>> class X(object):
... a = 1
... b = 2
>>> x = X()
>>> lookup(x, 'a')
1
>>> lookup(x, 'c')
Traceback (most recent call last):
...
LookupError: Object ... has no attribute nor key named `c`
"""
try:
return getattr(obj, name)
except AttributeError:
pass
try:
return obj[name]
except (KeyError, TypeError):
pass
raise LookupError(
'Object {0} has no attribute nor key named `{1}`'
.format(obj, name))


def mkdir(path, mode=0o777):
"""
Like `os.makedirs`, but does not throw an exception if PATH
Expand Down Expand Up @@ -2264,4 +2310,4 @@ def throw(self, *excinfo):
if __name__ == '__main__':
import doctest
doctest.testmod(name='utils',
optionflags=doctest.NORMALIZE_WHITESPACE)
optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS)

0 comments on commit a1a9ccd

Please sign in to comment.