Re: Getting a dictionary from an object

2005-08-03 Thread Mike Moum
Thanos Tsouanas wrote: I'm not sure what you're getting at, but have you tried this: class A(object): def __getitem__(self, ky): return self.__dict__[ky] for example: >>> class A(object): def __init__(self,a,b,c): self.a = a self.b = b

Re: Getting a dictionary from an object

2005-07-25 Thread Steven Bethard
Thanos Tsouanas wrote: > On Sun, Jul 24, 2005 at 02:14:15PM -0600, Steven Bethard wrote: > >>How about something like: >> dict((name, getattr(obj, name)) for name in dir(obj)) > > Pretty!!! > >>Looks like this will get instance attributes, class attributes and >>properties just fine. > > B

Re: Getting a dictionary from an object

2005-07-25 Thread Thanos Tsouanas
On Sun, Jul 24, 2005 at 02:14:15PM -0600, Steven Bethard wrote: > > How about something like: > dict((name, getattr(obj, name)) for name in dir(obj)) Pretty!!! > Looks like this will get instance attributes, class attributes and > properties just fine. But not SQLObject's objects... Any i

Re: Getting a dictionary from an object

2005-07-25 Thread bruno modulix
John Machin wrote: > Dark Cowherd wrote: > >>> voiceless-ly'rs >> >> >> What does this mean?? Just curious (googled that and ly'rs and didnt >> find anything relevant) s/ly'rs/ly y'rs/ > The voiceless part I understand to mean that Bruno is "shocked and > stunned and not a little bit amazed" [1]

Re: Getting a dictionary from an object

2005-07-25 Thread John Machin
Dark Cowherd wrote: >>voiceless-ly'rs > > What does this mean?? Just curious (googled that and ly'rs and didnt > find anything relevant) The voiceless part I understand to mean that Bruno is "shocked and stunned and not a little bit amazed" [1] at Steven's masterstroke which came out of the blu

Re: Getting a dictionary from an object

2005-07-24 Thread Dark Cowherd
> voiceless-ly'rs What does this mean?? Just curious (googled that and ly'rs and didnt find anything relevant) -- Dark Cowherd -- http://mail.python.org/mailman/listinfo/python-list

Re: Getting a dictionary from an object

2005-07-24 Thread Bruno Desthuilliers
*Grandmaster* Steven Bethard a écrit : > > How about something like: > dict((name, getattr(obj, name)) for name in dir(obj)) > ... voiceless-ly'rs -- http://mail.python.org/mailman/listinfo/python-list

Re: Getting a dictionary from an object

2005-07-24 Thread Steven Bethard
Thanos Tsouanas wrote: > Steven Bethard wrote: > >>Maybe I'm not understanding your problem, but have you looked at the >>builtin "vars()"? > > I didn't know about it, but I knew about object.__dict__ which is, as I > see equivalent with vars(object). But it doesn't do the job for me, > since i

Re: Getting a dictionary from an object

2005-07-24 Thread Bruno Desthuilliers
Steven D'Aprano a écrit : > On Sun, 24 Jul 2005 12:07:02 +0300, Thanos Tsouanas wrote: >> >>>Thanos Tsouanas wrote: >>> (snip) >>I didn't know about it, but I knew about object.__dict__ which is, as I >>see equivalent with vars(object). But it doesn't do the job for me, >>since it fails to grab al

Re: Getting a dictionary from an object

2005-07-24 Thread Bruno Desthuilliers
Steven D'Aprano a écrit : > On Sun, 24 Jul 2005 12:03:47 +0300, Thanos Tsouanas wrote: >> >>Please, tell me, how would you print it in my case? > > If I have understood you, you have some object like such: > > obj.foo = 1 > obj.bar = 2 > obj.spam = 'a' > obj.eggs = 'b' > > say. > > You want to

Re: Getting a dictionary from an object

2005-07-24 Thread Steven D'Aprano
On Sun, 24 Jul 2005 12:07:02 +0300, Thanos Tsouanas wrote: > On Sat, Jul 23, 2005 at 06:59:43PM -0600, Steven Bethard wrote: >> Thanos Tsouanas wrote: >> > I would like to have a quick way to create dicts from object, so that a >> > call to foo['bar'] would return obj.bar. >> > >> > The following

Re: Getting a dictionary from an object

2005-07-24 Thread Steven D'Aprano
On Sun, 24 Jul 2005 12:03:47 +0300, Thanos Tsouanas wrote: > On Sun, Jul 24, 2005 at 01:43:43PM +1000, Steven D'Aprano wrote: >> On Sun, 24 Jul 2005 02:09:54 +0300, Thanos Tsouanas wrote: >> > >> > print foo %do >> > >> > where do is a dictobj object... >> >> Are you telling me that the ONLY th

Re: Getting a dictionary from an object

2005-07-24 Thread Thanos Tsouanas
On Sun, Jul 24, 2005 at 03:01:40PM +0200, Bruno Desthuilliers wrote: > I gave you a solution based on the Decorator pattern in another post, > but there is also the possibility to add a __getitem__ method directly > to the to-be-formatted object's class: > > def mygetitem(obj, name): >return

Re: Getting a dictionary from an object

2005-07-24 Thread Bruno Desthuilliers
Thanos Tsouanas a écrit : >>On Sat, 23 Jul 2005 11:48:27 +0300, Thanos Tsouanas wrote: >> >>>Hello. >>> >>>I would like to have a quick way to create dicts from object, so that a >>>call to foo['bar'] would return obj.bar. >> (snip) > print foo %do > > where do is a dictobj object... I gave you

Re: Getting a dictionary from an object

2005-07-24 Thread Bruno Desthuilliers
Steven D'Aprano a écrit : > On Sun, 24 Jul 2005 02:09:54 +0300, Thanos Tsouanas wrote: > > (snip) > > Are you telling me that the ONLY thing you use dictobj objects for is to > print them? > > I don't think so. I do know how to print an object, amazingly. > > Perhaps you would like to explain

Re: Getting a dictionary from an object

