Re: [Tutor] Interesting problem
I may be missing something, but isn't this what __dict__ does? Just return self.__dict__. This is an old message, so this may have mentioned already. Sorry if that's the case. I'm a little behind. Kent Johnson wrote: Smith, Jeff wrote: Here would be the usage: myinst = MyClass() print myinst.getprops_as_dict() would print {'var1': 1, 'var2': 2, 'var3': 3} Needless to say I want the instance values which might be different for each instance. I know that I could code it brute force, but I want to be able to add properties without having to remember to update getprops_as_dict(). OK, so will a variation on my last recipe work? This looks for property attributes of the class and gets the corresponding property on the instance: def getprops_as_dict(self): return dict(pname, getattr(self, pname) for pname in dir(self.__class__) if isinstance(getattr(self.__class__, pname), property)) ) Kent For those who are interested, the dictionary created by getprops_as_dict() will be fed to string.Template.substitute Jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interesting problem
Smith, Jeff wrote: Consider a class with a lt of properties. I would like a member function which generates a dictionary where the keys are the property names and the values are the property values? Is this clear? How might I go about this? Jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Like some object.__dict__? Given this class: 1 class Foo: 2 def __init__(self, **kw): 3 self.__dict__.update(kw) 4 And creating an instance of it like this: In [17]: foo = Foo(**{bar:b, foo:f, bam:bm}) And accessing the properties of it like this: In [18]: foo.foo Out[18]: 'f' In [19]: foo.bar Out[19]: 'b' In [20]: foo.bam Out[20]: 'bm' You can get all properties of it like this: In [21]: foo.__dict__ Out[21]: {'bam': 'bm', 'bar': 'b', 'foo': 'f'} Is this what you're looking for? JJ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interesting problem
-Original Message- Consider a class with a lt of properties. I would like a member function which generates a dictionary where the keys are the property names and the values are the property values? Is this clear? I think so :-) How might I go about this? I think you could simply use the special object attribute __dict__. It holds a dictionary of all properties and their values. class Something: ... def __init__(self, item1, item2, item3, item4, item5): ... self.item1 = item1 ... self.item2 = item2 ... self.item3 = item3 ... self.item4 = item4 ... self.item5 = item5 ... s = Something(42,52,55,1,54) s.__dict__ {'item2': 52, 'item3': 55, 'item1': 42, 'item4': 1, 'item5': 54} Jeff HTH, Christian http://www.dowski.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interesting problem
I can see I wasn't clear :-) Here's the basic framework of what I'm looking for. Needless to say, this is just an example and not the real problem which is quite complicated and includes multiple classes. What I'm looking for is the actual implementation of get_props_as_dict which I've written here as pseudo-code class MyClass: def __init__(self): self._var1 = val1 self._var2 = val2 var1 = property(lambda s: s._var1) var2 = property(lambda s: s._var2) def _var3(self): return self._var1 + self._var2 var3 = property(_var3) def getprops_as_dict(self): d = dict() for prop in properties: d[prop_name] = prop_value return d Thanks, Jeff -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Smith, Jeff Sent: Thursday, June 23, 2005 2:01 PM To: tutor@python.org Subject: [Tutor] Interesting problem Consider a class with a lt of properties. I would like a member function which generates a dictionary where the keys are the property names and the values are the property values? Is this clear? How might I go about this? Jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interesting problem
Smith, Jeff wrote: I can see I wasn't clear :-) Here's the basic framework of what I'm looking for. Needless to say, this is just an example and not the real problem which is quite complicated and includes multiple classes. What I'm looking for is the actual implementation of get_props_as_dict which I've written here as pseudo-code class MyClass: def __init__(self): self._var1 = val1 self._var2 = val2 var1 = property(lambda s: s._var1) var2 = property(lambda s: s._var2) def _var3(self): return self._var1 + self._var2 var3 = property(_var3) def getprops_as_dict(self): d = dict() for prop in properties: d[prop_name] = prop_value return d Still not that clear. What do you want to see when you call MyClass().getprops_as_dict() ? Maybe this will give you some ideas: class MyClass: ... def __init__(self, val1, val2): ... self._var1 = val1 ... self._var2 = val2 ... var1 = property(lambda s: s._var1) ... var2 = property(lambda s: s._var2) ... def _var3(self): ... return self._var1 + self._var2 ... var3 = property(_var3) ... def getprops_as_dict(self): ... d = dict(self.__dict__) ... return d ... m=MyClass(1,2) Using just m.__dict__: m.getprops_as_dict() {'_var2': 2, '_var1': 1} The inspect module might be some help: import inspect for k, v in inspect.getmembers(m): ... print k, '=', v ... __doc__ = None __init__ = bound method MyClass.__init__ of __main__.MyClass instance at 0x008DD918 __module__ = __main__ _var1 = 1 _var2 = 2 _var3 = bound method MyClass._var3 of __main__.MyClass instance at 0x008DD918 getprops_as_dict = bound method MyClass.getprops_as_dict of __main__.MyClass instance at 0x008DD918 var1 = 1 var2 = 2 var3 = 3 This inspects the class for actual properties and shows their values. It won't print simple attributes (in m.__dict__) or attributes defined by user-defined descriptors though: for p in dir(m.__class__): ... pp = getattr(m.__class__, p) ... if isinstance(pp, property): ... print p, '=', getattr(m, p) ... var1 = 1 var2 = 2 var3 = 3 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interesting problem
Here would be the usage: myinst = MyClass() print myinst.getprops_as_dict() would print {'var1': 1, 'var2': 2, 'var3': 3} Needless to say I want the instance values which might be different for each instance. I know that I could code it brute force, but I want to be able to add properties without having to remember to update getprops_as_dict(). For those who are interested, the dictionary created by getprops_as_dict() will be fed to string.Template.substitute Jeff -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Kent Johnson Sent: Thursday, June 23, 2005 3:17 PM To: Python Tutor Subject: Re: [Tutor] Interesting problem Still not that clear. What do you want to see when you call MyClass().getprops_as_dict() ? Maybe this will give you some ideas: class MyClass: ... def __init__(self, val1, val2): ... self._var1 = val1 ... self._var2 = val2 ... var1 = property(lambda s: s._var1) ... var2 = property(lambda s: s._var2) ... def _var3(self): ... return self._var1 + self._var2 ... var3 = property(_var3) ... def getprops_as_dict(self): ... d = dict(self.__dict__) ... return d ... m=MyClass(1,2) Using just m.__dict__: m.getprops_as_dict() {'_var2': 2, '_var1': 1} The inspect module might be some help: import inspect for k, v in inspect.getmembers(m): ... print k, '=', v ... __doc__ = None __init__ = bound method MyClass.__init__ of __main__.MyClass instance at 0x008DD918 __module__ = __main__ _var1 = 1 _var2 = 2 _var3 = bound method MyClass._var3 of __main__.MyClass instance at 0x008DD918 getprops_as_dict = bound method MyClass.getprops_as_dict of __main__.MyClass instance at 0x008DD918 var1 = 1 var2 = 2 var3 = 3 This inspects the class for actual properties and shows their values. It won't print simple attributes (in m.__dict__) or attributes defined by user-defined descriptors though: for p in dir(m.__class__): ... pp = getattr(m.__class__, p) ... if isinstance(pp, property): ... print p, '=', getattr(m, p) ... var1 = 1 var2 = 2 var3 = 3 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interesting problem
Smith, Jeff wrote: Here would be the usage: myinst = MyClass() print myinst.getprops_as_dict() would print {'var1': 1, 'var2': 2, 'var3': 3} Needless to say I want the instance values which might be different for each instance. I know that I could code it brute force, but I want to be able to add properties without having to remember to update getprops_as_dict(). OK, so will a variation on my last recipe work? This looks for property attributes of the class and gets the corresponding property on the instance: def getprops_as_dict(self): return dict(pname, getattr(self, pname) for pname in dir(self.__class__) if isinstance(getattr(self.__class__, pname), property)) ) Kent For those who are interested, the dictionary created by getprops_as_dict() will be fed to string.Template.substitute Jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interesting problem
Consider a class with a lt of properties. I would like a member function which generates a dictionary where the keys are the property names and the values are the property values? Use the force... :-) Since Python uses dictionaries to store all those things already there must be a suitable bit of black magic that will serve it up on a plate. Doing some reading around the innards of classes should shed light on it. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interesting problem
That's what I was looking for. Although I couldn't get the below to work, I went with a different mod of the original you gave: def get_props_as_dict(self): d = dict() for entry in dir(self.__class__): if isinstance(getattr(self.__class__, entry), property): d[entry] = getattr(self, entry) return d Thanks! Jeff -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Kent Johnson Sent: Thursday, June 23, 2005 4:39 PM To: Python Tutor Subject: Re: [Tutor] Interesting problem Smith, Jeff wrote: Here would be the usage: myinst = MyClass() print myinst.getprops_as_dict() would print {'var1': 1, 'var2': 2, 'var3': 3} Needless to say I want the instance values which might be different for each instance. I know that I could code it brute force, but I want to be able to add properties without having to remember to update getprops_as_dict(). OK, so will a variation on my last recipe work? This looks for property attributes of the class and gets the corresponding property on the instance: def getprops_as_dict(self): return dict(pname, getattr(self, pname) for pname in dir(self.__class__) if isinstance(getattr(self.__class__, pname), property)) ) Kent For those who are interested, the dictionary created by getprops_as_dict() will be fed to string.Template.substitute Jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interesting problem
Smith, Jeff wrote: That's what I was looking for. Although I couldn't get the below to work, I went with a different mod of the original you gave: def get_props_as_dict(self): d = dict() for entry in dir(self.__class__): if isinstance(getattr(self.__class__, entry), property): d[entry] = getattr(self, entry) return d OK good! My code was untested and requires Python 2.4. I'm glad you could turn it into something that works for you. Kent Thanks! Jeff -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Kent Johnson OK, so will a variation on my last recipe work? This looks for property attributes of the class and gets the corresponding property on the instance: def getprops_as_dict(self): return dict(pname, getattr(self, pname) for pname in dir(self.__class__) if isinstance(getattr(self.__class__, pname), property)) ) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor