Re: [Tutor] Using a list comp in place of a for loop

2009-10-04 Thread Rich Lovely
2009/10/3 afith13 :
> Hi tutors,
>
> I've got a loop and a comprehension that do the same thing (as far as I can 
> tell):
>
> for i in range(N):
>  someList.append.newObject(i)
>
> ...and...
>
> [someList.append.newObject(i) for i in range(N)]
>
> I'm tempted to write this as a list comp because it's a big list and I've 
> read that list comps are faster than for loops in general. I'm hesitating 
> because I'm not sure if building a big list of "None" objects and then 
> throwing it away has any drawbacks.
>
> Thanks in advance,
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

If you want a list comprehension to do the same as your original code,
you need to write it as

someList = [newObject(i) for i in range(N)]

This avoids building the list of Nones, and only differs if someList
already has values in it, in which case you can use:

someList.extend(newObject(i) for i in range(N))

which will do exactly what your original code does, except using a
list comp (actually a generator, which is pretty much the same thing),
and won't generate and then throw away a list of Nones.

I prefer list comprehensions over for-loops wherever possible because
they're easier to read (IMHO).  As for speed, let's take a look:

>>> t_for = Timer("""l = []
... for i in range(100):
... l.append(i)""")
>>> t_slowComp = Timer("""l = []
... [l.append(i) for i in range(100)]
... """)
>>> t_comp = Timer("""l = [i for i in range(100)""")
>>> t_addComp = Timer("""l = []
... l += [i for i in range(100)]""")
>>> t_extend = Timer("""l = []
... l.extend(i for i in range(100))""")
>>> min(t_for.repeat(3,1))
0.826678279027
>>> min(t_slowComp.repeat(3,1))
0.95501802603394026
>>> min(t_comp.repeat(3,1))
0.35897579161587601
>>> min(t_addComp.repeat(3,1))
0.37727895584498583
>>> min(t_extend.repeat(3,1))
0.59708203137552118
>>>

As you can see, list comprehensions are better than twice the speed
(in this simple example) of a regular for loop.  With an existing
list, adding a comprehension is surprisingly fast (I was expecting
extend to be faster).  I wasn't expecting the slow comprehension to be
as slow as it is, but as you can see, it is slower than a standard for
loop.


Hope something in here has helped you, and has answered your question(s).
-- 
Rich "Roadie Rich" Lovely

There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.


P.S. In case you're wondering, I feel the minimum of Timer.repeat()
results is more applicable than an average, as I feel - and the docs
agree - that it's a sort of "best speed", ignoring as many other
drains of processing power as possible.)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __mul__ for different variable types?

2009-10-04 Thread Warren


Awesome, Rich, thanks!

- Warren
(war...@wantonhubris.com)




On Oct 4, 2009, at 5:31 PM, Rich Lovely wrote:


2009/10/4 Warren :


I'm a little confused on this one.

I have a Vector class that I want to be able to multiply by either  
another

vector or by a single float value.  How would I implement this in my
override of __mul__ within that class?

Do you check the variable type with a stack of "if isinstance"  
statements or

something?  What is the preferred Python way of doing this?

- Warren
(war...@wantonhubris.com)




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



I think isinstance is probably the most pythonic way of doing this,
and don't forget to add __rmul__ as well, for floats:

try:
   from numbers import Real
except NameError:
   Real = (int, long, float)

class Vector(object):
   #__init__ and other methods ommitted.
   def __mul__(self, other):
   """self * other"""
   if isinstance(other, Vector):
   # code for Vector * Vector
   elif isinstance(other, Number):
   # code for Vector * number
   else:
   return NotImplemented
   def __rmul__(self, other):
   """other * self"""
   return self.__mul__(other)

Note that I've got no type checking in __rmul__, because if only
Vector * Vector has a different value if the terms are swapped, and
other * self will use other.__mul__ if other is a Vector.

Also note the import at the top:  numbers.Real is an Abstract Base
Class use for all real number types, introduced in Python 2.6, so
simplifies testing types.  See
http://docs.python.org/library/numbers.html for info.  If it's not
there, the code demos another feature of isinstance most people don't
notice: the type argument can be a sequence of types,  so I set Real
to a tuple of all the builtin real number types (that I can remember
off the top of my head) if the import fails.

If you want multiplication to be defined for complex numbers, change
all occurances of Real to Number, and add complex to the tuple.

--
Rich "Roadie Rich" Lovely

There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __mul__ for different variable types?

2009-10-04 Thread Rich Lovely
2009/10/4 Warren :
>
> I'm a little confused on this one.
>
> I have a Vector class that I want to be able to multiply by either another
> vector or by a single float value.  How would I implement this in my
> override of __mul__ within that class?
>
> Do you check the variable type with a stack of "if isinstance" statements or
> something?  What is the preferred Python way of doing this?
>
> - Warren
> (war...@wantonhubris.com)
>
>
>
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

I think isinstance is probably the most pythonic way of doing this,
and don't forget to add __rmul__ as well, for floats:

try:
from numbers import Real
except NameError:
Real = (int, long, float)

class Vector(object):
#__init__ and other methods ommitted.
def __mul__(self, other):
"""self * other"""
if isinstance(other, Vector):
# code for Vector * Vector
elif isinstance(other, Number):
# code for Vector * number
else:
return NotImplemented
def __rmul__(self, other):
"""other * self"""
return self.__mul__(other)

Note that I've got no type checking in __rmul__, because if only
Vector * Vector has a different value if the terms are swapped, and
other * self will use other.__mul__ if other is a Vector.

Also note the import at the top:  numbers.Real is an Abstract Base
Class use for all real number types, introduced in Python 2.6, so
simplifies testing types.  See
http://docs.python.org/library/numbers.html for info.  If it's not
there, the code demos another feature of isinstance most people don't
notice: the type argument can be a sequence of types,  so I set Real
to a tuple of all the builtin real number types (that I can remember
off the top of my head) if the import fails.

If you want multiplication to be defined for complex numbers, change
all occurances of Real to Number, and add complex to the tuple.

-- 
Rich "Roadie Rich" Lovely

There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Using a list comp in place of a for loop

2009-10-04 Thread afith13
Hi tutors,

I've got a loop and a comprehension that do the same thing (as far as I can 
tell):

for i in range(N):
  someList.append.newObject(i)

...and...

[someList.append.newObject(i) for i in range(N)]

I'm tempted to write this as a list comp because it's a big list and I've read 
that list comps are faster than for loops in general. I'm hesitating because 
I'm not sure if building a big list of "None" objects and then throwing it away 
has any drawbacks.

Thanks in advance,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] using easy_install to download eggs

2009-10-04 Thread Tim Michelsen
Hello,
I would like to use easy_install to cache packages registered at PyPi locally.

How can I do this for packages?

I tried the hints from:
http://peak.telecommunity.com/DevCenter/EasyInstall#installing-on-un-networked-machines

It worked for some packages.
But for others, the command

easy_install -zxad. mercurial

just creates a subdirectory and not an *.egg file.

How can I
1) use easy_install to download packages from PyPi to a locally saved egg-file?
2) use easy_install to download archinves (*.tar.gz / *.zip) to download the
respective software package from the link indicated on PyPi?

Thanks in advance,
Timmie

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] small program

2009-10-04 Thread Luke Paireepinart
On Sun, Oct 4, 2009 at 1:59 PM, Andrius  wrote:

> Very good. Nice to hear that from another 'untouchable'. Must to
> confirm for your that I'm usually left wing fella, so, if you have
> nothing what to say for my in my case - please, fuck off.


Re: http://catb.org/~esr/faqs/smart-questions.html#not_losing
Good luck,
-Luke
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] __mul__ for different variable types?

2009-10-04 Thread Warren


I'm a little confused on this one.

I have a Vector class that I want to be able to multiply by either  
another vector or by a single float value.  How would I implement this  
in my override of __mul__ within that class?


Do you check the variable type with a stack of "if isinstance"  
statements or something?  What is the preferred Python way of doing  
this?


- Warren
(war...@wantonhubris.com)




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor