Re: [Tutor] question concerning deepcopy
Gregor Lingl wrote: > Hi all of you, > > I've some Vector class, which is a subclass of tuple and which is > working satisfactorily since long in different contexts. Now I've > constructed objects with attributes of Vec-type, which I wanted to > deepcopy. But that doesn't work, because I can't (deep)copy Vec-s: > > >>> from copy import deepcopy > >>> class Vec(tuple): > def __new__(cls, x, y): > return tuple.__new__(cls, (x,y)) > def __abs__(self): > return (self[0]**2+self[1]**2)**0.5 > ## more methods ... > > > >>> a=Vec(3,4) > >>> abs(a) > 5.0 > >>> b = deepcopy(a) > > Traceback (most recent call last): >File "", line 1, in -toplevel- > b = deepcopy(a) >File "C:\Python24\lib\copy.py", line 204, in deepcopy > y = _reconstruct(x, rv, 1, memo) >File "C:\Python24\lib\copy.py", line 336, in _reconstruct > y = callable(*args) >File "C:\Python24\lib\copy_reg.py", line 92, in __newobj__ > return cls.__new__(cls, *args) > TypeError: __new__() takes exactly 3 arguments (2 given) Apparently you need to define __getnewargs__() for your class. This works: def __getnewargs__(self): return (self[0], self[1]) __getnewargs__() is documented with the pickle module but it is used by deepcopy() as well. http://docs.python.org/lib/pickle-inst.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] question concerning deepcopy
Hi all of you, I've some Vector class, which is a subclass of tuple and which is working satisfactorily since long in different contexts. Now I've constructed objects with attributes of Vec-type, which I wanted to deepcopy. But that doesn't work, because I can't (deep)copy Vec-s: >>> from copy import deepcopy >>> class Vec(tuple): def __new__(cls, x, y): return tuple.__new__(cls, (x,y)) def __abs__(self): return (self[0]**2+self[1]**2)**0.5 ## more methods ... >>> a=Vec(3,4) >>> abs(a) 5.0 >>> b = deepcopy(a) Traceback (most recent call last): File "", line 1, in -toplevel- b = deepcopy(a) File "C:\Python24\lib\copy.py", line 204, in deepcopy y = _reconstruct(x, rv, 1, memo) File "C:\Python24\lib\copy.py", line 336, in _reconstruct y = callable(*args) File "C:\Python24\lib\copy_reg.py", line 92, in __newobj__ return cls.__new__(cls, *args) TypeError: __new__() takes exactly 3 arguments (2 given) Explanation? Remedy? Bug? Workaround? Regards, Gregor -- Gregor Lingl Reisnerstrasse 3/19 A-1030 Wien Telefon: +43 1 713 33 98 Mobil: +43 664 140 35 27 Website: python4kids.net ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and Oracle
On 4/27/06, Linda Kvam <[EMAIL PROTECTED]> wrote: I have Oracle 10g and Python 2.4 on my PC. I just installed cx_Oracle andthen entered - import cx_Oracle -in PythonWin and got the following error - >>> import cx_OracleTraceback (most recent call last):File "", line 1, in ?ImportError: DLL load failed: The specified procedure could not be found.>>> This is just a guess. You might need to run regsvr32 to tell WinXP where it is. -Tino ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Setting a global variable on class initialisation
> Sorry I didn't make the problem I'm working with all too clear. Basically > I'm working with a class (Directory) that upon creation reads in a 3 files > and from these files populates one main list. > This bit is fine. > > The trickier bit was that I need to use two of the sublists (e.g the > contents of two of the files) outwith the module. If the lists are data within your Directory then any class that needs to use that data is treading in dubious water. Either that or the data shouldn't be in Directory! Are you sure that there isn't a service that Directory can perform on behalf of your new class? > class is created. The lists are accessed from other classes and although I > tried passing the instance of Directory into the new class (Item) but this > was causing real problems I don't understand why this would cause problems. Its certainly the normal approach to this kind of design scenario. > I noted that I couldn't put my argument for an instance of Directory after > **args (presumably because **args is used to mop up the remaining > arguments Correct, you would need to either treat directory as partrty of the **args stuff or pass it explicitly before them. > as a list) and when I put it before it messed up the reading of the list. And this I don't understand. > So my code now has this format- > >def GetCategories(self): >global platformCategories >platformCategories = self.GetPlatformsFile() >global typeCategories >typeCategories = self.GetTypesFile() > > The method above is within Directory and updates the two lists with the > values I need. (The methods called within GetCategories do the returning > of > the lists.) I'm afraid I don;t see anythoing here that should require a global, but I don't really undestand why passing a Directory instance around wasn't working. Can you explain that part of the problem a bit more? I guess I'd also be interested in why another class needs access to the data in Directory - what is it going to do to the data that Directory couldn't do for it? Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to *really* copy a list
On 28/04/06, John Fouhy <[EMAIL PROTECTED]> wrote: > On 28/04/06, kevin parks <[EMAIL PROTECTED]> wrote: > > In most case you are fine operating on the list in place and altering the > > existing list. In some cases you want your code to stop molesting your poor > > mutables and really honestly sincerly copy the dang thing. In this case i am > > making a function that does odd smmetry mirroring. But i want my orginal > > list > > to remain intact > > > > def mirror(seq): > > """odd symmetry mirroring [1, 2, 3, 4] --> [1, 2, 3, 4, 3, 2, 1]""" > > foo=seq[:-1]# copy list, excluding last > > element for odd symetry > > foo.reverse() # flip it > > seq.extend(foo) > > return seq > > Hi Kevin, > > Your problem is this line: > seq.extend(foo) > > This is the line that mutates your original list. > > There are a few ways you could procede here. One way is to make a > copy of the argument, like this: > > def mirror(seq): > start = list(seq) > end = seq[:-1] > end.reverse() > start.extend(end) > return start > > Notice that we've not calling any methods on seq, so seq won't be > changed. The first line, "start = list(seq)", instructs python to > build a new list out of the elements of seq. You could also write > "start = seq[:]" here --- I'm not sure which is the preferred way. A little 'gotcha' with this is that if you have nested lists, these methods don't copy the nested lists, only the outer list (which makes sense, but can be surprising the first time you encounter it). If for some reason you want to copy nested lists, look into deepcopy(), otherwise you'll be fine. Also, personally I think list(seq) is better than seq[:] because it can cope with anything put into it that it could possibly work with (even dicts and iterators). >>> def i(): ... for n in range(3): ... yield n ... >>> list(i()) [0, 1, 2] >>> >>> d = {'a':1,'b':2} >>> list(d) ['a', 'b'] >>> i()[:] Traceback (most recent call last): File "", line 1, in ? TypeError: unsubscriptable object >>> d[:] Traceback (most recent call last): File "", line 1, in ? TypeError: unhashable type Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Setting a global variable on class initialisation
Hello again Sorry I didn't make the problem I'm working with all too clear. Basically I'm working with a class (Directory) that upon creation reads in a 3 files and from these files populates one main list. This bit is fine. The trickier bit was that I need to use two of the sublists (e.g the contents of two of the files) outwith the module. I didn't want to read the lists in again as I wanted the data to be set at the time the Directory class is created. The lists are accessed from other classes and although I tried passing the instance of Directory into the new class (Item) but this was causing real problems Item was trying to read in the lists: def __init__(self, filename, **args): I noted that I couldn't put my argument for an instance of Directory after **args (presumably because **args is used to mop up the remaining arguments as a list) and when I put it before it messed up the reading of the list. So my code now has this format- def GetCategories(self): #Assign the list values read in via platformCategories and typeCategories global platformCategories platformCategories = self.GetPlatformsFile() global typeCategories typeCategories = self.GetTypesFile() The method above is within Directory and updates the two lists with the values I need. (The methods called within GetCategories do the returning of the lists.) I think I've got a grasp on what was going wrong. ta Max RussellSenior Test Engineer BarcoBonnington Bond, 2 Anderson Place, Edinburgh EH6 5NP, UKTel + 44 (0) 131 472 5731 Fax + 44 (0) 131 472 4799www.barco.com[EMAIL PROTECTED]Unless indicated otherwise, the information contained in this message is privileged and confidential, and is intended only for the use of the addressee(s) named above and others who have been specifically authorized to receive it. If you are not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this message and/or attachments is strictly prohibited. The company accepts no liability for any damage caused by any virus transmitted by this email. Furthermore, the company does not warrant a proper and complete transmission of this information, nor does it accept liability for any delays. If you have received this message in error, please contact the sender and delete the message. Thank you. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor