Class initialization from a dictionary, how best?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • brianobush@gmail.com

    #1

    Class initialization from a dictionary, how best?

    #
    # My problem is that I want to create a
    # class, but the variables aren't known
    # all at once. So, I use a dictionary to
    # store the values in temporarily.
    # Then when I have a complete set, I want to
    # init a class from that dictionary.
    # However, I don't want to specify the
    # dictionary gets by hand
    # since it is error prone.
    # Thanks for any ideas, Brian


    # So I have a class defined that accepts a number of variables
    # and they must be all present at object creation time.
    class Test:
    def __init__(self, a, b, c):
    self.a = a
    self.b = b
    self.c = c
    def __str__(self):
    return '%s, %s, %d' % (self.a, self.b, self.c)

    # For example:
    t1 = Test('asd', 'sdf', 9)
    print t1

    # However, due to parsing XML, I am
    # creating the values incrementally
    # and I want to store them in a dictionary
    # and then map them easily to a class

    dictionary = {}

    # a mapping from source to destination
    mapping = {
    'a': str,
    'b': str,
    'c': int,
    }

    # a sample source of values
    test_source = {
    'a': 'test',
    'b': 'asdf',
    'c': 45
    }

    # now we go through and extract the values
    # from our source and build the dictionary

    for attr_name, function in mapping.items() :
    dictionary[attr_name] = function(test_s ource.get(attr_ name))

    print dictionary

    # Here is the problem I want to avoid:
    # Having to list the variable names
    # as strings in multiple places. It is enought to
    # have them in the 'mapping'
    # dictionary above

    t2 = Test(dictionary .get('a'), dictionary.get( 'b'),
    dictionary.get( 'c'))
    print t2

  • Nick Coghlan

    #2
    Re: Class initialization from a dictionary, how best?

    brianobush@gmai l.com wrote:[color=blue]
    > t2 = Test(dictionary .get('a'), dictionary.get( 'b'),
    > dictionary.get( 'c'))
    > print t2[/color]

    Try this:

    t2 = Test(**dictiona ry)

    This performs keyword argument expansion on the dictionary, matching the
    dictionary entries with the named arguments to the Test.__init__ function.

    Cheers,
    Nick.

    --
    Nick Coghlan | ncoghlan@email. com | Brisbane, Australia
    ---------------------------------------------------------------

    Comment

    • Bengt Richter

      #3
      Re: Class initialization from a dictionary, how best?

      On 13 Jan 2005 20:36:19 -0800, "brianobush@gma il.com" <brianobush@gma il.com> wrote:
      [color=blue]
      >#
      ># My problem is that I want to create a
      ># class, but the variables aren't known
      ># all at once. So, I use a dictionary to
      ># store the values in temporarily.[/color]
      Why?[color=blue]
      ># Then when I have a complete set, I want to
      ># init a class from that dictionary.[/color]
      Why do it that way?
      [color=blue]
      ># However, I don't want to specify the
      ># dictionary gets by hand
      ># since it is error prone.
      ># Thanks for any ideas, Brian
      >
      >
      > So I have a class defined that accepts a number of variables
      ># and they must be all present at object creation time.[/color]
      Why? Why not a method to check if it's valid yet, and then
      add the attributes as they're available, without intermediaries.
      You can make Test "smart" so it won't accept any other names than a,b,c
      and will automatically convert to str, str, and int. Etc.

      What are you actually doing? Is this a toy example of a more complex class?
      [color=blue]
      >class Test:
      >def __init__(self, a, b, c):
      >self.a = a
      >self.b = b
      >self.c = c
      >def __str__(self):
      >return '%s, %s, %d' % (self.a, self.b, self.c)
      >
      ># For example:
      >t1 = Test('asd', 'sdf', 9)
      >print t1
      >
      ># However, due to parsing XML, I am
      ># creating the values incrementally
      ># and I want to store them in a dictionary
      ># and then map them easily to a class[/color]
      Why store them in a dictionary? Why not create an empty Test instance,
      and incrementally add the attributes directly as they're available?

      What are you going to do with t1 and other instances?
      [color=blue]
      >
      >dictionary = {}
      >
      ># a mapping from source to destination
      >mapping = {
      >'a': str,
      >'b': str,
      >'c': int,
      >}
      >
      ># a sample source of values
      >test_source = {
      >'a': 'test',
      >'b': 'asdf',
      >'c': 45
      >}
      >
      ># now we go through and extract the values
      ># from our source and build the dictionary[/color]
      Just the three items in the test_source dictionary?
      Is that just a 3-item holding place until you[color=blue]
      >
      >for attr_name, function in mapping.items() :
      >dictionary[attr_name] = function(test_s ource.get(attr_ name))
      >
      >print dictionary
      >
      ># Here is the problem I want to avoid:
      ># Having to list the variable names
      ># as strings in multiple places. It is enought to
      ># have them in the 'mapping'
      ># dictionary above
      >
      >t2 = Test(dictionary .get('a'), dictionary.get( 'b'),
      >dictionary.get ('c'))
      >print t2
      >[/color]
      Why don't you just let Test do all the work, and update it incrementally
      until it is complete. You can use descriptors to manage state. You could
      do any number of things. The main problem is defining the _requirements_
      without premature implementation, never mind premature optimization ;-)

      You may have good reasons for wanting to do what you seem to want to do,
      but the picture is not clear to me ;-)

      Regards,
      Bengt Richter

      Comment

      • brianobush@gmail.com

        #4
        Re: Class initialization from a dictionary, how best?

        Yes, my examle here is a tiny part of a larger more complex issue. My
        application is an DOM XML parser that is reading attributes one at a
        time. My class that I am creating is used elsewhere and must have
        certain arguments for those uses to continue working. So, I seem to be
        left with creating an intermediate object. Before, I was simply
        creating an object:

        t = Test()
        for attr_name in mapping.keys():
        setattr(t, attr_name, value_from_sour ce)

        This I feel was ellegant, efficient and clear. However, what I have now
        works but is not clear.
        BTW, t1 is just for example and was just being printed
        Thanks, Brian

        Comment

        • Bengt Richter

          #5
          Re: Class initialization from a dictionary, how best?

          On 14 Jan 2005 07:32:06 -0800, "brianobush@gma il.com" <brianobush@gma il.com> wrote:
          [color=blue]
          >Yes, my examle here is a tiny part of a larger more complex issue. My
          >application is an DOM XML parser that is reading attributes one at a[/color]
          you mean like <tag attname="value" att2="v2">blah blah</tag> and you are
          grabbing things of interest out of a stream of info you are getting
          from call-backs? Or the equivalent?[color=blue]
          >time. My class that I am creating is used elsewhere and must have
          >certain arguments for those uses to continue working. So, I seem to be
          >left with creating an intermediate object. Before, I was simply[/color]
          Unless the "intermedia te object" accumulates information for multiple
          Test() instances, why couldn't t= Test() be its own "intermedia te object"?

          If you are accumulating info for multiple instances before creating them
          it is not clear from your description.
          [color=blue]
          >creating an object:
          >
          >t = Test()
          >for attr_name in mapping.keys():
          >setattr(t, attr_name, value_from_sour ce)
          >
          >This I feel was ellegant, efficient and clear. However, what I have now
          >works but is not clear.
          >BTW, t1 is just for example and was just being printed[/color]

          What about giving Test some methods to do what you'd like? E.g., a micro-step
          in that direction from the above would let you write

          t = Test()
          ...
          t.load_info(inf osource)

          Then the question becomes what infosource should be, or whether you really
          need it at all. IOW, if you are doing infosource.add_ info(info_id, info_value)
          why couldn't you do t.add_info(info _id, info_value), unless you have to
          do t2.add_info(... ) alternately with t.add_info(...) , and if that's the case,
          what is the criterion for choosing t vs t2? Maybe that could be done by something
          that automatically manufactures t's as needed in a pool of partially complete t's.

          But your real requirements are not clear enough here, so you may get help crossing
          a stream, but no one will be able to say you are already on the side of the stream
          you want to be later, and there's an easy path without need of crossing twice ;-)

          Regards,
          Bengt Richter

          Comment

          Working...