Re: which dictionary with attribute-style access?

2009-10-24 Thread Andreas Balogh

baloan wrote:

On Oct 22, 6:34 am, Gabriel Genellina gagsl-...@yahoo.com.ar
wrote:

class AttrDict(dict):
 A dict whose items can also be accessed as member variables.
 def __init__(self, *args, **kwargs):
 dict.__init__(self, *args, **kwargs)
 self.__dict__ = self
 def copy(self):
 return AttrDict(self)
 def __repr__(self):
 return 'AttrDict(' + dict.__repr__(self) + ')'
 @classmethod
 def fromkeys(self, seq, value = None):
 return AttrDict(dict.fromkeys(seq, value))
Looks fine as long as nobody uses an existing method name as adictionary 
key:


py d = AttrDict({'name':'value'})
py d.items()
[('name', 'value')]
py d = AttrDict({'items': [1,2,3]})
py d.items()
Traceback (most recent call last):
   File stdin, line 1, in module
TypeError: 'list' object is not callable

(I should have included a test case for this issue too).
Of course, if this doesn't matter in your application, go ahead and use  
it. Just be aware of the problem.


--
Gabriel Genellina


I see two ways to avoid collisions with existing method names:

1. (easy, reduced functionality) override __setattr__ and __init__,
test for keys matching existing method names, throw an exception if
exists (KeyError).
2. (complex, emulates dict) override __setattr__ and __init__, test
for keys matching existing method names, and store those keys in a
shadow dict. More problems arise when thinking about how to choose
access between dict method names and item keys.

--
Andreas Balogh
baloand at gmail dot com


That was indeed my last post. It seems for this thread Google does not sort correctly 
descending creation time fo the last post. This thread was not visible as new when I 
posted...


Regards, Andreas

--
Andreas Balogh
baloand (at) gmail.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: which dictionary with attribute-style access?

2009-10-22 Thread baloan
On Oct 22, 6:34 am, Gabriel Genellina gagsl-...@yahoo.com.ar
wrote:
  class AttrDict(dict):
       A dict whose items can also be accessed as member variables.
       def __init__(self, *args, **kwargs):
           dict.__init__(self, *args, **kwargs)
           self.__dict__ = self

       def copy(self):
           return AttrDict(self)

       def __repr__(self):
           return 'AttrDict(' + dict.__repr__(self) + ')'

      �...@classmethod
       def fromkeys(self, seq, value = None):
           return AttrDict(dict.fromkeys(seq, value))

 Looks fine as long as nobody uses an existing method name as adictionary 
 key:

 py d = AttrDict({'name':'value'})
 py d.items()
 [('name', 'value')]
 py d = AttrDict({'items': [1,2,3]})
 py d.items()
 Traceback (most recent call last):
    File stdin, line 1, in module
 TypeError: 'list' object is not callable

 (I should have included a test case for this issue too).
 Of course, if this doesn't matter in your application, go ahead and use  
 it. Just be aware of the problem.

 --
 Gabriel Genellina

I see two ways to avoid collisions with existing method names:

1. (easy, reduced functionality) override __setattr__ and __init__,
test for keys matching existing method names, throw an exception if
exists (KeyError).
2. (complex, emulates dict) override __setattr__ and __init__, test
for keys matching existing method names, and store those keys in a
shadow dict. More problems arise when thinking about how to choose
access between dict method names and item keys.

--
Andreas Balogh
baloand at gmail dot com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: which dictionary with attribute-style access?

2009-10-21 Thread Andreas Balogh
Gabriel, thanks for your hint. I've managed to create an implementation of an AttrDict 
passing Gabriels tests.


Any more comments about the pythonicness of this implementation?

class AttrDict(dict):
A dict whose items can also be accessed as member variables.
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
self.__dict__ = self

def copy(self):
return AttrDict(self)

def __repr__(self):
return 'AttrDict(' + dict.__repr__(self) + ')'

@classmethod
def fromkeys(self, seq, value = None):
return AttrDict(dict.fromkeys(seq, value))

--
Andreas Balogh
baloand (at) gmail.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: which dictionary with attribute-style access?

2009-10-21 Thread Gabriel Genellina
En Wed, 21 Oct 2009 18:40:01 -0300, Andreas Balogh balo...@gmail.com  
escribió:


Gabriel, thanks for your hint. I've managed to create an implementation  
of an AttrDict passing Gabriels tests.


Any more comments about the pythonicness of this implementation?

class AttrDict(dict):
 A dict whose items can also be accessed as member variables.
 def __init__(self, *args, **kwargs):
 dict.__init__(self, *args, **kwargs)
 self.__dict__ = self

 def copy(self):
 return AttrDict(self)

 def __repr__(self):
 return 'AttrDict(' + dict.__repr__(self) + ')'

 @classmethod
 def fromkeys(self, seq, value = None):
 return AttrDict(dict.fromkeys(seq, value))



Looks fine as long as nobody uses an existing method name as a dictionary  
key:


py d = AttrDict({'name':'value'})
py d.items()
[('name', 'value')]
py d = AttrDict({'items': [1,2,3]})
py d.items()
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: 'list' object is not callable

(I should have included a test case for this issue too).
Of course, if this doesn't matter in your application, go ahead and use  
it. Just be aware of the problem.


--
Gabriel Genellina

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


Re: which dictionary with attribute-style access?