2005-07-24 Thread Bruno Desthuilliers
Bruno Desthuilliers a écrit : (snip) > class Wrapper(object): > def __init__(self, obj): > self._obj = obj > def __getitem__(self, name): > return getattr(self._obj, name) If you want the Wrapper to be more like a Decorator (ie still can use the Wrapper object as if it was

Re: Getting a dictionary from an object

2005-07-24 Thread Thanos Tsouanas
On Sun, Jul 24, 2005 at 02:01:30PM +0200, Bruno Desthuilliers wrote: > Thanos Tsouanas a écrit : > > On Sun, Jul 24, 2005 at 01:43:43PM +1000, Steven D'Aprano wrote: > > > > Because *obviously* I don't know of these indexing and attribute > > grabbing machineries you are talking about in my case.

Re: Getting a dictionary from an object

2005-07-24 Thread Bruno Desthuilliers
Thanos Tsouanas a écrit : > On Sun, Jul 24, 2005 at 01:43:43PM +1000, Steven D'Aprano wrote: (snip) > >>Why jump through all those hoops to get attributes when Python already >>provides indexing and attribute grabbing machinery that work well? Why do >>you bother to subclass dict, only to mangle

Re: Getting a dictionary from an object

2005-07-24 Thread Thanos Tsouanas
On Sat, Jul 23, 2005 at 06:59:43PM -0600, Steven Bethard wrote: > Thanos Tsouanas wrote: > > I would like to have a quick way to create dicts from object, so that a > > call to foo['bar'] would return obj.bar. > > > > The following works, but I would prefer to use a built-in way if one > > exists.

Re: Getting a dictionary from an object

2005-07-24 Thread Thanos Tsouanas
On Sun, Jul 24, 2005 at 01:43:43PM +1000, Steven D'Aprano wrote: > On Sun, 24 Jul 2005 02:09:54 +0300, Thanos Tsouanas wrote: > > > > print foo %do > > > > where do is a dictobj object... > > Are you telling me that the ONLY thing you use dictobj objects for is to > print them? I'm sorry to dis

Re: Getting a dictionary from an object

2005-07-23 Thread Steven D'Aprano
On Sun, 24 Jul 2005 02:09:54 +0300, Thanos Tsouanas wrote: > On Sat, Jul 23, 2005 at 11:22:21PM +1000, Steven D'Aprano wrote: >> On Sat, 23 Jul 2005 11:48:27 +0300, Thanos Tsouanas wrote: >> > Hello. >> > >> > I would like to have a quick way to create dicts from object, so that a >> > call to fo

Re: Getting a dictionary from an object

2005-07-23 Thread Steven D'Aprano
On Sat, 23 Jul 2005 13:50:36 -0400, Mike Meyer wrote: >> I don't think this is particularly useful behaviour. How do you use it? > > def __str__(self): > return self._format % self That doesn't work. It calls self.__str__ recursively until Python halts the process. >>> class Thing(d

Re: Getting a dictionary from an object

2005-07-23 Thread Steven Bethard
Thanos Tsouanas wrote: > I would like to have a quick way to create dicts from object, so that a > call to foo['bar'] would return obj.bar. > > The following works, but I would prefer to use a built-in way if one > exists. Is there one? Maybe I'm not understanding your problem, but have you look

Re: Getting a dictionary from an object

2005-07-23 Thread Thanos Tsouanas
On Sat, Jul 23, 2005 at 11:22:21PM +1000, Steven D'Aprano wrote: > On Sat, 23 Jul 2005 11:48:27 +0300, Thanos Tsouanas wrote: > > Hello. > > > > I would like to have a quick way to create dicts from object, so that a > > call to foo['bar'] would return obj.bar. > > That looks rather confusing to

Re: Getting a dictionary from an object

2005-07-23 Thread Mike Meyer
Steven D'Aprano <[EMAIL PROTECTED]> writes: > On Sat, 23 Jul 2005 11:48:27 +0300, Thanos Tsouanas wrote: >> Hello. >> >> I would like to have a quick way to create dicts from object, so that a >> call to foo['bar'] would return obj.bar. > > That looks rather confusing to me. Why not just call obj.

Re: Getting a dictionary from an object

2005-07-23 Thread Steven D'Aprano
On Sat, 23 Jul 2005 11:48:27 +0300, Thanos Tsouanas wrote: > Hello. > > I would like to have a quick way to create dicts from object, so that a > call to foo['bar'] would return obj.bar. That looks rather confusing to me. Why not just call obj.bar, since it doesn't look like you are actually usi

Re: Getting a dictionary from an object

2005-07-23 Thread Bruno Desthuilliers
Thanos Tsouanas a écrit : > On Sat, Jul 23, 2005 at 12:06:57PM +0200, Paolino wrote: > >>use getattr(self.obj,key) possibly, as __getattribute__ gets total >>control on attribute access > > > Thanks, but what do you mean by 'total control'? __getattribute__ is really some kind of an evil blac

Re: Getting a dictionary from an object

2005-07-23 Thread Paolino
Thanos Tsouanas wrote: > On Sat, Jul 23, 2005 at 12:06:57PM +0200, Paolino wrote: > >>use getattr(self.obj,key) possibly, as __getattribute__ gets total >>control on attribute access > > > Thanks, but what do you mean by 'total control'? > Probably nothing to do with your question :( But: >>>

Re: Getting a dictionary from an object

2005-07-23 Thread Thanos Tsouanas
On Sat, Jul 23, 2005 at 11:30:19AM +0200, Bruno Desthuilliers wrote: > Thanos Tsouanas a écrit : > > class dictobj(dict): > > """ > > class dictobj(dict): > > A dictionary d with an object attached to it, > > which treats d['foo'] as d.obj.foo. > > """ > > def __init__(self,

Re: Getting a dictionary from an object

2005-07-23 Thread Thanos Tsouanas
On Sat, Jul 23, 2005 at 12:06:57PM +0200, Paolino wrote: > use getattr(self.obj,key) possibly, as __getattribute__ gets total > control on attribute access Thanks, but what do you mean by 'total control'? -- Thanos Tsouanas .: My Music: http://www.thanostsouanas.com/ http://thanos.sian

Re: Getting a dictionary from an object

2005-07-23 Thread Paolino
Thanos Tsouanas wrote: > Hello. > > I would like to have a quick way to create dicts from object, so that a > call to foo['bar'] would return obj.bar. > > The following works, but I would prefer to use a built-in way if one > exists. Is there one? > > class dictobj(dict): > """ > class

Re: Getting a dictionary from an object

2005-07-23 Thread Bruno Desthuilliers
Thanos Tsouanas a écrit : > Hello. > > I would like to have a quick way to create dicts from object, so that a > call to foo['bar'] would return obj.bar. > > The following works, but I would prefer to use a built-in way if one > exists. Is there one? > > Thanks in advance. > > class dictobj(di

Getting a dictionary from an object

2005-07-23 Thread Thanos Tsouanas
Hello. I would like to have a quick way to create dicts from object, so that a call to foo['bar'] would return obj.bar. The following works, but I would prefer to use a built-in way if one exists. Is there one? Thanks in advance. class dictobj(dict): """ class dictobj(dict): A dict