convert output to list(and nested dictionary)

max scalf oracle.blog3 at gmail.com
Wed Jul 22 13:57:21 EDT 2015


Hi Peter,

Could you please explain what i am doing wrong?  I did inspected the
"get_all_security_groups()" object using dir and i do need the get_data
function for this to work...as i have to parse the output...just getting
the rule and grants does not work...as it comes with extra verbiage that i
do NOT need in my dictionary...see below...

>>> for sg in sgs:
    for rule in sg.rules:
        print sg, sg.id, rule, rule.grants


SecurityGroup:wordpress-app-SG sg-99c4befc IPPermissions:-1(None-None)
[sg-e632d982-995635159130]
SecurityGroup:wordpress-app-SG sg-99c4befc IPPermissions:tcp(22-22) [
67.184.225.222/32]
SecurityGroup:wordpress-app-SG sg-99c4befc IPPermissions:tcp(1024-65535) [
10.0.2.10/32]
SecurityGroup:wordpress-app-SG sg-99c4befc IPPermissions:tcp(80-80) [
24.12.30.198/32]
SecurityGroup:wordpress-app-SG sg-99c4befc IPPermissions:udp(138-138) [
10.0.2.10/32]
SecurityGroup:wordpress-app-SG sg-99c4befc IPPermissions:udp(53-53) [
24.12.30.198/32]
SecurityGroup:wordpress-app-SG sg-99c4befc IPPermissions:tcp(30015-30015) [
0.0.0.0/0]
SecurityGroup:wordpress-app-SG sg-99c4befc IPPermissions:icmp(-1--1) [
10.0.2.10/32]
>>>

i was able to create a list(and nested dictionary) using below... i am open
to making this better if there is a way...

And please accept my apology(in case i am not following certain things) as
i am very new to python...

>>> mylist = []
>>>
>>> for sg in sgs:
    for rule in sg.rules:
        st = sg, sg.id, "inbound:", rule, " source:", rule.grants
        # without the below, we get a comman(,)
        s = str(st).replace(","," ")
        #print s
        jt = get_data(s)
        mylist.append(jt)


>>> mylist
[{'cidr': 'sg-e632d982-995635159130', 'port': 'None', 'proto': '1'},
{'cidr': '67.184.225.222/32', 'port': '22', 'proto': 'tcp'}, {'cidr': '
10.0.2.10/32', 'port': '1024', 'proto': 'tcp'}, {'cidr': '24.12.30.198/32',
'port': '80', 'proto': 'tcp'}, {'cidr': '10.0.2.10/32', 'port': '138',
'proto': 'udp'}, {'cidr': '24.12.30.198/32', 'port': '53', 'proto': 'udp'},
{'cidr': '0.0.0.0/0', 'port': '30015', 'proto': 'tcp'}, {'cidr': '
10.0.2.10/32', 'port': '', 'proto': 'icmp'}]
>>>
>>>

On Wed, Jul 22, 2015 at 12:41 PM, Peter Otten <__peter__ at web.de> wrote:

> max scalf wrote:
>
> > I was able to solve the above problem i listed with the
> following...please
> > let me know if that is the correct way of doing this...or i am way off?
> >
> > >>> for sg in sgs:
> >     for rule in sg.rules:
> >         st = sg, sg.id, "inbound:", rule, " source:", rule.grants
> >         s = str(st).replace(","," ")
> >         #print s
> >         get_data(s)
> >
> >
> > {'cidr': 'sg-e632d982-995635159130', 'port': 'None', 'proto': '1'}
> > {'cidr': '67.184.225.222/32', 'port': '22', 'proto': 'tcp'}
> > {'cidr': '10.0.2.10/32', 'port': '1024', 'proto': 'tcp'}
> > {'cidr': '24.12.30.198/32', 'port': '80', 'proto': 'tcp'}
> > {'cidr': '10.0.2.10/32', 'port': '138', 'proto': 'udp'}
> > {'cidr': '24.12.30.198/32', 'port': '53', 'proto': 'udp'}
> > {'cidr': '0.0.0.0/0', 'port': '30015', 'proto': 'tcp'}
> > {'cidr': '10.0.2.10/32', 'port': '', 'proto': 'icmp'}
>
> As Chris hinted -- that's the wrong approach. You should instead look at
> the
> attributes. What does
>
> for sg in sgs:
>     print "attributes of sg", dir(sg)
>     for rule in sg.rules:
>         print "attributes of rule", dir(rule)
>         break
>     break
>
> print? You should be able to determine the names of the interesting stuff
> from that. If not, try again with vars() instead of dir(), or, the horror!,
> see if you can find some documentation.
>
> Then build the dicts from these attributes, e. g.
>
> result = []
> for sg in sgs:
>     for rule in sg.rules:
>         result.append(dict(cidr=sg.foo, port=rule.bar, proto=rule.baz))
> print result
>
> It should be obvious that foo, bar, baz are not the correct attribute
> names,
> they are placeholders to convey the idea.
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150722/d0cb7440/attachment.html>


More information about the Python-list mailing list