convert output to list(and nested dictionary)

Chris Angelico rosuav at
Wed Jul 22 02:25:47 CEST 2015

On Wed, Jul 22, 2015 at 7:12 AM, max scalf <oracle.blog3 at> wrote:
> SecurityGroup:default sg-e1304484 inbound: IPPermissions:tcp(80-80)  source: []
> Here is the output i am looking for....
> rule1 = [{
>     'cidr': '',
>     'proto': 'tcp',
>     'port': 80
> },{

So if I'm understanding you correctly, one line (from your previous
iteration) will become one dictionary, right?

In that case, start by figuring out how to parse that line and produce
that output. In the example I've quoted, it should be pretty easy (the
'rule' seems to have your proto and port, and your 'rule.grants' has
the cidr), but looking over your data, I see some snags. Firstly, some
of your port numbers are None-None or -1--1, and I'm not sure what
those mean. (My guess: None-None occurs only when proto is -1, and
-1--1 happens when proto is icmp, which doesn't use port numbers. But
you'd have to investigate that.) Also, your cidr might be a list,
rather than a single item. What do you do if it's empty, or if it has
multiple? Third, your ports are very obviously a range (1024-65535
comes up), so you'll need to handle that. And finally, some of those
rules look like things I would permit, and some look like things I'd
reject. You may need to look into the rule definitions to see what you
can find. But the first thing to try would be to get hold of one of
those rule objects and start doing some introspection; dir(rule) and
help(rule) would be where I'd start.

Good luck! This shouldn't be too hard; the data seems to be all there already.


More information about the Python-list mailing list