2009-10-17 Thread Terry Reedy

Aahz wrote:

In article hb01po$s5...@news.eternal-september.org,
Andreas Balogh  balo...@gmail.com wrote:

My question to the Python specialists: which one is the most correct?
Are there restrictions with regards to pickling or copy()?
Which one should I choose?


What's your goal?  I'd probably do the dirt simple myself:

class AttrDict(dict):
def __getattr__(self, attr):
if attr in self:
return self[attr]
else:
raise AttributeError


Why the double lookup? Harking to another thread on using exceptions,

try:
return self[attr]
except KeyError:
raise AttributeError(attr)


def __setattr__(self, attr, value):
self[attr] = value

d = AttrDict()
d.foo = 'bar'
print d.foo


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


Re: which dictionary with attribute-style access?

2009-10-17 Thread Aahz
In article mailman.1614.1255818306.2807.python-l...@python.org,
Terry Reedy  tjre...@udel.edu wrote:
Aahz wrote:
 In article hb01po$s5...@news.eternal-september.org,
 Andreas Balogh  balo...@gmail.com wrote:

 My question to the Python specialists: which one is the most correct?
 Are there restrictions with regards to pickling or copy()?
 Which one should I choose?
 
 What's your goal?  I'd probably do the dirt simple myself:
 
 class AttrDict(dict):
 def __getattr__(self, attr):
 if attr in self:
 return self[attr]
 else:
 raise AttributeError

Why the double lookup? Harking to another thread on using exceptions,

try:
 return self[attr]
except KeyError:
 raise AttributeError(attr)

For this purpose, it's almost entirely a stylistic difference; I happen
to prefer using the test, but the other way is fine, too.
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

To me vi is Zen.  To use vi is to practice zen.  Every command is a
koan.  Profound to the user, unintelligible to the uninitiated.  You
discover truth everytime you use it.  --re...@lion.austin.ibm.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: which dictionary with attribute-style access?

2009-10-16 Thread Aahz
In article hb01po$s5...@news.eternal-september.org,
Andreas Balogh  balo...@gmail.com wrote:

My question to the Python specialists: which one is the most correct?
Are there restrictions with regards to pickling or copy()?
Which one should I choose?

What's your goal?  I'd probably do the dirt simple myself:

class AttrDict(dict):
def __getattr__(self, attr):
if attr in self:
return self[attr]
else:
raise AttributeError

def __setattr__(self, attr, value):
self[attr] = value

d = AttrDict()
d.foo = 'bar'
print d.foo
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

To me vi is Zen.  To use vi is to practice zen.  Every command is a
koan.  Profound to the user, unintelligible to the uninitiated.  You
discover truth everytime you use it.  --re...@lion.austin.ibm.com
-- 
http://mail.python.org/mailman/listinfo/python-list


which dictionary with attribute-style access?

2009-10-12 Thread Andreas Balogh

Hello,

googling I found several ways of implementing a dictionary with 
attribute-style access.


1. ActiveState cookbook: http://code.activestate.com/recipes/473786/

2. ActiveState cookbook: 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/361668


3. web2py codebase: Storage(dict)

I enclosed the three implementations below.

My question to the Python specialists: which one is the most correct?
Are there restrictions with regards to pickling or copy()?
Which one should I choose?

Regards, Andreas

--
Andreas Balogh
baloand (at) gmail.com

---
class AttrDict(dict):
 comments removed 
A dictionary with attribute-style access. It maps attribute 
access to the real dictionary.  

def __init__(self, init={}):
dict.__init__(self, init)

def __getstate__(self):
return self.__dict__.items()

def __setstate__(self, items):
for key, val in items:
self.__dict__[key] = val

def __repr__(self):
return %s(%s) % (self.__class__.__name__, dict.__repr__(self))

def __setitem__(self, key, value):
return super(AttrDict, self).__setitem__(key, value)

def __getitem__(self, name):
return super(AttrDict, self).__getitem__(name)

def __delitem__(self, name):
return super(AttrDict, self).__delitem__(name)

__getattr__ = __getitem__
__setattr__ = __setitem__

def copy(self):
ch = AttrDict(self)
return ch
---
class attrdict(dict):
 comments removed 
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
self.__dict__ = self
---
class Storage(dict):
 comments removed 
def __getattr__(self, key):
try:
return self[key]
except KeyError, k:
return None

def __setattr__(self, key, value):
self[key] = value

def __delattr__(self, key):
try:
del self[key]
except KeyError, k:
raise AttributeError, k

def __repr__(self):
return 'Storage ' + dict.__repr__(self) + ''

def __getstate__(self):
return dict(self)

def __setstate__(self, value):
for (k, v) in value.items():
self[k] = v
--
http://mail.python.org/mailman/listinfo/python-list


Re: which dictionary with attribute-style access?

2009-10-12 Thread Rhodri James
On Mon, 12 Oct 2009 20:58:35 +0100, Andreas Balogh balo...@gmail.com  
wrote:



Hello,

googling I found several ways of implementing a dictionary with  
attribute-style access.


1. ActiveState cookbook: http://code.activestate.com/recipes/473786/

2. ActiveState cookbook:  
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/361668


3. web2py codebase: Storage(dict)

I enclosed the three implementations below.

My question to the Python specialists: which one is the most correct?


Accessing the dictionary as a dictionary.

--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list