Re: [Tutor] using python to execute from Dir A in Dir B

2007-10-05 Thread Alan Gauld

"Andre Walker-Loud" <[EMAIL PROTECTED]> wrote

> this module.class - if someone with more experience would feel  
> inclined to provide an example, I would be very much appreciative.

The subprocess module documentation has several examples 
of using both the Popen class and the call function.

The given example for os.system is:

sts = os.system("mycmd" + " myarg")
==>
p = Popen("mycmd" + " myarg", shell=True)
sts = os.waitpid(p.pid, 0)To which you would need to add cwd=path making it:p = 
Popen("mycmd" + " myarg", shell=True, cwd=path)
sts = os.waitpid(p.pid, 0)The other difference using Popen is that it raises an 
OSError exception if the program fails to run so you should wrap the calls in a 
try/except if you want to catch that.HTH,


-- 
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] random number generator

2007-10-05 Thread Alan Gauld

"Jim Hutchinson" <[EMAIL PROTECTED]> wrote

> program that "should" work but doesn't. It generates a random number
> between 1 and 2 out to 10 decimal places.

Ok, Here's the problem. You are trying to compare two floating
point numbers. But python stores data in binary and displays
it in decimal. A lot of decimal numbers cannot be exactly
represented in binary so what you see displayed is an
approximation of the actual number stored in memory.

> while (guess != number):

So when you try to compare the number you entered with
the number stored in memory they are not quite the same.
The normal way of dealing with this in programs is to define
a very small number (often called epsilon or e) and check if
your guess is plus or minus e from the target. So your
code would become

e = 0.01
while not( guess - e < number < guess + e ):
# ...your code here

I briefly mention this in the Raw Materials topic of my tutor
when discussing Real numbers

HTH,

-- 
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] another quickie

2007-10-05 Thread Alan Gauld
"max baseman" <[EMAIL PROTECTED]> wrote

> do is open a text file, write to it, than save it. so far i have a
> GUI with the ability to right text (nothing amazing), but i don't
> know how to have it display the text from a text file any help would
> be great
>
> from Tkinter import *
> top=Tk()
> F=Frame(top)
> F.pack()
> F.txtBox=Text(top)
> F.txtBox.pack()
> F.pack()

Ok This gets you the basic text box. Now you need to open
the file, read it into a variable and insert that into the text box.
I assume you already know how to open the file and read it
into a variable. (If not see File handling in my tutorial)

To insert it into the text box use the insert method as
shown in my case study:

self.txtBox.insert(END, resultStr)

If you want to insert it at a specific location(like the start)
you can replace END with 1.0 as also shown in the
case study for deleting the content:

self.txtBox.delete(1.0, END)

Did you check the Tkinter reference link:

http://www.pythonware.com/library/tkinter/introduction/x8309-patterns.htm

It shows how to manipulate the contents of the Text
widget in various ways. Including inserting text.

PS. If you don't want to hard code the file name you can use
a Tkinter FileOpen standard dialog to get the filename(and path)
from the user prior to opening it.

http://www.pythonware.com/library/tkinter/introduction/x1164-data-entry.htm


To save your changes use the filesaveas dialog to get the target
filename(optional) and then Text.select to get the text out of the 
widget
and then the usual Python file methods to write the text out to file.

All the information you need should be in the links in this message.

HTH,

-- 
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] using python to execute from Dir A in Dir B

2007-10-05 Thread Alan Gauld
Not sure what happened to the formatting here... It should be:

-
"Alan Gauld" <[EMAIL PROTECTED]> wrote

The given example for os.system is:

sts = os.system("mycmd" + " myarg")
==>
p = Popen("mycmd" + " myarg", shell=True)
sts = os.waitpid(p.pid, 0)

To which you would need to add cwd=path making it:

p = Popen("mycmd" + " myarg", shell=True, cwd=path)
sts = os.waitpid(p.pid, 0)


The other difference using Popen is that it raises an
OSError exception if the program fails to run so you
should wrap the calls in a try/except if you want to
catch that.



HTH,


-- 
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
> 


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


[Tutor] Controlling the number of decimal places "Representation Error"

2007-10-05 Thread Carnell, James E

Question:
Is it a bad practice to avoid 0.1 representation errors
(0.101) by just doing the following?

#NOTE: all the myVariableName stuff is because Outlook keeps changing
everything I type.

#I need 2 decimal places (my input number shouldn't be over 255)

myNum = 1
myDiv = 3

#1/3 =  and I remember that the last 3 digits are decimals the
very last can have int() problems

