Python safe voice recorder, the right way

Overview TL; DR

I wrote a navigateDict function that makes dict safe navigation, similar to dict.get() but nested. It replaces the code, for example

 if 1 in data and 'i' in data[1] and 'a' in data[1]['i']: print data[1]['i']['a'] else: print "Not found" 

with roughly equivalent

 found = navigateDict(data, 1, 'i', 'a') if found is not None: print found else: print "Not found" 
  • Anything like this part of the standard library already?
  • Is there a more idiomatic way to do the same?
    • Any answer that requires entering any key of the path component more than once probably does not respond.

Additional Information

The implementation is as follows:

 # Allow fallback value other than None def navigateDictEx(d, keys, fallback=None): for key in keys: if key in d: d = d[key] else: return fallback return d def navigateDict(d, *keys): return navigateDictEx(d, keys) 

See summary, for example, use.

Pythonic or not, this feature reduces repetition in a place where redundancy is a bad idea. For example, when changing one component of a path in an example, up to three different values ​​are required, which must be changed as one in the original example, but only one in the modified example. Given my usual tendency to make mistakes, this is a big win.

Ultimately, I ask: is there something in the standard library that does this, or do I need to find a place for it in my project library?

If hits are expected to dominate misses

brionius correctly indicates that the KeyError trap will work:

 try: print data[1]['i']['a'] except KeyError: print "Not found" 

It may be the way I go; it is quite short and cuts the repetition. However, it reflects the assumption that there will be more hits than misses. If there is a better way to accept the opposite, I would also like to know.

+4
source share
3 answers

One way to do this:

 try: print data[1]['i']['a'] except KeyError: print "Not found!" 

This is in keeping with the spirit of the duck set. It may or may not be as fast as I think exception handling carries a certain amount of overhead, but it is certainly "safe."

+5
source

a decision like this is cool

https://twitter.com/raymondh/status/343823801278140417

 >>> from collections import defaultdict >>> infinite_defaultdict = lambda: defaultdict(infinite_defaultdict) >>> d = infinite_defaultdict() >>> d['x']['y']['z'] = 10 >>> if d['x']['y']['z']: print d['x']['y']['z'] #better reflects that misses are common 
+3
source

Years were late for the game, but for those who stumbled upon this, it still doesn't seem like a natural, smooth way to safely navigate like Python.

Enter RestResponse :

"RestResponse aims to be a free python object to interact with the RESTful JSON API"

This library includes a NoneProp object that allows you to safely move (and build) JSON data structures.

 >>> import RestResponse >>> data = RestResponse.parse({}) >>> data.property.is_none None >>> bool(data.property.is_none) False >>> isinstance(data.property.is_none, RestResponse.NoneProp) True >>> data.property.is_none = None >>> isinstance(data.property.is_none, RestResponse.NoneProp) False >>> print data.pretty_print() { "property": { "is_none": null } } 
0
source

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


All Articles