[addresses] Generalizing the functions used for address configs so they can be reused for per-country OSM configs, etc.
This commit is contained in:
0
scripts/geodata/configs/__init__.py
Normal file
0
scripts/geodata/configs/__init__.py
Normal file
31
scripts/geodata/configs/utils.py
Normal file
31
scripts/geodata/configs/utils.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import six
|
||||||
|
from collections import Mapping
|
||||||
|
|
||||||
|
|
||||||
|
def recursive_merge(a, b):
|
||||||
|
for k, v in six.iteritems(b):
|
||||||
|
if isinstance(v, Mapping):
|
||||||
|
existing = a.get(k, v)
|
||||||
|
merged = recursive_merge(existing, v)
|
||||||
|
a[k] = merged
|
||||||
|
else:
|
||||||
|
a[k] = b[k]
|
||||||
|
return a
|
||||||
|
|
||||||
|
|
||||||
|
class DoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def nested_get(obj, keys):
|
||||||
|
if len(keys) == 0:
|
||||||
|
return obj
|
||||||
|
try:
|
||||||
|
for key in keys[:-1]:
|
||||||
|
obj = obj.get(key, {})
|
||||||
|
if not hasattr(obj, 'items'):
|
||||||
|
return DoesNotExist
|
||||||
|
key = keys[-1]
|
||||||
|
return obj.get(key, DoesNotExist)
|
||||||
|
except AttributeError:
|
||||||
|
return DoesNotExist
|
||||||
Reference in New Issue
Block a user