[Tutor] Performance measurement tools

2011-10-18 Thread Ashish Gaonker
Hi

Can you suggest the best performance measurent tools for python programs.
specifically thread based python modules.

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


Re: [Tutor] Performance measurement tools

2011-10-18 Thread Hugo Arts
On Tue, Oct 18, 2011 at 10:49 AM, Ashish Gaonker  wrote:
> Hi
> Can you suggest the best performance measurent tools for python programs.
> specifically thread based python modules.
> --
> Thanks & Regards
> Ashish Gaonker

I've heard very good things about yappi:

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


Re: [Tutor] Keyboard Module

2011-10-18 Thread Wayne Werner
On Mon, Oct 17, 2011 at 8:25 PM, Ryan Strunk  wrote:

> > Bloated carries the connotation of unnecessary code, but pygame provides
> a lot of functionality. You may not need it all, and using pygame may
> require you to import more than you need. But thats not quite the same
> as saying pygame is bloated.
> > And its not that huge that it should stop you using it.
> > If it works, why not?
> Thanks for the clarification on that. Would there be any  benefit to using
> Pygame in addition to other libraries for game development, or is it robust
> enough that it should be able to stand on its own?
>

If you're just worried about game development, Pygame is a very strong
toolkit. My recommendation would be to use it, and see if it suits your
needs. If you discover that there are things that Pygame doesn't make fairly
easy, that's when you should start looking for other solutions.

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


Re: [Tutor] Keyboard Module

2011-10-18 Thread Alan Gauld

On 18/10/11 02:25, Ryan Strunk wrote:


And its not that huge that it should stop you using it.



Thanks for the clarification on that. Would there be any  benefit to using
Pygame in addition to other libraries for game development,


It would not be unusual to use other libraries with PyGame.
But those would not normally include another gaming framework.

> ...is it robust enough that it should be able to stand on its own?

If by "robust enough" you mean is it complete in and of itself, then the 
answer is probably no. PyGame tackles one subset of tasks, those 
commonly encountered building games. But if you need to access web 
pages, or databases etc then you would need to use other modules.


If you mean robust in the sense of being reliable, not buggy,
then yes I believe PyGame is as robust as most Python libraries.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


[Tutor] Other ways to use raw_input function

2011-10-18 Thread amt
Greetings! I got stuck at an exercise from the book learn python the hard
way about raw_input function.(exercise 11.2)

After reading from docs.python.org I found out that this function can
perform standard output without a trailing newline.Also it can take an input
line, convert it as a string( stripping a trailing newline) and return it.


The exercise question is : Can you find other ways to use it?

Here is the original code from the book:

print "How old are you?",
age = raw_input()
print "How tall are you?",
height = raw_input()
print "How much do you weigh?",
weight = raw_input()

print "So, you're %r old, %r tall and %r heavy." % (
age, height, weight)



Now, the only different way I was able to use raw_input is:


age = raw_input("How old are you? ")

height = raw_input("How tall are you? ")
weight = raw_input("How much do you weigh? ")

print "So, you're %r old, %r tall and %r heavy." % (age, height, weight)



Are there any other ways of using it? If yes can you please give me a
detailed example so I can understand.

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


Re: [Tutor] close failed in file object destructor:

2011-10-18 Thread Navneet

Hi,

I am trying to search a list for prime numbers but it's throwing me an 
error at line no.25.

I am not able to figure what exactly is the prob
ne help ??? Error is this:
$ python "prime1 - Copy.py"
Unhandled exception in thread started by
Traceback (most recent call last):
  File "prime1 - Copy.py", line 25, in findPrime
close failed in file object destructor:
Error in sys.excepthook:

program is below: Numberlist is number range from 1..1000


import sys
import threading
import thread
import time

class FindPno():

c = []
f = open("A;\Numberlist.txt")
for i in f:
c.append(i)
f.close()
##print len(c)
##Thread should start here
def __init__(self):
thread.start_new_thread(self.findPrime,(1,))

