[Tutor] Read file line by line

2005-01-25 Thread Gilbert Tsang
Hey you Python coders out there:
Being a Python newbie, I have this question while trying to write a 
script to process lines from a text file line-by-line:

#!/usr/bin/python
fd = open( "test.txt" )
content = fd.readline()
while (content != "" ):
   content.replace( "\n", "" )   
   # process content
   content = fd.readline()

2 questions:
1. Why does the assignment-and-test in one line not allowed in Python? 
For example, while ((content = fd.readline()) != ""):
2. I know Perl is different, but there's just no equivalent of while 
($line = ) { } ? I'm not asking for Python has to offer Perl 
already has, but simply wondering a good way to read from a file 
line-by-line.

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


[Tutor] Control flow

2005-01-28 Thread Gilbert Tsang
Hi there, I have this logic that I cannot wrap my mind it:
def go_jogging():
   # go out and jog
   return
if ( bad_weather =='y' ):
   # ask user only if weather is bad.
   b = input ( "Weather is really bad, still go out to jog?[y/n]" )
   if b == 'y':
  go_jogging()
   else:
   # program should exit now
else:
   go_jogging()
 

I can't get the program to stop processing further in the middle 
(apparently neither exit nor goto-label exist in Python, sorry for the 
C++ mindset) so I used exception to achieve what I want. I know in that 
example you could probably manipulate the logic so that program ends at 
the bottom of the if-tree. My question is then how to exit in the middle 
of a if-then-else tree? Thanks, Gilbert.

try:
   if ( bad_weather =='y' ):
   b = input ( "Weather is really bad, still go out to jog?[y/n]" )
   if b == 'y':
   go_jogging()
   else:
   raise Exception( "quit" )
   else:
   go_jogging()
except Exception, inst:
   print "Program exits now"
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Control flow

2005-01-31 Thread Gilbert Tsang
Thanks for the enthusiasm on how input/raw_input() works - my original 
intention was to ask a question on control flow so I didn't spend that 
much time testing out this piece of input code besides typing. But I did 
learn a lot. Thanks!

Gilbert
Jacob S. wrote:
I noticed that too, Liam.
b = input("Weather is really bad, still go out to jog? [y/n]") # 
Would it kill you to have whitespace in a prompt?
should really be
b = raw_input("Weather is really bad, still go out to jog? [y/n]")
to get the effect he wants.

input() doesn't only take integers, it takes valid python objects. 
Integers are objects, but so are lists, dictionaries, tuples,
actually it takes everything, BUT!!! it trys to return a valid python 
object for input.
So it will take a string--don't quote me on this--if you explicitly 
put the string in quotes.
If you don't put the string in quotes, it instead searches the 
namespaces for that object.
So say the user typed in bad_weather when the interpreter gave that 
prompt. Then, b == "y" evaluates true because bad_weather == "y". Did 
I explain it right? Or am I trying to explain something you already 
know? I know I get frustrated when people try to explain concepts that 
I already know...

HTH,
Jacob Schmidt
< erk, to the list, to the List!>
if ( bad_weather =='y' ):
  # ask user only if weather is bad.
  b = input ( "Weather is really bad, still go out to jog?[y/n]" )
  if b == 'y':
 go_jogging()
Anyone else notice that he's never gonna go jogging if the weather is 
bad?
Unless I've got input() wrong, it only takes integers... ?

Regards,
Liam Clarke
--
'There is only one basic human right, and that is to do as you damn 
well please.
And with it comes the only basic human duty, to take the consequences.

--
'There is only one basic human right, and that is to do as you damn 
well please.
And with it comes the only basic human duty, to take the consequences.
___
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] question on string

2005-08-01 Thread Gilbert Tsang
Hi there,

I would like to construct some string objects using the cprintf-style 
format:

command_string = "diff -u %s %s > %s.patch" % ( src, dst, file )

Of course it is illegal in python but I couldn't figure out a way to 
construct strings with that kind of formatting and substitution.

I have been looking and couldn't discover string constructor such as

command_string = string( "diff -u %s %s > %s.patch" % ( src, dst, file ) )

 From the documentation on docs.python.org, the closest is string 
Template (but my python installation doesn't come with this module). Any 
insights on initializing string in this manner?


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


Re: [Tutor] question on string

2005-08-01 Thread Gilbert Tsang




Thank you all for the enthusiastic responses. It must have been other
part of the script since the command string construction now works. I
also realized that "file" is a poor choice of variable name. Thanks
Danny for telling me about the subprocess module.

Best regards, Gilbert.


ZIYAD A. M. AL-BATLY wrote:

  On Mon, 2005-08-01 at 10:42 -0700, Gilbert Tsang wrote:
  
  
Hi there,

I would like to construct some string objects using the cprintf-style 
format:

command_string = "diff -u %s %s > %s.patch" % ( src, dst, file )

Of course it is illegal in python but I couldn't figure out a way to 
construct strings with that kind of formatting and substitution.

I have been looking and couldn't discover string constructor such as

command_string = string( "diff -u %s %s > %s.patch" % ( src, dst, file ) )

 From the documentation on docs.python.org, the closest is string 
Template (but my python installation doesn't come with this module). Any 
insights on initializing string in this manner?


Thanks, Gilbert.

  
  "file" is a reserved word!  This is why it's not working for you!
Change "file" to something else, like "result" for example.

Here:
>>> src = ''
>>> dst = 'file2.c'
>>> result = "result"
>>> command_string = "diff -u %s %s > %s.patch" % (src, dst, result)
>>> command_string
'diff -u file1.c file2.c > result.patch'

Is this what you want?

Ziyad.
  




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