Re: Swapping Content of Two Dictionaries.

2010-03-17 Thread Steven D'Aprano
On Wed, 17 Mar 2010 13:34:51 +0100, Peter Otten wrote: > Hatem Oraby wrote: > >> Hello, I want to swap the content of two dictionaries, the obvious way >> to do it is: >> a = {1:"I'am A"} >> b = {2:"I'm B"} >> temp = a >> a = b >> b = temp > > That can be simplified to > > a, b = b, a > > and

Re: Swapping Content of Two Dictionaries.

2010-03-17 Thread Stefan Behnel
Hatem Oraby, 17.03.2010 12:26: However, consider the case in which the dictionary we are referencing lives in another module: #external.py # #a = {1:"I'am A} import external temp = external.a external.a = b b = temp Looks like the interface of your module is broken. It shouldn't export the tw

Re: Swapping Content of Two Dictionaries.

2010-03-17 Thread Peter Otten
Hatem Oraby wrote: > Hello, I want to swap the content of two dictionaries, the obvious way to > do it is: > a = {1:"I'am A"} > b = {2:"I'm B"} > temp = a > a = b > b = temp That can be simplified to a, b = b, a and is almost certainly the right approach. > tempKeys = a.keys() > diffKeys = a.k

Re: Swapping superclass from a module

2009-05-17 Thread Terry Reedy
Peter Otten wrote: Terry Reedy wrote: If the names of superclasses is resolved when classes are instantiated, the patching is easy. If, as I would suspect, the names are resolved when the classes are created, before the module becomes available to the importing code, then much more careful a

Re: Swapping superclass from a module

2009-05-17 Thread Emanuele D'Arrigo
Wow, thank you all. Lots of ideas and things to try! I wish I knew which one is going to work best. The module I'm trying to (monkey!) patch is pxdom, and as it is a bit long (5700 lines of code in one file!) I'm not quite sure if the simplest patching method will work or the more complicated o

Re: Swapping superclass from a module

2009-05-17 Thread Peter Otten
Terry Reedy wrote: > Steven D'Aprano wrote: >> On Sat, 16 May 2009 09:55:39 -0700, Emanuele D'Arrigo wrote: >> >>> Hi everybody, >>> >>> let's assume I have a module with loads of classes inheriting from one >>> class, from the same module, i.e.: >> [...] >>> Now, let's also assume that myFile.py

Re: Swapping superclass from a module

2009-05-17 Thread Michele Simionato
Try this: class Base(object): pass class C(Base): pass class NewBase(object): pass C.__bases__ = (NewBase,) help(C) -- http://mail.python.org/mailman/listinfo/python-list

Re: Swapping superclass from a module

2009-05-16 Thread Terry Reedy
Steven D'Aprano wrote: On Sat, 16 May 2009 09:55:39 -0700, Emanuele D'Arrigo wrote: Hi everybody, let's assume I have a module with loads of classes inheriting from one class, from the same module, i.e.: [...] Now, let's also assume that myFile.py cannot be changed or it's impractical to do

Re: Swapping superclass from a module

2009-05-16 Thread Steven D'Aprano
On Sat, 16 May 2009 09:55:39 -0700, Emanuele D'Arrigo wrote: > Hi everybody, > > let's assume I have a module with loads of classes inheriting from one > class, from the same module, i.e.: [...] > Now, let's also assume that myFile.py cannot be changed or it's > impractical to do so. Is there a w

Re: Swapping superclass from a module

2009-05-16 Thread Arnaud Delobelle
"Emanuele D'Arrigo" writes: > On May 16, 8:17 pm, Arnaud Delobelle wrote: >> # Insert Wedge into each subclass of modfoo.Base >> for subclass in modfoo.Base.__subclasses__(): >>     if subclass.__module__ != 'modfoo': continue >>     attrs = dict(item for item in subclass.__dict__.items() >>    

Re: Swapping superclass from a module

2009-05-16 Thread Emanuele D'Arrigo
On May 16, 8:17 pm, Arnaud Delobelle wrote: > # Insert Wedge into each subclass of modfoo.Base > for subclass in modfoo.Base.__subclasses__(): >     if subclass.__module__ != 'modfoo': continue >     attrs = dict(item for item in subclass.__dict__.items() >                       if item[0][:2] !=

Re: Swapping superclass from a module

2009-05-16 Thread Arnaud Delobelle
"Emanuele D'Arrigo" writes: > Hi everybody, > > let's assume I have a module with loads of classes inheriting from one > class, from the same module, i.e.: > > ## myFile.py > class SuperClass(object) > class SubClass1(SuperClass) > class SubClass2(SuperClass) > class SubClass3(SuperClass) > > In

Re: Swapping values of two variables

2009-01-30 Thread Grant Edwards
On 2009-01-31, Aahz wrote: >>But could the swapping be done using less extra memory than this? What >>is the minimum amount of extra memory required to exchange two 32-bit >>quantities? What would be the pseudocode that achieves this minimum? > > This looks like a homework problem to me It l

Re: Swapping values of two variables

2009-01-30 Thread Aahz
In article , Eric Kang wrote: > >In python, I set: > >x=1 >y=3 > >z = x >x = y >y = z > > >This gave me 3 1, which are the values of x and y swapped. >The following would have given me the same result: >x, y = y, x > > > >But could the swapping be done using less extra memory than this? What >is

Re: Swapping values of two variables

2009-01-30 Thread Grant Edwards
> Grant Edwards wrote: > > On 2009-01-30, MRAB wrote: > > > >>> What is the minimum amount of extra memory required to exchange two > >>> 32-bit quantities? What would be the pseudocode that achieves this > >>> minimum? > >> x ^= y > >> y ^= x > >> x ^= y > >> > >> This is really only of use when

Re: Swapping values of two variables

2009-01-30 Thread Christian Heimes
Steven D'Aprano schrieb: > Ints in Python are *objects*, not 32-bit quantities. An int is 12 bytes > (96 bits) in size; a long will use as much memory as needed. If your > application needs to optimize a swap of two ints, then Python is probably > going to be much too memory-intensive for you.

Re: Swapping values of two variables

2009-01-30 Thread MRAB
Grant Edwards wrote: > On 2009-01-30, MRAB wrote: > >>> What is the minimum amount of extra memory required to exchange two >>> 32-bit quantities? What would be the pseudocode that achieves this >>> minimum? >> x ^= y >> y ^= x >> x ^= y >> >> This is really only of use when working in assembly l

Re: Swapping values of two variables

2009-01-29 Thread Steven D'Aprano
On Thu, 29 Jan 2009 21:23:48 -0800, Kottiyath wrote: > Is it possible to swap two floats without a variable? In Python? Sure. f = 1.23 g = 2.87 f, g = g, f This idiom is independent of the types of the objects: x = "hello world" y = [1, 2.0, None, "xyz", {}] x, y = y, x In other languages?

Re: Swapping values of two variables

2009-01-29 Thread tony . clarke5
On Jan 30, 3:31 am, Steven D'Aprano wrote: > On Thu, 29 Jan 2009 17:50:04 -0800, tony.clarke5 wrote: > > On Jan 30, 12:29 am, Eric Kang wrote: > >> In python, I set: > > >> x=1 > >> y=3 > > >> z = x > >> x = y > >> y = z > > >> This gave me 3 1, which are the values of x and y swapped. The > >> f

Re: Swapping values of two variables

2009-01-29 Thread Grant Edwards
On 2009-01-30, MRAB wrote: >> What is the minimum amount of extra memory required to exchange two >> 32-bit quantities? What would be the pseudocode that achieves this >> minimum? > > x ^= y > y ^= x > x ^= y > > This is really only of use when working in assembly language. And rarely then. ;)

Re: Swapping values of two variables

2009-01-29 Thread Kottiyath
On Jan 30, 8:31 am, Steven D'Aprano wrote: > On Thu, 29 Jan 2009 17:50:04 -0800, tony.clarke5 wrote: > > On Jan 30, 12:29 am, Eric Kang wrote: > >> In python, I set: > > >> x=1 > >> y=3 > > >> z = x > >> x = y > >> y = z > > >> This gave me 3 1, which are the values of x and y swapped. The > >> f

Re: Swapping values of two variables

2009-01-29 Thread Steven D'Aprano
On Thu, 29 Jan 2009 17:50:04 -0800, tony.clarke5 wrote: > On Jan 30, 12:29 am, Eric Kang wrote: >> In python, I set: >> >> x=1 >> y=3 >> >> z = x >> x = y >> y = z >> >> This gave me 3 1, which are the values of x and y swapped. The >> following would have given me the same result: x, y = y, x >>

Re: Swapping values of two variables

2009-01-29 Thread Steven D'Aprano
On Thu, 29 Jan 2009 16:29:11 -0800, Eric Kang wrote: > In python, I set: > > x=1 > y=3 > > z = x > x = y > y = z > > > This gave me 3 1, which are the values of x and y swapped. The following > would have given me the same result: x, y = y, x Yes. > But could the swapping be done using less

Re: Swapping values of two variables

2009-01-29 Thread tony . clarke5
On Jan 30, 12:29 am, Eric Kang wrote: > In python, I set: > > x=1 > y=3 > > z = x > x = y > y = z > > This gave me 3 1, which are the values of x and y swapped. > The following would have given me the same result: > x, y = y, x > > But could the swapping be done using less extra memory than this?

Re: Swapping values of two variables

2009-01-29 Thread MRAB
Eric Kang wrote: In python, I set: x=1 y=3 z = x x = y y = z This gave me 3 1, which are the values of x and y swapped. The following would have given me the same result: x, y = y, x But could the swapping be done using less extra memory than this? What is the minimum amount of extra memory

Re: swapping

2007-08-20 Thread Gabriel Genellina
En Sat, 18 Aug 2007 02:58:23 -0300, Beema shafreen <[EMAIL PROTECTED]> escribi�: > result: > klp5bub1 > > apn1apn2 > > but i have do the same for the revere ,to check the result like this for > eg: > apn2apn1 > what is the concept to do this I don't understand exactly what you want

Re: swapping numeric items in a list

2006-08-23 Thread Jiang Nutao
>; Sent: Wednesday, August 23, 2006 12:19 PM Subject: Re: swapping numeric items in a list > At Wednesday 23/8/2006 15:32, Jiang Nutao wrote: > >>This is what I got in the debugger: >> >>(Pdb) aa=array('b', [126, 55, 71, 112]) >>(Pdb) aa >>array(&#x

Re: swapping numeric items in a list

2006-08-23 Thread Gabriel Genellina
At Wednesday 23/8/2006 15:32, Jiang Nutao wrote: This is what I got in the debugger: (Pdb) aa=array('b', [126, 55, 71, 112]) (Pdb) aa array('b', [126, 55, 71, 112]) (Pdb) aa.byteswap() (Pdb) aa array('b', [126, 55, 71, 112]) Oh, sorry, to swap by two bytes "H" was the right typecode. But your

Re: swapping numeric items in a list

2006-08-23 Thread Fredrik Lundh
Jiang Nutao wrote: > array.byteswap() won't work for me easily. I tried this before my 1st post. > I defined > > aa = array('H', [0x12, 0x34, 0x56, 0x78]) > > Then did byteswap aa.byteswap(). The result was > > array('H', [0x1200, 0x3400, 0x5600, 0x7800]) > > You can see it byteswappe

Re: swapping numeric items in a list

2006-08-23 Thread Jiang Nutao
]> To: "Jiang Nutao" <[EMAIL PROTECTED]> Cc: Sent: Wednesday, August 23, 2006 11:28 AM Subject: Re: swapping numeric items in a list > At Wednesday 23/8/2006 14:44, Jiang Nutao wrote: > >>array.byteswap() won't work for me easily. I tried this before my 1st >&g

Re: swapping numeric items in a list

2006-08-23 Thread Gabriel Genellina
At Wednesday 23/8/2006 14:44, Jiang Nutao wrote: array.byteswap() won't work for me easily. I tried this before my 1st post. I defined aa = array('H', [0x12, 0x34, 0x56, 0x78]) Then did byteswap aa.byteswap(). The result was array('H', [0x1200, 0x3400, 0x5600, 0x7800]) You can see it

Re: swapping numeric items in a list

2006-08-23 Thread Jiang Nutao
Thank you all guys for the help. Guess I'm gonna pick bearophile's way. It's fast, neat, and easy to read. array.byteswap() won't work for me easily. I tried this before my 1st post. I defined aa = array('H', [0x12, 0x34, 0x56, 0x78]) Then did byteswap aa.byteswap(). The result was ar

Re: swapping numeric items in a list

2006-08-23 Thread Boris Borcic
Jiang Nutao wrote: > Hi, > > I simplify my problem like below > > To convert list > aa = [0x12, 0x34, 0x56, 0x78] > into > [0x34, 0x12, 0x78, 0x56] > > How to do it fast? My real list is huge. Mark Rintsch's suggestion appears best if applicable, but just to cite yet other ways to do

Re: swapping numeric items in a list

2006-08-23 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Jiang Nutao wrote: > To convert list > aa = [0x12, 0x34, 0x56, 0x78] > into > [0x34, 0x12, 0x78, 0x56] > > How to do it fast? My real list is huge. Use the `array` module and the `array.byteswap()` method. Ciao, Marc 'BlackJack' Rintsch -- http://mail.py

Re: swapping numeric items in a list

2006-08-22 Thread bearophileHUGS
Jiang Nutao: > To convert list > aa = [0x12, 0x34, 0x56, 0x78] > into > [0x34, 0x12, 0x78, 0x56] > How to do it fast? My real list is huge. Note that: >>> a = range(6) >>> a [0, 1, 2, 3, 4, 5] >>> a[::2] [0, 2, 4] >>> a[1::2] [1, 3, 5] So you can do: >>> a[::2], a[1::2] = a[1::2], a[::2

Re: swapping numeric items in a list

2006-08-22 Thread Simon Forman
Jiang Nutao wrote: > Hi, > > I simplify my problem like below > > To convert list > aa = [0x12, 0x34, 0x56, 0x78] > into > [0x34, 0x12, 0x78, 0x56] > > How to do it fast? My real list is huge. > > Thanks a lot. > Jason Here's simple and probably fast enough way (but it won't work right on

Re: swapping numeric items in a list

2006-08-22 Thread faulkner
for i in xrange(0, len(your_list), 2): your_list[i], your_list[i + 1] = your_list[i + 1], your_list[i] Jiang Nutao wrote: > Hi, > > I simplify my problem like below > > To convert list > aa = [0x12, 0x34, 0x56, 0x78] > into > [0x34, 0x12, 0x78, 0x56] > > How to do it fast? My real li