create/access dynamic growth list

Discussion in 'Python' started by sam, Mar 18, 2005.

  1. sam

    sam Guest

    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.
     
    sam, Mar 18, 2005
    #1
    1. Advertising

  2. sam

    Larry Bates Guest

    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
     
    Larry Bates, Mar 18, 2005
    #2
    1. Advertising

  3. sam

    sam Guest

    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
    >
     
    sam, Mar 19, 2005
    #3
  4. sam

    Kent Johnson Guest

    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
     
    Kent Johnson, Mar 19, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Generic Usenet Account

    Tracking the "memory growth" of a process

    Generic Usenet Account, Apr 5, 2004, in forum: C++
    Replies:
    5
    Views:
    485
    Kevin Goodsell
    Apr 6, 2004
  2. Generic Usenet Account

    Tracking the "memory growth" of a process

    Generic Usenet Account, Apr 5, 2004, in forum: C Programming
    Replies:
    4
    Views:
    352
    Kevin Goodsell
    Apr 6, 2004
  3. Denis Palas
    Replies:
    1
    Views:
    528
  4. Replies:
    3
    Views:
    1,064
    Martin Honnen
    Dec 20, 2006
  5. Frank Millman
    Replies:
    2
    Views:
    140
    Vlastimil Brom
    Dec 18, 2012
Loading...

Share This Page