Re: [Tutor] Key Error

2007-07-09 Thread Alan Gauld
"jim stockford" <[EMAIL PROTECTED]> wrote

>> (The tests at the end are poorly written too. 
> 
> If you'd be willing to share your strong words, I'd
> be grateful to learn better alternatives.

OK, The strong words referred to the entire piece and 
the biggest error was the lack of try/except or get().
However the tests are badly done IMHO for several 
reasons, namely:

if wssd<-989. or wspd<-989. or wmax<-989.: break
if wspd==0.: break

1) Both sets of tests result in a break so the last test 
should be combined with the others in a single line.
(as a sub point I'd also prefer to see both tests on 
wspd placed together for ease of maintenance)

2) The values are floating point but the programmer has 
lazily omited the end zero which makes it very hard to 
spot the trailing decimal point.

3) The last equality test is inherently unreliable when 
using floating point values since if the value were derived 
by calciulation it might be 'nearly zero', the best way to 
check equality for floats is to test within a range. (If this 
case is guaranteed safe I would expect a comment to 
explain why, because it's such an unusual situation...)

4) The spacing between the < and minus sign is non 
existent which could lead to the combination being read 
as a <- arrow sign. Now inPyton that doesn't mean 
anything but in other languages does, so a reader 
not familiar with Python (like our OP) might be 
misdirected, whereas a simple space would remove 
any doubt.

Some might also object to the break being on the 
same line as the test but personally I don't object to 
that. But the points above all have real impact on legibility,
maintainability and, potentially, reliability.

We wouldn't normally expect to see such pickiness 
in this list because its aimed at beginners, but if this 
is production code, and from the look of it, in the 
telecomms market - which usually demands very high 
standards, then the quality should be much higher.

I'd prefer the tests to be written like this:

e = 0.1
if -e <= wspd <= e or 
   wspd  <  -989.0  or
   wssd  <  -989.0  or 
   wmax <  -989.0: 
  break

Its more typing and more room but less ambiguous 
and easier to read IMHO.


Alan G.

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


Re: [Tutor] Bundle help!

2007-07-09 Thread Alan Gauld
"Sara Johnson" <[EMAIL PROTECTED]> wrote

>>Use append() to add more data, then sort again to get it in order:
In [6]: data.append(('Joe', 90))
In [7]: data.sort()
In [8]: data
Out[8]: [('Fred', 20), ('Joe', 90), ('Kent', 50), ('Sara', 80)]
>
>  What happens if I need to sort alphabetical and numerically?

You can supply your own comparison function to the sort routine.
You can also just specify the key to sort by for simple cases.

>  I'm taking the original list and the original values
>  i.e., ('Fred', 20), ('Joe', 90), ('Kent', 80)...  and switching it 
> so that it reads in both ways...
>  List 1, [('Fred', 20), ('Joe', 90), ('Kent', 50), ('Sara', 80)]
>  List 2, ('Fred', 20), ('Joe', 50), ('Kent', 80), ('Sara', 90)]

But you lost me here. You seem to be switching the values
in the tuples around and its not clear to me by what criteria.

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] Bundle help!

2007-07-09 Thread David Perlman
I think what you want to do is start from the beginning with two  
separate lists, sort each one however you want, and then either join  
them with zip() or simply reference them as (list1[n], list2[n]).

I believe there's also a way to use zip() to separate your list of  
tuples into separate lists, but I don't know how off the top of my  
head.  You can find out if you follow these instructions though:

Go to
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&group=comp.lang.py
enter 'zip inverse', and check search Python only.


On Jul 8, 2007, at 6:41 PM, Sara Johnson wrote:

> How would I do that so I wind up with both lists?  I have two lists  
> now, but if I try and reorder them they just reverse in  
> alphabetical order without affecting the second value (the number).
>

--
-dave
All I ask is that the kind of unsolvable that it turns out to be has
respectable precedents.  -Jerry Fodor


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


Re: [Tutor] Bundle help!

2007-07-09 Thread Sara Johnson
Sorry to be so confusing.  Just realized a dumb mistake I made.  It doesn't 
need to be resorted alphabetically and numerically.  I need one list 
alphabetical and one numerical.
   
   
   (Alphabetical) List 1, [('Fred', 20), ('Joe', 90), ('Kent', 50), ('Sara', 
80)]
  
  (Numerical) List 2,  [('Fred', 20), ('Kent', 50), ('Sara', 80) ('Joe', 90)]
   
   
  It looks like these are appended together, but to re-sort, how do I (not sure 
if this is a word) "unappend" them?
   
  Thanks,
Sara
  

Alan Gauld <[EMAIL PROTECTED]> wrote:
"Sara Johnson" wrote

>>Use append() to add more data, then sort again to get it in order:
In [6]: data.append(('Joe', 90))
In [7]: data.sort()
In [8]: data
Out[8]: [('Fred', 20), ('Joe', 90), ('Kent', 50), ('Sara', 80)]
>
> What happens if I need to sort alphabetical and numerically?

You can supply your own comparison function to the sort routine.
You can also just specify the key to sort by for simple cases.

> I'm taking the original list and the original values
> i.e., ('Fred', 20), ('Joe', 90), ('Kent', 80)... and switching it 
> so that it reads in both ways...
> List 1, [('Fred', 20), ('Joe', 90), ('Kent', 50), ('Sara', 80)]
> List 2, ('Fred', 20), ('Joe', 50), ('Kent', 80), ('Sara', 90)]

But you lost me here. You seem to be switching the values
in the tuples around and its not clear to me by what criteria.
   

   
-
Looking for a deal? Find great prices on flights and hotels with Yahoo! 
FareChase.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Bundle help!

2007-07-09 Thread David Perlman
Maybe this reference will help:
http://xahlee.org/perl-python/sort_list.html
Just the first part of the discussion there.

On Jul 9, 2007, at 9:54 AM, Sara Johnson wrote:

> Sorry to be so confusing.  Just realized a dumb mistake I made.  It  
> doesn't need to be resorted alphabetically and numerically.  I need  
> one list alphabetical and one numerical.
>
>
>  (Alphabetical) List 1, [('Fred', 20), ('Joe', 90), ('Kent', 50),  
> ('Sara', 80)]
>
> (Numerical) List 2,  [('Fred', 20), ('Kent', 50), ('Sara', 80)  
> ('Joe', 90)]
>
>
> It looks like these are appended together, but to re-sort, how do I  
> (not sure if this is a word) "unappend" them?
>

--
-dave
All I ask is that the kind of unsolvable that it turns out to be has
respectable precedents.  -Jerry Fodor


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


Re: [Tutor] Bundle help!

2007-07-09 Thread Sara Johnson
Sorry, just needing to clarify.  As I may have eluded to in other posts, this 
is sort of a script that was written and I'm making modifications to.  Due to 
my serious lack of experience, I'm afraid to rewrite anything.  However, would 
I accomplish the same result by copying the lists, then breaking them apart 
with zip()?  I haven't used that before, but I'm willing to try it as long as I 
don't ruin what's already been done.  
   
  Unfortunately, the link you posted didn't bring up anything.  I tried to 
search google groups and got a bunch of asian text (using comp.lang.py).
   
  Again, sorry for my lack of experience here.  I do speak C somewhat.
   
  Guess I should sign this:  
   
  HTDI...(hope that didn't irritate!)
   
  Sara

David Perlman <[EMAIL PROTECTED]> wrote:
  I think what you want to do is start from the beginning with two 
separate lists, sort each one however you want, and then either join 
them with zip() or simply reference them as (list1[n], list2[n]).

I believe there's also a way to use zip() to separate your list of 
tuples into separate lists, but I don't know how off the top of my 
head. You can find out if you follow these instructions though:

Go to
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&group=comp.lang.py
enter 'zip inverse', and check search Python only.


On Jul 8, 2007, at 6:41 PM, Sara Johnson wrote:

> How would I do that so I wind up with both lists? I have two lists 
> now, but if I try and reorder them they just reverse in 
> alphabetical order without affecting the second value (the number).
>

--
-dave
All I ask is that the kind of unsolvable that it turns out to be has
respectable precedents. -Jerry Fodor


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


   
-
Pinpoint customers who are looking for what you sell. ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Bundle help!

2007-07-09 Thread Alan Gauld
"Sara Johnson" <[EMAIL PROTECTED]> wrote 

> this is sort of a script that was written and I'm making 
> modifications to.  Due to my serious lack of experience, 
> I'm afraid to rewrite anything.  

This is probably a long shot given the code you've posted so far, 
but I don't suppose there are any design documents available? 
Structure charets, class diagrams? Even plain text or pseudo 
code?

In an ideal world it would be possible to find the faulty function 
without having to read any code, but few projects are that well 
documented. But then again few projects have nothing! If you 
can find it it might tell you at a high level how the code hangs 
together.

> However, would I accomplish the same result by copying 
> the lists, then breaking them apart with zip()?  

I'm not sure zip is the best tool for breaking lists apart, 
but I'm no expert with it. But its easy to do with a simple 
loop if you are dealing with a list of tuples:

list1 = []
list2 = []
for item in theList:
list1.append(item[0])
list2.append(item[1])

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] Resource request

