Discussion:
[Tutor] Generate Prime Numbers
Mirage Web Studio
2015-05-29 15:28:16 UTC
Permalink
Hello,

Below is a sample code i created.

Can i better it any way?

Thanks

George

-----------------------------------------------------------

import time
start_time = time.time()


def IsDivisibleBy3(number):#string variable
v=0
for c in number:
v=v+int(c)
if v%3==0:
return True
else:
return False

def IsDivisibleBy7(number):#string variable
last=int(number[-1])*2
length=len(number)-1
tnumber=number[0:length]
tnumber=int(tnumber)-last
if tnumber%7==0:
return True
else:
return False


def IsDivisibleBy9(number):#string variable
v=0
for c in number:
v=v+int(c)
if v%9==0:
return True
else:
return False

def IsPrime(number):
l=len(number)
if number[l-1] in ['2','4','5','6','8','0']:
#print("retuning base false")
return False

if IsDivisibleBy3(number):
#print("retuning 3 check false")
return False

if IsDivisibleBy7(number):
#print("retuning 7 check false")
return False

if IsDivisibleBy9(number):
#print("retuning 9 check false")
return False

number=int(number)
half=(number/2)+1
i=7
while half>=i:
if number%i==0:
return False
i=i+1
return True


primelist=[]

for i in range (11,200000,2):
number=str(i)
print "checking ",i

if IsPrime(number):
primelist.append(number)

print ("primes",len(primelist),primelist)


print("--- %s seconds ---" % (time.time() - start_time))

---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-05-29 17:48:38 UTC
Permalink
On 29/05/15 16:28, Mirage Web Studio wrote:

> Below is a sample code i created.
>
> Can i better it any way?

Of course. There is always improvements that can be made.
But in your case there are quite a few!

> def IsDivisibleBy3(number):#string variable
> v=0
> for c in number:
> v=v+int(c)
> if v%3==0:
> return True
> else:
> return False

def IsDivisibleBy3(number):#string variable
return not int(number) % 3

> def IsDivisibleBy7(number):#string variable

See above, but maybe better still

def isDivisibleByN(number, N):
return not int(number)%N

> def IsPrime(number):

Google for the "sieve of eratosthenes"
Or look it up on wikipedia.

That will give one of several possible
improvements to your algorithm.

> primelist=[]
>
> for i in range (11,200000,2):
> number=str(i)
> print "checking ",i
>
> if IsPrime(number):

Note, you are starting with a number, then converting
it to a string then in your functions converting it
back to a number.
That's crazy!

Also where do you store the primes less than 11?
ie. 1,3,5,7

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Cameron Simpson
2015-05-29 23:37:38 UTC
Permalink
On 29May2015 18:48, ***@btinternet.com <***@btinternet.com> wrote:
>On 29/05/15 16:28, Mirage Web Studio wrote:
>>def IsDivisibleBy3(number):#string variable
>> v=0
>> for c in number:
>> v=v+int(c)
>> if v%3==0:
>> return True
>> else:
>> return False
>
>def IsDivisibleBy3(number):#string variable
> return not int(number) % 3

To illustrate that there isn't just One Right Way, I would code the above like
this:

def IsDivisibleBy3(number): #string variable
return int(number) % 3 == 0

Alan's code relies on "not" using an expressions "nonzero"ness as a boolean
value. I _much_ prefer to directly state the test instead of relying on magic
numeric=>boolean effects.

The only other remark I would make is that it is probably a ba idea to write
this function to take a string. It expresses a numerics test - make it take a
number! Do the string conversion-to-int outside the function before it is
called.

>>def IsDivisibleBy7(number):#string variable
>
>See above, but maybe better still
>
>def isDivisibleByN(number, N):
> return not int(number)%N

Indeed, but again I would write an outright numeric expression and not rely on
"not(numeric-expression)".

>>def IsPrime(number):
>
>Google for the "sieve of eratosthenes"
>Or look it up on wikipedia.
>That will give one of several possible
>improvements to your algorithm.

Plus, it is incredibly easy to code the SoE in Python. So easy that I once did
it in about a minute in a Code Wars session. It is a lovely algorithm which is
easy to remember.

>>primelist=[]
>>
>>for i in range (11,200000,2):
>> number=str(i)
>> print "checking ",i
>>
>> if IsPrime(number):
>
>Note, you are starting with a number, then converting
>it to a string then in your functions converting it
>back to a number.
>That's crazy!

