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
self.c = c
def __getitem__(self, ky):
return self.__dict__[ky]


 >>> a = A(1,2,3)
 >>> a['a']
1
 >>> a['b']
2
 >>>

> 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 dictionary d with an object attached to it,
>   which treats d['foo'] as d.obj.foo.
> """
> def __init__(self, obj):
> self.obj = obj
> def __getitem__(self, key):
> return self.obj.__getattribute__(key)
> 

-- 
http://mail.python.org/mailman/listinfo/python-list


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.
> 
> But not SQLObject's objects...
> Any idea why? (Getting attribute errors, it seems that these
> "attributoids" are not listed in dir(obj), so i have to use my ugly
> dictobj class.. :(

I don't know how SQLObjects are implemented, but I'm guessing they use 
__getattr__ or __getattribute__:

py> class C(object):
... w = 1
... @property
... def x(self):
... return 2
... def __init__(self):
... self.y = 3
... def __getattr__(self, name):
... if name == 'z':
... return 4
...
py> c = C()
py> d = dict((name, getattr(c, name)) for name in dir(c))
py> d['w'], d['x'], d['y']
(1, 2, 3)
py> d['z']
Traceback (most recent call last):
   File "", line 1, in ?
KeyError: 'z'

Any attribute simulated through __getattr__ or __getattribute__ cannot 
be found by dir():

py> dir(c)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattr__', 
'__getattribute__', '__hash__', '__init__', '__module__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 
'__weakref__', 'w', 'x', 'y']

For this reason, I try to avoid implementing attributes through these 
methods, but sometimes it's unavoidable.

STeVe
-- 
http://mail.python.org/mailman/listinfo/python-list


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 idea why? (Getting attribute errors, it seems that these
"attributoids" are not listed in dir(obj), so i have to use my ugly
dictobj class.. :(

-- 
Thanos Tsouanas  .: My Music: http://www.thanostsouanas.com/
http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


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] at Steven's masterstroke which
> came out of the blue and trumped all previous efforts -- a true "deus ex
> machina", a thunderbolt from Olympus -- or if you want a one-word
> colloquialism, he's gobsmacked.
> 
> The -ly'rs part means that in his shocked state he has tried to emulate
> the timbot's characteristic sign-off [2], but failed to get the syntax
> correct.
> 

one-hundred-percent-correct-ly y'rs !-)


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])"
-- 
http://mail.python.org/mailman/listinfo/python-list


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 blue and trumped all previous efforts -- a true "deus ex 
machina", a thunderbolt from Olympus -- or if you want a one-word 
colloquialism, he's gobsmacked.

The -ly'rs part means that in his shocked state he has tried to emulate 
the timbot's characteristic sign-off [2], but failed to get the syntax 
correct.

[1] Billy Connolly
[2] http://www.python.org/tim_one/ ... or just check out a few postings 
by Tim Peters in this newsgroup.
-- 
http://mail.python.org/mailman/listinfo/python-list


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 it fails to grab all obj.foo's, some of them being properties,
> etc.

How about something like:
 dict((name, getattr(obj, name)) for name in dir(obj))

For example:

py> class C(object):
... x = 1
... @property
... def y(self):
... return 2
... def __init__(self):
... self.z = 3
...
py> c = C()
py> d = dict((name, getattr(c, name)) for name in dir(c))
py> d['x']
1
py> d['y']
2
py> d['z']
3

Looks like this will get instance attributes, class attributes and 
properties just fine.

STeVe
-- 
http://mail.python.org/mailman/listinfo/python-list


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 all obj.foo's, some of them being properties,
>>etc.
> 
(snip)
 > I don't think you are correct. As far as I can see, properties do have
> an entry in obj.__dict__ the same as other attributes, although there is
> certainly some strangeness going on with properties.
> 
> Using the sample code from here:
> http://www.python.org/2.2.3/descrintro.html#property
> 
> class C(object):
> def __init__(self):
> self.__x = 0
> def getx(self):
> return self.__x
> def setx(self, x):
> if x < 0: x = 0
> self.__x = x
> x = property(getx, setx)
> 
> I see _C__x in C().__dict__, exactly as expected. (snip)

Yes, but you don't see 'x'.  Poking into the object's __dict__ would 
defeat the whole point of computed attributes - which by the way need 
not directly map to a protected or private variable, nor even be 
properties (think: descriptors).

> I can't see any way to inspect a Python object and get a list of
> properties, 

I do :

def list_properties(obj):
 proptype = type(property()) # not defined in types
 klass = obj.__class__
 names = dir(klass) # so we get inherited attribs as well
 d  = dict([(name, getattr(klass, name)) for name in names])
 return [name for name, attrib in d.items() \
 if type(attrib) is proptype]



Note that this won't find all descriptors (I've tried and it really 
harder... there are a lot of things in a class.__dict__ that have a 
__get__() method, most of'em not defined in the types module).

Anyway, you won't need it... (I mean, the OP don't need it to solve it's 
problem)

> so you might have to keep your own list: add a class-attribute
> of your object which keeps a list of all the properties:
> 
> class Obj:
> # various methods, attributes and properties
> ...
> # keep a list of special properties that don't show 
> # up correctly in __dict__
> special = ['foo', 'bar']
> 
> # now define a special method that makes a copy of 
> # __dict__ and adds special properties to it
> 
> def objdict(self):
> D = self.__dict__.copy()
> # assume shallow copy is enough
> for property_name in self.special:
> D[property_name] = self.__getattribute__(property_name)
> return D
> 
> then call it when you need it:
> 
> print "My object has fields %(foo)s and %(bar)s." % obj.objdict()

This means adding responsabilities to the class when the need is 
obviously orthogonal to the class's responsabilities. Implementing a 
generic decorator pattern in Python does'nt require more code, doesn't 
requires the class nor the object to be modified at all, is probably 
more robust, and  is, well... more generic !-)  (should I say 'more 
pythonic' ?)

> It would be nice to see an easier way to introspect objects and get
> a list of properties.

You're dream is now reality. Now ain't *that* nice ?-)

Bruno
-- 
http://mail.python.org/mailman/listinfo/python-list


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 use it something like this:
> 
> print "My object has fields %(foo)s; %(bar)s; %(spam)s; %(eggs)s." % obj
> 
> except that doesn't work. So I would simply change the reference to obj to
> obj.__dict__ and it should do exactly what you want.

Nope, it doesn't work with computed attributes (properties, descriptors, 
...).

The most obvious solution is the decorator pattern - which is somewhat 
the op was trying to do.

Another solution is to dynamically add a __getitem__ method to obj 
before using'em that way, either by setting explicitly the method as an 
attribute of the object's class or by using the import_with_metaclass() 
trick from David Mertz.

(snip)

> It really does help to explain what your functional requirements are,
> instead of focusing on one, possibly pointless, implementation.
> 
+1 on this !-)
-- 
http://mail.python.org/mailman/listinfo/python-list


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 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 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 it fails to grab all obj.foo's, some of them being properties,
> etc.

You could have mentioned this earlier.

But I don't think you are correct. As far as I can see, properties do have
an entry in obj.__dict__ the same as other attributes, although there is
certainly some strangeness going on with properties.

Using the sample code from here:
http://www.python.org/2.2.3/descrintro.html#property

class C(object):
def __init__(self):
self.__x = 0
def getx(self):
return self.__x
def setx(self, x):
if x < 0: x = 0
self.__x = x
x = property(getx, setx)

I see _C__x in C().__dict__, exactly as expected. (The _C is Python's
standard name mangling of "semi-private" attributes starting with double
underscores.)

I can't see any way to inspect a Python object and get a list of
properties, so you might have to keep your own list: add a class-attribute
of your object which keeps a list of all the properties:

class Obj:
# various methods, attributes and properties
...
# keep a list of special properties that don't show 
# up correctly in __dict__
special = ['foo', 'bar']

# now define a special method that makes a copy of 
# __dict__ and adds special properties to it

def objdict(self):
D = self.__dict__.copy()
# assume shallow copy is enough
for property_name in self.special:
D[property_name] = self.__getattribute__(property_name)
return D

then call it when you need it:

print "My object has fields %(foo)s and %(bar)s." % obj.objdict()


It would be nice to see an easier way to introspect objects and get
a list of properties.


-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


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 thing you use dictobj objects for is to
>> print them?
> 
> I'm sorry to disappoint you, but yes.  When you have a long text
> template to fill-out, with lots of %(foo)s, and all those foos are
> attributes of an object, it really helps to have dictobj.

Ah, now we're making progress in finding out what the purpose of the
dictobj is! Thank you, this is starting to become clearer now.

>> I don't think so. I do know how to print an object, amazingly.
> 
> 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 use it something like this:

print "My object has fields %(foo)s; %(bar)s; %(spam)s; %(eggs)s." % obj

except that doesn't work. So I would simply change the reference to obj to
obj.__dict__ and it should do exactly what you want.

Does that help?

[snip]
> Because *obviously* I don't know of these indexing and attribute
> grabbing machineries you are talking about in my case.  If you cared to
> read my first post, all I asked was for the "normal", "built-in" way to
> do it.  Now, is there one, or not?

I did read your first post. Unfortunately, you had not explained what you
were trying to do very well. Your initial solution involved sub-classing
dict. I made the fatal mistake of trying to guess what you needed from
your sample code -- a natural mistake to make, given how vague your
requirements were. Or rather, non-existent.

It really does help to explain what your functional requirements are,
instead of focusing on one, possibly pointless, implementation.

If I have understood your functional requirements correctly, you don't
need "to have a quick way to create dicts from object, so that a call to
foo['bar'] would return obj.bar" at all.



-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


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 getattr(obj, name)
> 
> setattr(obj.__class__, '__getitem__', mygetitem)
> obj['bar']

I used what you suggested earlier with the Wrapper, without subclassing
dict anymore.  Thanks!

> 
> BTW, parts of this thread should remind us all that it's usually better 
> to clearly describe the *problem* before asking for comments on the 
> solution...
> 

"""I would like to have a quick way to create dicts from object, so
that a call to foo['bar'] would return obj.bar."""

Actually this is the problem, (I never said anything about _assigning_
new keys in foo), and the line following it is my question ;)

Thanks again!

-- 
Thanos Tsouanas  .: My Music: http://www.thanostsouanas.com/
http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


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 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 getattr(obj, name)

setattr(obj.__class__, '__getitem__', mygetitem)
obj['bar']



BTW, parts of this thread should remind us all that it's usually better 
to clearly describe the *problem* before asking for comments on the 
solution...


My 2 cents...
Bruno

-- 
http://mail.python.org/mailman/listinfo/python-list


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 how you use the rest of the
> functionality of the dictobj, instead of taking my words out of context
> and giving an inane answer.
> 
> 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 the dict __getitem__ method so
> that you can no longer retrieve items from the dict?
> 

The idea of the OP is not to use the dictobj as a full fledged dict,
just to wrap the obj in something that is dict-like enough to be used
for "%(attname)s" formatting. I also assume that he doesn't want to 
manually alter the code of each and every class to achieve this !-)

So we can certainly agree that subclassing dict here is overkill and a 
bit misleading, but there are probably better ways to express this 
feeling. Of course, it would have been simpler if the OP had tell us 
from the start what was it's use case, but what...

One could of course use metaclass tricks and the like to customize the 
objects __str__ or __repr__ (as in David Mertz's gnosis.magic package), 
but that would be overkill too IMHO.

The plain old Decorator[1] pattern is probably enough in this case, 
since it's quite easy to implement a generic Decorator in Python. 
Another solution could be to dynamically modify the to-be-wrapped 
object's class to add a __getitem__ method.





-- 
http://mail.python.org/mailman/listinfo/python-list


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 the wrapped object), you can add this:

 def __getattr__(self, name):
 return getattr(self._obj, name)

 def __setattr__(self, name, val):
 if name == '_obj':
 super(Wrapper, self).__setattr__(name, val)
 else:
 setattr(self._obj, name, val)

The Python cookbook may have some receipes too for this kind of funny 
things...
-- 
http://mail.python.org/mailman/listinfo/python-list


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.  If you cared to
> > read my first post, all I asked was for the "normal", "built-in" way to
> > do it.  Now, is there one, or not?
> 
> If you re-read your first post, you'll notice that you didn't say 
> anything about the intention, only about implementation !-)

"""The following works, but I would prefer to use a built-in way if one
exists.  Is there one?"""
 
> Now if your *only* need is to access object as a dict for formated 
> output, you don't need to subclass dict. This is (well, should be) enough:
> 
> class Wrapper(object):
>  def __init__(self, obj):
>  self._obj = obj
>  def __getitem__(self, name):
>  return getattr(self._obj, name)
> 
> This works with 'normal' attributes as well as with properties. Notice 
> that this wrapper is read-only, and don't pretend to be a real 
> dictionnary - but still it implements the minimum required interface for 
> "%(attname)s" like formatting.

Thanks!!  You made clear what 'the extra functionality' was.  Indeed
there is no need to subclass dict...

> HTH

it does!

> Bruno
> -- 
> http://mail.python.org/mailman/listinfo/python-list

-- 
Thanos Tsouanas  .: My Music: http://www.thanostsouanas.com/
http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


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 the dict __getitem__ method so
>>that you can no longer retrieve items from the dict?

> 
> Because *obviously* I don't know of these indexing and attribute
> grabbing machineries you are talking about in my case.  If you cared to
> read my first post, all I asked was for the "normal", "built-in" way to
> do it.  Now, is there one, or not?

If you re-read your first post, you'll notice that you didn't say 
anything about the intention, only about implementation !-)

Now if your *only* need is to access object as a dict for formated 
output, you don't need to subclass dict. This is (well, should be) enough:

class Wrapper(object):
 def __init__(self, obj):
 self._obj = obj
 def __getitem__(self, name):
 return getattr(self._obj, name)

This works with 'normal' attributes as well as with properties. Notice 
that this wrapper is read-only, and don't pretend to be a real 
dictionnary - but still it implements the minimum required interface for 
"%(attname)s" like formatting.

HTH
Bruno
-- 
http://mail.python.org/mailman/listinfo/python-list


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.  Is there one?
> 
> 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 it fails to grab all obj.foo's, some of them being properties,
etc.

vars() is good to know though, Thanks!

-- 
Thanos Tsouanas  .: My Music: http://www.thanostsouanas.com/
http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


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 disappoint you, but yes.  When you have a long text
template to fill-out, with lots of %(foo)s, and all those foos are
attributes of an object, it really helps to have dictobj.

> I don't think so. I do know how to print an object, amazingly.

Please, tell me, how would you print it in my case?

> Perhaps you would like to explain how you use the rest of the
> functionality of the dictobj, instead of taking my words out of context
> and giving an inane answer.

I dont see _ANY_ other functionality in the dictobj class.  Do you?

> 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 the dict __getitem__ method so
> that you can no longer retrieve items from the dict?

Because *obviously* I don't know of these indexing and attribute
grabbing machineries you are talking about in my case.  If you cared to
read my first post, all I asked was for the "normal", "built-in" way to
do it.  Now, is there one, or not?
 
-- 
Thanos Tsouanas  .: My Music: http://www.thanostsouanas.com/
http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


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 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 using the dictionary at all?
>> 
>> > [...]
>> 
>> I don't think this is particularly useful behaviour. How do you use it?
> 
> 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 don't think so. I do know how to print an object, amazingly.

Perhaps you would like to explain how you use the rest of the
functionality of the dictobj, instead of taking my words out of context
and giving an inane answer.

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 the dict __getitem__ method so
that you can no longer retrieve items from the dict?


-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


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(dict):
... _format = "Thing %s is good."
... def __str__(self):
... return self._format % self
...
>>> X = Thing()
>>> X  # calls __repr__ so is safe
{}
>>> str(X)  # not safe
  File "", line 4, in __str__
  File "", line 4, in __str__
  ...
  File "", line 4, in __str__
  File "", line 4, in __str__
RuntimeError: maximum recursion depth exceeded


-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


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 looked at the 
builtin "vars()"?

STeVe
-- 
http://mail.python.org/mailman/listinfo/python-list


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 me. Why not just call obj.bar, since it
> doesn't look like you are actually using the dictionary at all?
> 
> > [...]
> 
> I don't think this is particularly useful behaviour. How do you use it?

print foo %do

where do is a dictobj object...

-- 
Thanos Tsouanas  .: My Music: http://www.thanostsouanas.com/
http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


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.bar, since it
> doesn't look like you are actually using the dictionary at all?

Well, I needed exactly this functionality last week. I have a
collection of (rather messy) classes that have a slew of attributes as
values. I would have used a dictionary for this, but I didn't write
the code.

I have to be able to display these objects (in HTML, if it matters),
and have as a requirement that the format string live in a database.

My solution didn't look to different from dictobj. There's some extra
mechanism to fetch the format string from the database, and some
formatting of the attribute based on meta-information in the object,
but it's the same basic idea.

>> 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, obj):
>> self.obj = obj
>> def __getitem__(self, key):
>> return self.obj.__getattribute__(key)
>
> I don't think this is particularly useful behaviour. How do you use it?

def __str__(self):
return self._format % self


  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


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 using the dictionary at all?

> 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 dictionary d with an object attached to it,
>   which treats d['foo'] as d.obj.foo.
> """
> def __init__(self, obj):
> self.obj = obj
> def __getitem__(self, key):
> return self.obj.__getattribute__(key)

