ask the parseltongue programmer
Apr. 15th, 2015 09:11 pmSo, I'm mucking around with python - in particular, with F5's "bigsuds" library, which does web transactions with their "iControl" API. I've run into a data structure that is making me remember that I never took a data structures class.
I'm trying to get the list of health monitors for a given pool, using "LocalLB.Pool.get_monitor_instance([pool])". The get_monitor_instance API reference page just says I get a string back. Which, I do, but in python-speak, I get what looks like a dictionary which includes other dictionaries embedded in a list embedded in a list. Like this:
How the heck do I parse this? I'd like to be able to break it down to a dictionary so I can say "print thing["10.10.0.23"]['template_name']" where that would be "http", I guess.
ETA: based on the consensus, evidently by parsing it as JSON. ("Riiiiiiiiiight. What's acubit JSON?)" Next on my agenda, figuring out wtf this JSON thing is all about.
I'm trying to get the list of health monitors for a given pool, using "LocalLB.Pool.get_monitor_instance([pool])". The get_monitor_instance API reference page just says I get a string back. Which, I do, but in python-speak, I get what looks like a dictionary which includes other dictionaries embedded in a list embedded in a list. Like this:
[[{'enabled_state': True, 'instance': {'template_name': 'http', 'instance_definition': {'ipport': {'port': 80, 'address': '10.10.10.23'}, 'address_type': 'ATYPE_EXPLICIT_ADDRESS_EXPLICIT_PORT'}}, 'instance_state': 'INSTANCE_STATE_UP'}, {'enabled_state': True, 'instance': {'template_name': 'http', 'instance_definition': {'ipport': {'port': 80, 'address': '10.10.10.24'}, 'address_type': 'ATYPE_EXPLICIT_ADDRESS_EXPLICIT_PORT'}}, 'instance_state': 'INSTANCE_STATE_UP'}]]
ETA: based on the consensus, evidently by parsing it as JSON. ("Riiiiiiiiiight. What's a
no subject
Date: 2015-04-16 01:51 am (UTC)Parsing and generating JSON.
I'm particularly tickled that the Internet world has evlved from representing data in tight packed formats like ASN.1 to the other extreme, XML, and now mode table settled in with JSON, which is much more terse and readable... And is JavaScript.
no subject
Date: 2015-04-16 02:41 am (UTC)"BANDWIDTH IS PRECIOUS! WE MUST BE AS INFORMATION-DENSE AS POSSIBLE!"
"We have ALL THE BANDWIDTH! BE VERBOSE!"
"Seriously, gang? Fuck that. Let's be reasonable, here."
no subject
Date: 2015-04-16 01:51 am (UTC)no subject
Date: 2015-04-16 02:41 am (UTC)no subject
Date: 2015-04-16 02:36 am (UTC)no subject
Date: 2015-04-16 02:41 am (UTC)no subject
Date: 2015-04-16 10:45 am (UTC)isinstance(thatThing, str)
I don't know why a Python API would convert a data structure into a string before handing it back to you. That's perverse. If it is indeed a string, though, then within your Python code, you can convert it back into a data structure using eval():
monitors = eval(thatThing)
Then this code will populate a dictionary indexed by IP address, which seems to be what you want:
for m in monitors:
for inst in m:
monitorsByAddress[inst['instance']['instance_definition']['ipport']['address']] = inst
(Sorry if the syntactically meaningful white space doesn't make it into the posted comment. Obviously add a tab before the 2nd line of code and 2 tabs before the 3rd line of code.)
I don't see any need to go mucking around with JSON. You can do all this within the Python code.
no subject
Date: 2015-04-16 02:06 pm (UTC)I'll try this and see.
Thanks!
no subject
Date: 2015-04-16 03:23 pm (UTC)The two things I hate the most about it are: No trailing commas; no comments. For more human-readable fun (IMHO), try YAML instead.
no subject
Date: 2015-04-16 10:13 pm (UTC)