[Tutor] return values from function

2005-07-30 Thread Jorge Louis De Castro
Hi all,

I have the following code to traverse a directory structure from a given 
path:

import os, fnmatch, re
folders = []
r = re.compile(r'file_filter') # msn_username is passed by the user

def browse(junk, dirpath, namelist):
  for name in namelist:
if r.search(name):
  folders.append(os.path.join(dirpath, name))

userbase = os.environ['USERPROFILE']
# change directory to user profile folder
os.chdir(userbase)
os.path.walk(os.getcwd(), browse, None)

Now with the sample above I need global variables ("folders") to keep track 
of the directories traversed. Ideally I'd like to either have the browse 
function return a value that I could use or I'd like the ability to pass on 
arguments to the browse function, which I can't seem to be able to.

Any help would be appreciated.

Chrs

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


Re: [Tutor] fourier transform

2005-07-30 Thread Pawel Kraszewski
> Hello, I have a signal that I want to do a fourier transform on. I tried
> using FFT.fft(data).real but I don't understand the output.  what is output
> from this function, and why doesn't it require amplitude and time as
> inputs?

Please write the package you use for FFT. Standard Python doesn't have one. 
Perhaps you use some custom Python? Like Enthought Python? If so, the 
exhaustive guide is delivered with it in CHM (windows help) format.

1. In general FFT algorithms require you to supply just N complex values 
(complex contains both amplitude and phase of signal). If you supply real 
values, the system assumes phase=0 for each sample and takes given value for 
amplitude.

2. You DO supply a time information, you just didn't think of it this way: 
each consecutive sample is taken one fixed clock tick later, so position of 
sample in data gives you the time it was taken.

3. Results of FFT are in the form of complex vector of the same length as 
data. Starting from position 0 it gives you an constant (DC factor), position 
1 an amplitude and phase (remember - complex number gives you both amplitude 
and phase) of wave of the length table/2 and so on. If you take real value of 
this, you discard part of the information. AFAIR - taking real part gives you 
sine component, taking imaginary part gives you cosine component, taking 
absolute value gives you total amplitude and taking angle gives you phase.

4. The answer is symmetrical - usually you take only half of it. I don't 
remember the exact difference between the halves, but you may find it in any 
article on FFT.

Best regards,
 Pawel Kraszewski
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] return values from function

2005-07-30 Thread Kent Johnson
Jorge Louis De Castro wrote:
> Hi all,
> 
> I have the following code to traverse a directory structure from a given 
> path:
> 
> import os, fnmatch, re
> folders = []
> r = re.compile(r'file_filter') # msn_username is passed by the user
> 
> def browse(junk, dirpath, namelist):
>   for name in namelist:
> if r.search(name):
>   folders.append(os.path.join(dirpath, name))
> 
> userbase = os.environ['USERPROFILE']
> # change directory to user profile folder
> os.chdir(userbase)
> os.path.walk(os.getcwd(), browse, None)
> 
> Now with the sample above I need global variables ("folders") to keep track 
> of the directories traversed. Ideally I'd like to either have the browse 
> function return a value that I could use or I'd like the ability to pass on 
> arguments to the browse function, which I can't seem to be able to.

os.path.walk() has a facility to pass a user argument to the visit() function. 
From the docs:
walk(path, visit, arg)
Calls the function visit with arguments (arg, dirname, names) for each 
directory in the directory tree rooted at path (including path itself, if it is 
a directory).

Note that the third argument to walk(), is passed as the first argument to 
visit(). 
In your case, you should call
os.path.walk(os.getcwd(), browse, folders)

and define browse() as
def browse(folders, dirpath, namelist):

You could pass the regex as an argument also by making args the tuple (r, 
folders):
os.path.walk(os.getcwd(), browse, (r, folders))
def browse((r, folders), dirpath, namelist):

(Yes, you can do tuple unpacking in a def!)


You might also like to look at os.walk() which is easier to use. Instead of 
needing a callback, it generates a sequence that you use in a for loop. Your 
code would look like this:

import os, fnmatch, re
folders = []
r = re.compile(r'file_filter') # msn_username is passed by the user

userbase = os.environ['USERPROFILE']
# change directory to user profile folder
os.chdir(userbase)
for dirpath, dirnames, filenames in os.walk(os.getcwd()):
  for name in dirnames:
if r.search(name):
  folders.append(os.path.join(dirpath, name))

(Note this version isn't quite the same as yours; it only finds folders 
matching the name. I'm guessing that is what you want.)

Kent

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


Re: [Tutor] return values from function

2005-07-30 Thread Alan G
> I have the following code to traverse a directory structure from a 
> given path:

I'm not sure what you are using the browse function for.
Normally with path.walk you would use browse)() to modify
the namelist argument value and thus modify the path walked
by path.walk(). You appear to be trying to build a list of
files matching a pattern?

Have you looked at the glob module? Is the name pattern too
complex to be expressed using glob? or the fnmatch filter()
function? (I notice you import fnmatch but don't appear to
use it?)

Otherwise I think modifying the global list is the best
you can do.

> import os, fnmatch, re
> folders = []
> r = re.compile(r'file_filter') # msn_username is passed by the user
>
> def browse(junk, dirpath, namelist):
>  for name in namelist:
>if r.search(name):
>  folders.append(os.path.join(dirpath, name))
>
> userbase = os.environ['USERPROFILE']
> # change directory to user profile folder
> os.chdir(userbase)
> os.path.walk(os.getcwd(), browse, None)

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld 

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


Re: [Tutor] fourier transform

2005-07-30 Thread Christian Meesters
Jeff Peery wrote:
>
> Hello, I have a signal that I want to do a fourier transform on. I 
> tried using FFT.fft(data).real but I don't understand the output.  
> what is output from this function, and why doesn't it require 
> amplitude and time as inputs?

Hi Jeff,

As Danny wrote, your input are real numbers and your output will be 
complex numbers. I don't want to point you to the numarray 
documentation once more, but you might to check out this: 
http://mathworld.wolfram.com/FourierSeries.html as for why you get 
complex numbers back. (To get only the real value part try real_fft() 
in numarrayl.)
In general in numerical computation your input is simply a series of 
real numbers - of equal spacing in the dimension you want to consider. 
Which dimension you choose (amplitudes and time/phase or amplitudes and 
spacial frequencies and so on) is up to you. It depends on your 
physical or mathematical question. IMHO the best introduction you can 
get on Fourier Transforms & programming can be found in Numerical 
Recipies (here the "Numerical Recipies in C"-link 
http://www.library.cornell.edu/nr/bookcpdf.html - check chapter 12).

Have fun with FFTs!

Cheers
Christian

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


[Tutor] mysterious Python error

2005-07-30 Thread Tom Cloyd
I'm working on a WinXP SP-2 OS.

I'm too ill-informed to figure this out: When I execute

C:\Python24\python.exe

I get

'import site' failed.; use -v for traceback
ActivePython 2.4.1 Build 247 ...


I then ran this from a DOS window, and tried to redirect the -v output to  
a text file, so I could include it, but nothing I did was successful. I  
can't imagine why the output wouldn't redirect, but it would not. the file  
remained empty.

The output from the python.exe -v command was verbose. The import that  
failed was "import sitecustomize", which is in  
..\lib\sitepackages\sitecustomize.py

There was also protest about another line in that file: sys  
setdefaultencoding('utf-8') --
"NameError: name 'sys' is not defined."

The interpreter DID become active, after all this.

I attempted to fix this by removing and reinstalling the ActivePython  
2.4.1 Windows package. It didn't change anything at all.

Can anyone suggest what I might do with this?

Thanks for your help.

-- Tom

==
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< BestMindHealth.com / [EMAIL PROTECTED] >>
==

Using Opera's revolutionary e-mail client (program):  
http://www.opera.com/mail/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How do I get rid of blank space when running a program I've coded?

2005-07-30 Thread Nathan Pinno



Hi all,
 
I'll be brief. I always get a blank line whenever I run my Python programs 
that I've coded. How can I get rid of it. It's kind of annoying to press the 
spacebar in order to return to >>> in IDLE or the C:\ prompt in 
DOS.
 
Thanks,
Nathan
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How do I get rid of blank space when running a program I'vecoded?

2005-07-30 Thread Nathan Pinno



I meant the return key, not the spacebar. Sorry for the mix-up.

  - Original Message - 
  From: 
  Nathan 
  Pinno 
  To: tutor@python.org 
  Sent: Saturday, July 30, 2005 3:50 
  PM
  Subject: [Tutor] How do I get rid of 
  blank space when running a program I'vecoded?
  
  Hi all,
   
  I'll be brief. I always get a blank line whenever I run my Python 
  programs that I've coded. How can I get rid of it. It's kind of annoying to 
  press the spacebar in order to return to >>> in IDLE or the C:\ 
  prompt in DOS.
   
  Thanks,
  Nathan
  
  

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


[Tutor] What's the invalid synax? Error message and code supplied.

2005-07-30 Thread Nathan Pinno



Here's the message:
  File "D:\Python22\prog4.py", line 19    while 
option != 9:    ^SyntaxError: 
invalid syntax
And the code:
#This program finds the area and perimeter of circles, rectangles, and 
squares.def menu():    print '1) Area 
(Circle)'    print '2) Area 
(Rectangle)'    print '3) Area 
(Square)'    print '4) Perimeter 
(Circle)'    print '5) Perimeter 
(Square)'    print '6) Perimeter 
(Rectangle)'    print '9) Exit'
 
