[Tutor] Writing over a line in a text file
Hi I have a question regarding writing text to a file. I am directing output to a logfile. During one of the loops, I have a command that will issue a message on how long it is waiting for by doing something like this while something: print "\rNow waiting %s seconds .. " % seconds, sys.stdout.flush() print "\r ", I am trying to change my scripts so all info can be accessed via logfiles. Is there any way I can direct the output to a logfile, as above, without creating a new line for each print statement. I have tried changing the sys.stdout to the logfile in question but the print commands just force a new line for each print statement. Any ideas would be welcome. Cheers Kieran -- "Behind every great man, there is a great woman. Behind that woman is Mr.T." ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Need python 2.4 rpm for Suse 9.1
Hi,I searched the sites but was unable to find Python 2.4 rpm for Suse 9.1.Please send me a link where I can download this.ThanksAkanksha Yahoo! Sports Fantasy Football 06 - Go with the leader. Start your league today! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing over a line in a text file
kieran flanagan wrote: > Hi > > I have a question regarding writing text to a file. I am directing > output to a logfile. During one of the loops, I have a command that will > issue a message on how long it is waiting for by doing something like this > > while something: > > print "\rNow waiting %s seconds .. " % seconds, > sys.stdout.flush() > print "\r ", > > I am trying to change my scripts so all info can be accessed via > logfiles. Is there any way I can direct the output to a logfile, as > above, without creating a new line for each print statement. I have > tried changing the sys.stdout to the logfile in question but the print > commands just force a new line for each print statement. > > Any ideas would be welcome. Printing to a file will normally append to the file. To overwrite something in a file you have to seek to the location where you want to write and write the new information there. Here is a sketch which is probably wrong (!) because I don't ever have to do this... f = open('log.txt', 'w') print >>f, 'This line doesn\'t change' where = f.tell() # remember the current location in f for i in range(10): f.seek(where) print >>f, 'Counting...', i This will break if the rewrite is not at the end of the file (overwrites in the middle of a file can't change the length of the file) or if another thread is also writing the file. I wonder if a log file is the right mechanism for what you are trying to do? From your previous messages I guess you are doing a remote log. Maybe you should be monitoring log messages some other way? Or use tail to view the remote file so you just see the last line of the file? Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing over a line in a text file
kieran flanagan wrote: > Hi > > I have a question regarding writing text to a file. I am directing > output to a logfile. During one of the loops, I have a command that > will issue a message on how long it is waiting for by doing something > like this > > while something: > > print "\rNow waiting %s seconds .. " % seconds, > sys.stdout.flush() > print "\r ", > > I am trying to change my scripts so all info can be accessed via > logfiles. Is there any way I can direct the output to a logfile, as > above, without creating a new line for each print statement. I have > tried changing the sys.stdout to the logfile in question but the print > commands just force a new line for each print statement. I don't understand what the commas atthe end of your print commands are for. Nor do I understand what you're trying to do. Are you trying to redirect stdout to a file? why bother doing this? why not just do from time import sleep from random import randint f = file("log.txt","a") while x < y: f.write("\nNow waiting %s seconds\n" % randint(0,4)) f.close() Or do I misunderstand what your objective is? Also, I believe \r is windows-only, and you should use \n all the time, or \r\n. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing over a line in a text file
Thanks for both your replies Yes I think you may have misunderstood my orginal post Luke. The portion of code I gave will ensure each time this line is written it just writes over the previous line ( when printed to the console and \r is not windows specific). As I said, I have tried numerous ways to write these commands to a logfile so the line will just be written over ( the method you gave will just create a newline per iteration of the loop), as it is if I direct the output to the console. The section of code I gave is contained within a while loop, which is constantly checking for something to appear on a website. It simple prints out a line, letting the user know how long it has been waiting for. On the console this will just appear as one line ( each iteration writing the line over the previous one). In the logfile, this wil be a new line for each iteration of the while loop. To answer Kents questions, I previously had these outputs going to a logfile which could just be tailed. I now want to change the way I display the logs created. I firstly create a logfile within an apache server, this logfile appears as a link on a frontend website beside a corresponding test name. I then want to output all log messages to this one file. For what I am doing, it would be extremely useful for any number of users to just click on a link and have access to what is currently being run on the backend, in realtime. Using both logging and plain write messages, the output provides information on what function, script etc is running. There is one very large script, that runs a large number of other scripts to test and change data on a website. So my problem is I want to be able to have this one logfile accessible from the web and not have a huge amount of one line messages appearing ( within the various while loops), which just let the user know how long the script has been waiting. Kent, I will try your method and I hope this explains my objective a little better. Thanks KieranOn 6/21/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote: kieran flanagan wrote:> Hi>> I have a question regarding writing text to a file. I am directing> output to a logfile. During one of the loops, I have a command that> will issue a message on how long it is waiting for by doing something > like this>> while something:>> print "\rNow waiting %s seconds .. " % seconds,> sys.stdout.flush()> print "\r ",>> I am trying to change my scripts so all info can be accessed via> logfiles. Is there any way I can direct the output to a logfile, as> above, without creating a new line for each print statement. I have > tried changing the sys.stdout to the logfile in question but the print> commands just force a new line for each print statement.I don't understand what the commas atthe end of your print commands are for. Nor do I understand what you're trying to do.Are you trying to redirect stdout to a file?why bother doing this?why not just dofrom time import sleepfrom random import randintf = file(" log.txt","a")while x < y:f.write("\nNow waiting %s seconds\n" % randint(0,4))f.close()Or do I misunderstand what your objective is?Also, I believe \r is windows-only, and you should use \n all the time, or \r\n.___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor -- "Behind every great man, there is a great woman. Behind that woman is Mr.T." ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing over a line in a text file
kieran flanagan wrote: > Hi > > I have a question regarding writing text to a file. I am directing > output to a logfile. During one of the loops, I have a command that > will issue a message on how long it is waiting for by doing something > like this > > while something: > > print "\rNow waiting %s seconds .. " % seconds, > sys.stdout.flush() > print "\r ", > > I am trying to change my scripts so all info can be accessed via > logfiles. Is there any way I can direct the output to a logfile, as > above, without creating a new line for each print statement. I have > tried changing the sys.stdout to the logfile in question but the print > commands just force a new line for each print statement. Writing to any file appends characters. The \r does NOT back up. You might be able to use the tell() method of sys.stdout to determine the position just before the first print and the seek() method to reposition the file prior to each subsequent print. See "2.3.9 File Objects" in the Library Refernce. > > Any ideas would be welcome. > > Cheers > Kieran > > > -- > "Behind every great man, there is a great woman. Behind that woman is > Mr.T." > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need python 2.4 rpm for Suse 9.1
try at rpmfind.net or google it. but the immediate thought which comes up in my mind is: "use the source, luke" -- Senthil From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Akanksha GovilSent: Wednesday, June 21, 2006 5:36 PMTo: tutor@python.orgSubject: [Tutor] Need python 2.4 rpm for Suse 9.1 Hi,I searched the sites but was unable to find Python 2.4 rpm for Suse 9.1.Please send me a link where I can download this.ThanksAkanksha Yahoo! Sports Fantasy Football ’06 - Go with the leader. Start your league today! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] critique my script!
Christopher Spears wrote: > I can apparently call the functions sometimes without > (). Why is that? There is an important difference between f and f() - f is a reference to the function object itself, while f() is a *call* of the function. (Call is actually an operator, written (). You can write your own callable objects!) Functions are "first-class objects" in Python. That means that functions are values. They can be assigned to names, passed to functions, stored in data structures, etc. This is very useful in many ways, and requires that there be a way to get the function value. For example, here is a simple function: In [1]: def f(): ...: print 'f here' ...: return 3 ...: Calling f has a side effect - it prints - and it also returns a value - 3. In [2]: f() f here Out[2]: 3 This calls the function and assigns the return value to the variable x. In [3]: x = f() f here In [4]: x Out[4]: 3 The bare name 'f' is a reference to a function object: In [5]: f Out[5]: This can also be assigned: In [6]: y = f Now the name 'y' is a reference to the same function: In [7]: y Out[7]: Calling y is the same as calling f because it refers to the same function. In [8]: y() f here Out[8]: 3 I hope that helps! In an earlier email you wrote, > One of my main areas of confusion > is that I have seemed similar scripts written without > 'self.button' or 'self.box'. The programmer simply > uses 'button' or 'box' instead. When you write self.button, you are storing the value as an attribute of the class instance the method was called on. This attribute will persist as long as the instance does (unless you delete it yourself) and can be used in other instance method. If you write just button, the value is stored in a local variable and will be discarded when the method exits. For example, here is a simple class: In [9]: class F(object): ...: def __init__(self, a, b): ...: self.x = a ...: y = b ...: def show(self): ...: print 'F.x =', self.x ...: ...: In [11]: f=F(1,2) The parameter passed as a is saved as f.x: In [12]: f.x Out[12]: 1 There is no f.y, the variable y only exists within the __init__() method: In [13]: f.y -- exceptions.AttributeError D:\ AttributeError: 'F' object has no attribute 'y' f.x is accessible in other methods: In [14]: f.show() F.x = 1 In the case of your original script, you don't refer to any of the variables outside __init__(), so there is no reason for them to be attributes, they can all be local variables. In general, if you don't need the value outside the method, just store it in a local variable. HTH, Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] help with GUI
I am trying to write a GUI that consists of a scrollbar and two buttons. One button is called Convert, and the other is called Quit. The vertical scrollbar represents degrees Fahrenheit starting from 212 at the top and ending at 32 at the bottom. I want to be able to pick degrees Fahrenheit with the the scrollbar. Then I would click Convert, and the script should print the conversion into degrees Celsius on the screen. My first problem is that the knob on the scrollbar doesn't move when I drag it. I would like my GUI to by wider as well. Finally, I need to figure out how to get the number selected from the scrollbar to the function that does the conversion. Here is what I have written so far: #!/usr/bin/python import pygtk pygtk.require('2.0') import gtk def convert_to_celsius(self,widget,data=None): degC = (degF - 32)/1.8 print "Degrees Celsius: .2%f" % degC def scale_set_default_values(scale): scale.set_update_policy(gtk.UPDATE_CONTINUOUS) scale.set_digits(1) scale.set_value_pos(gtk.POS_LEFT) scale.set_draw_value(True) class Conversion_GUI: def __init__(self): self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.connect("destroy", lambda w: gtk.main_quit()) self.window.set_title("Convert to Celsius") box1 = gtk.VBox(False, 0) self.window.add(box1) box2 = gtk.HBox(False, 10) box2.set_border_width(10) box1.pack_end(box2, True, True, 0) box3 = gtk.HBox(False, 10) box3.set_border_width(10) box1.pack_end(box3, True, True, 0) adj1 = gtk.Adjustment(32.0, 212.0, 32.0, 0.1, 1.0, 1.0) self.vscale = gtk.VScale(adj1) self.vscale.set_size_request(20, 300) scale_set_default_values(self.vscale) box1.pack_start(self.vscale, True, True, 0) quit_button = gtk.Button("Quit") quit_button.connect("clicked", lambda w:gtk.main_quit()) convert_button = gtk.Button("Convert") #convert_button.connect("clicked", convert_to_celsius) box3.pack_start(convert_button, True, True, 0) box2.pack_start(quit_button, True, True, 0) self.vscale.show() convert_button.show() quit_button.show() box3.show() box2.show() box1.show() self.window.show() def main(self): gtk.main() return 0 if __name__ == '__main__': convert = Conversion_GUI() convert.main() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor