Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

parse a string (Cadence Allegro Netlist) to dictionary

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


lelandpeng at gmail

Nov 5, 2009, 12:02 PM

Post #1 of 5 (234 views)
Permalink
parse a string (Cadence Allegro Netlist) to dictionary

Hi,

I always use readline(), strip(), split() and so on to parse a string.
Is there some elegant way to parse the following string into a
dictionary {'50MHZ_CLK_SRC' : 'U122.2, R1395.1'}?

NET_NAME
'50MHZ_CLK_SRC'
'@TEST_LIB.TEST(SCH_1):50MHZ_CLK_SRC':
C_SIGNAL='@test_lib.test(sch_1):\50mhz_clk_src\';
NODE_NAME U122 2
'@TEST_LIB.TEST(SCH_1):PAGE92_I223 [at] INF_LOGIC(CHIPS)':
'CLK2': CDS_PINID='CLK2';
NODE_NAME R1395 1
'@TEST_LIB.TEST(SCH_1):PAGE92_I232 [at] INF_RESISTORS(CHIPS)':
'A': CDS_PINID='A';

Thanks,
Leland
--
http://mail.python.org/mailman/listinfo/python-list


emile at fenx

Nov 5, 2009, 1:05 PM

Post #2 of 5 (229 views)
Permalink
Re: parse a string (Cadence Allegro Netlist) to dictionary [In reply to]

On 11/5/2009 12:02 PM Leland said...
> Hi,
>
> I always use readline(), strip(), split() and so on to parse a string.
> Is there some elegant way to parse the following string into a
> dictionary {'50MHZ_CLK_SRC' : 'U122.2, R1395.1'}?
>
> NET_NAME
> '50MHZ_CLK_SRC'
> '@TEST_LIB.TEST(SCH_1):50MHZ_CLK_SRC':
> C_SIGNAL='@test_lib.test(sch_1):\50mhz_clk_src\';
> NODE_NAME U122 2
> '@TEST_LIB.TEST(SCH_1):PAGE92_I223 [at] INF_LOGIC(CHIPS)':
> 'CLK2': CDS_PINID='CLK2';
> NODE_NAME R1395 1
> '@TEST_LIB.TEST(SCH_1):PAGE92_I232 [at] INF_RESISTORS(CHIPS)':
> 'A': CDS_PINID='A';
>
> Thanks,
> Leland

This does it, but not very elegantly...

mydict = dict( (kk[0].replace("'",""),",".join(kk[1:]))
for kk in [ [ [ jj for jj in ii.split("\n") if jj.strip() ][0]
for ii in txt.split("_NAME")[1:] ] ])

Emile

--
http://mail.python.org/mailman/listinfo/python-list


metal29a at gmail

Nov 5, 2009, 4:23 PM

Post #3 of 5 (228 views)
Permalink
Re: parse a string (Cadence Allegro Netlist) to dictionary [In reply to]

On 11月6日, 上午4时02分, Leland <lelandp...@gmail.com> wrote:
> Hi,
>
> I always use readline(), strip(), split() and so on to parse a string.
> Is there some elegant way to parse the following string into a
> dictionary {'50MHZ_CLK_SRC' : 'U122.2, R1395.1'}?
>
> NET_NAME
> '50MHZ_CLK_SRC'
> '@TEST_LIB.TEST(SCH_1):50MHZ_CLK_SRC':
> C_SIGNAL='@test_lib.test(sch_1):\50mhz_clk_src\';
> NODE_NAME U122 2
> '@TEST_LIB.TEST(SCH_1):PAGE92_I223 [at] INF_LOGIC(CHIPS)':
> 'CLK2': CDS_PINID='CLK2';
> NODE_NAME R1395 1
> '@TEST_LIB.TEST(SCH_1):PAGE92_I232 [at] INF_RESISTORS(CHIPS)':
> 'A': CDS_PINID='A';
>
> Thanks,
> Leland

not very elegantly too.

x = re.findall("_NAME[\n\s']+((?<=').+(?=')|\w+\s+\w+)", s)
"""
print {x[0]: x[1:]}
>>> {'50MHZ_CLK_SRC': ['U122 2', 'R1395 1']}
"""
--
http://mail.python.org/mailman/listinfo/python-list


rhodri at wildebst

Nov 5, 2009, 4:42 PM

Post #4 of 5 (225 views)
Permalink
Re: parse a string (Cadence Allegro Netlist) to dictionary [In reply to]

On Thu, 05 Nov 2009 20:02:53 -0000, Leland <lelandpeng [at] gmail> wrote:

> Hi,
>
> I always use readline(), strip(), split() and so on to parse a string.
> Is there some elegant way to parse the following string into a
> dictionary {'50MHZ_CLK_SRC' : 'U122.2, R1395.1'}?
>
> NET_NAME
> '50MHZ_CLK_SRC'
> '@TEST_LIB.TEST(SCH_1):50MHZ_CLK_SRC':
> C_SIGNAL='@test_lib.test(sch_1):\50mhz_clk_src\';
> NODE_NAME U122 2
> '@TEST_LIB.TEST(SCH_1):PAGE92_I223 [at] INF_LOGIC(CHIPS)':
> 'CLK2': CDS_PINID='CLK2';
> NODE_NAME R1395 1
> '@TEST_LIB.TEST(SCH_1):PAGE92_I232 [at] INF_RESISTORS(CHIPS)':
> 'A': CDS_PINID='A';

Here's an inelegant way:

**** CODE ****

results = {}
net_name_next = False
net_name = None
node_names = []
for line in sourcefile:
line = line.strip()
if line.startswith('NET_NAME'):
if net_name is not None:
results[net_name] = ", ".join(node_names)
net_name = None
node_names = []
net_name_next = True
elif net_name_next:
net_name = line.strip("'")
net_name_next = False
elif line.startswith('NODE_NAME'):
node_names.append("{1}.{2}".format(*line.split()))

# Last time through
if net_name is not None:
results[net_name] = ", ".join(node_names)

**** END CODE ****

If you're prepared to allow the dictionary values to be lists rather than
strings, we can squash that down:

**** CODE ****

results = {None: []}
net_name = None
for line in sourcefile:
line = line.strip()
if line.startswith('NET_NAME'):
net_name = sourcefile.next().strip(" \t\n'")
results[net_name] = []
elif line.startswith('NODE_NAME'):
results[net_name].append("{1}.{2}".format(*line.split()))
del results[None]

**** END CODE ****

If you can guarantee that you won't meet a NODE_NAME before you see a
NET_NAME then you can lose the messing about with results[None]. Having
spent today picking up the pieces from an assumption that's rotted after
several years, I'm not feeling that brave. A slightly less messy version
of that would be:


**** CODE ****

results = {}
entry = []
for line in sourcefile:
line = line.strip()
if line.startswith('NET_NAME'):
entry = []
results[sourcefile.next().strip(" \t\n'")] = entry
elif line.startswith('NODE_NAME'):
entry.append("{1}.{2}".format(*line.split()))

**** END CODE ****

I'm a little dubious about doing mutable magic like this without copious
comments, though.

--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


callmeclaudius at gmail

Nov 6, 2009, 10:25 PM

Post #5 of 5 (222 views)
Permalink
Re: parse a string (Cadence Allegro Netlist) to dictionary [In reply to]

On Nov 5, 7:23 pm, metal <metal...@gmail.com> wrote:
> On 11月6日, 上午4时02分, Leland <lelandp...@gmail.com> wrote:
>
>
>
> > Hi,
>
> > I always use readline(), strip(), split() and so on to parse a string.
> > Is there some elegant way to parse the following string into a
> > dictionary {'50MHZ_CLK_SRC' : 'U122.2, R1395.1'}?
>
> > NET_NAME
> > '50MHZ_CLK_SRC'
> > '@TEST_LIB.TEST(SCH_1):50MHZ_CLK_SRC':
> > C_SIGNAL='@test_lib.test(sch_1):\50mhz_clk_src\';
> > NODE_NAME U122 2
> > '@TEST_LIB.TEST(SCH_1):PAGE92_I223 [at] INF_LOGIC(CHIPS)':
> > 'CLK2': CDS_PINID='CLK2';
> > NODE_NAME R1395 1
> > '@TEST_LIB.TEST(SCH_1):PAGE92_I232 [at] INF_RESISTORS(CHIPS)':
> > 'A': CDS_PINID='A';
>
> > Thanks,
> > Leland
>
> not very elegantly too.
>
> x = re.findall("_NAME[\n\s']+((?<=').+(?=')|\w+\s+\w+)", s)
> """
> print {x[0]: x[1:]}>>> {'50MHZ_CLK_SRC': ['U122 2', 'R1395 1']}
>
> """

@metal

Apparently you and I differ considerably on our conceptions of
elegance.
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.