Is there a cleaner way to combine empty list checks in Python?

I have a rather complex object (deserialized json, so I don't have too much control over it) that I need to check for presence and sort through rather deep elements, so right now I have something like this

if a.get("key") and a["key"][0] and a["key"][0][0] :
    for b in a["key"][0][0] :
        #Do something

which works but is pretty ugly. There seems to be a better way to do this, and what a more elegant solution?

+3
source share
3 answers
try:
  bs = a["key"][0][0]
# Note: the syntax for catching exceptions is different in old versions
# of Python. Use whichever one of these lines is appropriate to your version.
except KeyError, IndexError, TypeError:   # Python 3
except (KeyError, IndexError, TypeError): # Python 2
  bs = []
for b in bs:

And you can pack it into a function if you don't mind longer lines:

def maybe_list(f):
  try:
    return f()
  except KeyError, IndexError, TypeError:
    return []

for b in maybe_list(lambda: a["key"][0][0]):
+14
source

I would write a custom indexer function as follows:

def safe_indexer(obj, *indices):
    for idx in indices:
        if not obj: break

        if hasattr(obj, "get"):
            obj = obj.get(idx)
        else:
            obj = obj[idx]

    return obj

Using:

a = {"key": {0: {0: "foo"} } };
print safe_indexer(a, "key", 0, 0)
print safe_indexer(a, "bad", 0, 0)

Output:

foo
None
+3

:

try:
    for b in a['key'][0][0]:
        # do stuff.
except KeyError, TypeError, IndexError:
    # respond appropriately.
+2

Source: https://habr.com/ru/post/1719846/


All Articles