2007-07-09 Thread Tony Noyeaux

I am looking for a good online resource for
 
the "def Menu" and/or "elif choice" commands.
 
 
 
Any help would be much appreciated.
 
 
 
_
Don't get caught with egg on your face. Play Chicktionary!  
http://club.live.com/chicktionary.aspx?icid=chick_wlmailtextlink___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Resource request

2007-07-09 Thread Alan Gauld

"Tony Noyeaux" <[EMAIL PROTECTED]> wrote

> I am looking for a good online resource for
> 
> the "def Menu" 

def is the command for defining a function.
Menu, in this vcase, is the name of the function being defined.

You will find functions explained in the Modules and Functions 
topic of my tutorial.

> and/or "elif choice" commands.

I'm not sure why you think its a case of and/or? You need 
to know about elif quite apart from, and in addition to, functions.

The use of if/elif/else is described in the branching topic 
of my tutorial.
 
Given your questions it sounds like you are a beginner to 
programming in general, not just Python. In which case 
you should consider going through any of the tutorials 
listed here:

http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

possibly mine! :-)

-- 
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] Fastest way to iterate through a file

2007-07-09 Thread Tiger12506
It seems to me that what you need is this.
http://www.python.net/crew/mhammond/win32/Downloads.html

I think the key that you are missing here is that python does not include 
functions that you ask without downloading something special. Check the 
link, download those extensions, and read the documents included with them. 
Particularly this section:
- Python for...
  |_ Win32 API
 |_ Modules
|_ win32api

HTH,
Jacob S.

