Re: [Tutor] Help Optimise Code

2008-11-21 Thread Rich Lovely
On a small side note, the docs say array.array is supposed to be  
efficient.  Testing has shown in this function, a list is faster (at  
least for x<10). A set is faster still - at least over the same  
range on my computer,, but you can't guarantee ordering, which makes  
it inconsistent - and potentially broken.


The version I've got in my original post (using a single variable and  
short-circuiting logic) is almost twice as fast as the version using  
two separate tests.  I don't know why, so suggestions would be  
appreciated.


I'll add in a version of the count6 function. I must have forgotten  
that maths lesson.


I've just thought of something that might be faster than using mod,  
but I'll have to wait until I get home to test it.
(using a list of itertools.cycle's to produce a true/false from a  
tuple with prime length, yielding a number iff not any(l[:sqrtX]). No  
division involved...) If that make no sense to anyone, it makes less  
sense to me, sounds ok, though...

---
Richard "Roadie Rich" Lovely
Part of the JNP|UK Famille
www.theJNP.com

(Sent from my iPod - please allow me a few typos: it's a very small  
keyboard)


On 19 Nov 2008, at 09:26 PM, Lie Ryan <[EMAIL PROTECTED]> wrote:


On Wed, 19 Nov 2008 13:13:18 +, Richard Lovely wrote:


I'm pretty new to code optimisation, so I thought I'd ask you all for
advice.

I'm making an iterative prime number generator. This is what I've  
got so

far:

Code: Select all
import math, array

def count2(start_at=0):
   'Yield every third integer, beginning with start_at'
   # this has been
   tested as faster than using itertools.count
   while True:
   yield start_at
   start_at += 2

def iprimes():
   'generate an endless sequence of prime numbers'
   yield 2
   yield 3
   yield 5
   sqrt = math.sqrt
   # 'L' for unsigned long - not tested if
   # using a smaller type is faster
   knownPrimes = array.array("L",(3,5))
   for x in count2(7):
   # take extra function calls out of the inner loop
   sqrtX = sqrt(x)
   for p in knownPrimes:
   test = (not x % p) and -1 or p > sqrtX
   if test == -1: # (not > x % p) == true
   break
   elif test: # (p > sqrtX) == true
   yield x
   knownPrimes.append(x)
   break



Do you know that every prime number is in the form 6*x+1 or 6*x-1,  
except
2 and 3. This means that instead of checking all odd numbers, you  
could

loop over 6 numbers then yield n - 1 and n + 1.

def count6(start):
   while True:
   start += 6
   yield start - 1
   yield start + 1

And I've seen that you generated prime by dividing things up (actually
modulus). Division and modulus is the slowest arithmetic operator,  
avoid
it if you can. If you knows the upper bound beforehand, it is faster  
to

use multiplication and an array of fixed size, i.e. "Sieve of
Erasthotenes". If you intend to generate primes without known upper  
bound

though, using sieve complexify things up.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Decimal fixed point representation

2008-11-21 Thread Dinesh B Vadhia
Hi Alan

That's right, it is the Decimal module I'm trying to understand.  And, it is 
how to represent a decimal floating point number as a common/vulgar fraction eg 
1.234 = 1234/1000.  How do you do this using the Decimal module?  The 
motivation is to avoid floating point calculations and use integers only (don't 
ask why!).  Cheers!

Dinesh




Date: Fri, 21 Nov 2008 01:03:05 -
From: "Alan Gauld" <[EMAIL PROTECTED]>
Subject: Re: [Tutor] Decimal fixed point representation
To: tutor@python.org
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
reply-type=original


"Dinesh B Vadhia" <[EMAIL PROTECTED]> wrote 

> I'm trying to get my head around the Decimal module to 
> understand how to represent a decimal floating point 
> number as an integer (or integers).  

I'm not sure what you mean by that.

Is it the use of the Decimal module you are querying?

Is it how to represent a ecimal fraction as an 
integer - ie how to lose the numbers after the point?