myResult = (myNum * 1000)/myDiv 

myArray = []

For I in range( wookie loads ):
 myArray.append(myResult) #this thing can get big, but I don't
need that much precision
  #but I do need to keep it small since
it might be running on
  #an embedded system


Sincerely,

James Carnell

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


[Tutor] Python Magazine

2007-10-05 Thread Jeff Johnson
I've just been told by the editors at Python Magazine that the first
issue is out. It's all-electronic so anyone can download and read it.
Let them know what you think:

 http://www.pythonmagazine.com/c/issue/2007/10

You can also subscribe for print + online.

-- 
Jeff

Jeff Johnson
[EMAIL PROTECTED]
SanDC, Inc.
623-582-0323
Fax 623-869-0675
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Controlling the number of decimal places "Representation Error"

2007-10-05 Thread Kent Johnson
Carnell, James E wrote:
> Question:
> Is it a bad practice to avoid 0.1 representation errors
> (0.101) by just doing the following?

I'm afraid I don't understand either the problem you are trying to solve 
or your proposed solution. Can you say more about it?

Kent

> 
> #NOTE: all the myVariableName stuff is because Outlook keeps changing
> everything I type.
> 
> #I need 2 decimal places (my input number shouldn't be over 255)
> 
> myNum = 1
> myDiv = 3
> 
> #1/3 =  and I remember that the last 3 digits are decimals the
> very last can have int() problems
> 
> myResult = (myNum * 1000)/myDiv 
> 
> myArray = []
> 
> For I in range( wookie loads ):
>  myArray.append(myResult) #this thing can get big, but I don't
> need that much precision
>   #but I do need to keep it small since
> it might be running on
>   #an embedded system
> 
> 
> Sincerely,
> 
> James Carnell
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

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


[Tutor] finding square roots

2007-10-05 Thread Elaine
Does anyone know which is the more efficient way of
finding a square root in Python:

sqrt(x)  or   x ** 0.5

???

Thanks,
 Elaine


   

Building a website is a piece of cake. Yahoo! Small Business gives you all the 
tools to get online.
http://smallbusiness.yahoo.com/webhosting 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finding square roots

2007-10-05 Thread Eric Brunson
Elaine wrote:
> Does anyone know which is the more efficient way of
> finding a square root in Python:
>
> sqrt(x)  or   x ** 0.5
>   

I dunno, let's check:

 >>> import timeit
 >>> t = timeit.Timer( "12 ** .5" )
 >>> print t.timeit(1000)
2.29147315025
 >>> t = timeit.Timer( "sqrt(12)", "from math import sqrt" )
 >>> print t.timeit(1000)
7.17679214478

Looks like ** is about three times faster than sqrt(), but that could be 
the function overhead.

> ???
>
> Thanks,
>  Elaine
>
>
>
> 
> Building a website is a piece of cake. Yahoo! Small Business gives you all 
> the tools to get online.
> http://smallbusiness.yahoo.com/webhosting 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   

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


[Tutor] questions about previous emails

2007-10-05 Thread Fangwen Lu
Dear all-

I am a new-comer to [EMAIL PROTECTED] I guess  a lot of my future questions may 
have been asked by others already. 

As I am a new-comer, I don't have the previous emails. 
 
I wonder whether there is a way for searching for previous questions and 
answers so that I don't need to ask simple questions to overload the message 
flows. 

Thanks.

Fangwen

   
-
Be a better Heartthrob. Get better relationship answers from someone who knows.
Yahoo! Answers - Check it out. ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finding square roots

2007-10-05 Thread Alan Gauld

"Eric Brunson" <[EMAIL PROTECTED]> wrote

> >>> import timeit
> >>> t = timeit.Timer( "12 ** .5" )
> >>> print t.timeit(1000)
> 2.29147315025
> >>> t = timeit.Timer( "sqrt(12)", "from math import sqrt" )
> >>> print t.timeit(1000)
> 7.17679214478
>
> Looks like ** is about three times faster than sqrt(), but that 
> could be
> the function overhead.

>>> timeit.Timer("import math; math.sqrt(64)").timeit(1000)
0.002298138362618829
>>> timeit.Timer("import math; 64 ** 0.5").timeit(1000)
0.018225722100503106

I get the opposite result by includsing the importt in both
timing sesions. Looks like it was the import that was slow.
But that shouldn't affect real world usage sinmce you only
do it once...

Alan G.


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


Re: [Tutor] questions about previous emails