def findPrime(self,tid):
global tlock
##print "I am here"
tlock = thread.allocate_lock()
##print "I am here"
tlock.acquire()
##print "I am here"
for i1 in range(len(c)):   ##this is the 25th line
for i2 in range(2,int(c[i1])):

if int(c[i1]) == 1:
print "I am here"
tlock.release()
break
if int(c[i1]) == 2:
print c
print "I am here"
tlock.release()
break
rem = int(c[i1])%i2
if rem == 0:
print "I am here"
tlock.release()
break
if i2 == int(c[i1])-1:
print int(c[i1]), "This is the Thread",tid
print "I am here"
tlock.release()

tlock.release()

if __name__ == '__main__':
a = FindPno()

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


Re: [Tutor] Other ways to use raw_input function

2011-10-18 Thread Wayne Werner
On Tue, Oct 18, 2011 at 12:58 PM, amt <0101...@gmail.com> wrote:

> Greetings! I got stuck at an exercise from the book learn python the hard
> way about raw_input function.(exercise 11.2)
>
> After reading from docs.python.org I found out that this function can
> perform standard output without a trailing newline.Also it can take an input
> line, convert it as a string( stripping a trailing newline) and return it.
>
>
> The exercise question is : Can you find other ways to use it?
>  Are there any other ways of using it? If yes can you please give me
> a detailed example so I can understand.
>

 The most common other way that I use it is on Windows. If you want the
terminal to stay open after your program has finished executing you can do
something like this:

# print_odds.py
# Prints some odd numbers

for x in xrange(1, 20, 2):
print x

raw_input("Press  to continue")



I've done similar things when debugging in a loop - instead of using print,
I use raw_input and it lets me see the output and waits for me to hit enter
to continue the loop.

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


Re: [Tutor] close failed in file object destructor:

2011-10-18 Thread Dave Angel

On 10/18/2011 01:59 PM, Navneet wrote:

Hi,

I am trying to search a list for prime numbers but it's throwing me an 
error at line no.25.

I am not able to figure what exactly is the prob
ne help ??? Error is this:
$ python "prime1 - Copy.py"
Unhandled exception in thread started by
Traceback (most recent call last):
  File "prime1 - Copy.py", line 25, in findPrime
close failed in file object destructor:
Error in sys.excepthook:

program is below: Numberlist is number range from 1..1000


import sys
import threading
import thread
import time

class FindPno():

c = []
f = open("A;\Numberlist.txt")
for i in f:
c.append(i)
f.close()
##print len(c)
##Thread should start here
def __init__(self):
thread.start_new_thread(self.findPrime,(1,))

def findPrime(self,tid):
global tlock
##print "I am here"
tlock = thread.allocate_lock()
##print "I am here"
tlock.acquire()
##print "I am here"
for i1 in range(len(c)):   ##this is the 25th line
for i2 in range(2,int(c[i1])):

if int(c[i1]) == 1:
print "I am here"
tlock.release()
break
if int(c[i1]) == 2:
print c
print "I am here"
tlock.release()
break
rem = int(c[i1])%i2
if rem == 0:
print "I am here"
tlock.release()
break
if i2 == int(c[i1])-1:
print int(c[i1]), "This is the Thread",tid
print "I am here"
tlock.release()

tlock.release()

if __name__ == '__main__':
a = FindPno()

I have no idea what you're trying to accomplish here.  The code is far 
too complex to solve the problem you've described, and very inefficient 
besides.  And threads won't help speed up a CPU-bound Python program of 
this type.


I also have no idea how you got the traceback you showed.  After I 
cleaned up a lot of other stuff, I get the error:

Traceback (most recent call last):
  File "prim.py", line 52, in 
a = FindPno()
  File "prim.py", line 19, in __init__
self.findPrime(0)
  File "prim.py", line 28, in findPrime
for i2 in range(2,int(c[i1])):
NameError: global name 'c' is not defined

And the problem with that is that the class attribute 'c' would need a 
prefix to address it in a method.  Simplest would be to say

 self.c
each place you have c.  or else just declare   global c inside the class 
scope.


Is this program transliterated from some other language?  Or were there 
some strange constraints in the original assignment?


