create/access dynamic growth list

S

sam

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.
 
L

Larry Bates

sam said:
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
 
S

sam

Larry said:
sam said:
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.
 
K

Kent Johnson

sam said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top