2007-10-05 Thread Eric Brunson
Fangwen Lu wrote:
> Dear all-
>
> I am a new-comer to [EMAIL PROTECTED] I guess  a lot of my future 
> questions may have been asked by others already.
>
> As I am a new-comer, I don't have the previous emails.
>
> I wonder whether there is a way for searching for previous questions 
> and answers so that I don't need to ask simple questions to overload 
> the message flows.

Click on the link at the bottom of every message and there is a link in 
the middle of the page directing you to the archives.

Hope the helps,
e.

>
> Thanks.
>
> Fangwen
>
> 
> Be a better Heartthrob. Get better relationship answers 
> from
>  
> someone who knows.
> Yahoo! Answers - Check it out.
> 
>
> ___
> 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] finding square roots

2007-10-05 Thread Eric Brunson
Alan Gauld wrote:
> "Eric Brunson" <[EMAIL PROTECTED]> wrote
>
>   
> import timeit
> t = timeit.Timer( "12 ** .5" )
> print t.timeit(1000)
>   
>> 2.29147315025
>> 
> t = timeit.Timer( "sqrt(12)", "from math import sqrt" )
> print t.timeit(1000)
>   
>> 7.17679214478
>>
>> Looks like ** is about three times faster than sqrt(), but that 
>> could be
>> the function overhead.
>> 
>
>   
 timeit.Timer("import math; math.sqrt(64)").timeit(1000)
 
> 0.002298138362618829
>   
 timeit.Timer("import math; 64 ** 0.5").timeit(1000)
 
> 0.018225722100503106
>
> I get the opposite result by includsing the importt in both
> timing sesions. Looks like it was the import that was slow.
> But that shouldn't affect real world usage sinmce you only
> do it once...
>   

Maybe I'm mis-remembering the documentation of timeit.  I thought the 
second argument to the Timer constructor was "setup code", i.e. code to 
be executed once before executing the first argument in the timing loop.

> Alan G.
>
>
> ___
> 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] questions about previous emails

2007-10-05 Thread Alan Gauld

"Fangwen Lu" <[EMAIL PROTECTED]> wrote

> I wonder whether there is a way for searching for previous questions
> and answers so that I don't need to ask simple questions to
> overload the message flows.

There are at leasty 3 archives, two of which are searchable.

The official archive on the Pyhon.org web site (not searchable)

The Activestate archive (searchable)

The Gmane.org news feed archive (searchable)

The latter allows you to receive group email as if it were
a newsgroup rather than email which some (including me!)
find more convenient. (There is also a web interface if you
need it)

Alan G.


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


Re: [Tutor] finding square roots

2007-10-05 Thread Alan Gauld

"Eric Brunson" <[EMAIL PROTECTED]> wrote

>> t = timeit.Timer( "sqrt(12)", "from math import sqrt" )
>> print t.timeit(1000)
>>> 7.17679214478

> timeit.Timer("import math; math.sqrt(64)").timeit(1000)
>> 0.002298138362618829
> timeit.Timer("import math; 64 ** 0.5").timeit(1000)
>> 0.018225722100503106
>>
>> I get the opposite result by includsing the importt in both
>> timing sessions. Looks like it was the import that was slow.
>
> Maybe I'm mis-remembering the documentation of timeit.  I thought 
> the
> second argument to the Timer constructor was "setup code", i.e. code 
> to
> be executed once before executing the first argument in the timing 
> loop.

It seems to suggest that, so I don't understand the reason,
but in any case its a fairer comparison to include the import in both 
tests.
Doing it your way gives me:

>>> timeit.Timer("64 ** 0.5", "import math").timeit(1000)
0.00039243745641215355
>>> timeit.Timer("math.sqrt(64)", "import math").timeit(1000)
0.00062668800364917843
>>>

Which favours ** but by a smaller margin.

Interesting,


-- 
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] finding square roots

2007-10-05 Thread wesley chun
oddly enough, i get different results doing it the "old-fashioned" way:

$ sqrt.py
using **
0.000447034835815
using sqrt()
0.000399112701416

$ sqrt.py
using **
0.00043797492981
using sqrt()
0.000399827957153

$ sqrt.py
using **
0.00043797492981
using sqrt()
0.000402927398682

here's the code snippet... *seems* right to me, but let me know u see any flaws:

from math import sqrt
from time import time

print "using **"
i = 0
now = time()
while i < 1000:
64 ** 0.5
i += 1
print time()-now

print "using sqrt()"
i = 0
now = time()
while i < 1000:
sqrt(64)
i += 1
print time()-now

-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] "alias" instance of logger

2007-10-05 Thread Robert Jackson
I'm working with an instance of a Python logger.



Some code:



log = 
logging.basicConfig(level=logging.DEBUG,filename="/home/richard/templog",filemode='w')



Later in my program I do:

log.info("finished step 4.")



Python spits out this error:



Traceback (most recent call last):

  File "", line 1, in 

AttributeError: 'NoneType' object has no attribute 'info'

I CAN, however, do:
logging.info("finished step 4.")

And it works.



What confuses me about this is that I can do something like this:



# call another function and pass it the logger instance:

foo(logging)



And, if I define foo() like this:



def foo(log):

   # this works fine!

   log.info("finished step 4.")



The log.info works fine inside of foo().



Why is it that I can pass logging as an instance into a function, and
use whatever instance name I wants inside of foo(), but I can't assign
an "alias" for the logging instance inside of main() (by doing instancealias = 
logging.basic())?

/r





   

Boardwalk for $500? In 2007? Ha! Play Monopoly Here and Now (it's updated for 
today's economy) at Yahoo! Games.
http://get.games.yahoo.com/proddesc?gamekey=monopolyherenow  

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


Re: [Tutor] "alias" instance of logger

2007-10-05 Thread Robert Jackson
By the way, I could have SWORN that I've done this successfully in the past in 
another program.  I am relatively certain I have used log.LEVEL() in some of my 
programs in the past, but I can't figure out how I did it.

/r

- Original Message 
From: Robert Jackson <[EMAIL PROTECTED]>
To: tutor@python.org
Sent: Friday, October 5, 2007 10:22:26 PM
Subject: "alias" instance of logger

I'm working with an instance of a Python logger.



Some code:



log = 
logging.basicConfig(level=logging.DEBUG,filename="/home/richard/templog",filemode='w')



Later in my program I do:

log.info("finished step 4.")



Python spits out this error:



Traceback (most recent call last):

  File "", line 1, in 

AttributeError: 'NoneType' object has no attribute 'info'

I CAN, however, do:
logging.info("finished step 4.")

And it works.



What confuses me about this is that I can do something like this:



# call another function and pass it the logger instance:

foo(logging)



And, if I define foo() like this:



def foo(log):

   # this works fine!

   log.info("finished step 4.")



The log.info works fine inside of foo().



Why is it that I can pass logging as an instance into a function, and
use whatever instance name I wants inside of foo(), but I can't assign
an "alias" for the logging instance inside of main() (by doing instancealias = 
logging.basic())?

/r





   

Boardwalk for $500? In 2007? Ha! Play Monopoly Here and Now (it's updated for 
today's economy) at Yahoo! Games.
http://get.games.yahoo.com/proddesc?gamekey=monopolyherenow





   

Take the Internet to Go: Yahoo!Go puts the Internet in your pocket: mail, news, 
photos & more. 
http://mobile.yahoo.com/go?refer=1GNXIC

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


Re: [Tutor] "alias" instance of logger

2007-10-05 Thread Kent Johnson
Robert Jackson wrote:

> log = 
> logging.basicConfig(level=logging.DEBUG,filename="/home/richard/templog",filemode='w')

logging.basicConfig() does not return a logger, it returns None.

> Later in my program I do:
> 
> log.info("finished step 4.")
> 
> Python spits out this error:
> Traceback (most recent call last):
> 
>   File "", line 1, in 
> 
> AttributeError: 'NoneType' object has no attribute 'info'

Because log is None and None does not have an 'info' attribute.
> 
> I CAN, however, do:
> logging.info("finished step 4.")

Sure, logging is the logging module and it has an info function. This is 
normal usage.

> What confuses me about this is that I can do something like this:
> 
> # call another function and pass it the logger instance:
> 
> foo(logging)

You are not passing a logger, you are passing the logging module itself.

> And, if I define foo() like this:
> 
> def foo(log):
> 
># this works fine!
> 
>log.info("finished step 4.")
> 
> The log.info works fine inside of foo().

Sure, because inside of foo, the name 'log' is bound to the actual 
parameter passed in which is the logging module.

> Why is it that I can pass logging as an instance into a function, and
> use whatever instance name I wants inside of foo(),

That is true of every function parameter, you can give them whatever 
name you want.

> but I can't assign
> an "alias" for the logging instance inside of main() (by doing instancealias 
> = logging.basic())?

Because you are not aliasing logging here. Just assign
log = logging

though I don't see the point, just use logging.info() etc.

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