Is it how to represent it as a common/vulgar fraction
eg 1.234 = 1234/1000

Or something else?

> Am I barking mad or is this possible?

It is possible that you are barking mad I suppose. 

As to whether what you are asking about decimals 
is possible, that depends on what you are actually 
asking :-)

Alan G

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Decimal fixed point representation

2008-11-21 Thread Kent Johnson
On Fri, Nov 21, 2008 at 8:22 AM, Dinesh B Vadhia
<[EMAIL PROTECTED]> wrote:
> Hi Alan
>
> That's right, it is the Decimal module I'm trying to understand.  And, it is
> how to represent a decimal floating point number as a common/vulgar fraction
> eg 1.234 = 1234/1000.  How do you do this using the Decimal module?  The
> motivation is to avoid floating point calculations and use integers only
> (don't ask why!).  Cheers!

The Decimal type does not represents numbers as fractions, it
represents them as decimal floating point, i.e. a floating point
representation using base 10 instead of base 2.

A fractions module was introduced in Python 2.6 that does represent
numbers as a numerator and denominator:
http://docs.python.org/library/fractions.html

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] python reverse engineering tools

2008-11-21 Thread amit sethi
Can somebody tell me about any python to UML reverse engineering tools . I
was trying pynsource but it uses the old opengl namespace (this is what i
assume the problem is from what i read from the openGL documentation)
and thus it gives an import error
from wxPython.ogl import *
ImportError: No module named ogl
now i tried changing it  to   (again from documentation)

import wx.lib.ogl as ogl
but it still does not work. Can  somebody tell me what the problem might be
.



-- 
A-M-I-T S|S
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] import data (txt/csv) into list/array and manipulation

2008-11-21 Thread trias

Cool,

 Does anyone else have any other thoughts on this problem?








-- 
View this message in context: 
http://www.nabble.com/import-data-%28txt-csv%29-into-list-array-and-manipulation-tp20424075p20623480.html
Sent from the Python - tutor mailing list archive at Nabble.com.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Decimal fixed point representation

2008-11-21 Thread Bill Campbell
On Fri, Nov 21, 2008, Dinesh B Vadhia wrote:
>
>   That's right, it is the Decimal module I'm trying to understand.  And,
>   it is how to represent a decimal floating point number as a
>   common/vulgar fraction eg 1.234 = 1234/1000.  How do you do this using
>   the Decimal module?  The motivation is to avoid floating point
>   calculations and use integers only (don't ask why!).  Cheers!

I can understand why if one is writing accounting applications that have to
be accurate to the penny (or whatever in the local currency).  Accountants
and auditors don't like rounding errors.

The accounting system I wrote in the mid-80s, and still use today, is
written mostly in C, and handles all monitary calculations in cents in long
integers with appropriate input and output routines to make them look to
the world as it would expect.

There was (is?) a FixedPoint package in python that I use for handling
dollar amounts.  It came out before the Decimal type, and I am not fixing
something that works.

The only machines I worked on extensively that handled decimal arithmatic
very well were the Burroughs Medium Systems B-2500 through B-4800 which did
everything in decimal which made a lot of sense since they were designed
primarily to run COBOL accounting applications.

Bill
-- 
INTERNET:   [EMAIL PROTECTED]  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:  (206) 236-1676  Mercer Island, WA 98040-0820
Fax:(206) 232-9186

Scientists are explorers. Philosophers are tourists. -- Richard Feynman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] python question

2008-11-21 Thread Daniel J Kramer
Hi

I have just joined this list. I need some help working on a Python
application I am working on.  I am working on a quiz game where the users
gain points when the answer questions correctly.  I have written the
skeleton of the quiz with If, Elif and Else statements.  it runs perfectly.

I am very unclear on how to score points at the end of each round and how to
import images.

Can anyone help out here or have any ideas on the best way to execute this?
I have included a copy of the quiz so you can get an idea of what I am
trying to do

thank you so much and I look forward to hearing from you soon