import mathprint 'By Nathan Pinno'print 'ID# 
2413448'printprint 'Program 4 - Functions'printmenu()option 
= int(raw_input('Option (1,2,3,4,5,6,9): ')while option != 
9:    if option == 
1:    r = int(raw_input('Radius: 
')    print 'The area of the circle = 
',pi*(r**2)    option = 
int(raw_input('Option (1,2,3,4,5,6,9): ')    elif option == 
2:    l = int(raw_input('Length: 
')    w = int(raw_input('Width: 
')    print 'The area of the 
rectangle = ',l*w    option = 
int(raw_input('Option (1,2,3,4,5,6,9): ')    elif option == 
3:    s = int(raw_input('Side: 
')    print 'The area of a square = 
',s**2    option = 
int(raw_input('Option (1,2,3,4,5,6,9): ')    elif option == 
4:    d = int(raw_input('Diameter: 
')    print 'The perimeter of the 
circle = ',pi*d    option = 
int(raw_input('Option (1,2,3,4,5,6,9): ')    elif option == 
5:    l = int(raw_input('Length: 
')    w = int(raw_input('Width: 
')    print 'The perimeter of the 
rectangle = ',(2*l)+(2*w)    option = 
int(raw_input('Option (1,2,3,4,5,6,9): ')    elif option == 
6:    s = int(raw_input('Side: 
')    print 'The perimeter of a 
square = ',s*4    option = 
int(raw_input('Option (1,2,3,4,5,6,9): ')    
else:    print 'That is not an 
option. Please choose an option.'    
option = int(raw_input('Option (1,2,3,4,5,6,9): ')print 
'Goodbye.'
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the invalid synax? Error message and code supplied.

2005-07-30 Thread python-tutor
   
I'd count the parenthesis on the prior line:


option = int(raw_input('Option (1,2,3,4,5,6,9): ')
while option != 9:


Also when you notice that you are copying and pasting the same line over and 
over, it may be time to think about reorganizing the code a little bit (Once 
you have it working).

Happy Debugging 

--Todd


On Saturday 30 July 2005 06:19 pm, Nathan Pinno wrote:
> Here's the message:
>   File "D:\Python22\prog4.py", line 19
> while option != 9:
> ^
> SyntaxError: invalid syntax
>
> And the code:
> #This program finds the area and perimeter of circles, rectangles, and
> squares. def menu():
> print '1) Area (Circle)'
> print '2) Area (Rectangle)'
> print '3) Area (Square)'
> print '4) Perimeter (Circle)'
> print '5) Perimeter (Square)'
> print '6) Perimeter (Rectangle)'
> print '9) Exit'
>
> import math
> print 'By Nathan Pinno'
> print 'ID# 2413448'
> print
> print 'Program 4 - Functions'
> print
> menu()
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> while option != 9:
> if option == 1:
> r = int(raw_input('Radius: ')
> print 'The area of the circle = ',pi*(r**2)
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> elif option == 2:
> l = int(raw_input('Length: ')
> w = int(raw_input('Width: ')
> print 'The area of the rectangle = ',l*w
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> elif option == 3:
> s = int(raw_input('Side: ')
> print 'The area of a square = ',s**2
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> elif option == 4:
> d = int(raw_input('Diameter: ')
> print 'The perimeter of the circle = ',pi*d
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> elif option == 5:
> l = int(raw_input('Length: ')
> w = int(raw_input('Width: ')
> print 'The perimeter of the rectangle = ',(2*l)+(2*w)
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> elif option == 6:
> s = int(raw_input('Side: ')
> print 'The perimeter of a square = ',s*4
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> else:
> print 'That is not an option. Please choose an option.'
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> print 'Goodbye.'
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the invalid synax? Error message and codesupplied.

2005-07-30 Thread Nathan Pinno
There is only 1 parenthesis on the prior line, but lots of commas.
- Original Message - 
From: <[EMAIL PROTECTED]>
To: 
Sent: Saturday, July 30, 2005 5:22 PM
Subject: Re: [Tutor] What's the invalid synax? Error message and 
codesupplied.


>
> I'd count the parenthesis on the prior line:
>
>
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> while option != 9:
>
>
> Also when you notice that you are copying and pasting the same line over 
> and
> over, it may be time to think about reorganizing the code a little bit 
> (Once
> you have it working).
>
> Happy Debugging
>
> --Todd
>
>
> On Saturday 30 July 2005 06:19 pm, Nathan Pinno wrote:
>> Here's the message:
>>   File "D:\Python22\prog4.py", line 19
>> while option != 9:
>> ^
>> SyntaxError: invalid syntax
>>
>> And the code:
>> #This program finds the area and perimeter of circles, rectangles, and
>> squares. def menu():
>> print '1) Area (Circle)'
>> print '2) Area (Rectangle)'
>> print '3) Area (Square)'
>> print '4) Perimeter (Circle)'
>> print '5) Perimeter (Square)'
>> print '6) Perimeter (Rectangle)'
>> print '9) Exit'
>>
>> import math
>> print 'By Nathan Pinno'
>> print 'ID# 2413448'
>> print
>> print 'Program 4 - Functions'
>> print
>> menu()
>> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
>> while option != 9:
>> if option == 1:
>> r = int(raw_input('Radius: ')
>> print 'The area of the circle = ',pi*(r**2)
>> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
>> elif option == 2:
>> l = int(raw_input('Length: ')
>> w = int(raw_input('Width: ')
>> print 'The area of the rectangle = ',l*w
>> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
>> elif option == 3:
>> s = int(raw_input('Side: ')
>> print 'The area of a square = ',s**2
>> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
>> elif option == 4:
>> d = int(raw_input('Diameter: ')
>> print 'The perimeter of the circle = ',pi*d
>> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
>> elif option == 5:
>> l = int(raw_input('Length: ')
>> w = int(raw_input('Width: ')
>> print 'The perimeter of the rectangle = ',(2*l)+(2*w)
>> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
>> elif option == 6:
>> s = int(raw_input('Side: ')
>> print 'The perimeter of a square = ',s*4
>> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
>> else:
>> print 'That is not an option. Please choose an option.'
>> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
>> print 'Goodbye.'
> ___
> 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] How do I make a Python program keep repeating?

2005-07-30 Thread Nathan Pinno



Hi all,
 
How do I make a program keep repeating until the user is done with 
it?
 
Thanks,
Nathan
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How do I make a Python program keep repeating?

2005-07-30 Thread python-tutor
How does the user indicate that they are done with the program?

One solution is to ask at the end of each iteration if they want to repeat.

Psuedocode:

keep_going=True
while(keep_going)
   Run your program stuff
   keep_going = get_user_response("Do you want to run again?")


--Todd



On Saturday 30 July 2005 08:10 pm, Nathan Pinno wrote:
> Hi all,
>
> How do I make a program keep repeating until the user is done with it?
>
> Thanks,
> Nathan
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] When I run this code, it just keeps repeating.

2005-07-30 Thread Nathan Pinno



When I run the following code it just keeps repeating. Here is a screen 
shot showing what I mean:
Mini CalculatorBy Nathan Pinno
 
CALCULATE MENU1) Add2) Subraction3) Multiplication4) 
Division w/o remainder5) Division with remaider6) Exponation7) 
Square roots9) ExitOption: 5First number:4Second number:24 / 
2 =  2  R  0First number:3Second number:63 / 6 
=  0  R  3First number:5Second number:65 / 6 =  
0  R  5First number:
 
Here is the relevant code:
# This is a small calculator.def menu():    print 
"CALCULATE MENU"    print "1) Add"    
print "2) Subraction"    print "3) 
Multiplication"    print "4) Division w/o 
remainder"    print "5) Division with 
remaider"    print "6) Exponation"    
print "7) Square roots"    print "9) Exit"
 
def cal():    global cal_opt    
cal_opt = int(raw_input("Option: "))
 
print "Mini Calculator"print "By Nathan 
Pinno"printmenu()cal()while cal_opt != 9:    
if cal_opt == 1:    X = input("First 
number:" )    Y = input("Second 
number:" )    print X, "+", Y, "= ",X 
+ Y    elif cal_opt == 
2:    X = input("First number:" 
)    Y = input("Second number:" 
)    print X, "-", Y, "= ",X - 
Y    elif cal_opt == 
3:    X = input("First number:" 
)    Y = input("Second number:" 
)    print X, "*", Y, "= ",X * 
Y    elif cal_opt == 
4:    X = input("First number:" 
)    Y = input("Second number:" 
)    if Y == 
0:    print 
"Division by zero ot 
allowed!"    
Y = input("Second number:" )    
else:    
print X, "/", Y, "= ",X / Y    elif cal_opt == 
5:    X = input("First number:" 
)    Y = input("Second number:" 
)    if Y == 
0:    print 
"Division by zero ot 
allowed!"    
Y = input("Second number:" )    
else:    
print X, "/", Y, "= ",X / Y," R ", X % Y    elif cal_opt == 
6:    X = input("First number:" 
)    Y = input("Power:" 
)    print X, "**", Y, "= 
",X**Y    elif cal_opt == 
7:    X = input("Number to find the 
square root of:" )    print "The 
square root of", X, " = ",X**0.5    
else:    print "That's not an option. 
Try again."    
menu()    cal()print 
"Goodbye"
 
How do I stop this, and make it go back to the main menu?
 
Thanks in advance,
Nathan
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When I run this code, it just keeps repeating.

2005-07-30 Thread python-tutor
Looks like you are making some pretty good progress.

The short answer to your question is that the menu & user input need to be 
inside the while loop.  That way cal_opt has a chance to change value before 
it gets evaluated by the while loop again.

Another comment - the global cal_opt is considered evil by many.  Better would 
be:

def cal():
   return  int(raw_input("Option: "))

print "Mini Calculator"
print "By Nathan Pinno"
print

while cal_opt != 9:
   menu()  
   cal_opt=cal()

   if cal_opt == 1:
 X = input("First number:" )
 Y = input("Second number:" )
 print X, "+", Y, "= ",X + Y

   


Keep hacking away at it...  you're making good progress.

--Todd


On Saturday 30 July 2005 09:55 pm, Nathan Pinno wrote:
> When I run the following code it just keeps repeating. Here is a screen
> shot showing what I mean: Mini Calculator
> By Nathan Pinno
>
> CALCULATE MENU
> 1) Add
> 2) Subraction
> 3) Multiplication
> 4) Division w/o remainder
> 5) Division with remaider
> 6) Exponation
> 7) Square roots
> 9) Exit
> Option: 5
> First number:4
> Second number:2
> 4 / 2 =  2  R  0
> First number:3
> Second number:6
> 3 / 6 =  0  R  3
> First number:5
> Second number:6
> 5 / 6 =  0  R  5
> First number:
>
> Here is the relevant code:
> # This is a small calculator.
> def menu():
> print "CALCULATE MENU"
> print "1) Add"
> print "2) Subraction"
> print "3) Multiplication"
> print "4) Division w/o remainder"
> print "5) Division with remaider"
> print "6) Exponation"
> print "7) Square roots"
> print "9) Exit"
>
> def cal():
> global cal_opt
> cal_opt = int(raw_input("Option: "))
>
> print "Mini Calculator"
> print "By Nathan Pinno"
> print
> menu()
> cal()
> while cal_opt != 9:
> if cal_opt == 1:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "+", Y, "= ",X + Y
> elif cal_opt == 2:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "-", Y, "= ",X - Y
> elif cal_opt == 3:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "*", Y, "= ",X * Y
> elif cal_opt == 4:
> X = input("First number:" )
> Y = input("Second number:" )
> if Y == 0:
> print "Division by zero ot allowed!"
> Y = input("Second number:" )
> else:
> print X, "/", Y, "= ",X / Y
> elif cal_opt == 5:
> X = input("First number:" )
> Y = input("Second number:" )
> if Y == 0:
> print "Division by zero ot allowed!"
> Y = input("Second number:" )
> else:
> print X, "/", Y, "= ",X / Y," R ", X % Y
> elif cal_opt == 6:
> X = input("First number:" )
> Y = input("Power:" )
> print X, "**", Y, "= ",X**Y
> elif cal_opt == 7:
> X = input("Number to find the square root of:" )
> print "The square root of", X, " = ",X**0.5
> else:
> print "That's not an option. Try again."
> menu()
> cal()
> print "Goodbye"
>
> How do I stop this, and make it go back to the main menu?
>
> Thanks in advance,
> Nathan
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When I run this code, it just keeps repeating.

2005-07-30 Thread Nathan Pinno
Thanks Todd. Can you please look at my other messages that you haven't 
answered, and see what's wrong with them, or what the answer is.
- Original Message - 
From: <[EMAIL PROTECTED]>
To: 
Sent: Saturday, July 30, 2005 8:10 PM
Subject: Re: [Tutor] When I run this code, it just keeps repeating.


> Looks like you are making some pretty good progress.
>
> The short answer to your question is that the menu & user input need to be
> inside the while loop.  That way cal_opt has a chance to change value 
> before
> it gets evaluated by the while loop again.
>
> Another comment - the global cal_opt is considered evil by many.  Better 
> would
> be:
>
> def cal():
>   return  int(raw_input("Option: "))
>
> print "Mini Calculator"
> print "By Nathan Pinno"
> print
>
> while cal_opt != 9:
>   menu()
>   cal_opt=cal()
>
>   if cal_opt == 1:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "+", Y, "= ",X + Y
>
>   
>
>
> Keep hacking away at it...  you're making good progress.
>
> --Todd
>
>
> On Saturday 30 July 2005 09:55 pm, Nathan Pinno wrote:
>> When I run the following code it just keeps repeating. Here is a screen
>> shot showing what I mean: Mini Calculator
>> By Nathan Pinno
>>
>> CALCULATE MENU
>> 1) Add
>> 2) Subraction
>> 3) Multiplication
>> 4) Division w/o remainder
>> 5) Division with remaider
>> 6) Exponation
>> 7) Square roots
>> 9) Exit
>> Option: 5
>> First number:4
>> Second number:2
>> 4 / 2 =  2  R  0
>> First number:3
>> Second number:6
>> 3 / 6 =  0  R  3
>> First number:5
>> Second number:6
>> 5 / 6 =  0  R  5
>> First number:
>>
>> Here is the relevant code:
>> # This is a small calculator.
>> def menu():
>> print "CALCULATE MENU"
>> print "1) Add"
>> print "2) Subraction"
>> print "3) Multiplication"
>> print "4) Division w/o remainder"
>> print "5) Division with remaider"
>> print "6) Exponation"
>> print "7) Square roots"
>> print "9) Exit"
>>
>> def cal():
>> global cal_opt
>> cal_opt = int(raw_input("Option: "))
>>
>> print "Mini Calculator"
>> print "By Nathan Pinno"
>> print
>> menu()
>> cal()
>> while cal_opt != 9:
>> if cal_opt == 1:
>> X = input("First number:" )
>> Y = input("Second number:" )
>> print X, "+", Y, "= ",X + Y
>> elif cal_opt == 2:
>> X = input("First number:" )
>> Y = input("Second number:" )
>> print X, "-", Y, "= ",X - Y
>> elif cal_opt == 3:
>> X = input("First number:" )
>> Y = input("Second number:" )
>> print X, "*", Y, "= ",X * Y
>> elif cal_opt == 4:
>> X = input("First number:" )
>> Y = input("Second number:" )
>> if Y == 0:
>> print "Division by zero ot allowed!"
>> Y = input("Second number:" )
>> else:
>> print X, "/", Y, "= ",X / Y
>> elif cal_opt == 5:
>> X = input("First number:" )
>> Y = input("Second number:" )
>> if Y == 0:
>> print "Division by zero ot allowed!"
>> Y = input("Second number:" )
>> else:
>> print X, "/", Y, "= ",X / Y," R ", X % Y
>> elif cal_opt == 6:
>> X = input("First number:" )
>> Y = input("Power:" )
>> print X, "**", Y, "= ",X**Y
>> elif cal_opt == 7:
>> X = input("Number to find the square root of:" )
>> print "The square root of", X, " = ",X**0.5
>> else:
>> print "That's not an option. Try again."
>> menu()
>> cal()
>> print "Goodbye"
>>
>> How do I stop this, and make it go back to the main menu?
>>
>> Thanks in advance,
>> Nathan
> ___
> 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] Now what do I do?(was Re: When I run this code, it just keeps repeating.)

2005-07-30 Thread Nathan Pinno
Here is another screen shot:
Mini Calculator
By Nathan Pinno

CALCULATE MENU
1) Add
2) Subraction
3) Multiplication
4) Division w/o remainder
5) Division with remaider
6) Exponation
7) Square roots
9) Exit
Option: 3
First number:4
Second number:6
4 * 6 =  24
CALCULATE MENU
1) Add
2) Subraction
3) Multiplication
4) Division w/o remainder
5) Division with remaider
6) Exponation
7) Square roots
9) Exit
Option: 9
That's not an option. Try again.
CALCULATE MENU
1) Add
2) Subraction
3) Multiplication
4) Division w/o remainder
5) Division with remaider
6) Exponation
7) Square roots
9) Exit
Option: 6
Goodbye

And the latest code:
# This is a small calculator.
def menu():
print "CALCULATE MENU"
print "1) Add"
print "2) Subraction"
print "3) Multiplication"
print "4) Division w/o remainder"
print "5) Division with remaider"
print "6) Exponation"
print "7) Square roots"
print "9) Exit"

def cal():
return int(raw_input("Option: "))

print "Mini Calculator"
print "By Nathan Pinno"
print
while cal_opt != 9:
menu()
cal_opt = cal()
if cal_opt == 1:
X = input("First number:" )
Y = input("Second number:" )
print X, "+", Y, "= ",X + Y
elif cal_opt == 2:
X = input("First number:" )
Y = input("Second number:" )
print X, "-", Y, "= ",X - Y
elif cal_opt == 3:
X = input("First number:" )
Y = input("Second number:" )
print X, "*", Y, "= ",X * Y
elif cal_opt == 4:
X = input("First number:" )
Y = input("Second number:" )
if Y == 0:
print "Division by zero ot allowed!"
Y = input("Second number:" )
else:
print X, "/", Y, "= ",X / Y
elif cal_opt == 5:
X = input("First number:" )
Y = input("Second number:" )
if Y == 0:
print "Division by zero ot allowed!"
Y = input("Second number:" )
else:
print X, "/", Y, "= ",X / Y," R ", X % Y
elif cal_opt == 6:
X = input("First number:" )
Y = input("Power:" )
print X, "**", Y, "= ",X**Y
elif cal_opt == 7:
X = input("Number to find the square root of:" )
print "The square root of", X, " = ",X**0.5
else:
print "That's not an option. Try again."
menu()
cal()
print "Goodbye"

Thanks in advance,
Nathan Pinno.
- Original Message - 
From: <[EMAIL PROTECTED]>
To: 
Sent: Saturday, July 30, 2005 8:10 PM
Subject: Re: [Tutor] When I run this code, it just keeps repeating.


> Looks like you are making some pretty good progress.
>
> The short answer to your question is that the menu & user input need to be
> inside the while loop.  That way cal_opt has a chance to change value 
> before
> it gets evaluated by the while loop again.
>
> Another comment - the global cal_opt is considered evil by many.  Better 
> would
> be:
>
> def cal():
>   return  int(raw_input("Option: "))
>
> print "Mini Calculator"
> print "By Nathan Pinno"
> print
>
> while cal_opt != 9:
>   menu()
>   cal_opt=cal()
>
>   if cal_opt == 1:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "+", Y, "= ",X + Y
>
>   
>
>
> Keep hacking away at it...  you're making good progress.
>
> --Todd
>
>
> On Saturday 30 July 2005 09:55 pm, Nathan Pinno wrote:
>> When I run the following code it just keeps repeating. Here is a screen
>> shot showing what I mean: Mini Calculator
cut screen shot and code
>> Thanks in advance,
>> Nathan
> ___
> 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] How do I get rid of blank space when running a program I'vecoded?

2005-07-30 Thread Kent Johnson
Do you have a call to raw_input() at the end of your program? That will make 
you press return to exit the program. It's useful for command-line programs 
that you want to run by double-clicking them but you can take it out if you 
find it annoying.

Kent

Nathan Pinno wrote:
> I meant the return key, not the spacebar. Sorry for the mix-up.
> 
> - Original Message -
> *From:* Nathan Pinno 
> *To:* tutor@python.org 
> *Sent:* Saturday, July 30, 2005 3:50 PM
> *Subject:* [Tutor] How do I get rid of blank space when running a
> program I'vecoded?
> 
> Hi all,
>  
> I'll be brief. I always get a blank line whenever I run my Python
> programs that I've coded. How can I get rid of it. It's kind of
> annoying to press the spacebar in order to return to >>> in IDLE or
> the C:\ prompt in DOS.
>  
> Thanks,
> Nathan
> 
> 
> 
> ___
> 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 maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Now what do I do?(was Re: When I run this code, it just keeps repeating.)

2005-07-30 Thread python-tutor
You are almost there.

Do you undestand why you are getting the output that you are getting?

Pretend that you are the computer and walk through what happens when you enter 
9 at the menu:


 while cal_opt != 9:
 menu()
 cal_opt = cal() # This is where you entered '9' so cal_opt = '9'   
 if cal_opt == 1:   # False so skipped
 X = input("First number:" )  
 Y = input("Second number:" )
 print X, "+", Y, "= ",X + Y


<-- code cut out -->

 elif cal_opt == 7:  # False so skipped to else
 X = input("Number to find the square root of:" )
 print "The square root of", X, " = ",X**0.5
 else:  # skipped to here. You never checked for an == '9'
 print "That's not an option. Try again."
 menu()  #menu is printed
 cal()  # You entered 6, but it isn't saved in any variable
  # From here, the loop starts over at the while up top.
  # this time the while is false because cal_opt = '9' so
  # the computer goes to the first line outside of the loop
 print "Goodbye"   # which is this.


So the output makes sense, even if it isn't what you intended.  There are a 
couple of ways to fix this.  Can you figure it out on your own?

--Todd



On Saturday 30 July 2005 10:39 pm, Nathan Pinno wrote:
> Here is another screen shot:
> Mini Calculator
> By Nathan Pinno
>
> CALCULATE MENU
> 1) Add
> 2) Subraction
> 3) Multiplication
> 4) Division w/o remainder
> 5) Division with remaider
> 6) Exponation
> 7) Square roots
> 9) Exit
> Option: 3
> First number:4
> Second number:6
> 4 * 6 =  24
> CALCULATE MENU
> 1) Add
> 2) Subraction
> 3) Multiplication
> 4) Division w/o remainder
> 5) Division with remaider
> 6) Exponation
> 7) Square roots
> 9) Exit
> Option: 9
> That's not an option. Try again.
> CALCULATE MENU
> 1) Add
> 2) Subraction
> 3) Multiplication
> 4) Division w/o remainder
> 5) Division with remaider
> 6) Exponation
> 7) Square roots
> 9) Exit
> Option: 6
> Goodbye
>
> And the latest code:
> # This is a small calculator.
> def menu():
> print "CALCULATE MENU"
> print "1) Add"
> print "2) Subraction"
> print "3) Multiplication"
> print "4) Division w/o remainder"
> print "5) Division with remaider"
> print "6) Exponation"
> print "7) Square roots"
> print "9) Exit"
>
> def cal():
> return int(raw_input("Option: "))
>
> print "Mini Calculator"
> print "By Nathan Pinno"
> print
> while cal_opt != 9:
> menu()
> cal_opt = cal()
> if cal_opt == 1:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "+", Y, "= ",X + Y
> elif cal_opt == 2:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "-", Y, "= ",X - Y
> elif cal_opt == 3:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "*", Y, "= ",X * Y
> elif cal_opt == 4:
> X = input("First number:" )
> Y = input("Second number:" )
> if Y == 0:
> print "Division by zero ot allowed!"
> Y = input("Second number:" )
> else:
> print X, "/", Y, "= ",X / Y
> elif cal_opt == 5:
> X = input("First number:" )
> Y = input("Second number:" )
> if Y == 0:
> print "Division by zero ot allowed!"
> Y = input("Second number:" )
> else:
> print X, "/", Y, "= ",X / Y," R ", X % Y
> elif cal_opt == 6:
> X = input("First number:" )
> Y = input("Power:" )
> print X, "**", Y, "= ",X**Y
> elif cal_opt == 7:
> X = input("Number to find the square root of:" )
> print "The square root of", X, " = ",X**0.5
> else:
> print "That's not an option. Try again."
> menu()
> cal()
> print "Goodbye"
>
> Thanks in advance,
> Nathan Pinno.
> - Original Message -
> From: <[EMAIL PROTECTED]>
> To: 
> Sent: Saturday, July 30, 2005 8:10 PM
> Subject: Re: [Tutor] When I run this code, it just keeps repeating.
>
> > Looks like you are making some pretty good progress.
> >
> > The short answer to your question is that the menu & user input need to
> > be inside the while loop.  That way cal_opt has a chance to change value
> > before
> > it gets evaluated by the while loop again.
> >
> > Another comment - the global cal_opt is considered evil by many.  Better
> > would
> > be:
> >
> > def cal():
> >   return  int(raw_input("Option: "))
> >
> > print "Mini Calculator"
> > print "By Nathan Pinno"
> > print
> >
> > while cal_opt != 9:
> >   menu()
> >   cal_opt=cal()
> >
> >   if cal_opt == 1:
> > X = input("First number:" )
> > Y = input("Second number:" )
> > print X, "+", Y, "= ",X + Y
> >
> >   
> >
> >
> > Keep hacking away at it...  you're making good progress.
> >
> > --Todd
> >
> > On Saturday 30 July 2005 09:55 pm, Nathan Pinno wrot

