rmd: (wtfdog)
[personal profile] rmd
So, 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:

[[{'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'}]]


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 a cubit JSON?)" Next on my agenda, figuring out wtf this JSON thing is all about.

Date: 2015-04-16 01:51 am (UTC)
From: [identity profile] johnromkey.livejournal.com
That looks like JSON to me... It's replaced XML (thank goodness) as the common way of representing data when talking to web services. Python should have a library for
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.

Date: 2015-04-16 02:41 am (UTC)
From: [identity profile] rmd.livejournal.com
Thanks!

"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."

Date: 2015-04-16 01:51 am (UTC)
ceo: (code)
From: [personal profile] ceo
I don't know Python, but that looks a lot like JSON.

Date: 2015-04-16 02:41 am (UTC)
From: [identity profile] rmd.livejournal.com
Consensus matches your theory! Thanks.

Date: 2015-04-16 02:36 am (UTC)
From: [identity profile] i-leonardo.livejournal.com
JSON. import json; x = json.loads(...)

Date: 2015-04-16 02:41 am (UTC)
From: [identity profile] rmd.livejournal.com
Huh. Okay! Thanks!

Date: 2015-04-16 10:45 am (UTC)
From: [identity profile] chhotii.livejournal.com
How do you know it's a string? Are you SURE it's a string? Did you do this:

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.

Date: 2015-04-16 02:06 pm (UTC)
From: [identity profile] rmd.livejournal.com
Well, the underlying API iControl says that it returns a string. I'm not sure if the intervening python libraries do something funky with it.

I'll try this and see.

Thanks!

Date: 2015-04-16 03:23 pm (UTC)
irilyth: (Default)
From: [personal profile] irilyth
JSON is pretty fun and easy; see https://docs.python.org/2/library/json.html for more. A thing I had a hard time keeping straight for a while: "Encoding" means "turn a Python object into JSON-formatted text",and is "dump" because you're dumping objects into text; and "decoding" means "turn JSON-formatted text into a Python object", and is "load" because you're loading text into a Python object.

The two things I hate the most about it are: No trailing commas; no comments. For more human-readable fun (IMHO), try YAML instead.

Date: 2015-04-16 10:13 pm (UTC)
From: [identity profile] dfjdejulio.livejournal.com
Screw it all, let's go back to hand-encoding ASN.1 into BER. That's neither human-readable *nor* efficient!

Profile

rmd: (Default)
rmd

June 2025

S M T W T F S
1234567
89 1011121314
15161718192021
22232425262728
2930     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 19th, 2026 09:36 pm
Powered by Dreamwidth Studios