Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - parse a string (Cadence Allegro Netlist) to dictionary

 
Thread Tools Search this Thread
Old 11-05-2009, 08:02 PM   #1
Default 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)AGE92_I223@INF_LOGIC.CY230 5(CHIPS)':
'CLK2': CDS_PINID='CLK2';
NODE_NAME R1395 1
'@TEST_LIB.TEST(SCH_1)AGE92_I232@INF_RESISTORS.R ESISTOR(CHIPS)':
'A': CDS_PINID='A';

Thanks,
Leland


Leland
  Reply With Quote
Old 11-05-2009, 09:05 PM   #2
Emile van Sebille
 
Posts: n/a
Default Re: parse a string (Cadence Allegro Netlist) to dictionary
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)AGE92_I223@INF_LOGIC.CY230 5(CHIPS)':
> 'CLK2': CDS_PINID='CLK2';
> NODE_NAME R1395 1
> '@TEST_LIB.TEST(SCH_1)AGE92_I232@INF_RESISTORS.R ESISTOR(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



Emile van Sebille
  Reply With Quote
Old 11-06-2009, 12:23 AM   #3
metal
 
Posts: n/a
Default Re: parse a string (Cadence Allegro Netlist) to dictionary
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)AGE92_I223@INF_LOGIC.CY230 5(CHIPS)':
> 'CLK2': CDS_PINID='CLK2';
> NODE_NAME R1395 1
> '@TEST_LIB.TEST(SCH_1)AGE92_I232@INF_RESISTORS.R ESISTOR(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
  Reply With Quote
Old 11-07-2009, 06:25 AM   #4
Joel Davis
 
Posts: n/a
Default Re: parse a string (Cadence Allegro Netlist) to dictionary
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)AGE92_I223@INF_LOGIC.CY230 5(CHIPS)':
> > 'CLK2': CDS_PINID='CLK2';
> > NODE_NAME R1395 1
> > '@TEST_LIB.TEST(SCH_1)AGE92_I232@INF_RESISTORS.R ESISTOR(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.


Joel Davis
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Give you enough string functions in Java web reporting tool freezea Software 0 10-08-2009 09:03 AM
Java String Problems rbnbenjamin General Help Related Topics 0 02-03-2009 11:02 PM
Dazzle Box... swt458 Hardware 2 01-15-2008 06:06 AM
Spelling Dictionaries Slytherin Computer Support 9 09-15-2007 05:45 PM
PART 6, CAx, Structural Engineering, other, updated 30/October/2006 kashumoto_tokugawa Computer Information 0 11-07-2006 04:48 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46