Indeed! Numbers throughout if at all possible.

Cheers,
Cameron Simpson <***@zip.com.au>
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-05-30 08:49:15 UTC
Permalink
On 30/05/15 00:37, Cameron Simpson wrote:

>> def IsDivisibleBy3(number):#string variable
>> return not int(number) % 3
>
> To illustrate that there isn't just One Right Way, I would code the
> above like this:
>
> def IsDivisibleBy3(number): #string variable
> return int(number) % 3 == 0
>
> Alan's code relies on "not" using an expressions "nonzero"ness as a
> boolean value. I _much_ prefer to directly state the test instead of
> relying on magic numeric=>boolean effects.

In this case I do too.
Especially since my version using the 'not' as well as
implied Truth value is less clear for a beginner such
as the OP. On reflection the explicit test is clearer.

If the 'not' had not been necessary I'd have been happy
without the == test. But introducing the not can be confusing.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Mirage Web Studio
2015-05-30 18:14:38 UTC
Permalink
On 2015-05-29 11:18 PM, Alan Gauld wrote:
> On 29/05/15 16:28, George wrote:
>
>> Below is a sample code i created.
>>
>> Can i better it any way?
>
> Of course. There is always improvements that can be made.
> But in your case there are quite a few!
>
>> def IsDivisibleBy3(number):#string variable
>> v=0
>> for c in number:
>> v=v+int(c)
>> if v%3==0:
>> return True
>> else:
>> return False
>
> def IsDivisibleBy3(number):#string variable
> return not int(number) % 3
>
>> def IsDivisibleBy7(number):#string variable
>
> See above, but maybe better still
>
> def isDivisibleByN(number, N):
> return not int(number)%N
>
>> def IsPrime(number):
>
> Google for the "sieve of eratosthenes"
> Or look it up on wikipedia.
>
> That will give one of several possible
> improvements to your algorithm.
>
>> primelist=[]
>>
>> for i in range (11,200000,2):
>> number=str(i)
>> print "checking ",i
>>
>> if IsPrime(number):
>
> Note, you are starting with a number, then converting
> it to a string then in your functions converting it
> back to a number.
> That's crazy!
>
> Also where do you store the primes less than 11?
> ie. 1,3,5,7
>
> HTH

Hello,

I thank u all for the replies. I have checked sieve of Eratosthenes
and have at first devised a solution using class-object, thinking it
easier, but it proved to be slower than my basic algorithm which i
submitted earlier, results were my algorithm processed 100 thousand
numbers in 80 or so sec but class based algorithm produced same result
in about 230 sec. After putting some thought i used dict for a change
with the sieve method and am able to produce primes for 1 million nos in
about 10 sec, which is better than both earlier algorithms. I am
submitting both.

My query is does using classes slowed it or the python hardcoded
algorithm for dicts was better?
and
if you had to implement the sieve algorithm how would u have done it.

Thank u


George

---------------------
class based algorithm
---------------------

import time

starttime=time.time()

class Number:
def __init__(self,number,p=None,n=None):
self.no=number
self.marked=None
self.p=p
self.n=n


node=Number(2,None,None)
start=node

counter=1
for i in range(3,110000):
counter+=1
newnode=Number(i,node)
node.n=newnode
node=newnode

node=start


while start != None:
if start.marked==True:
start=start.n
continue
else:
newprime = start.no
print ("\nNewPrime",newprime,"\nMarking no:")
tmpnode=start
while tmpnode !=None:
for i in range (newprime):
tmpnode=tmpnode.n
if tmpnode==None:
break
if tmpnode==None:
break
#print ( tmpnode.no, end=" ")
tmpnode.marked=True
start=start.n

print ("primes")
counter=0
while node!=None:
if not node.marked:
counter+=1
print(node.no)

node=node.n

print("--- %s seconds ---" % (time.time() - starttime), counter)


---------------------------
dict based algorithm
-------------------------



import time

starttime=time.time()

max=6000000

nodict={}

for i in range(2,max):
nodict[i]=0

for no in sorted(nodict.keys()):
x=no+no
while x<max:
nodict[x]=1
x+=no

counter=0
for no in sorted(nodict.keys()):
if nodict[no]==0:
counter+=1
print ("prime",no)

