[Tutor] (no subject)

2010-11-09 Thread First Ramses
-- 
mzperx
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension question

2010-11-09 Thread Richard D. Moores
On Mon, Nov 8, 2010 at 23:49, Richard D. Moores  wrote:
> On Mon, Nov 8, 2010 at 22:47, Richard D. Moores  wrote:
>> On Mon, Nov 8, 2010 at 21:31, Richard D. Moores  wrote:
>>
>>> That sqrt(n) works for speeding up the finding of primes, but here we
>>> want to use int(n/2) (and why didn't I think of that?), which results
>>> in about a 2x speedup. See .
>>
>> NO! Use  int(n/2)+1 .  I'll correct that in
>>  and report back.
>
> See 
>
> But now I'll have to redo again using Stefan's ingenious suggestion.
>
> Dick

Here's what I wrote using Stefan's suggestion:

def proper_divisors_sum(n):
pd_list = []
for x in range(1, int(n**.5)+1):
if n % x == 0:
factor = int(x)
factor2 = int(n/factor)
pd_list.extend([factor, factor2])
pd_list = list(set(pd_list))
pd_list.sort()
pd_list = pd_list[:-1]
return sum(pd_list)

Take a look at the difference in speed his suggestion made, and the
larger n is, the greater the difference:


At n = 100,000,000 the speed improvement over the fastest of the other
3 versions was 2579x!

Employing append() instead of extend() as here:

def proper_divisors_sum(n):
pd_list = []
for x in range(1, int(n**.5)+1):
if n % x == 0:
factor = int(x)
pd_list.append(factor)
factor2 = int(n/factor)
pd_list.append(factor2)
pd_list = list(set(pd_list))
pd_list.sort()
pd_list = pd_list[:-1]
return sum(pd_list)

improves speed by 20% or 30% for large n, but I didn't paste those results.

I'll begin a new thread soon to ask for advice on optimizing a script
that finds amiable pairs, and of course employs proper_divisors_sum().

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


[Tutor] Need help with script for finding pairs of amicable numbers

2010-11-09 Thread Richard D. Moores
Here's what I have so far: . My
immediate question is, is there a general way to determine the
multiplier of n in line 44? Of course, by trial and error I can find
out pretty exactly what it should be to find a pair that I know of.
But of course I want to go on and find the next pair after the last
one in math world's list, (63020, 76084). (See line 9.)

And of course, as a Tutoree I welcome any Tutor's advice on how to
otherwise improve the script.

BTW I forgot to remark out the "continue", line 53.

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


Re: [Tutor] How to copy file from a source, I do not know the source file name, Only path to the src directory I know

2010-11-09 Thread Evert Rol
> Hi All
>   I am trying our "Python" .
>   My aim is to copy a file to a location and later removing the
> file.I had written a small script which works.It works only if I
> specify the source file name. I would like to make it work in a
> scenario in which I do not know the name of the source file(the source
> directory will have only one file at any time)

Have a look at the os module, specifically os.listdir()
The glob module could also be handy.

Cheers,

  Evert

> 
> I am posting the script which I have done,How to modify the script to
> copy the source file even if I do not know the name of the file
> my script is below
> 
> ###
> #!/usr/bin/python
> 
> # This script aims to copy file from one directoy to anothe and later
> removing the files
> # This script works when I know the source file name, I want to this
> script to function even if I do not know the name of
> # the file in "/home/joseph/Projects/Training/Python/example/One/"
> 
> import shutil
> # "FromOne" is a file in /home/joseph/Projects/Training/Python/example/One/
> src = "/home/joseph/Projects/Training/Python/example/One/FromOne"
> #
> dst2 = "/home/joseph/Projects/Training/Python/example/Two/"
> dst3 = "/home/joseph/Projects/Training/Python/example/Three/"
> #
> 
> #
> #shutil.move(src, dst)
> shutil.copy(src,dst2)
> shutil.move(src,dst3)
> 
> ##
> 
> 
> 
> 
> -- 
> Thanks
> Joseph John
> http://www.oss101.com/
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] List comprehension question

2010-11-09 Thread Stefan Behnel

Richard D. Moores, 09.11.2010 12:07:

That sqrt(n) works for speeding up the finding of primes, but here we
want to use int(n/2) (and why didn't I think of that?), which results
in about a 2x speedup. See.


NO! Use  int(n/2)+1 .  I'll correct that in
  and report back.


See

But now I'll have to redo again using Stefan's ingenious suggestion.


Here's what I wrote using Stefan's suggestion:

def proper_divisors_sum(n):
 pd_list = []
 for x in range(1, int(n**.5)+1):
 if n % x == 0:
 factor = int(x)
 factor2 = int(n/factor)
 pd_list.extend([factor, factor2])
 pd_list = list(set(pd_list))
 pd_list.sort()
 pd_list = pd_list[:-1]
 return sum(pd_list)


Have a look at divmod():

http://docs.python.org/library/functions.html#divmod

Note that you don't need to convert 'x' to an int. It already has that type 
(see the docs on range()). Also, int(n/factor) is the same as n//factor here.


Stefan

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


[Tutor] How to copy file from a source, I do not know the source file name, Only path to the src directory I know

2010-11-09 Thread Joseph John
Hi All
   I am trying our "Python" .
   My aim is to copy a file to a location and later removing the
file.I had written a small script which works.It works only if I
specify the source file name. I would like to make it work in a
scenario in which I do not know the name of the source file(the source
directory will have only one file at any time)

I am posting the script which I have done,How to modify the script to
copy the source file even if I do not know the name of the file
my script is below

###
#!/usr/bin/python

# This script aims to copy file from one directoy to anothe and later
removing the files
# This script works when I know the source file name, I want to this
script to function even if I do not know the name of
# the file in "/home/joseph/Projects/Training/Python/example/One/"

import shutil
# "FromOne" is a file in /home/joseph/Projects/Training/Python/example/One/
src = "/home/joseph/Projects/Training/Python/example/One/FromOne"
#
dst2 = "/home/joseph/Projects/Training/Python/example/Two/"
dst3 = "/home/joseph/Projects/Training/Python/example/Three/"
#

#
#shutil.move(src, dst)
shutil.copy(src,dst2)
shutil.move(src,dst3)

##




-- 
Thanks
Joseph John
http://www.oss101.com/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to copy file from a source, I do not know the source file name, Only path to the src directory I know

2010-11-09 Thread Joel Goldstick
On Tue, Nov 9, 2010 at 7:15 AM, Joseph John  wrote:

> Hi All
>   I am trying our "Python" .
>   My aim is to copy a file to a location and later removing the
> file.I had written a small script which works.It works only if I
> specify the source file name. I would like to make it work in a
> scenario in which I do not know the name of the source file(the source
> directory will have only one file at any time)
>
> I am posting the script which I have done,How to modify the script to
> copy the source file even if I do not know the name of the file
> my script is below
>
> ###
> #!/usr/bin/python
>
> # This script aims to copy file from one directoy to anothe and later
> removing the files
> # This script works when I know the source file name, I want to this
> script to function even if I do not know the name of
> # the file in "/home/joseph/Projects/Training/Python/example/One/"
>
> import shutil
> # "FromOne" is a file in /home/joseph/Projects/Training/Python/example/One/
> src = "/home/joseph/Projects/Training/Python/example/One/FromOne"
> #
> dst2 = "/home/joseph/Projects/Training/Python/example/Two/"
> dst3 = "/home/joseph/Projects/Training/Python/example/Three/"
> #
>
> #
> #shutil.move(src, dst)
> shutil.copy(src,dst2)
> shutil.move(src,dst3)
>
> ##
>
>
>
>
> --
> Thanks
> Joseph John
> http://www.oss101.com/


os.listdir(*path*)¶ 

Return a list containing the names of the entries in the directory given by
*path*. The list is in arbitrary order. It does not include the special
entries '.' and '..' even if they are present in the directory.




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



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


Re: [Tutor] How to copy file from a source, I do not know the source file name, Only path to the src directory I know

2010-11-09 Thread Evert Rol
> I was able to solve it
> by
> "
> dirList=os.listdir(src)
> for fname in dirList:
>   print fname
>   ffname = '/home/joseph/Projects/Training/Python/example/One/'+fname

tip: use os.path.join for concatenating paths. It's smart, and (in principle) 
OS-agnostic.

Also, if you use 'os.listdir(src)', why not use src again when assigning ffname?


Cheers,

  Evert

>   print ffname
>   #shutil.copy(ffname,dst2)
>   shutil.move(ffname,dst3)
> "
> 
> 
> On Tue, Nov 9, 2010 at 4:38 PM, Evert Rol  wrote:
>>> Hi All
>>>   I am trying our "Python" .
>>>   My aim is to copy a file to a location and later removing the
>>> file.I had written a small script which works.It works only if I
>>> specify the source file name. I would like to make it work in a
>>> scenario in which I do not know the name of the source file(the source
>>> directory will have only one file at any time)
>> 
>> Have a look at the os module, specifically os.listdir()
>> The glob module could also be handy.
>> 
>> Cheers,
>> 
>>  Evert
>> 
>>> 
>>> I am posting the script which I have done,How to modify the script to
>>> copy the source file even if I do not know the name of the file
>>> my script is below
>>> 
>>> ###
>>> #!/usr/bin/python
>>> 
>>> # This script aims to copy file from one directoy to anothe and later
>>> removing the files
>>> # This script works when I know the source file name, I want to this
>>> script to function even if I do not know the name of
>>> # the file in "/home/joseph/Projects/Training/Python/example/One/"
>>> 
>>> import shutil
>>> # "FromOne" is a file in /home/joseph/Projects/Training/Python/example/One/
>>> src = "/home/joseph/Projects/Training/Python/example/One/FromOne"
>>> #
>>> dst2 = "/home/joseph/Projects/Training/Python/example/Two/"
>>> dst3 = "/home/joseph/Projects/Training/Python/example/Three/"
>>> #
>>> 
>>> #
>>> #shutil.move(src, dst)
>>> shutil.copy(src,dst2)
>>> shutil.move(src,dst3)
>>> 
>>> ##
>>> 
>>> 
>>> 
>>> 
>>> --
>>> Thanks
>>> Joseph John
>>> http://www.oss101.com/
>>> ___
>>> Tutor maillist  -  Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>> 
>> 
> 
> 
> 
> -- 
> Thanks
> Joseph John
> http://www.oss101.com/

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


Re: [Tutor] List comprehension question

2010-11-09 Thread Richard D. Moores
On Tue, Nov 9, 2010 at 03:35, Stefan Behnel  wrote:
> Richard D. Moores, 09.11.2010 12:07:
>
> That sqrt(n) works for speeding up the finding of primes, but here we
> want to use int(n/2) (and why didn't I think of that?), which results
> in about a 2x speedup. See.

 NO! Use  int(n/2)+1 .  I'll correct that in
   and report back.
>>>
>>> See
>>>
>>> But now I'll have to redo again using Stefan's ingenious suggestion.
>>
>> Here's what I wrote using Stefan's suggestion:
>>
>> def proper_divisors_sum(n):
>>     pd_list = []
>>     for x in range(1, int(n**.5)+1):
>>         if n % x == 0:
>>             factor = int(x)
>>             factor2 = int(n/factor)
>>             pd_list.extend([factor, factor2])
>>     pd_list = list(set(pd_list))
>>     pd_list.sort()
>>     pd_list = pd_list[:-1]
>>     return sum(pd_list)
>
> Have a look at divmod():
>
> http://docs.python.org/library/functions.html#divmod
>
> Note that you don't need to convert 'x' to an int. It already has that type
> (see the docs on range()). Also, int(n/factor) is the same as n//factor
> here.

From your suggestions I made 2 versions:

def proper_divisors_sum1(n):
pd_list = []
for x in range(1, int(n**.5)+1):
quotient, remainder = divmod(n, x)
if remainder == 0:
pd_list.append(x)
pd_list.append(quotient)
pd_list = list(set(pd_list))
pd_list.sort()
pd_list = pd_list[:-1]
return sum(pd_list)

def proper_divisors_sum2(n):
pd_list = []
for x in range(1, int(n**.5)+1):
if n % x == 0:
pd_list.append(x)
pd_list.append(n//x)
pd_list = list(set(pd_list))
pd_list.sort()
pd_list = pd_list[:-1]
return sum(pd_list)

The first actually slowed down what I had before, but the second made
for a nice speed-up.

Thanks, Stephan.

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


Re: [Tutor] How to copy file from a source, I do not know the source file name, Only path to the src directory I know

2010-11-09 Thread Joseph John
Thanks
 I was able to solve it
by
"
dirList=os.listdir(src)
for fname in dirList:
print fname
ffname = '/home/joseph/Projects/Training/Python/example/One/'+fname
print ffname
#shutil.copy(ffname,dst2)
shutil.move(ffname,dst3)
"


On Tue, Nov 9, 2010 at 4:38 PM, Evert Rol  wrote:
>> Hi All
>>   I am trying our "Python" .
>>   My aim is to copy a file to a location and later removing the
>> file.I had written a small script which works.It works only if I
>> specify the source file name. I would like to make it work in a
>> scenario in which I do not know the name of the source file(the source
>> directory will have only one file at any time)
>
> Have a look at the os module, specifically os.listdir()
> The glob module could also be handy.
>
> Cheers,
>
>  Evert
>
>>
>> I am posting the script which I have done,How to modify the script to
>> copy the source file even if I do not know the name of the file
>> my script is below
>>
>> ###
>> #!/usr/bin/python
>>
>> # This script aims to copy file from one directoy to anothe and later
>> removing the files
>> # This script works when I know the source file name, I want to this
>> script to function even if I do not know the name of
>> # the file in "/home/joseph/Projects/Training/Python/example/One/"
>>
>> import shutil
>> # "FromOne" is a file in /home/joseph/Projects/Training/Python/example/One/
>> src = "/home/joseph/Projects/Training/Python/example/One/FromOne"
>> #
>> dst2 = "/home/joseph/Projects/Training/Python/example/Two/"
>> dst3 = "/home/joseph/Projects/Training/Python/example/Three/"
>> #
>>
>> #
>> #shutil.move(src, dst)
>> shutil.copy(src,dst2)
>> shutil.move(src,dst3)
>>
>> ##
>>
>>
>>
>>
>> --
>> Thanks
>> Joseph John
>> http://www.oss101.com/
>> ___
>> Tutor maillist  -  tu...@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
>



-- 
Thanks
Joseph John
http://www.oss101.com/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension question

2010-11-09 Thread Martin A. Brown

Hello,

 : def proper_divisors_sum(n):
 : pd_list = []
 : for x in range(1, int(n**.5)+1):
 : if n % x == 0:
 : factor = int(x)
 : pd_list.append(factor)
 : factor2 = int(n/factor)
 : pd_list.append(factor2)
 : pd_list = list(set(pd_list))
 : pd_list.sort()
 : pd_list = pd_list[:-1]
 : return sum(pd_list)

A few questions--noting, of course, that I'm not reading this with 
an eye toward performance, which it seems you are, but these occur 
to me:

  * Why use a list (pd_list = []), when you want unique divisors?
Why not, pd = set() ?  Then, instead of pd_list.append(factor), 
pd.add(factor) or something like pd.update((factor,factor2))?
(See also my suggestion below.)

  * Also, since you are throwing away the divisor n, anyway, 
why not skip it from the beginning?  Like this:

  pd_list = [1,] 
  for x in range(2, int(n**.5)+1):

  * So, I'm not terribly math aware, but I don't think I have 
substantially changed the meaning of your program.  I find 
breaking out the functions makes things a bit clearer to 
somebody who might be reading my code later.

Combining these suggestions and splitting into separate functions 
(you don't really need to sort before summing), I end up with the 
below.  Note, that I stuff the proper divisor 1 in the set at 
creation time.  This is probably not worth it from an optimization 
perspective, but as I have learned, the only way to know is to time 
it (and I didn't time it).

  def proper_divisors_sum(n):
  return sum(proper_divisors(n))
  
  def proper_divisors_sorted(n):
  return sorted(proper_divisors(n))
  
  def proper_divisors(n):
  pd = set((1,))
  for x in range(2, int(n**.5)+1):
  factor, mod = divmod(n,x)
  if mod == 0:
  pd.update((x,factor))
  return list(pd)

Have a great day,

-Martin

-- 
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension question

2010-11-09 Thread Richard D. Moores
On Tue, Nov 9, 2010 at 05:51, Martin A. Brown  wrote:
>
> Hello,
>
>  : def proper_divisors_sum(n):
>  :     pd_list = []
>  :     for x in range(1, int(n**.5)+1):
>  :         if n % x == 0:
>  :             factor = int(x)
>  :             pd_list.append(factor)
>  :             factor2 = int(n/factor)
>  :             pd_list.append(factor2)
>  :     pd_list = list(set(pd_list))
>  :     pd_list.sort()
>  :     pd_list = pd_list[:-1]
>  :     return sum(pd_list)
>
> A few questions--noting, of course, that I'm not reading this with
> an eye toward performance, which it seems you are, but these occur
> to me:
>
>  * Why use a list (pd_list = []), when you want unique divisors?
>    Why not, pd = set() ?  Then, instead of pd_list.append(factor),
>    pd.add(factor) or something like pd.update((factor,factor2))?
>    (See also my suggestion below.)
>
>  * Also, since you are throwing away the divisor n, anyway,
>    why not skip it from the beginning?  Like this:
>
>      pd_list = [1,]
>      for x in range(2, int(n**.5)+1):
>
>  * So, I'm not terribly math aware, but I don't think I have
>    substantially changed the meaning of your program.  I find
>    breaking out the functions makes things a bit clearer to
>    somebody who might be reading my code later.
>
> Combining these suggestions and splitting into separate functions
> (you don't really need to sort before summing), I end up with the
> below.  Note, that I stuff the proper divisor 1 in the set at
> creation time.  This is probably not worth it from an optimization
> perspective, but as I have learned, the only way to know is to time
> it (and I didn't time it).
>
>      def proper_divisors_sum(n):
>          return sum(proper_divisors(n))
>
>      def proper_divisors_sorted(n):
>          return sorted(proper_divisors(n))
>
>      def proper_divisors(n):
>          pd = set((1,))
>          for x in range(2, int(n**.5)+1):
>              factor, mod = divmod(n,x)
>              if mod == 0:
>                  pd.update((x,factor))
>          return list(pd)

Thanks for your ideas, Martin. I adopted your idea of starting at 2,
with an initial list of [1].  It resulted in a small increase in
speed. And you're right, I don't need to sort.

Speed is important, because the function is the heart of my amiable
numbers script. Did you happen to see that post of mine?

#Here's my fastest version before seeing Martin's post:
def proper_divisors_sum1(n):
pd_list = []
for x in range(1, int(n**.5)+1):
if n % x == 0:
pd_list.append(x)
pd_list.append(n//x)
pd_list = list(set(pd_list))
pd_list.sort()
pd_list = pd_list[:-1]
return sum(pd_list)

#This uses several of Martin's ideas
# It's now my fastest version
def proper_divisors_sum2(n):
pd = set((1,))
for x in range(2, int(n**.5)+1):
if n % x == 0:
pd.update((x, n//x))
return sum(list(pd))

Here's what I put together from all your points, except for the
splitting off of several small functions:

# This incorporates all of Martin's ideas
# Seems that divmod() is a speed killer
def proper_divisors_sum3(n):
pd = set((1,))
for x in range(2, int(n**.5)+1):
factor, mod = divmod(n,x)
if mod == 0:
pd.update((x,factor))
return sum(list(pd))

It may be that I've haven't done your suggestions justice, but that
function is 76% slower than proper_divisors_sum2().

See  for a speed test with n =
100,000 and 100,000 loops

Thanks again, Martin.

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


Re: [Tutor] Too different 2.6 vs 2.7?

2010-11-09 Thread Mac Ryan
On Mon, 2010-11-08 at 17:16 -0800, Terry Carroll wrote:
> The only feature I'm pining for at all is the new argparse; but it
> depends on your needs. 

Ubuntu 10.10 here (running python 2.6.6) I use argparse without any
problems: I believe the difference between 2.6 and 2.7 [I am not an
expert on the subject, so correct me if I am wrong] is that with 2.7
argparse comes as *part of the standard library*, but the library per-se
exists since some time, is in the repo, and can be used normally also in
previous versions of python, once installed on the system.

Mac.

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


Re: [Tutor] query result set

2010-11-09 Thread bob gailer
When starting a new subject, please start with a new email. Do not 
"hijack" an existing one, as that causes your query to appear as part of 
the hijacked thread.



--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] Columnar Transposition Cipher question

2010-11-09 Thread Stefan Behnel

Steven D'Aprano, 09.11.2010 05:01:

http://pypi.python.org/pypi/obfuscate


Hmm - why is the Windows installer on that page called "linux-i686"?

Stefan

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


Re: [Tutor] Columnar Transposition Cipher question

2010-11-09 Thread Eike Welk
On Tuesday 09.11.2010 18:36:43 Stefan Behnel wrote:
> Steven D'Aprano, 09.11.2010 05:01:
> > http://pypi.python.org/pypi/obfuscate
> 
> Hmm - why is the Windows installer on that page called "linux-i686"?

It was probably created on Linux. Python's Distutils create installers for 
Windows even on Linux. They get file names that contain "linux-". You must 
rename them, otherwise your users get confused.  

I did never try them, because I have no Windows computer. Do these Windows 
installers work when they are created on Linux?


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


Re: [Tutor] List comprehension question

2010-11-09 Thread Steven D'Aprano

Richard D. Moores wrote:


See  for a speed test with n =
100,000 and 100,000 loops


As a general rule, you shouldn't try to roll your own speed tests. There 
are various subtleties that can throw your results right out. Timing 
small code snippets on modern multi-tasking computers is fraught with 
difficulties.


Fortunately Python comes with that battery already included: the timeit 
module. You can use it from the shell like this:


python -m timeit -s "setup code goes here" "test code goes here"

At the Python interactive interpreter, the most useful pattern I've 
found is:


from timeit import Timer
t = Timer("proper_divisors(n)",
"from __main__ import proper_divisors; n = 10")
min(t.repeat(repeat=5, number=10))


Or if you're happy with Python's defaults, that last line can be just:

min(t.repeat())



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


Re: [Tutor] Columnar Transposition Cipher question

2010-11-09 Thread Steven D'Aprano

Eike Welk wrote:

On Tuesday 09.11.2010 18:36:43 Stefan Behnel wrote:

Steven D'Aprano, 09.11.2010 05:01:

http://pypi.python.org/pypi/obfuscate

Hmm - why is the Windows installer on that page called "linux-i686"?


It was probably created on Linux. 


That would be it.

> Python's Distutils create installers for
Windows even on Linux. They get file names that contain "linux-". You must 
rename them, otherwise your users get confused.  


I didn't realise that.

I did never try them, because I have no Windows computer. Do these Windows 
installers work when they are created on Linux?


I'm pretty sure I tried it, once, but I might be confabulating.



--
Steven

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


Re: [Tutor] List comprehension question

2010-11-09 Thread Albert-Jan Roskam
For larger blocks of code, cProfile may also be useful for speed tests.

 Cheers!!
Albert-Jan


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have 
the 
Romans ever done for us?
~~





From: Steven D'Aprano 
To: tutor@python.org
Sent: Tue, November 9, 2010 9:54:24 PM
Subject: Re: [Tutor] List comprehension question

Richard D. Moores wrote:

> See  for a speed test with n =
> 100,000 and 100,000 loops

As a general rule, you shouldn't try to roll your own speed tests. There are 
various subtleties that can throw your results right out. Timing small code 
snippets on modern multi-tasking computers is fraught with difficulties.

Fortunately Python comes with that battery already included: the timeit module. 
You can use it from the shell like this:

python -m timeit -s "setup code goes here" "test code goes here"

At the Python interactive interpreter, the most useful pattern I've found is:

from timeit import Timer
t = Timer("proper_divisors(n)",
"from __main__ import proper_divisors; n = 10")
min(t.repeat(repeat=5, number=10))


Or if you're happy with Python's defaults, that last line can be just:

min(t.repeat())



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



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


Re: [Tutor] List comprehension question

2010-11-09 Thread Richard D. Moores
On Tue, Nov 9, 2010 at 12:54, Steven D'Aprano  wrote:
> Richard D. Moores wrote:
>
>> See  for a speed test with n =
>> 100,000 and 100,000 loops
>
> As a general rule, you shouldn't try to roll your own speed tests. There are
> various subtleties that can throw your results right out. Timing small code
> snippets on modern multi-tasking computers is fraught with difficulties.

Yes, but I thought that if I ran the tests the way I did, several
times and then reversing the order of the functions, and seeing that
the results were very similar, I could be pretty sure of my findings.
Could you point out where taking even that amount of care could lead
me astray?

>
> Fortunately Python comes with that battery already included: the timeit
> module. You can use it from the shell like this:
>
> python -m timeit -s "setup code goes here" "test code goes here"
>
> At the Python interactive interpreter, the most useful pattern I've found
> is:
>
> from timeit import Timer
> t = Timer("proper_divisors(n)",
>    "from __main__ import proper_divisors; n = 10")
> min(t.repeat(repeat=5, number=10))
>
>
> Or if you're happy with Python's defaults, that last line can be just:
>
> min(t.repeat())

That's terrific, Stephen! I've used timeit before, but only for really
small snippets. With your encouragement I experimented and after some
trial and error, I came up with this method for using your template:

1. Don't try to dump all you (Steven) wrote in at once.
2. First, do the import.
>>> from timeit import Timer
3. Then dump in the function (steps 2 and 3 can be reversed, I think)
4. then
 >>>t = Timer("proper_divisors(n)",
  "from __main__ import proper_divisors; n = 10")
5. Finally,
>>>min(t.repeat(repeat=5, number=10))
6. Wait for the result.

(please tell me if I could cut down the number of steps by combining a
couple of them.)

I was pleased that for

def proper_divisors(n):
pd = set((1,))
for x in range(2, int(n**.5)+1):
if n % x == 0:
pd.update((x, n//x))
return sum(list(pd))

I got 8.312734300029753

and for

def proper_divisors(n):
pd = set((1,))
for x in range(2, int(n**.5)+1):
factor, mod = divmod(n,x)
if mod == 0:
pd.update((x,factor))
return sum(list(pd))

got 14.859849506012608

That's about the same speed ratio (1.79) that I reported here earlier,
I believe. I quote: "It may be that I haven't done your suggestions
justice, but that
function is 76% slower than proper_divisors_sum2()"

Question: When I dump in more that one line of any code (properly
indented), after running it once, I've never known how to run it again
without a redump. The up arrow just gets me one line at a time. So,
how to do it?

Thanks,

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


Re: [Tutor] List comprehension question

2010-11-09 Thread Alan Gauld


"Richard D. Moores"  wrote


Question: When I dump in more that one line of any code (properly
indented), after running it once, I've never known how to run it 
again

without a redump. The up arrow just gets me one line at a time. So,
how to do it?


It depends on your IDE.

On IDLE I think its Alt-P and Alt-N to cycle through previous and
next lines of history.

On Pythonwin its Ctrl-Up/Ctrl-Down

IDLE and Pythonwin put in whole blocks.

The XP CMD prompt uses up/down keys but only does single lines

A La Mode had some neat cut n paste features for correctly pasting
in blocks of code evern from other places - like email

Not sure about any of the others.

Alan FG. 



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


Re: [Tutor] Columnar Transposition Cipher question

2010-11-09 Thread Emile van Sebille

On 11/9/2010 1:29 PM Steven D'Aprano said...

I'm pretty sure I tried it, once, but I might be confabulating.


Cool -- I learned a new word today!

I had to look it up as I thought you were profabulating...  :)

Emile


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


Re: [Tutor] List comprehension question

2010-11-09 Thread C or L Smith
>>   1. Re: List comprehension question (Richard D. Moores)
>>> ?: def proper_divisors_sum(n):

>>> A few questions--noting, of course, that I'm not reading this with
>>> an eye toward performance, which it seems you are, but these occur
>>> to me:

Tim Peters had a beautiful little version of divisors at

http://stackoverflow.com/questions/1010381/python-factorization

A modified version of this is part of the python CAS system sympy (sympy.org). 
>From sympy you simply do:

>>> from sympy import divisors
>>> list(divisors(256))
[1, 2, 4, 8, 16, 32, 64, 128, 256]

So your proper divisors would just be sum(divisors(n)) - n.
The divisors function there is in the ntheory/factor_.py file.

/c





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


Re: [Tutor] List comprehension question

2010-11-09 Thread Richard D. Moores
On Tue, Nov 9, 2010 at 18:29, C or L Smith  wrote:

> >From sympy you simply do:
>
 from sympy import divisors
 list(divisors(256))
> [1, 2, 4, 8, 16, 32, 64, 128, 256]
>
> So your proper divisors would just be sum(divisors(n)) - n.
> The divisors function there is in the ntheory/factor_.py file.

Thanks for letting me know about this.

def proper_divisors_sum(n):
return sum(list(divisors(n))) - n

Using Steven's suggested speed test
this gets 6.2404818210135886

My up-to-now fastest version,

def proper_divisors_sum(n):
pd = set((1,))
for x in range(2, int(n**.5)+1):
if n % x == 0:
pd.update((x, n//x))
return sum(list(pd))

gets 6.1753780857622473

So they're about even.

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


Re: [Tutor] List comprehension question

2010-11-09 Thread Stefan Behnel

Richard D. Moores, 10.11.2010 08:24:

def proper_divisors_sum(n):
 pd = set((1,))
 for x in range(2, int(n**.5)+1):
 if n % x == 0:
 pd.update((x, n//x))
 return sum(list(pd))


You keep using redundant operations. What "sum(list(s))" does, for s being 
a set, is:


1) create a list of the length of s
2) iterate over s, copying elements into the list
3) sum up the values in the list

What makes you think that the intermediate list is needed?

$ python2.6 -m timeit -s 's = set(range(1000))' 'sum(list(s))'
1 loops, best of 3: 20.7 usec per loop
$ python2.6 -m timeit -s 's = set(range(1000))' 'sum(s)'
10 loops, best of 3: 14.4 usec per loop

Stefan

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