Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > create/access dynamic growth list

Reply
Thread Tools

create/access dynamic growth list

 
 
sam
Guest
Posts: n/a
 
      03-18-2005
Hi,

I have a configuration file need to be processed (read/write) by python.
Currently I the following method can only read and store data that
python read a line from a configuraiton file:
def _parse (self):
# parse message
m = self.FWShow_Command.match (conf_data)
if (m == None):
raise FW_Command.Error ("corrupt macro definition")

# store generic data
macro = {}
macro["key"] = m.group (1)
macro["value"] = m.group (2)

return (conf_data, macro)

Since there are unknown number of lines in a configuration file, I want
to store the array of "macro" in a list.
How can I do that in Python?

Thanks
Sam.
 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      03-18-2005
sam wrote:
> Hi,
>
> I have a configuration file need to be processed (read/write) by python.
> Currently I the following method can only read and store data that
> python read a line from a configuraiton file:
> def _parse (self):
> # parse message
> m = self.FWShow_Command.match (conf_data)
> if (m == None):
> raise FW_Command.Error ("corrupt macro definition")
>
> # store generic data
> macro = {}
> macro["key"] = m.group (1)
> macro["value"] = m.group (2)
>
> return (conf_data, macro)
>
> Since there are unknown number of lines in a configuration file, I want
> to store the array of "macro" in a list.
> How can I do that in Python?
>
> Thanks
> Sam.

Pretty sketchy description, but I'll take a shot.

fp=open(conf_data, 'r')
macro_lines=fp.readlines()
fp.close()

After this macro_lines will be a list with one line per element
such that macro_list[0] is the first line, macro_list[1] the
second, etc.

Larry Bates

 
Reply With Quote
 
 
 
 
sam
Guest
Posts: n/a
 
      03-19-2005
Larry Bates wrote:

> sam wrote:
>
>>Hi,
>>
>>I have a configuration file need to be processed (read/write) by python.
>>Currently I the following method can only read and store data that
>>python read a line from a configuraiton file:
>> def _parse (self):
>> # parse message
>> m = self.FWShow_Command.match (conf_data)
>> if (m == None):
>> raise FW_Command.Error ("corrupt macro definition")
>>
>> # store generic data
>> macro = {}
>> macro["key"] = m.group (1)
>> macro["value"] = m.group (2)
>>
>> return (conf_data, macro)
>>
>>Since there are unknown number of lines in a configuration file, I want
>>to store the array of "macro" in a list.
>>How can I do that in Python?
>>
>>Thanks
>>Sam.

>
> Pretty sketchy description, but I'll take a shot.
>
> fp=open(conf_data, 'r')
> macro_lines=fp.readlines()
> fp.close()
>
> After this macro_lines will be a list with one line per element
> such that macro_list[0] is the first line, macro_list[1] the
> second, etc.
>

Thanks for the suggestion.
Since each line of the configuration file need to be parsed into "key"
and "value" pairs, these pair of tokens need to be stored in another
list. Can I use the following operation to store these pair of tokens?
eg.
Assumed macro_hash["key"]="some key"
macro_hash["value"]="some value"
then build a list that contains a list of macro_hash:
macro_list.append(macro)
when treverse the macro_list, it would be similar to perl:
foreach macro (in macro_list)
key = macro["key"]
value = macro["value"]

Sam.


> Larry Bates
>

 
Reply With Quote
 
Kent Johnson
Guest
Posts: n/a
 
      03-19-2005
sam wrote:
> Since each line of the configuration file need to be parsed into "key"
> and "value" pairs, these pair of tokens need to be stored in another
> list. Can I use the following operation to store these pair of tokens?
> eg.
> Assumed macro_hash["key"]="some key"
> macro_hash["value"]="some value"
> then build a list that contains a list of macro_hash:
> macro_list.append(macro)
> when treverse the macro_list, it would be similar to perl:
> foreach macro (in macro_list)
> key = macro["key"]
> value = macro["value"]
>


Why not just store the key, value pairs as actual keys and values of a map? Creating a list of maps
seems a bit contorted here. Instead of
> macro_hash["key"]="some key"
> macro_hash["value"]="some value"


followed by creating a list of macro_hashes, just use
macro_hash["some key"] = "some value"

You lose the order of values but gain much easier lookup.

Alternatively just create a list of (key, value) pairs with
macro_list = []
macro_list.append( ("some key", "some value") )

which preserves the order at the expense of easy random access.

Kent
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
py2exe is on Sourceforge list of top growth projects Frank Millman Python 2 12-18-2012 08:15 PM
DVD Recorders Ready For A Growth Spurt In U.S. Allan DVD Video 0 01-24-2005 10:28 PM
HD-DVD growth will be much slower than DVD's was RichA DVD Video 38 12-24-2004 11:22 PM
Unexperienced photographer looking for camera with "growth" possibilities Bram de Jong Digital Photography 3 06-13-2004 07:18 PM
GARTNER: Rapid Linux Growth Boosts Server Market TechNews Computer Support 0 05-26-2004 08:42 PM



Advertisments