print("--- %s seconds ---" % (time.time() - starttime), counter)

---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Danny Yoo
2015-05-30 19:56:47 UTC
Permalink
I'll review the code a bit.


> import time
>
> starttime=time.time()
>
> class Number:
> def __init__(self,number,p=None,n=None):
> self.no=number
> self.marked=None
> self.p=p
> self.n=n

It would be helpful to document what the types of 'p' and 'n' are
here. Without any comments, I don't have good expectations on what to
expect yet. And without types, documentation in Python is doubly
crucial to help folks understand what to expect.



> node=Number(2,None,None)
> start=node
>
> counter=1
> for i in range(3,110000):
> counter+=1
> newnode=Number(i,node)
> node.n=newnode
> node=newnode


Ok, I see. Your structure earlier is meant to represent a
doubly-linked linked data structure. The fields 'n' stands for
"next", and 'p' stands for "previous", and you're constructing a
linked list of numbers, each of which should have links to the next
and previous elements. You're basically building up a thread of
nodes:

3 -> 4 -> 5 -> 6 -> ...

The code above doesn't seem to do the backwards linking with the 'p'
previous attribute. You might want to remove that from your data
structure definition if you're not using it.


... but that being said: do you need to represent linked structure
here? Linked structure is very flexible, but with certain costs:
"random" access is slower because you have to keep following links to
get from one end of the list to another point. If you want to mark
every 5th element in the collection, a linked list will force you to
walk every intermediate element in between, as you do later on in your
'while' loop. In contrast, an array-like structure will let you
access nodes by index very quickly: you can jump to every fifth
element directly, skipping over the other ones.


That is, it's important to note that the version with linked lists
isn't slow because it's using classes: it's slow fundamentally because
it's not actually taking advantage of the structure of numbers and the
ability to do quick, random access based on numbers.


You should see improvement by using an array-like list. Something like:

###################################
class Number:
def __init__(self,number):
self.no=number
self.marked=None

numbers = []
for i in range(110000):
newnumber = Number(i)
numbers.append(newnumber)
###################################

to initialize the basic structures. Here, numbers[3] will be the
Number(3), numbers[6] will be the Number(6), and so on. This direct
correspondence between index and the value at that index is what makes
array-like structures very useful. With this ability to directly
index, the code that does the sieving no longer needs to manually
march down links: it can index over the multiples of n.

Try it with the array-like list. You should find it instructive.
You've pretty much got the rest of the code ready: it should be almost
identical with the dict-based code you present later.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-05-30 23:34:46 UTC
Permalink
On 30/05/15 19:14, Mirage Web Studio wrote:

> and have at first devised a solution using class-object, thinking it
> easier, but it proved to be slower than my basic algorithm which i
> submitted earlier,

I'm not surprised. You seem to have a talent for finding complex
solutions to fairly simple problems :-)

> in about 230 sec. After putting some thought i used dict for a change
> with the sieve method and am able to produce primes for 1 million nos in
> about 10 sec, which is better than both earlier algorithms.

Yes, using built in data structures (implemented in highly
optimised C) will nearly always be faster than building
your own in interpreted Python.

> My query is does using classes slowed it or the python hardcoded
> algorithm for dicts was better?

See above.
classes are great for modelling concepts that are not available
in the language. But both numbers and collections are well
provided for in Python.

> if you had to implement the sieve algorithm how would u have done it.

Similarly to your dict approach but probably using lists.
After all lists are naturally indexed by numbers so using
a dict keyed by numbers doesn't add much value.

There are also a few tweaks you can do to improve efficiency
but you are pretty much there.

> ---------------------
> class based algorithm
> ---------------------
>
> import time
>
> starttime=time.time()
>
> class Number:
> def __init__(self,number,p=None,n=None):
> self.no=number
> self.marked=None
> self.p=p
> self.n=n

A class with no methods other than init() is usually a bad sign.
You should always think very carefully whether another data
structure might not be more suitable. The whole point of
objects is that you do things to them. That means you need
methods to make them useful.

> node=Number(2,None,None)

Note the extra work you make Python do to initialise a number
each time compared to just assigning a built-in integer object.

> start=node
>
> counter=1
> for i in range(3,110000):
> counter+=1
> newnode=Number(i,node)
> node.n=newnode
> node=newnode
>
> node=start
>
>
> while start != None:
> if start.marked==True:
> start=start.n
> continue
> else:
> newprime = start.no
> print ("\nNewPrime",newprime,"\nMarking no:")
> tmpnode=start
> while tmpnode !=None:

Notice you now have a loop within a loop. If you are
looking for speed that's another bad sign. Compare with
your dict version there are two loops but they are not
nested, they run one after the other (and the first one
could be avoided using a list)

> for i in range (newprime):

And now you have a third nested loop. Oh dear!

> tmpnode=tmpnode.n
> if tmpnode==None:
> break
> if tmpnode==None:
> break
> #print ( tmpnode.no, end=" ")
> tmpnode.marked=True
> start=start.n
>
> print ("primes")
> counter=0
> while node!=None:
> if not node.marked:
> counter+=1
> print(node.no)
>
> node=node.n
>
> print("--- %s seconds ---" % (time.time() - starttime), counter)

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Mirage Web Studio
2015-06-10 17:41:36 UTC
Permalink
On 2015-05-31 5:04 AM, Alan Gauld wrote:
> On 30/05/15 19:14, George wrote:

Excuse me please for replying late.

I got lists to use the method and it is more efficient and faster.
(Takes about 10 secs to process first 50 mil numbers)

But now another problem i seem to notice that only 1 core of my amd
Athlon X2 4core processor is being used. I suppose if all the four
cores are simultaneously used then the programme might run even faster.
Is there a way.

Kindly guide me.

Thank You.

George




---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-06-10 19:08:15 UTC
Permalink
In a message of Wed, 10 Jun 2015 23:11:36 +0530, Mirage Web Studio writes:
>
>
>
>On 2015-05-31 5:04 AM, Alan Gauld wrote:
>> On 30/05/15 19:14, George wrote:
>
>Excuse me please for replying late.
>
>I got lists to use the method and it is more efficient and faster.
>(Takes about 10 secs to process first 50 mil numbers)
>
>But now another problem i seem to notice that only 1 core of my amd
>Athlon X2 4core processor is being used. I suppose if all the four
>cores are simultaneously used then the programme might run even faster.
>Is there a way.
>
>Kindly guide me.
>
>Thank You.
>
>George

If you want, you can use the STM branch of the pypy interpreter. This
is a Python without the global interpreter lock. One of the tests
we did was, surprise, to calculate prime numbers.

See the blog post here:
http://morepypy.blogspot.se/2014/11/tornado-without-gil-on-pypy-stm.html

Laura

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-06-11 15:46:13 UTC
Permalink
Alas, PyPy STM only works for 64 bit linux for now. You are catching
the PyPy project at the edge of its 'bleeding edge research'. We think
in 1 or 2 releases windows users should be able to get it too, and
I will let you know when that happens. And no promises. These sorts of
things have a habit of being 'a simple matter of this and that' on paper
but actually much harder when it comes to writing code, so while it
will happen, when is an open question.

Sorry about that,
Laura
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-10 23:03:08 UTC
Permalink
On 10/06/15 18:41, Mirage Web Studio wrote:

> But now another problem i seem to notice that only 1 core of my amd
> Athlon X2 4core processor is being used. I suppose if all the four
> cores are simultaneously used then the programme might run even faster.
> Is there a way.

One of the problems with the "standard" CPython implementation is
that it only uses one CPU, even if you write threaded code. There
are implementations that address that, but I've never used them
so can't comment on whether they would help in your case.

I'm also not sure how Jython or IronPython handle things, they
might use their internal threading engines more effectively.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Michelle Meiduo Wu
2015-06-11 13:25:25 UTC
Permalink
Hi there,
I'm looking for a language to write test scripts for our application. I read some document and found Pytest can be used to write simpler code compared with using unittest in Python. Does anybody know what's other pros and cons of using these two for writing test scripts?
Thank you,Michelle



_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-11 17:55:23 UTC
Permalink
On 11/06/15 14:25, Michelle Meiduo Wu wrote:
> Hi there,

Hi,
Please start a new thread for a new subject. Do not reply to an
old thread. As it is, your query now appears buried in a
discussion about prime numbers.

> I'm looking for a language to write test scripts for our application.

Can you elaborate? What language is the app written in? On what OS?
What kind of testing do you want to do (performance, coverage,
functionality? something else?)