Re: [Tutor] How do I get rid of blank space when running a program I'vecoded?

2005-07-30 Thread Nathan Pinno
I never have it. Although when I ask for option I use 
int(raw_input('Option:')) instead of raw_input('Option:'). That's usually 
near the top, but part of a while loop.
- Original Message - 
From: "Kent Johnson" <[EMAIL PROTECTED]>
Cc: 
Sent: Saturday, July 30, 2005 8:55 PM
Subject: Re: [Tutor] How do I get rid of blank space when running a program 
I'vecoded?


> Do you have a call to raw_input() at the end of your program? That will 
> make you press return to exit the program. It's useful for command-line 
> programs that you want to run by double-clicking them but you can take it 
> out if you find it annoying.
>
> Kent
>
> Nathan Pinno wrote:
>> I meant the return key, not the spacebar. Sorry for the mix-up.
>>
>> - Original Message -
>> *From:* Nathan Pinno 
>> *To:* tutor@python.org 
>> *Sent:* Saturday, July 30, 2005 3:50 PM
>> *Subject:* [Tutor] How do I get rid of blank space when running a
>> program I'vecoded?
>>
>> Hi all,
>>
>> I'll be brief. I always get a blank line whenever I run my Python
>> programs that I've coded. How can I get rid of it. It's kind of
>> annoying to press the spacebar in order to return to >>> in IDLE or
>> the C:\ prompt in DOS.
>>
>> Thanks,
>> Nathan
>>
>> 
>>
>> ___
>> 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 maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Socket Programming

2005-07-30 Thread Joseph Quigley
I've got a little problem with my socket program(s):

#Client
import socket
#create an INET, STREAMing socket
s = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 2000))
s.send('hello!'[totalsent:])

#Server
import socket
#create an INET, STREAMing socket
serversocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
#bind the socket to a public host,
# and a well-known port
serversocket.bind((socket.gethostname(), 2000))
#become a server socket
serversocket.listen(5)

while 1:
#accept connections from outside
clientsocket, address = serversocket.accept()
serversocket.recv()

I have no idea how to get the server to receive and print a message or 
for the client to send the message. Does anyone know of some good python 
networking tutorials designed for newbies? (Not ones on 
www.awaretek.com. They seem to expect you to know some things that I 
don't even know about.)

Thanks,
Joe Q.

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


Re: [Tutor] Now what do I do?(was Re: When I run this code, it just keeps repeating.)

2005-07-30 Thread Nathan Pinno
Glad I didn't put up my mini calculator apps on my site. Would have had
complaints from people. But this thread has given me ideas on how to fix
them all, and how to fix Giant Calculator as well.
- Original Message - 
From: "Reed L. O'Brien" <[EMAIL PROTECTED]>
To: "Nathan Pinno" <[EMAIL PROTECTED]>
Sent: Saturday, July 30, 2005 9:05 PM
Subject: Re: [Tutor] Now what do I do?(was Re: When I run this code, it just
keeps repeating.)


> THis help?
>
>
> # This is a small calculator.
> def menu():
>print "CALCULATE MENU"
>print "1) Add"
>print "2) Subraction"
>print "3) Multiplication"
>print "4) Division w/o remainder"
>print "5) Division with remaider"
>print "6) Exponation"
>print "7) Square roots"
>print "9) Exit"
>
> def cal():
>cal_opt = int(raw_input("Option: "))
>return cal_opt
>
> print "Mini Calculator"
> print "By Nathan Pinno"
> print
> while 1:  <== here
>menu()
>cal_opt = cal()
>if cal_opt == 1:
>X = input("First number:" )
>Y = input("Second number:" )
>print X, "+", Y, "= ",X + Y
>elif cal_opt == 2:
>X = input("First number:" )
>Y = input("Second number:" )
>print X, "-", Y, "= ",X - Y
>elif cal_opt == 3:
>X = input("First number:" )
>Y = input("Second number:" )
>print X, "*", Y, "= ",X * Y
>elif cal_opt == 4:
>X = input("First number:" )
>Y = input("Second number:" )
>if Y == 0:
>print "Division by zero ot allowed!"
>Y = input("Second number:" )
>else:
>print X, "/", Y, "= ",X / Y
>elif cal_opt == 5:
>X = input("First number:" )
>Y = input("Second number:" )
>if Y == 0:
>print "Division by zero ot allowed!"
>Y = input("Second number:" )
>else:
>print X, "/", Y, "= ",X / Y," R ", X % Y
>elif cal_opt == 6:
>X = input("First number:" )
>Y = input("Power:" )
>print X, "**", Y, "= ",X**Y
>elif cal_opt == 7:
>X = input("Number to find the square root of:" )
>print "The square root of", X, " = ",X**0.5
>elif cal_opt == 9: ##<== here
>break ###<= here
>else:
>print "That's not an option. Try again."
>menu()
>cal()
> print "Goodbye"
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Help with file I/O.

2005-07-30 Thread Nathan Pinno



If anyone will help me learn file I/O, it would be appreciated. I went 
through Josh Cogliani's Non-Programmer's Tutorial for Python, and I still can't 
understand it.
 
Thanks in advance,
Nathan Pinno
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] cant get started

2005-07-30 Thread F22AceRaptor



it said this is for newbies but I download the 2.4 and I tried the 
stuff it said on the tutorial like the control-z or sys.exit () I tried the 
control-p or the python -c but it always says "'something' is not defined" or " 
invalid syntax" where do I go or what do I do. do i go the the command 
line or the shell cause I tried both please write me 
back
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] question on numarray in python

2005-07-30 Thread Xiangyi Meng
Hi there,

I ran across this error when running a few lines of 
code in debian python, but this runs perfectly in
windows python. Basically I created a dummy matrix
called "gamma" (using kroneckerproduct in numarray)
and printed out its mean.  Here is the code:
gamma = kroneckerproduct(ones((N, 1)),identity(T))
print gamma.mean()

and Here is the error message:
Traceback (most recent call last):
  File "vul_prov.py", line 149, in ?
VLs = mv_prov(hhc, idyrisk, provid, 1)
  File
"/home/meng/China/Extent/Data/Urban2002/Empirics/tmp/vulnerability.py",
line 605, in mv_prov
print gamma.mean()
  File
"/usr/lib/python2.4/site-packages/numarray/numarraycore.py",
line 1137, in mean
return self.sum()/(self.nelements()*1.0)
  File
"/usr/lib/python2.4/site-packages/numarray/numarraycore.py",
line 1133, in sum
return ufunc.add.reduce(ufunc.add.areduce(self,
type=type).flat, type=type)
IndexError: too many indices.

Thank you for helping on this! Oh, btw, the version
for python in both platforms is 2.4.1

Xiangyi

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


[Tutor] JBUS and Python which way

2005-07-30 Thread Miguel Lopes
Hi,

I understand the subject is advanced, but I'm a newbie without a technological 
background making some technology research for a company I work for.

I wonder if there is a library for accessing/interpreting JBUS protocol in 
Python.

Also, I've searched the net looking for some information so that I could have a 
birds-eye-view on this subject and got the impression that a possibility is to 
have the communication (JBUS protocol / buffering) managed by some hardware 
component. Is this so? Can some-one give me some pointers/resources on this 
subject. Would it still be possible to work with Python.

My aim is to have an idea of the alternatives technologies for accessing 
information produced by a machine with a JBUS interface (RS232) and how to 
access this information realtime in Python (connecting a PC locally via serial 
port).

Hope this makes any sense.
All help appretiatted.
Txs,
mjkel

---
Chegou o Clix ADSL até 16 Megas
Internet + Telefone desde € 22,5/mês
Acabe de vez com os € 15 da assinatura telefónica!
Saiba mais em http://adsl.clix.pt

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


Re: [Tutor] cant get started

2005-07-30 Thread Nathan Pinno



This is an easy one. Go to the IDLE, I 
found it was easier. The latest Python refused to work on mine, so I'm using 
2.2.3, and it works fine.

  - Original Message - 
  From: 
  [EMAIL PROTECTED] 
  To: tutor@python.org 
  Sent: Friday, July 29, 2005 10:22 
PM
  Subject: [Tutor] cant get started
  
  it said this is for newbies but I download the 2.4 and I tried the 
  stuff it said on the tutorial like the control-z or sys.exit () I tried 
  the control-p or the python -c but it always says "'something' is not defined" 
  or " invalid syntax" where do I go or what do I do. do i go the the 
  command line or the shell cause I tried both please write me 
  back
  
  

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


Re: [Tutor] Help with file I/O.

2005-07-30 Thread python-tutor
Have you worked through the diveintopython.org tutorial/book?  Chapter 6 
covers file handling.  If/When you get stuck post your code/question and ask 
for help.

--Todd

On Saturday 30 July 2005 11:49 pm, Nathan Pinno wrote:
> If anyone will help me learn file I/O, it would be appreciated. I went
> through Josh Cogliani's Non-Programmer's Tutorial for Python, and I still
> can't understand it.
>
> Thanks in advance,
> Nathan Pinno
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] learning classes (kinda long)

2005-07-30 Thread Reed L. O'Brien
This is a little long
a program consisting of
bsserver.py ## to define server class and methods
bscaller.py ## to define a test class subclasses for services (imap
http pop dns et al)
bslogger.py ## to log and analyze tests and send emails on
exceptions/failures
bsmain.py  ## that instantiates classes and controls flow
bs.conf  ## the config
given a configuration:

[foo]

fqdn: foo.dom.com
services: http,imap,pop

[bar]

fqdn: bar.dom.org
services: dns,http

then given:

config = ConfigParser.ConfigParser()
config.read('config.txt')

I want to be able to create instances of a class, via a loop; feeding in
the values from the config. something like:

for section in config.sections():
section = bsserver.Server() 

first I would like for this instance to be named the name of the host in
the section
but i keep getting instances named section I think doing it may require
something more complex than my current understanding. How do I get them
to become individuallly named
Instances?

Additionanlly I would like to call them and pass in their name from teh
section part of the config and also the options fqdn as well as services
as a list.

for section in config.sections():
section = bsserver.Server(config.get(section, 'fqdn'),
config.get(section, 'services')

currently my server class is defined as:

class Server:
def fqdn(self, value):
self.fqdn
def services(self, value):
self.services = value
def name(self, value):
self.name = value

I have read through learning python and it's info about classes.  While
I feel like I understand it as I read it, I am not doing well
implemeting it.  Any pointers to good reading material is appreciated
and will be read, but discussion of classes would benefit most who are
interested

TIA,
~r
 






-- 
4.6692916090
'cmVlZG9icmllbkBnbWFpbC5jb20=\n'.decode('base64')
http://www.spreadfirefox.com/?q=affiliates&id=16474&t=1
keyID: 0x0FA09FCE
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] learning classes (kinda long)

2005-07-30 Thread luke
hello.

I didn't read or understand your code that well.
but you asked:

> for section in config.sections():
> section = bsserver.Server() 
> 
> first I would like for this instance to be named the name of the host in
> the section
> but i keep getting instances named section I think doing it may require
> something more complex than my current understanding. How do I get them
> to become individuallly named
> Instances?

did you think about using a dictionary?
example that might apply:
#start of code


class aclass:
def __init__(self,astring):
self.avar = astring+"!!!"

listofstrings = ["hello","hi","what's up"]
classesdict = {}
for item in listofstrings:
classesdict[item] = aclass(item)

print classesdict["hello"].avar
#should print "hello!!!"


print classesdict["hi"].avar
#should print "hi!!!"


print classesdict["what's up"].avar
#should print "what's up!!!"


#end of code
I hope that helps ya.
-Luke

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


Re: [Tutor] Socket Programming

2005-07-30 Thread Danny Yoo


> I have no idea how to get the server to receive and print a message or
> for the client to send the message. Does anyone know of some good python
> networking tutorials designed for newbies? (Not ones on
> www.awaretek.com. They seem to expect you to know some things that I
> don't even know about.)

Hi Joe,

Before we start: it might be easier to start off with a high-level
networking protocol, like XMLRPC.  There are examples of making xmlrpc
clients and servers in the Library documentation:

http://www.python.org/doc/lib/xmlrpc-client-example.html
http://www.python.org/doc/lib/simple-xmlrpc-servers.html


Sockets tend to be pretty low-level at times, with its own set of issues.
Have you seen AMK's Socket HOWTO tutorial already?

http://www.amk.ca/python/howto/sockets/

... wait, ok, I see, your example code below appears to come from the
Socket HOWTO tutorial after all!  Ok, so you're trying those examples now?
Let's take a closer look.


> #Client
> import socket
> #create an INET, STREAMing socket
> s = socket.socket(
> socket.AF_INET, socket.SOCK_STREAM)
> s.connect(("localhost", 2000))
> s.send('hello!'[totalsent:])


The last statement looks a little weird, since 'totalsent' isn't defined.
Are you just trying to send a message to the server?  If so, then:

##
s.send("hello!\n")
##

should do something, although there's no guarantee that all seven bytes
will go across the network.  So something like:

##
bytesReallySent = 0
while bytesReallySent != len('hello!\n'):
bytesReallySent += s.send("hello!\n"[bytesReallySent:])
##

might work better: it will continue calling s.send() until all the bytes
in the message have been sent over.  Of course, this is really paranoid:
the socket probably will be able to pass all seven bytes at once, but for
longer strings, a socket's send() method doesn't guarantee how much it can
send at once.


As you can see, this is a really low-level detail sort of thing: we can
avoid some of these issues by using a higher-level file interface:

##
import socket
s = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 2000))
f = s.makefile()
##

At this point, f is a file-like object, and we should be able to use a
file's write() method:

##
f.write('hello!\n')
##

and the high-level interface should guarantee that this will work in one
shot.


However, even then, we may need to worry about buffering and blocking
issues, so we may need to use a flush() to guarantee that things are
passed across the network.

##
f.flush()
##



Let's look at the server side code:

> #Server
> import socket
> #create an INET, STREAMing socket
> serversocket = socket.socket(
> socket.AF_INET, socket.SOCK_STREAM)
> #bind the socket to a public host,
> # and a well-known port
> serversocket.bind((socket.gethostname(), 2000))
> #become a server socket
> serversocket.listen(5)
>
> while 1:
> #accept connections from outside
> clientsocket, address = serversocket.accept()
> serversocket.recv()
  

I believe the last statement is off; you probably want to use the
'clientsocket' here, not 'serversocket'.

The idea is that there's a central serversocket that listens to a
particular port, and that's the socket that's being bound in:

serversocket.bind((socket.gethostname(), 2000))

And every time a client comes, it talks to the serversocket.  The
serversocket accepts the connection, but delegates communication off to
the clientsocket instead, the one that gets returned from
serversocket.accept():

clientsocket, address = serversocket.accept()

So:

##
clientsocket.recv()
##

should work better.

The Socket Programming HOWTO talks about the difference between the
"server socket" and a "client socket" in a little more detail, near the
end of Section 2.


If you have more questions, please feel free to ask!

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


Re: [Tutor] Help with file I/O.

2005-07-30 Thread Danny Yoo


> If anyone will help me learn file I/O, it would be appreciated. I went
> through Josh Cogliani's Non-Programmer's Tutorial for Python, and I
> still can't understand it.

Hi Nathan,

Can you point out what parts don't make sense?  We can try to explain
better, but we can only do so if you either show us code, or quote the
part of the tutorial that mystifies you.  If we can talk about it more,
maybe we'll be able to figure out what part you're getting stuck on.


Good luck!

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


Re: [Tutor] Help with file I/O.

2005-07-30 Thread Nathan Pinno
Well, you saw my password program. That was my first attempt at using file 
I/O. Thought I'd try it big time. You saw where that went.
Nathan
- Original Message - 
From: "Danny Yoo" <[EMAIL PROTECTED]>
To: "Nathan Pinno" <[EMAIL PROTECTED]>
Cc: 
Sent: Sunday, July 31, 2005 12:13 AM
Subject: Re: [Tutor] Help with file I/O.


>
>
>> If anyone will help me learn file I/O, it would be appreciated. I went
>> through Josh Cogliani's Non-Programmer's Tutorial for Python, and I
>> still can't understand it.
>
> Hi Nathan,
>
> Can you point out what parts don't make sense?  We can try to explain
> better, but we can only do so if you either show us code, or quote the
> part of the tutorial that mystifies you.  If we can talk about it more,
> maybe we'll be able to figure out what part you're getting stuck on.
>
>
> Good luck!
>
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] cant get started

2005-07-30 Thread Danny Yoo


On Sat, 30 Jul 2005 [EMAIL PROTECTED] wrote:

> it said this is for newbies but I download the 2.4 and I tried the stuff
> it said on the tutorial like the control-z or sys.exit () I tried the
> control-p or the python -c but it always says "'something' is not
> defined" or "  invalid syntax"

Hello,

I agree with Nathan; try using IDLE, which is bundled with Python 2.4, so
you should have it already.

This tutorial may help:

http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/

It's a graphical tour that shows how to get started with Python.  The
tutorial's a little old (and some of the screenshots are stale!), but most
of it should still be applicable.


Feel free to ask questions.  Good luck!

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


Re: [Tutor] Help with file I/O.

2005-07-30 Thread Danny Yoo


On Sun, 31 Jul 2005, Nathan Pinno wrote:

> Well, you saw my password program. That was my first attempt at using
> file I/O. Thought I'd try it big time. You saw where that went.

Ok, let's take a look.  It was from this message, right?

http://mail.python.org/pipermail/tutor/2005-July/039478.html

That was such a while back that I think you probably learned a lot since
then, and I think a few of the issues there were less about I/O and more
about just general programming.

Let's try a few things, just to see why you're getting stuck.  Can you
write a program that reads in a file, and just prints all of its lines out
to screen?

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


Re: [Tutor] JBUS and Python which way

2005-07-30 Thread Danny Yoo


On Fri, 29 Jul 2005, Miguel Lopes wrote:

> I understand the subject is advanced, but I'm a newbie without a
> technological background making some technology research for a company I
> work for.

Hi Miguel,

You may want to ask this on comp.lang.python instead.  The Modbus/JBUS
protocol might be a bit advanced and specialized for the group here; I
don't think we can help you that effectively.


> My aim is to have an idea of the alternatives technologies for accessing
> information produced by a machine with a JBUS interface (RS232) and how
> to access this information realtime in Python (connecting a PC locally
> via serial port).

The only thing that I can mention here is the pyserial project, which
provides access to the serial port:

http://pyserial.sourceforge.net/

I'd have to assume that some software would have to be written to talk the
JBUS protocol across the serial port, but I'm hitting the limits of my
ability to use Google effectively in this area... *grin* So I'd strongly
recommend asking this to a wider audience.

Good luck to you.

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