After cleanup, the program does work, but I had to get rid of the thread 
stuff to make it work here.  As written, the program terminates before 
the extra thread does any work.


--

DaveA

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


Re: [Tutor] close failed in file object destructor:

2011-10-18 Thread bob gailer

Following written before Dave Angel's post but sent afterwards.

On 10/18/2011 1:59 PM, Navneet wrote:

Hi,

I am trying to search a list for prime numbers but it's throwing me an 
error at line no.25.

I am not able to figure what exactly is the prob
ne help ??? Error is this:
$ python "prime1 - Copy.py"
Unhandled exception in thread started by
Traceback (most recent call last):
  File "prime1 - Copy.py", line 25, in findPrime
close failed in file object destructor:
Error in sys.excepthook:

program is below: 


This program could not produce the above exception. Run it. You should get:
f = open("A;\Numberlist.txt")
IOError: [Errno 2] No such file or directory: 'A;\\Numberlist.txt'

due to the ; in the path name.

If you fix that then you should get:
Traceback (most recent call last):
  File "N:\python\findprime.py", line 25, in findPrime
for i1 in range(len(c)):   ##this is the 25th line
NameError: global name 'c' is not defined

Please fix these problems and return with a new program and the output 
from that program.



Numberlist is number range from 1..1000


Huh? What is Numberlist? I only see Numberlist.txt, which I assume is a 
file. How can a file be number range from 1..1000?


Please instead post a sample of the actual file. My *guess* is the file 
looks like:

1
2
3
etc.
My guess may be correct, but having to guess wastes all our time.




import sys
import threading
import thread
import time

class FindPno():

c = []
f = open("A;\Numberlist.txt")
for i in f:
c.append(i)
f.close()


replace the above 5 lines with
c = open(corrected path).readlines()
or even better
c = {int(x) for x in ("A;\corrected path)]
then you can dispense with

##print len(c)
##Thread should start here
def __init__(self):
thread.start_new_thread(self.findPrime,(1,))

def findPrime(self,tid):
global tlock
##print "I am here"
tlock = thread.allocate_lock()
##print "I am here"
tlock.acquire()
##print "I am here"
for i1 in range(len(c)):   ##this is the 25th line
for i2 in range(2,int(c[i1])):

if int(c[i1]) == 1:
print "I am here"
tlock.release()
break
if int(c[i1]) == 2:
print c

Why print the entire list?

print "I am here"
tlock.release()
break
rem = int(c[i1])%i2
if rem == 0:
print "I am here"
tlock.release()
break
if i2 == int(c[i1])-1:
print int(c[i1]), "This is the Thread",tid
print "I am here"
tlock.release()

tlock.release()

if __name__ == '__main__':
a = FindPno()


Why did you use a class? You don't need it, and it complicates things.
Why put some of the code in the mainline of the class and some in 
__init__. I see no need for that separation.
Since you use print statements to monitor progress how about addind 
something so you know which print statement was called. Perhaps print "I 
am here 1", print "I am here 2", etc.

Why have the file at all? Why not just start with c = range(1,1000)?

--
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] close failed in file object destructor:

2011-10-18 Thread bob gailer

One more thing -

tlock = thread.allocate_lock()

should be executed once before starting threads.

--
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] Other ways to use raw_input function

2011-10-18 Thread Alan Gauld

On 18/10/11 18:58, amt wrote:


After reading from docs.python.org  I found out
that this function can perform standard output without a trailing
newline.


It can print a prompt message, yes.

> Also it can take an input line, convert it as a string

It doesn't convert it to a string, it reads a string.
Specifically it reads the raw character string input
by the user. Thats why its called raw_input()


stripping a trailing newline) and return it.




The exercise question is : Can you find other ways to use it?


There are many ways to use it just as there are many ways to use any 
function. But the function will always do the same thing.


For example you can wrap the function in a type conversion to get 
numbers instead of strings:


quantity = int( raw_input("How many? ") )

Or you can use it to pause a program, for example if displaying a text 
file a page/screen at a time, or while debugging.





Here is the original code from the book:

