Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94386 > unrolled thread

Re: convert output to list(and nested dictionary)

Started byPeter Otten <__peter__@web.de>
First post2015-07-22 19:41 +0200
Last post2015-07-22 19:41 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: convert output to list(and nested dictionary) Peter Otten <__peter__@web.de> - 2015-07-22 19:41 +0200

#94386 — Re: convert output to list(and nested dictionary)

FromPeter Otten <__peter__@web.de>
Date2015-07-22 19:41 +0200
SubjectRe: convert output to list(and nested dictionary)
Message-ID<mailman.877.1437586910.3674.python-list@python.org>
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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web