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


Groups > comp.lang.python > #94386

Re: convert output to list(and nested dictionary)

Path csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; "'',": 0.07; '#print': 0.09; 'foo,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'attributes,': 0.16; 'attributes.': 0.16; 'convey': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'attribute': 0.18; '>>>': 0.20; 'not,': 0.22; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'header:X -Complaints-To:1': 0.26; 'chris': 0.26; 'correct': 0.28; "skip:' 10": 0.28; 'print': 0.30; 'that.': 0.30; 'problem': 0.33; 'rule': 0.33; "skip:' 20": 0.34; 'should': 0.36; 'instead': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'doing': 0.38; 'names': 0.38; 'stuff': 0.38; 'wrong': 0.38; 'or,': 0.38; 'does': 0.39; 'build': 0.40; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; 'determine': 0.61; 'obvious': 0.76; "'1'}": 0.84; 'approach.': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: convert output to list(and nested dictionary)
Date Wed, 22 Jul 2015 19:41:37 +0200
Organization None
References <CAKoJ+qB7mLX+tLAqsO=nSocjVLtLn+TCWZ34z8YVFCKKt10ntQ@mail.gmail.com> <CAB_tDZyB+mCg9yWg9oG0-OL-NHVfCSifkMrD0x3k4LPNx-dhNw@mail.gmail.com> <CAKoJ+qCzMetyyqC-2rg0KB2kmFdVeNkVj053OYeXn9meZ1FJxQ@mail.gmail.com> <CAKoJ+qDp5ii8o1L4ZvimMNyr5Bgm-DBnNy9E5XzB4r_oZY4Ngg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd98ea.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.877.1437586910.3674.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1437586910 news.xs4all.nl 2867 [2001:888:2000:d::a6]:55654
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:94386

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web