print  "How old are you?",
age  =  raw_input()
print  "How tall are you?",
height  =  raw_input()
print  "How much do you weigh?",
weight  =  raw_input()
print  "So, you're%r  old,%r  tall and%r  heavy."  %  (
 age,  height,  weight)


Obviously you can remove the print statements.
And you could convert the height and weight values
to numerical ones (probably floats).


Now, the only different way I was able to use raw_input is:

age =raw_input("How old are you? ")
height=  raw_input("How tall are you?")
weight=  raw_input("How much do you weigh?")

print  "So, you're %r old, %r tall and %r heavy."  %  (age,  height,  weight)


Yes, that would be the normal way to do it, or with type conversion:

age= int( raw_input("How old are you? ") )
height = float( raw_input("How tall are you?") )
weight = float( raw_input("How much do you weigh?") )

print  "So, you're %d old, %f tall and %f heavy."  %  (age,  height, 
weight)


HTH

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


[Tutor] Socket not connecting

2011-10-18 Thread Jacob Bender
Dear Tutors,

 I'm the same person who asked about intruders getting into my computer
via socket. I tried copying a socket program from online, that worked on my
computer, and executed perfectly. However, I did not open a port for python,
but I do have an exception in my firewall. Anyhow, I called the same friend
and had him run a client program when I was running the host program. The
host program did absolutely nothing and my IP was programmed into the client
as the "Host IP". I took all necessary steps and even turned off my
antivirus, which didn't help at all. I also told my friend and now he has an
exception on his antivirus, but still nothing is working. I've been using
port 80 the whole time. Is there anything I did wrong, or a step I've
missed? Here is the source code for both the server and the client I was
talking about:

*Client:*
*
*
# Echo client program
import socket

HOST = '# my IP was once here'# The remote host
PORT = 80   # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)

*
*
*Server:*
*
*
# Echo server program
import socket

HOST = '' # Symbolic name meaning all available interfaces
PORT = 80  # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()

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


Re: [Tutor] Socket not connecting

2011-10-18 Thread Walter Prins
Hi Jacob,

On 19 October 2011 00:26, Jacob Bender  wrote:

>
>  I'm the same person who asked about intruders getting into my computer
> via socket. I tried copying a socket program from online, that worked on my
> computer, and executed perfectly. However, I did not open a port for python,
> but I do have an exception in my firewall. Anyhow, I called the same friend
> and had him run a client program when I was running the host program. The
> host program did absolutely nothing and my IP was programmed into the client
> as the "Host IP". I took all necessary steps and even turned off my
> antivirus, which didn't help at all. I also told my friend and now he has an
> exception on his antivirus, but still nothing is working. I've been using
> port 80 the whole time. Is there anything I did wrong, or a step I've
> missed? Here is the source code for both the server and the client I was
> talking about:
>


I presume you tried to make this work over the internet.  If so, a few
questions: Do you (or rather, does your PC) have a public IP address?  If
not, then did you configure your router to port forward port 80 from its IP
address (which is your public IP address) to your PC's (impliedly) local
private address?  And did you program your router's public IP address into
the client as the IP address to connect to?

Cheers,

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


[Tutor] Project

2011-10-18 Thread Narguess Dadfar
I have to project all shapefiles to NAD_1983_UTM_Zone_10N

I use Python 2.6

what codes I have used is as follow:


import arcpy
inFolder = arcpy.GetParameterAsText(0)
outC = arcpy.GetParameterAsText(1)
arcpy.env.workspace = inFolder
fcList = arcpy.ListFeatureClasses()
try:
desc = arcpy.Describe(outC)
spatialRef = desc.SpatialReference
for featureClass in fcList:
arcpy.Project_management (inFolder, outC)
rootName = " "
if  fc.endswith[".shp]:
rootName + fc[:4]
arcpy.AddMessage("\n" + "The files " + projectedFeatureClasses[:2] + "have
successfully been reprojected" + "\n")
except:
arcpy.AddMessage ("This project is incomplete")
arcpy.AddMessage(arcpy.Getmessage())

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