> I read some document and found Pytest can be used to write
> simpler code compared with using unittest in Python.

Yes, but PyTest (and unittest) are for testing units of Python
code. So they really only apply if your application is written
in Python. And in any case you should really have written
all the unit tests long before completing your application.

> Does anybody know what's other pros and cons

We need to understand more about what it is you want to test.
And what kinds of tests you want to write.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Cameron Simpson
2015-06-12 05:58:03 UTC
Permalink
On 11Jun2015 18:55, ***@btinternet.com <***@btinternet.com> wrote:
>On 11/06/15 14:25, Michelle Meiduo Wu wrote:
>>I read some document and found Pytest can be used to write
>> simpler code compared with using unittest in Python.
>
>Yes, but PyTest (and unittest) are for testing units of Python
>code. So they really only apply if your application is written
>in Python.

Though if you like the framework, you could write very thin Python wrappers
calling something else, and test them. Might be more trouble thatn it is worth
though.

>And in any case you should really have written
>all the unit tests long before completing your application.

Ah, the S-word :-)

Cheers,
Cameron Simpson <***@zip.com.au>
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-06-12 10:48:24 UTC
Permalink
In a message of Fri, 12 Jun 2015 15:58:03 +1000, Cameron Simpson writes:
>On 11Jun2015 18:55, ***@btinternet.com <***@btinternet.com> wrote:
>>On 11/06/15 14:25, Michelle Meiduo Wu wrote:
>>>I read some document and found Pytest can be used to write
>>> simpler code compared with using unittest in Python.
>>
>>Yes, but PyTest (and unittest) are for testing units of Python
>>code. So they really only apply if your application is written
>>in Python.

Actually, there are many people using pytest to test code written in
other languages. We wrote this:
https://pypi.python.org/pypi/oejskit

to test javascript inside browsers using pytest, for instance.

There is a version for running ruby tests and haskell tests that
I know about, and undoubtably many more that I do not.

Laura

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-12 16:26:42 UTC
Permalink
On 12/06/15 11:48, Laura Creighton wrote:

>>> Yes, but PyTest (and unittest) are for testing units of Python
>>> code. So they really only apply if your application is written
>>> in Python.
>
> Actually, there are many people using pytest to test code written in
> other languages. We wrote this:
> https://pypi.python.org/pypi/oejskit

I stand corrected but it still seems to me like its easier to
test in the language in which you develop. And most languages
have testing frameworks these days.

And when there are large paradigm differences in the languages
that's even more true. Testing JS (or even Haskell) in Python
seems like a weird choice!

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-06-12 20:08:55 UTC
Permalink
In a message of Fri, 12 Jun 2015 17:26:42 +0100, Alan Gauld writes:
>I stand corrected but it still seems to me like its easier to
>test in the language in which you develop. And most languages
>have testing frameworks these days.

Many languages have extremely poor testing frameworks. And when it
came time to do testing of the javascript code of the web client
of our python app, we found that there wasn't anything available
for doing exactly that.

>And when there are large paradigm differences in the languages
>that's even more true. Testing JS (or even Haskell) in Python
>seems like a weird choice!

I don't see this at all. By this logic, we would have been better
off writing a test framework in javascript to handle our javascript
code. But our experience is that, whenever possible, you are always
better off not using javascript and programming in another language.
Plus the real challenge here was testing our code in the collection
of browsers expected to run it. We found lots and lots and lots
of javascript bugs.

Py.test (as pytest was called at the time) was already happy to do
practically all of the heavy lifting for us. Pick this up, throw it
at something, get a result compare the result to what you are looking
for -- all of that was handled. So all we had to do was write the
stuff that actually went out and ran the things in the browser.

It's not a lot of code.
https://bitbucket.org/pedronis/js-infrastructure/src

Laura

>--
>Alan G
>Author of the Learn to Program web site
>http://www.alan-g.me.uk/
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Michelle Meiduo Wu
2015-06-12 21:51:55 UTC
Permalink
Yes, I also found Py.test is a good testing framework for automation testing.
Another one is Robot testing framework. It's also a testing framework based on Python Unittest framework.
Hard to pick one to use:)
Thank you,Michelle

