Re: [Tutor] cannot get a label message to display immediately
On 15/08/15 06:44, Bill Allen wrote: In my case, as simple as this: def processing(*args): #my initial button click calls this info.set('PROCESSING, PLEASE WAIT...') #the label message I was root.after(1000, process_part) #the long running data process That works for getting the message printed but it still leaves the problem that your UI locks up during the long process. If its only for a couple of seconds it might be a mild hiccup but if your processing took, say 5s or longer, the user is likely to think the program is broken and may force kill the window or process or take similarly drastic action. That's why it's important to break the long process into chunks and call after() from within it. (or run it in the background) To do otherwise is to risk having your process cut short in mid flow with the data potentially only half processed - and you won't know which half! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] try and file existence
On 15/08/2015 04:39, Steven D'Aprano wrote: On Fri, Aug 14, 2015 at 06:28:09PM -0700, Clayton Kirkwood wrote: try: fp = open( user_preferences ) except( PermissionError ): else: with open(user_preferences ) as f: try: fp = open(user_preferences) except (IOError, OSError) as e: handle_error() else: with fp as f: handle_file() I'll just point out that you can catch finer grained errors owing to https://www.python.org/dev/peps/pep-3151/. There is a handy little table here https://docs.python.org/3/library/exceptions.html#exception-hierarchy -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cannot get a label message to display immediately
On Sat, Aug 15, 2015 at 2:21 AM, Alan Gauld wrote: That works for getting the message printed but it still leaves > > the problem that your UI locks up during the long process. > If its only for a couple of seconds it might be a mild hiccup > but if your processing took, say 5s or longer, the user is > likely to think the program is broken and may force kill > the window or process or take similarly drastic action. > > That's why it's important to break the long process into > chunks and call after() from within it. (or run it in the > background) To do otherwise is to risk having your process > cut short in mid flow with the data potentially only > half processed - and you won't know which half! Yes, I see. I will start working on reorganizing the code with that in mind. One other thing that I have found that is quite interesting is that with my current code the use of after() works as expect with the message to the user showing up in the UI - if I run it through the IDLE editor. However, when I run the program from the command line or compile (package) the program with pyinstaller and run it as a standalone executable the message to the user does not show up in the UI! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cannot get a label message to display immediately
> > > On Sat, Aug 15, 2015 Bill Allen wrote: > > Yes, I see. I will start working on reorganizing the code with that in > mind. One other thing that I have found that is quite interesting is that > with my current code the use of after() works as expect with the message to > the user showing up in the UI - if I run it through the IDLE editor. > However, when I run the program from the command line or compile (package) > the program with pyinstaller and run it as a standalone executable the > message to the user does not show up in the UI! > Correction! That was not what was happening. Simple mistake in my code was bypassing the call to the fuction with the after() statement and running my main data processing routine directly. I had forgot to bind to the routine with after() in it. So, program worked OK when I clicked the button but not when I hit Return!. Dumb... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] try and file existence
On Fri, Aug 14, 2015 at 10:39 PM, Steven D'Aprano wrote: > > On Fri, Aug 14, 2015 at 06:28:09PM -0700, Clayton Kirkwood wrote: > > what is the best way to find out if a file exists? > > Try to open it and see what happens. If the open() succeeds, then the > file exists and can be read. If it fails, then either the file doesn't > exist, or it can't be read. Inspect the error to find out which. > > There is also os.path.exists(filename), but you should avoid using that > if possible. The problem is this: > > if os.path.exists(filename): > # file exists *right now* > # but a millisecond later, some other program deletes it... > # and now it doesn't exist any more > with open(filename) as f: # gives an error > ... I understand your points, but wonder then what is the intended use for os.path.exists()? That is, in what types of circumstances would it be both appropriate and safe to use? boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] variable existence q
10 top_directory = "/users/Clayton/Pictures" def override_defaults(): 56 return( top_directory, filetypes, target_directory ) 80 top_directory, filetypes, target_directory = override_defaults() File "C:/Users/Clayton/python/find picture duplicates/find picture duplicates", line 80, in top_directory, filetypes, target_directory = override_defaults() File "C:/Users/Clayton/python/find picture duplicates/find picture duplicates", line 56, in override_defaults return( top_directory, filetypes, target_directory ) UnboundLocalError: local variable 'top_directory' referenced before assignment I am facing the above error: 10 occurs first 80 then runs 56 appears to not work, the function logically does nothing I thought that variables in the main were visible to defined functions in the same file, as long as the assignment occurs physically before use. When debugging, inside of override_defaults sees the correct value. What am I not seeing? TIA, Clayton ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] variable existence q
Clayton Kirkwood wrote: > 10 top_directory = "/users/Clayton/Pictures" > > def override_defaults(): > 56 return( top_directory, filetypes, target_directory ) > > 80 top_directory, filetypes, target_directory = override_defaults() > > > File "C:/Users/Clayton/python/find picture duplicates/find picture > duplicates", line 80, in > top_directory, filetypes, target_directory = override_defaults() > File "C:/Users/Clayton/python/find picture duplicates/find picture > duplicates", line 56, in override_defaults > return( top_directory, filetypes, target_directory ) > UnboundLocalError: local variable 'top_directory' referenced before > assignment > > I am facing the above error: > 10 occurs first > 80 then runs > 56 appears to not work, the function logically does nothing > I thought that variables in the main were visible to defined functions in > the same file, as long as the assignment occurs physically before use. I don't think it's relevant here, but generally speaking the order in the file doesn't matter, only the order of execution matters. For example >>> def f(): return x ... >>> x = 42 >>> >>> print(f()) 42 Even though the assignment to x occurs physically after the function definition, as the function is invoked after that assignment you don't get a NameError. > When debugging, inside of override_defaults sees the correct value. > What am I not seeing? There must be an assignment to top_directory inside override_defaults(). This assignment turns top_directory into a local variable: >>> def f(): ... if False: x = 42 # this turns x into a local name ... return x ... >>> x = 42 >>> f() Traceback (most recent call last): File "", line 1, in File "", line 3, in f UnboundLocalError: local variable 'x' referenced before assignment >>> x # the global x is defined, but not visible inside the function 42 Wether a name is local to the function or global is determined statically by the compiler. This is different from class definitions. Compare: >>> x = 42 >>> class A: x = x ... >>> A.x 42 >>> def f(): x = x ... >>> f() Traceback (most recent call last): File "", line 1, in File "", line 1, in f UnboundLocalError: local variable 'x' referenced before assignment ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] variable existence q
On 15/08/2015 22:11, Peter Otten wrote: Clayton Kirkwood wrote: 10 top_directory = "/users/Clayton/Pictures" def override_defaults(): 56 return( top_directory, filetypes, target_directory ) 80 top_directory, filetypes, target_directory = override_defaults() File "C:/Users/Clayton/python/find picture duplicates/find picture duplicates", line 80, in top_directory, filetypes, target_directory = override_defaults() File "C:/Users/Clayton/python/find picture duplicates/find picture duplicates", line 56, in override_defaults return( top_directory, filetypes, target_directory ) UnboundLocalError: local variable 'top_directory' referenced before assignment I am facing the above error: 10 occurs first 80 then runs 56 appears to not work, the function logically does nothing I thought that variables in the main were visible to defined functions in the same file, as long as the assignment occurs physically before use. I don't think it's relevant here, but generally speaking the order in the file doesn't matter, only the order of execution matters. For example def f(): return x ... x = 42 print(f()) 42 Even though the assignment to x occurs physically after the function definition, as the function is invoked after that assignment you don't get a NameError. When debugging, inside of override_defaults sees the correct value. What am I not seeing? There must be an assignment to top_directory inside override_defaults(). This assignment turns top_directory into a local variable: def f(): ... if False: x = 42 # this turns x into a local name ... return x ... x = 42 f() Traceback (most recent call last): File "", line 1, in File "", line 3, in f UnboundLocalError: local variable 'x' referenced before assignment x # the global x is defined, but not visible inside the function 42 Wether a name is local to the function or global is determined statically by the compiler. This is different from class definitions. Compare: x = 42 class A: x = x ... A.x 42 def f(): x = x ... f() Traceback (most recent call last): File "", line 1, in File "", line 1, in f UnboundLocalError: local variable 'x' referenced before assignment Your explanation doesn't make any sense to me. I'd have thought that having assigned top_directory at line 10, but then trying to reassign it at line 80, means that the function now knows nothing about it, hence the error. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] try and file existence
In a message of Sat, 15 Aug 2015 14:24:21 -0500, boB Stepp writes: >I understand your points, but wonder then what is the intended use for >os.path.exists()? That is, in what types of circumstances would it be >both appropriate and safe to use? > >boB If you want to locate dangling symlinks, os.path.exists will return False, so the symlink is there, but the file it pointed to is long gone. Laura ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] variable existence q
On 8/15/2015 2:47 PM, Mark Lawrence wrote: On 15/08/2015 22:11, Peter Otten wrote: Clayton Kirkwood wrote: 10 top_directory = "/users/Clayton/Pictures" def override_defaults(): 56 return( top_directory, filetypes, target_directory ) 80 top_directory, filetypes, target_directory = override_defaults() File "C:/Users/Clayton/python/find picture duplicates/find picture duplicates", line 80, in top_directory, filetypes, target_directory = override_defaults() File "C:/Users/Clayton/python/find picture duplicates/find picture duplicates", line 56, in override_defaults return( top_directory, filetypes, target_directory ) UnboundLocalError: local variable 'top_directory' referenced before assignment Your explanation doesn't make any sense to me. I'd have thought that having assigned top_directory at line 10, but then trying to reassign it at line 80, means that the function now knows nothing about it, hence the error. Assigning to a variable inside a function makes that variable local, which must have happened as per the error message: UnboundLocalError: local variable 'top_directory'... As Peter noted, somewhere within override_defaults there's an assignment to it. Changing to def override_defaults(top_directory=top_directory): should initialize it in case the assignment path isn't processed. Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] try and file existence
> -Original Message- > From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On > Behalf Of Laura Creighton > Sent: Saturday, August 15, 2015 2:49 PM > To: boB Stepp > Cc: l...@openend.se; tutor > Subject: Re: [Tutor] try and file existence > > In a message of Sat, 15 Aug 2015 14:24:21 -0500, boB Stepp writes: > >I understand your points, but wonder then what is the intended use for > >os.path.exists()? That is, in what types of circumstances would it be > >both appropriate and safe to use? > > > >boB > > If you want to locate dangling symlinks, os.path.exists will return False, so > the symlink is there, but the file it pointed to is long gone. Can't you do that with os.path.open() and get a value in os.path.status? (I think that is the thing to call) crk > > Laura > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] variable existence q
top_directory = "/users/Clayton/Pictures" target_directory = top_directory #directory we are checking filetypes = ('jpg', 'png', 'avi', 'mp4', 'mov', 'bmp') imports... def override_defaults(): with open( user_preferences ) as f: for line in f.readline(): llist = line.split() if llist[0] == '#': #comment line to ignore continue elif llist[0] == 'top_directory': if len(llist) == 1: pass else: top_directory = llist[1] elif llist[0] == 'target_directory': if len(llist) == 1: pass else: target_directory = llist[1] else: #assume only filetypes now or until next comment or other keyword if llist[0] == 'filetypes': #allow keyword w/wo following types if llist.length() == 1: continue #assume user plans either not interested in types or types coming on later line llist.pop([0]) #skip keyword and start recording filetypes.append(llist[0:]) #assume line contains 0, consumes blank lines, or more media files w/wo leading dot continue 56return( top_directory, filetypes, target_directory ) 80 top_directory, filetypes, target_directory = override_defaults()> The error message again is: File "C:/Users/Clayton/python/find picture duplicates/find picture duplicates", line 80, in top_directory, filetypes, target_directory = override_defaults() File "C:/Users/Clayton/python/find picture duplicates/find picture duplicates", line 56, in override_defaults return( top_directory, filetypes, target_directory ) UnboundLocalError: local variable 'top_directory' referenced before assignment > > Your explanation doesn't make any sense to me. I'd have thought that > > having assigned top_directory at line 10, but then trying to reassign > > it at line 80, means that the function now knows nothing about it, > > hence the error. > > Assigning to a variable inside a function makes that variable local, which must > have happened as per the error message: > UnboundLocalError: local variable 'top_directory'... > > As Peter noted, somewhere within override_defaults there's an assignment > to it. Changing to > def override_defaults(top_directory=top_directory): > should initialize it in case the assignment path isn't processed. Above is the actual code. The file /user/user preferences exists but is empty. Defaults are at the top. For what it is worth, the debugger stopped in the function shows the values stated as the defaults at the top. If I understand correctly, the readline() would drop out, but even if it doesn't no assignments would be made for top_directory or target_directory. I thought that top_directory was global to this file. I am hearing that it doesn't matter whether the assignment is above or below the function definition. I should be able to use the tuple for the results of the call, right? In this case, no assignment was made. If I understand, the function sees the global. If that is changed inside the function, doesn't it change the global? Crk > > Emile > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] variable existence q
On 8/15/2015 3:38 PM, Clayton Kirkwood wrote: top_directory = "/users/Clayton/Pictures" target_directory = top_directory #directory we are checking filetypes = ('jpg', 'png', 'avi', 'mp4', 'mov', 'bmp') imports... def override_defaults(): with open( user_preferences ) as f: for line in f.readline(): llist = line.split() if llist[0] == '#': #comment line to ignore continue elif llist[0] == 'top_directory': if len(llist) == 1: pass else: top_directory = llist[1] elif llist[0] == 'target_directory': if len(llist) == 1: pass else: target_directory = llist[1] else: #assume only filetypes now or until next comment or other keyword if llist[0] == 'filetypes': #allow keyword w/wo following types if llist.length() == 1: continue #assume user plans either not interested in types or types coming on later line llist.pop([0]) #skip keyword and start recording filetypes.append(llist[0:]) #assume line contains 0, consumes blank lines, or more media files w/wo leading dot continue 56return( top_directory, filetypes, target_directory ) 80 top_directory, filetypes, target_directory = override_defaults()> The error message again is: File "C:/Users/Clayton/python/find picture duplicates/find picture duplicates", line 80, in top_directory, filetypes, target_directory = override_defaults() File "C:/Users/Clayton/python/find picture duplicates/find picture duplicates", line 56, in override_defaults return( top_directory, filetypes, target_directory ) UnboundLocalError: local variable 'top_directory' referenced before assignment Your explanation doesn't make any sense to me. I'd have thought that having assigned top_directory at line 10, but then trying to reassign it at line 80, means that the function now knows nothing about it, hence the error. Assigning to a variable inside a function makes that variable local, which must have happened as per the error message: UnboundLocalError: local variable 'top_directory'... As Peter noted, somewhere within override_defaults there's an assignment to it. Changing to def override_defaults(top_directory=top_directory): should initialize it in case the assignment path isn't processed. Above is the actual code. The file /user/user preferences exists but is empty. Defaults are at the top. For what it is worth, the debugger stopped in the function shows the values stated as the defaults at the top. If I understand correctly, the readline() would drop out, but even if it doesn't no assignments would be made for top_directory or target_directory. Actual assignment isn't required to make it a local variable -- only the fact that it appears on the left hand side of an assignment statement with the function. I thought that top_directory was global to this file. I am hearing that it doesn't matter whether the assignment is above or below the function definition. I should be able to use the tuple for the results of the call, right? In this case, no assignment was made. If I understand, the function sees the global. Not any more -- it's a local variable because the assignment, while not executed, exists. If that is changed inside the function, doesn't it change the global? Only if you include the globals statement before the variable is referenced. Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] try and file existence
On 15Aug2015 15:20, Clayton Kirkwood wrote: Behalf Of Laura Creighton [..] To: boB Stepp In a message of Sat, 15 Aug 2015 14:24:21 -0500, boB Stepp writes: >I understand your points, but wonder then what is the intended use for >os.path.exists()? That is, in what types of circumstances would it be >both appropriate and safe to use? If you want to locate dangling symlinks, os.path.exists will return False, so the symlink is there, but the file it pointed to is long gone. Can't you do that with os.path.open() and get a value in os.path.status? (I think that is the thing to call) Open does more that os.stat (which is what os.path.exists uses underneath). There are plenty of times you will want to know a file exists but not have permission to open it. Also, open can have side effects if the target file is a device or a socket/pipe. Always use the smallest thing you can to achieve an effect: stat is smaller than open. Cheers, Cameron Simpson Q: How many user support people does it take to change a light bulb? A: We have an exact copy of the light bulb here and it seems to be working fine. Can you tell me what kind of system you have? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] variable existence q
On 15/08/2015 23:38, Clayton Kirkwood wrote: top_directory = "/users/Clayton/Pictures" target_directory = top_directory #directory we are checking filetypes = ('jpg', 'png', 'avi', 'mp4', 'mov', 'bmp') imports... def override_defaults(): with open( user_preferences ) as f: for line in f.readline(): llist = line.split() if llist[0] == '#': #comment line to ignore continue elif llist[0] == 'top_directory': if len(llist) == 1: pass else: top_directory = llist[1] elif llist[0] == 'target_directory': if len(llist) == 1: pass else: target_directory = llist[1] else: #assume only filetypes now or until next comment or other keyword if llist[0] == 'filetypes': #allow keyword w/wo following types if llist.length() == 1: continue #assume user plans either not interested in types or types coming on later line llist.pop([0]) #skip keyword and start recording filetypes.append(llist[0:]) #assume line contains 0, consumes blank lines, or more media files w/wo leading dot continue 56return( top_directory, filetypes, target_directory ) 80 top_directory, filetypes, target_directory = override_defaults()> The error message again is: File "C:/Users/Clayton/python/find picture duplicates/find picture duplicates", line 80, in top_directory, filetypes, target_directory = override_defaults() File "C:/Users/Clayton/python/find picture duplicates/find picture duplicates", line 56, in override_defaults return( top_directory, filetypes, target_directory ) UnboundLocalError: local variable 'top_directory' referenced before assignment Your explanation doesn't make any sense to me. I'd have thought that having assigned top_directory at line 10, but then trying to reassign it at line 80, means that the function now knows nothing about it, hence the error. Assigning to a variable inside a function makes that variable local, which must have happened as per the error message: UnboundLocalError: local variable 'top_directory'... As Peter noted, somewhere within override_defaults there's an assignment to it. Changing to def override_defaults(top_directory=top_directory): should initialize it in case the assignment path isn't processed. Above is the actual code. The file /user/user preferences exists but is empty. Defaults are at the top. For what it is worth, the debugger stopped in the function shows the values stated as the defaults at the top. If I understand correctly, the readline() would drop out, but even if it doesn't no assignments would be made for top_directory or target_directory. I thought that top_directory was global to this file. I am hearing that it doesn't matter whether the assignment is above or below the function definition. I should be able to use the tuple for the results of the call, right? In this case, no assignment was made. If I understand, the function sees the global. If that is changed inside the function, doesn't it change the global? Crk You are trying to change it at line 80. Do you really want to do that? If no I suggest you spell it TOP_DIRECTORY to indicate that it is a constant that should not be changed. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] How best to determine if a db exists before trying to open it? [Was: try and file existence]
On Sat, Aug 15, 2015 at 6:00 PM, Cameron Simpson wrote: > On 15Aug2015 15:20, Clayton Kirkwood wrote: >>> >>> Behalf Of Laura Creighton > > [..] >>> >>> To: boB Stepp >>> In a message of Sat, 15 Aug 2015 14:24:21 -0500, boB Stepp writes: >>> >I understand your points, but wonder then what is the intended use for >>> >os.path.exists()? That is, in what types of circumstances would it be >>> >both appropriate and safe to use? >>> >>> If you want to locate dangling symlinks, os.path.exists will return >>> False, so >>> the symlink is there, but the file it pointed to is long gone. >> >> >> Can't you do that with os.path.open() and get a value in os.path.status? >> (I >> think that is the thing to call) > > > Open does more that os.stat (which is what os.path.exists uses underneath). > > There are plenty of times you will want to know a file exists but not have > permission to open it. Also, open can have side effects if the target file > is a device or a socket/pipe. > > Always use the smallest thing you can to achieve an effect: stat is smaller > than open. I actually had a specific example in mind when I asked my question. My current project will require me to create and then use an SQLite db. My concern is that after: import sqlite3 db = sqlite3.connect("my_db.db") 1) This will open the db if it exists already, which is normally what I will want. But... 2) My understanding is that if for whatever reason the db file is not found, then the connect statement will create a new instance of the db, which is what I normally would not want (Except at the time of initial creation). I'm just now in the process of reading up on SQLite, SQL, and Python's DB API. So far I have seen no mention that the connect statement returns anything if the db file does not already exist. If I am understanding everything so far, I think that my situation would be appropriate for using os.path.exists(). Is this correct? Thanks! boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] try and file existence
On Sat, Aug 15, 2015 at 02:24:21PM -0500, boB Stepp wrote: > On Fri, Aug 14, 2015 at 10:39 PM, Steven D'Aprano wrote: > > There is also os.path.exists(filename), but you should avoid using that > > if possible. The problem is this: > > > > if os.path.exists(filename): > > # file exists *right now* > > # but a millisecond later, some other program deletes it... > > # and now it doesn't exist any more > > with open(filename) as f: # gives an error > > ... > > I understand your points, but wonder then what is the intended use for > os.path.exists()? That is, in what types of circumstances would it be > both appropriate and safe to use? def print_file_names(possible_names): print("List of file names checked") print("--" for name in possible_names: if os.path.exists(name): print(name) else: print("missing:", name) -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] try and file existence
On Sat, Aug 15, 2015 at 6:41 PM, Steven D'Aprano wrote: > On Sat, Aug 15, 2015 at 02:24:21PM -0500, boB Stepp wrote: >> I understand your points, but wonder then what is the intended use for >> os.path.exists()? That is, in what types of circumstances would it be >> both appropriate and safe to use? > > def print_file_names(possible_names): > print("List of file names checked") > print("--" > for name in possible_names: > if os.path.exists(name): > print(name) > else: > print("missing:", name) Your example, giving about the most benign possible uses, is for emphasis? boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] variable naming conventions
It seems every book I read these days uses camel case for variable names in Python. I was once told that using underscores is preferred. Is there a preference in the Python community or does it really matter? I'd like to instill good habits while I'm learning. Thanks in advance, -- Deb Wyatt in WA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] variable naming conventions
On Sat, Aug 15, 2015 at 9:24 PM, D Wyatt wrote: > It seems every book I read these days uses camel case for variable names in > Python. I was once told that using underscores is preferred. Is there a > preference in the Python community or does it really matter? I'd like to > instill good habits while I'm learning. > > Thanks in advance, > -- > Deb Wyatt in WA > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor lower case. underscores -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] variable naming conventions
On 16/08/2015 02:24, D Wyatt wrote: It seems every book I read these days uses camel case for variable names in Python. I was once told that using underscores is preferred. Is there a preference in the Python community or does it really matter? I'd like to instill good habits while I'm learning. Thanks in advance, If it's your code for your use do whatever you like. I prefer camel case as it saves reaching for the SHIFT-MINUS combination, others detest it. Even the famous PEP 8 (https://www.python.org/dev/peps/pep-0008/) is only a guide. However if I was working on a project in collaboration with others I would certainly expect to stick with the standards that the project insisted on, even if I didn't like them personally. Anybody who deliberately ignores standards in this situation should be hung, drawn and quartered after spending an extremely long amount of time in agony in The Comfy Chair :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Where should unit test files go in a project directory structure and some related questions?
In this Montessori Classroom Project (mcm) I am working on, I hope to incorporate as much of the advice I've gotten here as I can remember. So I want to do version control, TDD, etc., and do it as well as I can. Especially since this thing looks to grow considerably over time. I have not ever thought about formal project directory structures before, so I may need a bit of guidance here. My current skeleton structure is: /Projects /mcm /.git /db __init__.py /ui __init__.py main.py My intent is to use main.py to start the program. I know I am going to be using a database and a user interface, thus their folders. I don't yet have a feeling for how many additional directories I might have for the bulk of the program logic code. Right now I am trying to figure out how to arrange my unit test file(s). My initial thoughts are to have a single test directory with separate subdirectories corresponding to each folder which has source code. Is this a good way to do things? While searching through the Tutor archives I found one of Steve's answers on project structure, which I copied part of below. Unfortunately it did not address testing organization: --- Re: [Tutor] Project directory structure Steven D'Aprano Thu, 30 Jan 2014 03:26:22 -0800 If you're creating something a little more formal, say you plan to make it public, there is a convention for laying out project directories: myproject +-- CHANGES.txt +-- LICENCE.txt +-- MANIFEST.in +-- README.txt +-- setup.py +-- src +-- myproject.py although the src directory is not compulsory. If you're creating a package, rather than a single module, then you do need to use a special directory structure: mypackage +-- __init__.py +-- __main__.py +-- cheese.py +-- eggs.py +-- spam.py --- It looks like I ought to combine the myproject and mypackage recommendations into a coherent whole. It looks like my current subfolders of /db and /ui as well as main.py should go under the subfolder /src. The suggested files CHANGES.txt, LICENSE.txt, and README.txt look like good things to include. I don't know yet what MANIFEST.in is about. Makes me think of a shipping manifest, but that is probably not its function. And I don't know what set-up needs setup.py would typically serve. I will have to search for answers to these. Does this cover everything I need to be concerned about as far as project organization so that I do not run into future problems? TIV! boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] variable naming conventions
On 15Aug2015 18:24, D Wyatt wrote: It seems every book I read these days uses camel case for variable names in Python. I was once told that using underscores is preferred. Is there a preference in the Python community or does it really matter? I'd like to instill good habits while I'm learning. PEP 8 is lowercase with underscores for normal variables. Class names tend to be CamelCase. I try to follow this. One advantage in staying with this is that you share this convention with the stdlib and with a lot of other Python code, which make it easier for you to read because it aligns with your own habits. Ideally, anyway. If you're not invested in another style, and not working in someone else's codebase with its own conventions, try PEP 8. Cheers, Cameron Simpson Oh, what tangled webs we weave, when first we practice to deceive. And when we've practiced for awhile, How we do improve our style!- Dorothy Parker ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] variable existence q
> -Original Message- > From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On > Behalf Of Mark Lawrence > Sent: Saturday, August 15, 2015 4:05 PM > To: tutor@python.org > Subject: Re: [Tutor] variable existence q > > On 15/08/2015 23:38, Clayton Kirkwood wrote: > > top_directory = "/users/Clayton/Pictures" > > target_directory = top_directory #directory we are checking > > filetypes = ('jpg', 'png', 'avi', 'mp4', 'mov', 'bmp') > > > > imports... > > > > def override_defaults(): > > with open( user_preferences ) as f: > > for line in f.readline(): > > llist = line.split() > > if llist[0] == '#': #comment line to ignore > > continue > > elif llist[0] == 'top_directory': > > if len(llist) == 1: > > pass > > else: > > top_directory = llist[1] > > elif llist[0] == 'target_directory': > > if len(llist) == 1: > > pass > > else: > > target_directory = llist[1] > > else: #assume only filetypes now or until next comment or > > other keyword > > if llist[0] == 'filetypes': #allow keyword w/wo > > following types > > if llist.length() == 1: > > continue #assume user plans either not > > interested in types or types coming on later line > > llist.pop([0]) #skip keyword and start > > recording > > filetypes.append(llist[0:]) #assume line contains 0, > > consumes blank lines, or more media files w/wo leading dot > > continue > > 56return( top_directory, filetypes, target_directory ) > > 80 top_directory, filetypes, target_directory = override_defaults()> > > > > The error message again is: > >File "C:/Users/Clayton/python/find picture duplicates/find picture > > duplicates", line 80, in > > top_directory, filetypes, target_directory = override_defaults() > >File "C:/Users/Clayton/python/find picture duplicates/find picture > > duplicates", line 56, in override_defaults > > return( top_directory, filetypes, target_directory ) > > UnboundLocalError: local variable 'top_directory' referenced before > > assignment > > > >>> Your explanation doesn't make any sense to me. I'd have thought > >>> that having assigned top_directory at line 10, but then trying to > >>> reassign it at line 80, means that the function now knows nothing > >>> about it, hence the error. > >> > >> Assigning to a variable inside a function makes that variable local, > >> which > > must > >> have happened as per the error message: > >> UnboundLocalError: local variable 'top_directory'... > >> > >> As Peter noted, somewhere within override_defaults there's an > >> assignment to it. Changing to > >> def override_defaults(top_directory=top_directory): > >> should initialize it in case the assignment path isn't processed. > > > > Above is the actual code. The file /user/user preferences exists > > but is empty. Defaults are at the top. For what it is worth, the > > debugger stopped in the function shows the values stated as the > > defaults at the top. If I understand correctly, the readline() would > > drop out, but even if it doesn't no assignments would be made for > > top_directory or target_directory. I thought that top_directory was > > global to this file. I am hearing that it doesn't matter whether the > > assignment is above or below the function definition. I should be able > > to use the tuple for the results of the call, right? In this case, no > > assignment was made. If I understand, the function sees the global. If > > that is changed inside the function, doesn't it change the global? > > > > Crk > > You are trying to change it at line 80. Do you really want to do that? > If no I suggest you spell it TOP_DIRECTORY to indicate that it is a constant > that should not be changed. No, I am saying at the top that the defaults are set, and if running the override_defaults changes them, then they get changed. I have defaults, and I allow the user to override them. crk > > -- > My fellow Pythonistas, ask not what our language can do for you, ask what > you can do for our language. > > Mark Lawrence > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Where should unit test files go in a project directory structure and some related questions?
On Sat, Aug 15, 2015 at 9:10 PM, boB Stepp wrote: > Right now I am trying to figure out how to arrange my unit test > file(s). My initial thoughts are to have a single test directory with > separate subdirectories corresponding to each folder which has source > code. Is this a good way to do things? Apparently, based on tonight's searching, there is a large diversity of opinion on where to put one's tests! I did find one good web article, "Python Project Howto", at http://infinitemonkeycorps.net/docs/pph/ that seems really good. It is oriented on how to prep your project for open source distribution and is quite detailed. On the question I am interested in, the author proposes this project structure: googlemaps/ # Project Hosting .svn/ # Version Control googlemaps/ # Quality Code googlemaps.py test/ # Unit Testing test_googlemaps.py doc/ # Documentation index.rst html/ index.html README.txt LICENSE.txt # Licensing setup.py # Packaging MANIFEST.in I think that this article would make a good read for anyone in a similar learning situation as mine. Cheers! boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] How to test my code's interactions with SQLite db?
Being committed to TDD for this project, I am not yet ready to write code till I figure out how to write the tests. It occurs to me that writing tests for code that interacts with the SQLite db may be non-trivial (At least for me!). After doing some online research tonight, it appears that I need to create a test SQLite db. I don't see any other way that I can test such code without having a db to test against. So this leads to several questions: 1) It would seem that I need to install a stand-alone version of SQLite, so that I can create this test db. Either that or write a separate Python program whose sole purpose would be to create this test db. But if I go with installing a stand-alone SQLite, will I run into version conflicts with whatever version of SQLite is bundled in the standard library of Python 3.4.3? 2) If I install the test db I can conceptually see that I should be able to test all of the Python code that interacts with it. However, do I need to figure out some way to test what will eventually become the *real* db that the program will generate and use? How will I know if my test db structure and the resulting actual db structure that the ultimate user(s) will populate are in agreement? Or am I over-analyzing here? TIA! -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor