parse a string (Cadence Allegro Netlist) to dictionary

View: New views
5 Messages — Rating Filter:   Alert me  

parse a string (Cadence Allegro Netlist) to dictionary

by Leland-12 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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='@...(sch_1):\50mhz_clk_src\';
NODE_NAME     U122 2
 '@TEST_LIB.TEST(SCH_1):PAGE92_I223@...(CHIPS)':
 'CLK2': CDS_PINID='CLK2';
NODE_NAME     R1395 1
 '@TEST_LIB.TEST(SCH_1):PAGE92_I232@...(CHIPS)':
 'A': CDS_PINID='A';

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

Re: parse a string (Cadence Allegro Netlist) to dictionary

by Emile van Sebille :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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='@...(sch_1):\50mhz_clk_src\';
> NODE_NAME     U122 2
>  '@TEST_LIB.TEST(SCH_1):PAGE92_I223@...(CHIPS)':
>  'CLK2': CDS_PINID='CLK2';
> NODE_NAME     R1395 1
>  '@TEST_LIB.TEST(SCH_1):PAGE92_I232@...(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

Re: parse a string (Cadence Allegro Netlist) to dictionary

by metal-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 11月6日, 上午4时02分, Leland <lelandp...@...> 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='@...(sch_1):\50mhz_clk_src\';
> NODE_NAME     U122 2
>  '@TEST_LIB.TEST(SCH_1):PAGE92_I223@...(CHIPS)':
>  'CLK2': CDS_PINID='CLK2';
> NODE_NAME     R1395 1
>  '@TEST_LIB.TEST(SCH_1):PAGE92_I232@...(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

Re: parse a string (Cadence Allegro Netlist) to dictionary

by Rhodri James :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, 05 Nov 2009 20:02:53 -0000, Leland <lelandpeng@...> 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='@...(sch_1):\50mhz_clk_src\';
> NODE_NAME     U122 2
>  '@TEST_LIB.TEST(SCH_1):PAGE92_I223@...(CHIPS)':
>  'CLK2': CDS_PINID='CLK2';
> NODE_NAME     R1395 1
>  '@TEST_LIB.TEST(SCH_1):PAGE92_I232@...(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

Re: parse a string (Cadence Allegro Netlist) to dictionary

by Joel Davis-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Nov 5, 7:23 pm, metal <metal...@...> wrote:

> On 11月6日, 上午4时02分, Leland <lelandp...@...> 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='@...(sch_1):\50mhz_clk_src\';
> > NODE_NAME     U122 2
> >  '@TEST_LIB.TEST(SCH_1):PAGE92_I223@...(CHIPS)':
> >  'CLK2': CDS_PINID='CLK2';
> > NODE_NAME     R1395 1
> >  '@TEST_LIB.TEST(SCH_1):PAGE92_I232@...(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