Storing dictionary locations as a string and using eval - alternatives?
Victor Hooi
victorhooi at gmail.com
Wed Aug 19 23:59:01 EDT 2015
Hi,
I have a textfile with a bunch of JSON objects, one per line.
I'm looking at parsing each of these, and extract some metrics from each line.
I have a dict called "metrics_to_extract", containing the metrics I'm looking at extracting. In this, I store a name used to identify the metric, along with the location in the parsed JSON object.
Below is my code:
>>>>>>>>>>>>>>>>>>>>>>>
metrics_to_extract = {
'current_connections': "server_status_json['connections']['current']",
'resident_memory': "server_status_json['mem']['resident']"
}
def add_point(name, value, timestamp, tags):
return {
"measurement": name,
"tags": tags,
# "time": timestamp.isoformat(),
"time": timestamp,
"fields": {
"value": float(value)
}
}
with open(input_file, 'r') as f:
json_points = []
for line in f:
if line.startswith("{"):
server_status_json = json.loads(line)
# pp.pprint(server_status_json)
# import ipdb; ipdb.set_trace()
timestamp = server_status_json['localTime']
tags = {
'project': project,
'hostname': server_status_json['host'],
'version': server_status_json['version'],
'storage_engine': server_status_json['storageEngine']['name']
}
for key, value in metrics_to_extract.items():
json_points.append(add_point(key, eval(value), timestamp, tags))
# client.write_points(json_points)
else:
print("non matching line")
>>>>>>>>>>>>>>>>>>>>>>>
My question is - I'm using "eval" in the above, with the nested location (e.g. "server_status_json['mem']['resident']") stored as a string.
I get the feeling this isn't particularly idiomatic or a great way of doing it - and would be keen to hear alternative suggestions?
Thanks,
Victor
More information about the Python-list
mailing list