I don't think this is particularly useful behaviour. How do you use it?

py> D = dictobj("hello world")
py> D
{}
py> D.obj
'hello world'
py> D["food"] = "spam"
py> D
{'food': 'spam'}
py> D["food"]
Traceback (most recent call last):
  File "", line 1, in ?
  File "", line 5, in __getitem__
AttributeError: 'str' object has no attribute 'food'


-- 
Steven.


-- 
http://mail.python.org/mailman/listinfo/python-list


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 black voodoo mumbo jumbo 
juju magic method...


More seriously:
__getattr__() (if defined) is called when and if the 'normal' attribute 
lookup process failed. It's main use is to delegate attribute access to 
a proxied/wrapped/decorated object.
__getattribute__() (if defined) completely override the attribute lookup 
process - so it gets full control over attribute access.

This does not directly concern what you're doing now, but still, for a 
number of reasons, the canonical pythonic way to do named attribute 
access is with the builtin getattr() function, that will call on the 
appropriate lookup mechanism.

As a general rule, '__magic__' methods are implementation, not API, so 
you shouldn't call on them directly unless you have a relly god 
reason to do so.

HTH
Bruno

-- 
http://mail.python.org/mailman/listinfo/python-list


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:
 >>> class A(object):
...   def __getattr__(self,attr):
... return 'whatever'
...
 >>> a=A()
 >>> a.b
'whatever'
 >>> getattr(a,'b')
'whatever'
 >>> a.__getattribute__('b')
Traceback (most recent call last):
   File "", line 1, in ?
AttributeError: 'A' object has no attribute 'b'
 >>>

This can probably touch you if your objects uses defualt searching for 
attributes.






___ 
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB 
http://mail.yahoo.it
-- 
http://mail.python.org/mailman/listinfo/python-list


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, obj):
> > self.obj = obj
> > def __getitem__(self, key):
> > return self.obj.__getattribute__(key)
> 
> I'd replace this last line with:
>return getattr(self.obj, key)
> 
> Now given your specs, I don't see what's wrong with your solution.

I just dont want to use my class, if one already exists in the
libraries (or any other way to achieve the same thing), that's all ;)

Thanks for the tip.

-- 
Thanos Tsouanas  .: My Music: http://www.thanostsouanas.com/
http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


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.sians.org/ .: Sians Music: http://www.sians.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


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 dictobj(dict):
> A dictionary d with an object attached to it,
>   which treats d['foo'] as d.obj.foo.
> """
> def __init__(self, obj):
> self.obj = obj
> def __getitem__(self, key):
> return self.obj.__getattribute__(key)
use getattr(self.obj,key) possibly, as __getattribute__ gets total 
control on attribute access





___ 
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB 
http://mail.yahoo.it
-- 
http://mail.python.org/mailman/listinfo/python-list


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(dict):
> """
> class dictobj(dict):
> A dictionary d with an object attached to it,
>   which treats d['foo'] as d.obj.foo.
> """
> def __init__(self, obj):
> self.obj = obj
> def __getitem__(self, key):
> return self.obj.__getattribute__(key)

I'd replace this last line with:
   return getattr(self.obj, key)


Now given your specs, I don't see what's wrong with your solution.
-- 
http://mail.python.org/mailman/listinfo/python-list