Re: [Tutor] I can't believe this needs to be this complex

2008-08-03 Thread Alan Gauld

Dick Moores [EMAIL PROTECTED] wrote

code I time. What's an example of code that would have System be 
greater than zero? And what's the distinction between User and 
System? (I'm using Win XP, if that's relevant.)


It may be that XP doesn't report System time.

Well, here's one that reads in the text of Dickens' _Little Dorrit_ 
and returns the word count:


IPython CPU timings (estimated):
Total runs performed: 10
  Times :  Total   Per run
  User  : 5.94446752311 s, 0.594446752311 s.
  System:0.0 s,0.0 s.


I would definitely have expected some system time there.

This one is a Gui that has an Exit button. I called it and then hit 
the button:


IPython CPU timings (estimated):
  User  : 10.5294301371 s.
  System:0.0 s.


That suggests that it is counting elapsed time as user time
in which case I'm not sure how it counts system time.
It may be an XP issue.

Anyone got different results under Linux/MacOS?

Alan G.


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


Re: [Tutor] Developing Macro: Is it possible in Python?

2008-08-03 Thread Alan Gauld

Federo [EMAIL PROTECTED] wrote


Is it possible to do macro with Python?


Macro means different things in different context.


Macro should be able to click on given x,y screen
location (one click, double click), drag scroll bar up / down etc..


It seems that you are referring to simulating user actions.

The answer then is yes its possible but not trivial.
You can use the low level Windows API to send messages
to windows simulating mouse clicks etc. But it will usually require
monitoring the messages first using a program like Windows Spy.
The result is extremely fragile in that any change in the windows
environment can render your code useless.


Is there any other way beside macro to control windows
based application?


If it supports COM then you can use COM objects to
manipulate things directly. The winall package has support
for COM or you can use the API directly via ctypes.

I confess I tend to use VBScript for this kind of thing,
IMHO it plays with Windows much more simply than Python.

Alan G. 



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


Re: [Tutor] Output never stops

2008-08-03 Thread Alan Gauld


David [EMAIL PROTECTED] wrote 


the output never stops when I run this in a terminal


choice = raw_input(Enter the name Lary or Joan (-1 to quit): )
while choice != '-1': 
  person = {'Lary': 43,'Joan': 24}

  if choice == 'Lary':
  print Lary's age is:, person.get('Lary')
  elif choice == 'Joan':
  print Joan's age is:, person.get('Joan')
  else:
   print 'Bad Code'


You set choice outside the while loop then never change 
it so the while test will always be true and loop forever.
You need to copy the raw_input line into the body of 
the while loop to reset choice.


Also for good style you should move the person = {} line outside 
the loop since you only want to set up the dictionary once, 
not every time you execute the loop. The dictionary never 
changes so recreating it every time is wasteful.


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] I can't believe this needs to be this complex

2008-08-03 Thread Dick Moores

At 01:00 AM 8/3/2008, Alan Gauld wrote:

Dick Moores [EMAIL PROTECTED] wrote

code I time. What's an example of code that would have System be 
greater than zero? And what's the distinction between User and 
System? (I'm using Win XP, if that's relevant.)


It may be that XP doesn't report System time.

Well, here's one that reads in the text of Dickens' _Little Dorrit_ 
and returns the word count:


IPython CPU timings (estimated):
Total runs performed: 10
  Times :  Total   Per run
  User  : 5.94446752311 s, 0.594446752311 s.
  System:0.0 s,0.0 s.


I would definitely have expected some system time there.

This one is a Gui that has an Exit button. I called it and then hit 
the button:


IPython CPU timings (estimated):
  User  : 10.5294301371 s.
  System:0.0 s.


That suggests that it is counting elapsed time as user time
in which case I'm not sure how it counts system time.
It may be an XP issue.

Anyone got different results under Linux/MacOS?


_Little Dorrit_ is available in us-ascii at 
http://www.gutenberg.org/etext/963; _War and Peace_ at 
http://www.gutenberg.org/etext/2600.


Dick


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


Re: [Tutor] I can't believe this needs to be this complex

2008-08-03 Thread Kent Johnson
On Sun, Aug 3, 2008 at 4:00 AM, Alan Gauld [EMAIL PROTECTED] wrote:
 Dick Moores [EMAIL PROTECTED] wrote

 code I time. What's an example of code that would have System be greater
 than zero? And what's the distinction between User and System? (I'm using
 Win XP, if that's relevant.)

 It may be that XP doesn't report System time.

From the IPython help for 'run':

-t: print timing information at the end of the run.  IPython will
giveyou an estimated CPU time consumption for your script, which
underUnix uses the resource module to avoid the wraparound
problems oftime.clock().  Under Unix, an estimate of time spent on
system tasksis also given (for Windows platforms this is reported
as 0.0).

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


[Tutor] Decimals 'not equal to themselves' (e.g. 0.2 equals 0.200000001)

2008-08-03 Thread CNiall
I am very new to Python (I started learning it just yesterday), but I 
have encountered a problem.


I want to make a simple script that calculates the n-th root of a given 
number (e.g. 4th root of 625--obviously five, but it's just an example 
:P), and because there is no nth-root function in Python I will do this 
with something like x**(1/n).


However, with some, but not all, decimals, they do not seem to 'equal 
themselves'. This is probably a bad way of expressing what I mean, so 
I'll give an example:

 0.5
0.5
 0.25
0.25
 0.125
0.125
 0.2
0.20001
 0.33
0.33002

As you can see, the last two decimals are very slightly inaccurate. 
However, it appears that when n in 1/n is a power of two, the decimal 
does not get 'thrown off'. How might I make Python recognise 0.2 as 0.2 
and not 0.20001?


This discrepancy is very minor, but it makes the whole n-th root 
calculator inaccurate. :\


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


Re: [Tutor] I can't believe this needs to be this complex

2008-08-03 Thread Dick Moores

At 05:15 AM 8/3/2008, Kent Johnson wrote:

On Sun, Aug 3, 2008 at 4:00 AM, Alan Gauld [EMAIL PROTECTED] wrote:
 Dick Moores [EMAIL PROTECTED] wrote

 code I time. What's an example of code that would have System be greater
 than zero? And what's the distinction between User and System? 
(I'm using

 Win XP, if that's relevant.)

 It may be that XP doesn't report System time.

From the IPython help for 'run':

-t: print timing information at the end of the run.  IPython will
giveyou an estimated CPU time consumption for your script, which
underUnix uses the resource module to avoid the wraparound
problems oftime.clock().  Under Unix, an estimate of time spent on
system tasksis also given


Thanks, Kent. RTFM!


(for Windows platforms this is reported
as 0.0).


I wonder why it is reported for Windows at all?

Dick


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


Re: [Tutor] Decimals 'not equal to themselves' (e.g. 0.2 equals 0.200000001)

2008-08-03 Thread Thomas Pani

CNiall wrote:
I want to make a simple script that calculates the n-th root of a given 
number (e.g. 4th root of 625--obviously five, but it's just an example 
:P), and because there is no nth-root function in Python I will do this 
with something like x**(1/n).


Side note: of course there are python built-in ways to do that. You just 
named one yourself:


In [6]: 625**(1.0/4)
Out[6]: 5.0

also:

In [9]: pow(625, 1.0/4)
Out[9]: 5.0

However, with some, but not all, decimals, they do not seem to 'equal 
themselves'.


As you can see, the last two decimals are very slightly inaccurate. 
However, it appears that when n in 1/n is a power of two, the decimal 
does not get 'thrown off'. How might I make Python recognise 0.2 as 0.2 
and not 0.20001?



You just can't store 0.1 as a binary floating point.
You might want to read:
http://www.network-theory.co.uk/docs/pytut/FloatingPointArithmeticIssuesandLimitations.html
http://www.network-theory.co.uk/docs/pytut/RepresentationError.html

The decimal module provides decimal floating point arithmetic:
http://docs.python.org/lib/module-decimal.html

like in:
In [1]: 0.2 * 2
Out[1]: 0.40002

In [2]: from decimal import Decimal

In [3]: Decimal('0.2') * 2
Out[3]: Decimal(0.4)

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


Re: [Tutor] I can't believe this needs to be this complex

2008-08-03 Thread Dick Moores

At 05:15 AM 8/3/2008, Kent Johnson wrote:

On Sun, Aug 3, 2008 at 4:00 AM, Alan Gauld [EMAIL PROTECTED] wrote:
 Dick Moores [EMAIL PROTECTED] wrote

 code I time. What's an example of code that would have System be greater
 than zero? And what's the distinction between User and System? 
(I'm using

 Win XP, if that's relevant.)

 It may be that XP doesn't report System time.

From the IPython help for 'run':

-t: print timing information at the end of the run.  IPython will
giveyou an estimated CPU time consumption for your script, which
underUnix uses the resource module to avoid the wraparound
problems oftime.clock().  Under Unix, an estimate of time spent on
system tasksis also given


Thanks, Kent. RTFM!


(for Windows platforms this is reported
as 0.0).


I wonder why it is reported for Windows at all?

Dick


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


Re: [Tutor] Output never stops

2008-08-03 Thread David

Alan Gauld wrote:


David [EMAIL PROTECTED] wrote

the output never stops when I run this in a terminal


choice = raw_input(Enter the name Lary or Joan (-1 to quit): )
while choice != '-1':   person = {'Lary': 43,'Joan': 24}
  if choice == 'Lary':
  print Lary's age is:, person.get('Lary')
  elif choice == 'Joan':
  print Joan's age is:, person.get('Joan')
  else:
   print 'Bad Code'


You set choice outside the while loop then never change it so the 
while test will always be true and loop forever.
You need to copy the raw_input line into the body of the while loop to 
reset choice.


Also for good style you should move the person = {} line outside the 
loop since you only want to set up the dictionary once, not every time 
you execute the loop. The dictionary never changes so recreating it 
every time is wasteful.


HTH,


Thanks Alan, also your tutorial/book is a big help. I think I got it :)
#!/usr/bin/python

person = {'Lary':43,'Joan':24}

choice = 0
while choice != '-1':
   if choice == '':
   print You must enter Lary or Joan to continue! (-1 to quit): 
   choice = raw_input(
   Who's age do want to know, Lary or Joan? (-1 to quit): )
   if choice == 'Lary':
   print Lary's age is:, person.get('Lary')

   elif choice == 'Joan':
   print Joan's age is:, person.get('Joan')

   else:
   print Goodbye!


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

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


Re: [Tutor] Decimals 'not equal to themselves' (e.g. 0.2 equals 0.200000001)

2008-08-03 Thread Kent Johnson
On Sun, Aug 3, 2008 at 10:04 AM, CNiall [EMAIL PROTECTED] wrote:
 I want to make a simple script that calculates the n-th root of a given
 number (e.g. 4th root of 625--obviously five, but it's just an example :P),
 and because there is no nth-root function in Python I will do this with
 something like x**(1/n).

 However, with some, but not all, decimals, they do not seem to 'equal
 themselves'. This is probably a bad way of expressing what I mean, so I'll
 give an example:

 0.125
 0.2
 0.20001
 0.33
 0.33002

 As you can see, the last two decimals are very slightly inaccurate. However,
 it appears that when n in 1/n is a power of two, the decimal does not get
 'thrown off'. How might I make Python recognise 0.2 as 0.2 and not
 0.20001?

This is a limitation of floaating point numbers. A discussion is here:
http://docs.python.org/tut/node16.html

Your root calculator can only find answers that are as accurate as the
representation allows.

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


Re: [Tutor] Decimals 'not equal to themselves' (e.g. 0.2 equals0.200000001)

2008-08-03 Thread Alan Gauld

CNiall [EMAIL PROTECTED] wrote

However, with some, but not all, decimals, they do not seem to 
'equal themselves'. This is probably a bad way of expressing what I 
mean, so I'll give an example:

 0.5
0.5
 0.2
0.20001


As you can see, the last two decimals are very slightly inaccurate. 
However, it appears that when n in 1/n is a power of two, the 
decimal does not get 'thrown off'.


And that is the clue. Computers represent data as binary, ie powers
of two. If a number cannot be expressed exactly as a power of two
then a computer (any binary computer!) cannot represent the number
exactly. It is the same in natural arithmetic where we use base 10.
We cannot exactly represent numbers like 1/3 as a decimal number
we have to approximate to 0. Likewise with 1/7 etc.

In most cases the approximation is good enough and we just live
with it. (But its not a good idea to use floating point numbers to
represent money!). If you do need exact numbers you can use the
decimal module which will do what you want but at the expense
of adding complexity.

How might I make Python recognise 0.2 as 0.2 and not 
0.20001?


Mostl;y it won;t matter, what you will want is to be able to print
it as 0.2. You can do this in a number of ways but the most flexible
is to use a format string:


x = 0.2
print %6f % x

0.2000

The percent inside the string is a marker which is substituted
by the value after the percent outside the string (ie x in this case)
The number following the first % is the length of
representation - 6 chars in this case.

There are several other numbers and symbols you can use to
coerce the representation to be as you wish - left/roight aligned,
leading zeros, scientific notation etc

Search the Python docs for format and you should find the
details...

The other thing to watch is when comparing float values.


y = 1 - 0.8
y == x

False

e = 0.01
x-e  y  x+e

True


You can find out more about format stringsin the Simple Sequences
topic of my tutorial.
You can find out a little bit more about floating point numbers in the
Raw Data topic of my tutorial under the heading 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] Developing Macro: Is it possible in Python?

2008-08-03 Thread Paul McGuire
I have used pywinauto for such tasks in the past.

http://pywinauto.openqa.org/
 
In my case, I used pywinauto to automate mouse clicks on a browser in order
to auto-play a Flash game running in the browser.  I had to use PIL to take
screenshots and then process images to read the screen.

-- Paul

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


Re: [Tutor] Developing Macro: Is it possible in Python?

2008-08-03 Thread Emile van Sebille

Federo wrote:
snip

Above actions can be easily performed using Macro Scheduler. I am looking for 
possibility to do the same with Python?


Hi Federo,

I regularly combine Macro Scheduler with python by having my python code 
write mSched scripts.  I find the combination of the two particularly 
adept at controlling legacy windows apps on current windows platforms.


The main function accepts a command list and name, writes a .scp msched 
script file and invokes the mSched command with os.system.


When I first considered how to approach this in about 2002, I looked at 
Mark Hammonds extensions and tried that, but it seemed to me at the time 
that the older apps simply didn't play nice and I fought harder to 
implement a pure python solution than the problem deserved.


Now, it's easy to review the python created msched scripts and 'follow 
along' to catch the odd bug.  mSched's capabilities have grown over that 
time as well and added some commands that make timing issues more 
predictable.  Although, I admit I'm never actually in the mSched 
environment so I may well be doing things with python that are entirely 
doable in mSched.  But, with python as glue I can control and coordinate 
as many disparate environments as the solution requires and it's mostly 
quick and easy.


Emile

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


Re: [Tutor] Firstrade Authentication ...

2008-08-03 Thread Kent Johnson
On Sun, Aug 3, 2008 at 12:45 PM, Federo [EMAIL PROTECTED] wrote:
 Jeff, Kent Hi!

 If possible I would most appreciate to use mechanize approach Jeff suggested.
 Python plagin: http://wwwsearch.sourceforge.net/mechanize/

This seems to get to the Thank you for applying for a Firstrade account. page:
import urllib2
import urllib
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)
f = opener.open('https://investor.firstrade.com/firstrade/login.do')
data = f.read()
f.close()

params = dict(username='janezfedero', password='kmet500', destination='')
params['login.x'] = 'Log+In'
params = urllib.urlencode(params)
f = opener.open('https://investor.firstrade.com/firstrade/login.do', params)
data = f.read()
f.close()
print(data)

Differences from your code:
- Does a GET on the login page - the session cookie is set here, not on the POST
- Includes all the form parameters

Kent

PS Please respond to the list, not to me personally.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] newbie: write ArcGIS lists to file?

2008-08-03 Thread zack holden

Dear List,
This is my first time on the list, and my first run at Python, please forgive 
my ignorance. 

I'm trying to use Python to access ArcGIS modules in order to perform the same 
task on multiple files in a folder. 

I'm able to access a set of files via: 

import arcgisscripting, string
gp = arcgisscripting.create()
gp.Workspace = E:/data

rasterSelect = []
  
rasterList = gp.ListRasters(, tif)
raster = rasterList.Next()

  while raster:
if raster.endswith(_dt.tif): 
print raster
rasterSelect.append(raster)
raster = rasterList.Next()

This produces a list of all the files I have in my folder. 

I need to begin producing .csv files of this list that I can access using other 
programs.

Would someone be willing to post a few lines of code showing me how to write 
the list the code above creates to an external file? 

Gratefully,

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


Re: [Tutor] Firstrade Authentication ...

2008-08-03 Thread Patrick

Kent Johnson wrote:
[snip]


params = dict(username='janezfedero', password='kmet500', destination='')


I hope this is a fake username  password

Regards,
Patrick

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


[Tutor] Any Italian speakers?

2008-08-03 Thread Alan Gauld
I received this message which Google tells me is Italian but then 
only gives a partial translation...


Can anyone help?

---
voglio chiderti solo 1 cosa ... ma secondo te qualle e il 
miglior programma X programmare???

grazie ...

da Cristian
-

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


Re: [Tutor] Output never stops

2008-08-03 Thread Alan Gauld


David [EMAIL PROTECTED] wrote

Thanks Alan, also your tutorial/book is a big help. I think I got it 
:)


Close but not quite there yet.


choice = 0
while choice != '-1':
   if choice == '':
   print You must enter Lary or Joan to continue! (-1 to quit): 


   choice = raw_input(
   Who's age do want to know, Lary or Joan? (-1 to quit): 
)

   if choice == 'Lary':
   print Lary's age is:, person.get('Lary')
   elif choice == 'Joan':
   print Joan's age is:, person.get('Joan')
   else:
   print Goodbye!


Consider what happens if I enter a blank name.
You print Goodbye but then go round the loop again prompting
for another choice. You probably want the Goodbye to only
be printed if choice == '-1'?

And the test for the empty string should ptrobably be after
you ask for the input?

Also the normal way to access a dictionary value is to
use  square brackets:

person['Lary']

get() does have the advantage that you can add a default
value that is returned if the key doesn't exist. But that's
not relevant in this case.

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] List elements as indices to another list

2008-08-03 Thread Carlos Laviola
Hi,

I have a simple matrix (nested list) defined as such:

M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

I'm trying to come up with a different way of getting its, well,
reverse antidiagonal, since the actual antidiagonal of M goes from
M[0, N] to M[N, 0] according to
http://planetmath.org/encyclopedia/AntiDiagonalMatrix.html:

j = 0
for i in range(len(M)-1, -1, -1):
print M[i][j]
j += 1

This works fine, but I was looking for a different solution, just for
kicks, and came across something interesting. I can do:

 i, j = range(len(M)), range(len(M)-1, -1, -1)
 i
[0, 1, 2]
 j
[2, 1, 0]

Theoretically, I could then just iterate over range(len(M)) and grab
M[i[N]j[N]], but that's not legal. What would be the right way of doing
this?

Thanks in advance,
Carlos



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


Re: [Tutor] newbie: write ArcGIS lists to file?

2008-08-03 Thread Alan Gauld


zack holden [EMAIL PROTECTED] wrote


I need to begin producing .csv files of this list that I can
access using other programs.

Would someone be willing to post a few lines of code
showing me how to write the list the code above creates to an 
external file?


Check the documentation for the csv module, I beliebe it supports
both reading and writing CSV files.

Alan G. 



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


Re: [Tutor] List elements as indices to another list

2008-08-03 Thread Alan Gauld


Carlos Laviola [EMAIL PROTECTED] wrote


i

[0, 1, 2]

j

[2, 1, 0]

Theoretically, I could then just iterate over range(len(M)) and grab
M[i[N]j[N]], but that's not legal. What would be the right way of 
doing

this?


M [ i[N] ]  [ j[N] ]

You just missed a couple of brackets...

HTH,

Alan G 



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