sorry for this late response, it's just not as simple as I've thought.

2009/11/15 mdipierro <mdipie...@cs.depaul.edu>:
>
> perhaps a __copy__?
> thanks for you help on this.
>
__copy__ is a helper function for copy.copy(), just as __len__ is for
len(). Copy module is needed anyway.

> On Nov 15, 3:34 am, "Xie&Tian" <mft...@gmail.com> wrote:
>> It's nothing wrong with using copy module of standard python, I just
>> find it more convenient with a .copy() method. And plus, since Storage
>> inherits dict, developer can call .copy() and expect a Storage object
>> returned. Currently it returns dict and that breaks the coherence of
>> the behaviour of Storage.
>>
>> Sorry I didn't test the patch thoroughly. I'll post another later.
>>
>> 2009/11/15 mdipierro <mdipie...@cs.depaul.edu>:
>>
>>
>>
>>
>>
>> > I like idea but there is a problem. It break backward compatibility
>> > for apps that use a session variable named 'copy'
>>
>> >>>> class A(dict):
>> > ...     def __getattr__(self,key): return self[key]
>> > ...     def __setattr__(self,key,value): self[key]=value
>> > ...     def copy(self): return self
>> >>>> a=A()
>> >>>> a.b=5
>> >>>> a.b
>> > 5
>> >>>> a.copy()
>> > {'b': 5}
>> >>>> a.copy=6
>> >>>> a.copy
>> > <bound method A.copy of {'copy': 6, 'b': 5}>
>> > should be 6 for backward compatibility
>>

if you start web2py by --shell (iPython as console, without my patch),
you can try this:

In [1]: from gluon.storage import Storage
In [2]: a = Storage()
In [3]: a.copy = 3
In [4]: a.copy
Out[4]: <built-in method copy of Storage object at 0x8eb277c>

Obviously Storage object isn't that backward compatible either. But
the interesting part is that dict itself DOES NOT allow copy method be
assigned a new value:

In [20]: b = dict()
In [21]: b.copy = 5
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/xie/local/web2py-read-only/<ipython console> in <module>()
AttributeError: 'dict' object attribute 'copy' is read-only

So, to achieve real compatibility with dict, Storage.copy needs be
read-only. I tried the following updated patch to make it read-only
but didn't work:

--- storage.py  2009-11-13 17:47:25.000000000 +0800
+++ storage.py  2009-11-19 23:30:28.000000000 +0800
@@ -80,6 +80,12 @@
         for (k, v) in value.items():
             self[k] = v

+    def __copy(self):
+        self_ = Storage()
+        for k in self.keys():
+            self_[k] = self[k]
+        return self_
+    copy = property(lambda self: self.__copy)

 def load_storage(filename):
     fp = open(filename, 'rb')


Any ideas?

-- 
Luyun Xie
谢路云
http://magefromhell.blogspot.com/
(http://blog.hellmage.info/)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to