>>I need some of a something to be imported into python
>
> Maybe.
>
>> these are the functions I need, anyway know anything that might do
>> any of
>> the following?
>
> Assuming you are still talking about Windows XP...
>
>> suppose the class' name is autowindow:
>
> What kind of class? No such class exists so are you
> proposing to create it? Is it based on a Windows object?
>
> Remember python is a programming language not a GUI toolkit.
> If you want to do things to a GUI you need a toolkit. It might
> be linked to the native Win32 API through winall or ctypes or
> it might be a cross platform toolkit like Tkinter or wxPython,
> but its not part of Python.
>
>> autowindow.gainfocus(handle)
>> put the window of that handle into focus.
>
> The Win32 SetFocus function does that.
>
>> import autowindow
>> autowindow.imagechecksum()
>> autowindow.imagechecksum("c:\image\image.bmp")
>> autowindow.areachecksum()
>>
>> sum = autowindow.areachecksum(x1,y1,x2,y2) absolute screen
>> coordinate
>
> Its unlikely any GUI toolkit will do that, it sounds more like
> a job for a graphics toolkit like PIL.
>
>> autowindow.getmousepos()
>
> Mouse events carry the x/y coordinates.
> Just trap any mouse move and it will tell you where it is.
>
>> autowindow.restart()
>> autowindow.shutdown()
>
> This is hardware dependant, some computers won;t allow it.
> But on Windows you can try using the API as described on this page
> (which I found with a simple google search on Win32 restart shutdown)
>
> http://blogs.msdn.com/brad_mccabe/archive/2005/03/02/383542.aspx
>
>> autowindow.winexist()
>> true/false = autowindow.winexist()
>> handle or window name. (no class wanted!)
>
> FindWindow will effectively do this.
>
>> autowindow.listwindows()
>
> EnumWindows does this for you
>
> We discussed both of these recently.
>
>> autowindow.GainFocus()
>
> You already asked for this.
>
>> autowindow.KeyboardEvent(text)
>
> Yep, all toolkits allow you to trap a keyboard event.
> Most distinguish between key down and key up as well
> as the generic keypress.
>
> Or if you want to simulate a keyboard event you can use
> PostMessage. Again we discussed this recently.
>
>> autowindow.KeyboardEvent("Python rocks!", keyholddelay )
>> keyholddelay = miliseconds.
>
> No idea what this is supposed to be/do.
>
>> autowindow.mouseclick(button, clicks)
>> autowindow.MouseEvent(x, y, button, clicks)
>> autowindow.mousemove()
>> autowindow.mousemove(x,y, speed)
>> autowindow.winMove(x, y)
>> autowindow.winResize(x, y)
>> autowindow.winMinimize()
>> autowindow.winMaximize()
>> autowindow.winClose()
>
> These are all standard event types.
>
>> they all take handle
>
> And if you want to simulate them use PostMessage
>
>> autowindow.listwindows()
>> autowindow.listwindows("window name")
>> returns a list of handles if multiple
>
> You are repeating yourself again.
>
>> autowindow.hotkey()
>> autowindow.hotkey(keyboard key, function)
>> keyboard key = any key on keyboard
>> function = function to start
>
> Not sure, there may be a Windows function for this but
> I haven't seen it...
>
>> auntwindow.run ( /source/exe.exe, "image.bmp" )
>
> Try ExecFile
> or WinExec
>
>> autowindow.msgbox("window name", "window message", box_number )
>
> Standard windows MsgBox function
>
> Have you actually tried looking for these on MSDN for yourself.
> They are standard windows functions. They are mapped into Python
> by the toolkirs but the basic functions are pure windows and
> documented in MSDN.
>
> Recall I  recommended reading the web page on how to ask smart
> questions? One of the key points is not to ask the same questions
> multiple times of the same audience - and especially not in the
> same message!
>
> Also provide some context abvout why you need the information.
> What you are trying to achieve etc. Othewise you will get the same
> level of vagueness back that you provide.
>
> -- 
> 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] file methods

2007-07-09 Thread elis aeris

python 3.9 File Objects  of Python Library Reference



from the document i know that if I want to open a text file I do:

f = open("text.txt", "r+")

and thus create f as an file object i can then use.

however, i don't understand these functions

.readline
.readlines
.read
.xlinesread

I have a file like this one:

command = func_babara
parameter_1 = 300
parameter_2 = 300
parameter_3 = 50
parameter_4 = 0
parameter_5 = 0
parameter_6 = 0


,as you see, i need to process it one line at a time and use this .ini file
as a configuration file.

how do I use those functions, I don't understand the libarry reference.

also, I need to output lines like above to text.txt,  how do I do it?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] file methods

2007-07-09 Thread Luke Paireepinart
elis aeris wrote:
> python 3.9 File Objects  of Python Library Reference
>
>
>
> from the document i know that if I want to open a text file I do:
>
> f = open("text.txt", "r+")
>
> and thus create f as an file object i can then use.
>
> however, i don't understand these functions
>
> .readline
> .readlines
> .read
> .xlinesread
>
> I have a file like this one:
>
> command = func_babara
> parameter_1 = 300
> parameter_2 = 300
> parameter_3 = 50
> parameter_4 = 0
> parameter_5 = 0
> parameter_6 = 0
>
>
> ,as you see, i need to process it one line at a time and use this .ini 
> file as a configuration file.
>
> how do I use those functions, I don't understand the libarry reference.
>
> also, I need to output lines like above to text.txt,  how do I do it?
elis - have you looked into any of the tutorials that people have 
referred you to?
It appears that you aren't putting forth the effort to learn Python,
and you're just presenting us with any issues you need to solve (I.E. 
getting us to do it for you).
Personally, I don't have any desire to help you when I get the 
impression that you aren't trying to learn at all.
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] file methods

2007-07-09 Thread John Fouhy
On 10/07/07, elis aeris <[EMAIL PROTECTED]> wrote:
> from the document i know that if I want to open a text file I do:
>
> f = open("text.txt", "r+")
>
> and thus create f as an file object i can then use.
>
> however, i don't understand these functions
>
> .readline
> .readlines
> .read
> .xlinesread

The best way to find out what the functions do is to experiment with them.

eg:

>>> f = open('text.txt', 'r')
>>> f.readlines()

and look at the output.

However, that said, the modern way to read a file one line at a time is:

f = open('text.txt', 'r')
for line in f:
# do something with line

This will set the variable 'line' to each line of the file in turn.

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


Re: [Tutor] file methods

2007-07-09 Thread bhaaluu
Greetings,

On 7/9/07, John Fouhy <[EMAIL PROTECTED]> wrote:
>
> The best way to find out what the functions do is to experiment with them.
>
> eg:
>
> >>> f = open('text.txt', 'r')
> >>> f.readlines()
>
> and look at the output.

I like that idea. I made a simple english plain text file, thus:

first line
second line
third line

and named it text.txt.

I start the Python interactive interpreter:
>>>

>>> open('text.txt').read()
'first line\nsecond line\nthird line\n'

>>> len(open('text.txt').read())
34

>>> file=open('text.txt').read()
>>> print file
first line
second line
third line

>>> file.readlines()
Traceback (most recent call last):
  File "", line 1, in ?
AttributeError: 'str' object has no attribute 'readlines'

> However, that said, the modern way to read a file one line at a time is:
>
> f = open('text.txt', 'r')
> for line in f:
> # do something with line
>
> This will set the variable 'line' to each line of the file in turn.

>>> for line in file:
. . .print line
. . .
f
i
r
s
t

l
i
n
e


s
e
c
o
n
d

l
i
n
e


t
h
i
r
d

l
i
n
e


>>>

>
> --
> John.

>>> Ctrl-D

Oh well, back to the tutorials 8^D
-- 
bhaaluu at gmail dot com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] file methods

2007-07-09 Thread John Fouhy
On 10/07/07, bhaaluu <[EMAIL PROTECTED]> wrote:
> >>> file.readlines()
> Traceback (most recent call last):
>   File "", line 1, in ?
> AttributeError: 'str' object has no attribute 'readlines'

This error here is caused by this earlier statement:

> >>> file=open('text.txt').read()

'file' is now a string, not a file-like object.  This also causes the
behaviour you get when iterating 'for line in file'...

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


Re: [Tutor] Bundle help!

2007-07-09 Thread Bill Burns
[Sara Johnson]
> Sorry to be so confusing.  Just realized a dumb mistake I made.  It 
> doesn't need to be resorted alphabetically and numerically.  I need one 
> list alphabetical and one numerical.
>  
>  
>  (Alphabetical) List 1, [('Fred', 20), ('Joe', 90), ('Kent', 50), 
> ('Sara', 80)]
>   
> (Numerical) List 2,  [('Fred', 20), ('Kent', 50), ('Sara', 80) ('Joe', 90)]
>  



I didn't keep track of the thread very well, so I don't know if you have
one single list or two separate lists?? I'll assume you have a single
list and you want to create two new lists sorted differently:


# Single 'Master List'.
masterList = [('Sara', 80), ('Kent', 50), ('Joe', 90), ('Fred', 20)]

# Create a list for our alpha sort.
alphaList = list(masterList)

# Simply calling sort() will sort it alphabetically.
alphaList.sort()
print 'Alphabetical List: ', alphaList

def sortByNum(tup):
 # Helper function.
 return tup[1]

# Create a list for our numerical sort.
numList = list(masterList)

# sort() can take an optional key parameter, which allows us to define
# our own sorting method. This sort() calls the sortByNum() function,
# which I've defined to only look at the 2nd item in the tuple (the
# numerical portion.
numList.sort(key=sortByNum)
print 'Numerical List: ', numList

# Or you could also use a lambda in place of the separate
# function def, which would eliminate sortByNum() completely.
#~ numList.sort(key=lambda tup: tup[1])
#~ print 'Numerical List: ', numList

# And another method using the operator module...
#~ import operator
#~ numList.sort(key=operator.itemgetter(1))
#~ print 'Numerical List: ', numList


HTH,

Bill






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