Re: [Tutor] python / gtk / Webkit app wrapper.
On 28/06/13 02:13, Darin Lawson Hosking wrote: Alan - This is my first python program (actually first anything, besides simple one line shell scripts) and if I broke protocol I do apologize. Its not a matter of breaking protocol, just posting where you get the highest chance of getting a good answer. As it happens you got lucky but for non core Python stuff it's usually more helpful to try a more specialized forum. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] multiple function returns
On 28/06/13 05:18, Jim Mooney wrote: What's the Pythonic standard on multiple returns from a function? There is no standard. Multiple returns are quite common but they come with all the usual caveats for using them, they can introduce complexity and hard to find bugs so use them sensibly. Computer Science purists don't like them for good theoretical reasons, but personally I find them one of the least offensive of the "rule breakers" and forcing a single exit can often involve ugly extra conditionals etc. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unwanted 'zero' ending
On 28/06/13 02:32, Jim Mooney wrote: Hmm, so it seems a lot of trouble for a few hardcoded tests I could run myself from the IDE interpreter window. The point is that you should write the doc strings before you write the code. Then anyone can test that your function does at least work for the original design cases. And if you change it you can run that minimum test set very easily and quickly. the function out myself. I guess there is no easy substitute for simply beating up your functions with a slew of garbage, since you're the one who understands them ;') And that's precisely why the person who writes the code is the worst possible person to test it. If you know how it works you are usually blind sided to it's defects! Ideally all testing should be done by a third party, but in practice it's rarely possible. But it's one of the ideas behind the concept of pair-programming. But if you want comprehensive testing, regardless of who is doing it, you do need more than doctest. It's a useful sanity check and much better than nothing or even random hacking at the >>> prompt. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unwanted 'zero' ending
On 28/06/13 17:31, Peter Otten wrote: Whatever approach you pick, unittest, doctest, or a combination, your code will improve -- not just because you are verifying its correctness to some extent, but also because the inherent question "How can I test it?" will lead to a better overall structure. Well said. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] multiple function returns
On 28/06/13 14:18, Jim Mooney wrote: What's the Pythonic standard on multiple returns from a function? It seems easiest to just return from the point where the function fails or succeeds, even it that's multiple points. Or is it considered best to defer everything to one return at the end? The first. Python functions have one entry point, the top of the function. They can have multiple exit points, anywhere you have a return statement. Languages like Pascal enforce a single exit point, which means you end up writing rubbish code like this: # Using Python syntax instead of Pascal def function(arg): done = False result = some_calculation(arg) if condition(): done = True if not done: result = more_calculations() if condition(): done = True if not done: result = even_more_calculations() if condition(): done = True if not done: result = are_we_done_yet() return result compared to: def function(arg): result = some_calculation(arg) if condition(): return result result = more_calculations() if condition(): return result result = even_more_calculations() if condition(): return result return are_we_done_yet() -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help appending data to a logfile
On 06/28/2013 07:27 AM, Matt D wrote: As for the shutil.copy() function, how complex can it be? It takes two file names, source and destination. It does not need a with statement since it wants strings, not file handles. You might get into trouble on some OS's with the source file already open, but since you open it with append, it should be trivial to close and reopen it. The problem here is that the, I am pretty sure that, using anything from shutil overwrite what is currently in the 'dst' file. So if I am starting and stopping the program, that file is going to lose everything. There has to be a way around this. And that's exactly what Save-As is supposed to do. If you want something different, you might have said so. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help appending data to a logfile
On 28/06/13 23:18, Matt D wrote: what if i did some thing like this i saw on stackoverflow: f = open("bigfile.txt", "w") That clears any existing content of bigfile.txt, and opens it for writing. Do you intend to clear the content? for tempfile in tempfiles: What is in tempfiles? A list of files opened for reading? Where does this list come from? while True: data = tempfile.read(65536) if data: f.write(data) else: break This copies the content of each tempfile into bigfile.txt. could i make the 'logfile.txt. a tempfile? I don't know. What do you mean by tempfile? and could the f.write be changed to f.append? No, files do not have an append method. Open the file in append mode, then all writes will append to the end of the file. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] multiple function returns
On 06/28/2013 12:06 PM, Jim Mooney wrote: def foo(x): ... if x: ... return True ... return False I'll leave it to you to work out why that works. It's very handy! Hey, it saves typing an "else" and as you know, I'm the Lazy Typist. My program to make dicts, lists, etc from a single string, using no special characters, has saved me so much little-finger typing already I'd never part with it, flawed as it is. It's good-enough ;') If one were trying to save keystrokes here, one would just use foo = bool and be done with it. Or at least, def foo(x): return bool(x) But saving keystrokes should seldom be the goal, unless you're dealing with a disability. In most cases, the comments and the testing code will be much bigger than the actual running code. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] multiple function returns
On 28/06/13 19:51, Jim Mooney wrote: Now you'll make me learn what lambda is. Ah, it's like an anonymous function in jQuery, That's the idea but Python lambdas are a bit crippled so its really an anonymous expression... :-( But if you know JQuery then lambdas should be easy to pick up. And they are really useful in GUI and Functional programming contexts. In summary: foo = lambda : return is equivalent to def foo(paramList): return expression HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help appending data to a logfile
On 28/06/13 20:54, Matt D wrote: def openFile(self, evt): with wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.txt*", wx.SAVE) as dlg: if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() mypath = os.path.basename(path) with open(mypath, "a") as f: f.write(self.logfile) self.logfile.close() It's not clear what state self.logfile is in but assuming you have been writing to it the cursor will be at the end of the file so you can't write anything from it. You would need to do a seek(0) first. But f.write(self.logfile) will not write the contents of logfile. You will need to read that first so I think you need: with open(mypath, "a") as f: self.logfile.seek(0) # go to beginning of the file f.write(self.logfile.read()) # write the contents self.logfile.close() # close but do not delete But I must say this seems like an inordinately complicated way of doing things and quite out of the norm for other applications. Can you back up several steps and explain again what exactly you are trying to achieve? What is the user experience supposed to be? What is being recorded where and why? How does a user interact with it? Logging is such a well established process for most applications that one of a very few patterns are usually followed. You seem to be trying to invent a whole new paradigm here? (And that may not be a bad thing, but we should at least understand why it's needed!) hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help appending data to a logfile
On 28/06/13 22:25, Matt D wrote: Python UI. I have a thread that waits on new data and when it comes in it gets displayed in TextCtrl fields. every time this happens my logger puts those values into a text file one row at a time. this how i open and the file for logging data: # open a file named "logfile.txt" in "a" append mode; self.logfile = open('logfile.txt', 'a') What I don't understand is why you are using a hard coded filename? Why not use the user generated filename here when you first open the file? You can either set it as part of user preferences in a config file or prompt the user if one hasn't been set or use a default value if they don't set it. But however it gets set it shouldn't need to be hard coded. and i have a loop that writes 8 comma separated values in row with this: self.logfile.write('%s,'%(str(f)) It might be better to build the string and then write it out once to the file. But that's a minor matter. however, using this setup the user cannot select the file to save the log to, it just gets written into the home folder. so i came up with this is to allow the user can open a file from the UI: #open file dialog - def openFile(self, evt): with wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.*", wx.OPEN) as dlg: if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() mypath = os.path.basename(path) #with open(mypath, "a") as f: but i hadn't figured a way to get what is in the 'logfile.txt' into the file the user opens with the file dialog. so i was trying this: OK, There is nothing special about that its just copying data into a new file. The bit I don't understand is why continue to log the data in the hardcoded name? Why not change the name of the log file? If need be you can copy any existing logfile(or rename it) into the user supplied name but then all new log entries go to the new file. Its this strange business of maintaining two files I don't understand. ...the tempfile is closed which i was under the impression would destroy the file automatically and free up the memory. It destroys the file object in memory but that does nothing to the file on disk. After all if you closed a file in your editor you wouldn't expect it to be deleted from your hard drive! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help appending data to a logfile
On 06/28/2013 08:04 PM, Alan Gauld wrote: On 28/06/13 22:25, Matt D wrote: Python UI. I have a thread that waits on new data and when it comes in it gets displayed in TextCtrl fields. every time this happens my logger puts those values into a text file one row at a time. this how i open and the file for logging data: # open a file named "logfile.txt" in "a" append mode; self.logfile = open('logfile.txt', 'a') What I don't understand is why you are using a hard coded filename? Why not use the user generated filename here when you first open the file? You can either set it as part of user preferences in a config file or prompt the user if one hasn't been set or use a default value if they don't set it. But however it gets set it shouldn't need to be hard coded. and i have a loop that writes 8 comma separated values in row with this: self.logfile.write('%s,'%(str(f)) It might be better to build the string and then write it out once to the file. But that's a minor matter. however, using this setup the user cannot select the file to save the log to, it just gets written into the home folder. so i came up with this is to allow the user can open a file from the UI: #open file dialog - def openFile(self, evt): with wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.*", wx.OPEN) as dlg: if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() mypath = os.path.basename(path) #with open(mypath, "a") as f: but i hadn't figured a way to get what is in the 'logfile.txt' into the file the user opens with the file dialog. so i was trying this: OK, There is nothing special about that its just copying data into a new file. The bit I don't understand is why continue to log the data in the hardcoded name? Why not change the name of the log file? If need be you can copy any existing logfile(or rename it) into the user supplied name but then all new log entries go to the new file. Its this strange business of maintaining two files I don't understand. ...the tempfile is closed which i was under the impression would destroy the file automatically and free up the memory. It destroys the file object in memory but that does nothing to the file on disk. After all if you closed a file in your editor you wouldn't expect it to be deleted from your hard drive! Matt probably read somewhere about an interface like tempfile.TemporaryFile where the file has no explicit name, and will be deleted from disk, and the space reused as soon as it is closed. I don't believe he's using such an interface, however. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor