Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

Commit

Permalink
Addig nested dict getter (getval()) and testing output.
Browse files Browse the repository at this point in the history
  • Loading branch information
inactivist committed Jan 30, 2013
1 parent d088d58 commit 01eee35
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions streamer/scripts/filter-locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,24 @@ def four_floats(vals):
parser.add_argument('bbox',
metavar='bounding-box',
type=four_floats)

opts = parser.parse_args()

# Get bounding box coordinates (minlon, minlat, maxlon, maxlat)

def getval(d, nested_key, default=None):
""" Simple way to get a deep value from a nested dict. """
keys = nested_key.split('.')
o = d
for k in keys:
o = o.get(k)
if k == keys[-1]:
return o
elif not isinstance(o, dict):
break
return default


def output(opts, line, json_obj):
print line
print "%s: %s" % (json_obj['id_str'], [getval(json_obj, k) for k in ['user.screen_name', 'text']])

for line in sys.stdin:
try:
Expand Down

0 comments on commit 01eee35

Please sign in to comment.