> To: ***@btinternet.com
> From: ***@openend.se
> Date: Fri, 12 Jun 2015 22:08:55 +0200
> Subject: Re: [Tutor] Python&Pytest
> CC: ***@openend.se; ***@python.org
>
> In a message of Fri, 12 Jun 2015 17:26:42 +0100, Alan Gauld writes:
> >I stand corrected but it still seems to me like its easier to
> >test in the language in which you develop. And most languages
> >have testing frameworks these days.
>
> Many languages have extremely poor testing frameworks. And when it
> came time to do testing of the javascript code of the web client
> of our python app, we found that there wasn't anything available
> for doing exactly that.
>
> >And when there are large paradigm differences in the languages
> >that's even more true. Testing JS (or even Haskell) in Python
> >seems like a weird choice!
>
> I don't see this at all. By this logic, we would have been better
> off writing a test framework in javascript to handle our javascript
> code. But our experience is that, whenever possible, you are always
> better off not using javascript and programming in another language.
> Plus the real challenge here was testing our code in the collection
> of browsers expected to run it. We found lots and lots and lots
> of javascript bugs.
>
> Py.test (as pytest was called at the time) was already happy to do
> practically all of the heavy lifting for us. Pick this up, throw it
> at something, get a result compare the result to what you are looking
> for -- all of that was handled. So all we had to do was write the
> stuff that actually went out and ran the things in the browser.
>
> It's not a lot of code.
> https://bitbucket.org/pedronis/js-infrastructure/src
>
> Laura
>
> >--
> >Alan G
> >Author of the Learn to Program web site
> >http://www.alan-g.me.uk/
> _______________________________________________
> Tutor maillist - ***@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-06-12 23:04:26 UTC
Permalink
In a message of Fri, 12 Jun 2015 17:51:55 -0400, Michelle Meiduo Wu writes:
>Yes, I also found Py.test is a good testing framework for automation testing.
>Another one is Robot testing framework. It's also a testing framework based on Python Unittest framework.
>Hard to pick one to use:)

pytest (formerly py.test) came out of the pypy project. We needed
something rather more complicated to test our python code given that
we could not 'make a new pypy' every time we needed to run our tests.
(That took hours). So we built this thing. However, Michael Foord
ported a whole lot of what we needed (but the standard python
unittest module didn't have) back into unittest. And these days
lots of pytest has been rewritten enough times that I am no longer
familiar with the code. But at the time we needed to write oejskit,
py.test was the place to do it, in part because if we needed any
changes to py.test to accomodate what we wanted, we could just
sit down and write that too. Getting changes into something that
is in the python standard library is a lot harder -- the political
process is often orders of magnitude harder than just having working
code. But py.test was a much more swiftly changing codebase at
that time, and hardly anybody used it, and if we changed things so
that their tests broke, they mostly were also benefitting from the
new functionality -- and you don't develop on the bleeding edge
if you aren't in some way excited by such changes ...

These days, pytest is very stable, doesn't change much, and has
tons of satisfied users who would be very, very, very upset if we
upset their applecarts. So that doesn't happen. But it makes
decisions like yours harder. I personally find unittest to be
so verbose that it makes writing tests unpleasant. But other people
do not share my dislike of verboseness, so my advice, again, is to
try both for a week. And then see how you feel about it.

Note that if you decide you like pytest, it comes with a script that
I wrote to convert standard library unittests to pytest tests, so
you won't waste that work. I don't know of any scripts that go
the other way, but they may exist.

best of luck,
Laura


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-06-11 21:11:39 UTC
Permalink
In a message of Thu, 11 Jun 2015 09:25:25 -0400, Michelle Meiduo Wu writes:
>Hi there,
>I'm looking for a language to write test scripts for our application. I read some document and found Pytest can be used to write simpler code compared with using unittest in Python. Does anybody know what's other pros and cons of using these two for writing test scripts?
>Thank you,Michelle

It is very much a 'try it and see what you like' sort of thing.

Laura

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Stefan Behnel
2015-05-30 05:45:05 UTC
Permalink
Mirage Web Studio schrieb am 29.05.2015 um 17:28:
> Below is a sample code i created.
> Can i better it any way?

Absolutely. Prime number generation is a very well researched and fun to
implement topic. Thus many people have done it before.

See this for algorithmic improvements:

https://pypi.python.org/pypi/pyprimes

Stefan


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Continue reading on narkive:
Loading...