Quick Filter Dictionary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • abcd

    #1

    Quick Filter Dictionary

    Hi,
    I have a dictionary which may contain various keys/values, however,
    it will always contain 'name' and 'age' keys.

    This dictionary is kept inside a class, such as....

    class Person:
    def __init__(self, name, age):
    self.data = {'name' : name, 'age' : age}

    def getData(self, includeNameAge= False):
    if includeNameAge: return self.data
    tmp = <shallow or deep copy of 'data'>
    del tmp['name']
    del tmp['age']
    return tmp

    The getdata method should return the data stored for the
    person, however, it should be able to return the data excluding the
    'name' and 'age'. Any suggestions on how to best implement this?
    Keep in mind this sample code is meant to be an example, I realize
    that a person would probably always want the name and age returned.

    thanks

  • abcd

    #2
    Re: Quick Filter Dictionary

    On Mar 14, 7:29 am, "abcd" <codecr...@gmai l.comwrote:
    Hi,
    I have a dictionary which may contain various keys/values, however,
    it will always contain 'name' and 'age' keys.
    >
    This dictionary is kept inside a class, such as....
    >
    class Person:
    def __init__(self, name, age):
    self.data = {'name' : name, 'age' : age}
    >
    def getData(self, includeNameAge= False):
    if includeNameAge: return self.data
    tmp = <shallow or deep copy of 'data'>
    del tmp['name']
    del tmp['age']
    return tmp
    >
    The getdata method should return the data stored for the
    person, however, it should be able to return the data excluding the
    'name' and 'age'. Any suggestions on how to best implement this?
    Keep in mind this sample code is meant to be an example, I realize
    that a person would probably always want the name and age returned.
    >
    thanks

    I guess one solution might be....

    def getData(self, includeNameAge= False):
    if includeNameAge: return self.data
    return dict(filter(lam bda item: item[0] not in ('name', 'age'),
    self.data.items ()))

    any other suggestions, improvements?

    Comment

    • Bruno Desthuilliers

      #3
      Re: Quick Filter Dictionary

      abcd a écrit :
      On Mar 14, 7:29 am, "abcd" <codecr...@gmai l.comwrote:
      >Hi,
      > I have a dictionary which may contain various keys/values, however,
      >it will always contain 'name' and 'age' keys.
      >>
      > This dictionary is kept inside a class, such as....
      >>
      > class Person:
      Do yourself a favor : use new-style classes:
      class Person(object):
      > def __init__(self, name, age):
      > self.data = {'name' : name, 'age' : age}
      You may improve this part with kwargs:

      def __init__(self, name, age, **kw):
      kw.update(name= name, age=age)
      self.data = kw

      Also, and FWIW, object attributes are stored in self.___dict__. Do you
      have a compelling reason to use yet another 'data' dict ?
      > def getData(self, includeNameAge= False):
      > if includeNameAge: return self.data
      > tmp = <shallow or deep copy of 'data'>
      tmp = self.data.copy( )
      > del tmp['name']
      > del tmp['age']
      > return tmp
      The problem with this implementation (as well as for the other below) is
      that in the first case you return a reference to the data dict itself,
      while in the second case you return a new dict.

      p = Person('bibi', 42, foo='bar')
      data = p.getData(True)
      data['baaz'] = 'quux'
      del data['name']
      print p.data

      p = Person('bibi', 42, foo='bar')
      p.getData()
      data['baaz'] = 'quux'
      del data['foo']
      print p.data

      Better to *always* return a copy IMHO.

      Comment

      • Steve Holden

        #4
        Re: Quick Filter Dictionary

        abcd wrote:
        On Mar 14, 7:29 am, "abcd" <codecr...@gmai l.comwrote:
        >Hi,
        > I have a dictionary which may contain various keys/values, however,
        >it will always contain 'name' and 'age' keys.
        >>
        > This dictionary is kept inside a class, such as....
        >>
        > class Person:
        > def __init__(self, name, age):
        > self.data = {'name' : name, 'age' : age}
        >>
        > def getData(self, includeNameAge= False):
        > if includeNameAge: return self.data
        > tmp = <shallow or deep copy of 'data'>
        > del tmp['name']
        > del tmp['age']
        > return tmp
        >>
        > The getdata method should return the data stored for the
        >person, however, it should be able to return the data excluding the
        >'name' and 'age'. Any suggestions on how to best implement this?
        >Keep in mind this sample code is meant to be an example, I realize
        >that a person would probably always want the name and age returned.
        >>
        >thanks
        >
        >
        I guess one solution might be....
        >
        def getData(self, includeNameAge= False):
        if includeNameAge: return self.data
        return dict(filter(lam bda item: item[0] not in ('name', 'age'),
        self.data.items ()))
        >
        any other suggestions, improvements?
        >
        You might find

        return dict((x,y) for (x, y) in self.data.iteri tems()
        if x not in ('name', 'age'))

        easier to understand. Or you might not ...

        regards
        Steve
        --
        Steve Holden +44 150 684 7255 +1 800 494 3119
        Holden Web LLC/Ltd http://www.holdenweb.com
        Skype: holdenweb http://del.icio.us/steve.holden
        Blog of Note: http://holdenweb.blogspot.com
        See you at PyCon? http://us.pycon.org/TX2007

        Comment

        Working...