-- 
Daniel J Kramer
Constant Fables
249 12th st #3
Brooklyn, NY 11215
(h) 347 223 4571
(m) 646 427 7430


test.py
Description: Binary data
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help Optimise Code

2008-11-21 Thread Kent Johnson
On Wed, Nov 19, 2008 at 8:13 AM, Richard Lovely
<[EMAIL PROTECTED]> wrote:

> Please don't suggest changing languages. I like python. Although if
> you want to write an extension for me, and provide the source and a
> makefile, please feel free. I have a MinGW install that's doing
> nothing.  (Just kidding - almost.)

Take a look at psyco, Cython, ShedSkin, Pyrex...

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python reverse engineering tools

2008-11-21 Thread Alan Gauld


"amit sethi" <[EMAIL PROTECTED]> wrote

Can somebody tell me about any python to UML reverse engineering 
tools .


I can't help wit the specific but I can give a word of caution on
reverse engineering from code to UML - don't expect too much!

The problem is that in a dynamic language it is very hard
for the reverse engineering to determine types of attributes
etc so everything tends to end up pointing at object - the
common superclass. That results in class diagrams that
are less than meaningful and an unreadable mess of
spaghetti pointing to one place.

Actually this tends to happen even on well designed
static language programs too since most OOP interrfaces
should be defined in terms of superclasses. UML to code
works well because the designer controls the connectivity
in the diagram, code to UML  tends to be less successful
in my experience (Lisp, Smalltalk and C++) and leaves
almost as much work as manually reverse engineering.

However if you do find something that works please
let us know!

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python question

2008-11-21 Thread David
Daniel J Kramer wrote:
> Hi
> 
> I have just joined this list. I need some help working on a Python
> application I am working on.  I am working on a quiz game where the
> users gain points when the answer questions correctly.  I have written
> the skeleton of the quiz with If, Elif and Else statements.  it runs
> perfectly.
> 
> I am very unclear on how to score points at the end of each round and
> how to import images.
> 
> Can anyone help out here or have any ideas on the best way to execute
> this?  I have included a copy of the quiz so you can get an idea of what
> I am trying to do
> 
> thank you so much and I look forward to hearing from you soon
> 
> -- 
> Daniel J Kramer
> Constant Fables
> 249 12th st #3
> Brooklyn, NY 11215
> (h) 347 223 4571
> (m) 646 427 7430
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
I am new also, but something like;

score = 0
# check answer
if answer == correct:
print "\nRight!",
score += 1
else:
print "\nWrong.",
print "Score:", score, "\n\n"

-- 
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python reverse engineering tools

2008-11-21 Thread amit sethi
Well actually there was an interesting tool i discovered, Lumpy .
http://www.greenteapress.com/thinkpython/swampy/lumpy.html
which was developed with the intention of being a teaching tool.
Although it would be a nice idea if the people in this list evaluate
it and give their response because I as a beginner don't think  can
comment on its effectiveness.


On Sat, Nov 22, 2008 at 6:17 AM, Alan Gauld <[EMAIL PROTECTED]>wrote:

>
> "amit sethi" <[EMAIL PROTECTED]> wrote
>
>  Can somebody tell me about any python to UML reverse engineering tools .
>>
>
> I can't help wit the specific but I can give a word of caution on
> reverse engineering from code to UML - don't expect too much!
>
> The problem is that in a dynamic language it is very hard
> for the reverse engineering to determine types of attributes
> etc so everything tends to end up pointing at object - the
> common superclass. That results in class diagrams that
> are less than meaningful and an unreadable mess of
> spaghetti pointing to one place.
>
> Actually this tends to happen even on well designed
> static language programs too since most OOP interrfaces
> should be defined in terms of superclasses. UML to code
> works well because the designer controls the connectivity
> in the diagram, code to UML  tends to be less successful
> in my experience (Lisp, Smalltalk and C++) and leaves
> almost as much work as manually reverse engineering.
>
> However if you do find something that works please
> let us know!
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
A-M-I-T S|S
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor