Re: [Tutor] Python and Tkinter Programming by Grayson--New Version?
Sent from my iPod ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and Tkinter Programming by Grayson--New Version?
Yves Dextraze yd...@videotron.ca wrote Sent from my iPod There is no mention on Amazon of any new editions and they usually announce several months in advance... A pity a new Tkinter book using Tix and ttk instead of PMW would be a really useful resource! Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Random Number Question
On 25-11-10 08:00, Mac Ryan wrote: On Thu, 25 Nov 2010 00:58:23 + Adam Barkadam.jt...@gmail.com wrote: Ah yes always avoid giving your modules names that appear in the standard library. It goes wrong, sometimes in unexpected ways. I was wondering... apart from checking each name individually, is there any easy-peasy way to get a list of names used in the standard library (I am thinking to something like dir()? This is the webpage I always use for searching an appropriate module: http://docs.python.org/modindex.html Cheers, Timo Mac. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Random Number Question
On Thu, 25 Nov 2010 11:09:10 +0100 Timo timomli...@gmail.com wrote: I was wondering... apart from checking each name individually, is there any easy-peasy way to get a list of names used in the standard library (I am thinking to something like dir()? This is the webpage I always use for searching an appropriate module: http://docs.python.org/modindex.html I was more thinking to some introspective capacity of python itself rather than a web page... yet, thank you for the link that I did not know beforehand! :) Mac. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Random Number Question
Well, you can try import x, but your problem wasn't that you used the same filename as some Python script on your path, the problem was you then tried to import that other script. So to avoid infinite recursion, all you have to do is avoid importing your own filename, which should be easy enough to do (assuming there aren't any indirect loops, of course.. but admitting that possibility is the price you pay for having the advantages of a dynamic language) On Thursday 25 November 2010, Mac Ryan wrote: On Thu, 25 Nov 2010 00:58:23 + Adam Bark adam.jt...@gmail.com wrote: Ah yes always avoid giving your modules names that appear in the standard library. It goes wrong, sometimes in unexpected ways. I was wondering... apart from checking each name individually, is there any easy-peasy way to get a list of names used in the standard library (I am thinking to something like dir()? Mac. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] If os.path.lexists() isn't working properly
Hi Dave! Thank you for your suggestion I haven't prevent the problems you're describing, but I'm newbie in this stuff so, where should I repalce the code? Thank you again! 2010/11/24 Dave Angel da...@ieee.org On 01/-10/-28163 02:59 PM, Susana Iraiis Delgado Rodriguez wrote: Hello Peter! I added the line you suggested me and found out that I was just searching for the filenames without pointing to a specific directory, so Python took its directory (Python26) as default. After that I need to add a '\' to separate the path from the filename because it was reading them as a single string. But finally it worked nicely! here is the corrected script. snip wksht.row(row).write(1, filename) n = os.path.splitext(filename) p = ruta+'\\'+n[0]+'.prj' Use os.path.join() to combine these nodes. If you use an explicit backslash you can trigger two problems: 1) Your code won't be portable to other platforms 2) You could have trouble if one of the components already has a path separator. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] If os.path.lexists() isn't working properly
On 11/25/2010 09:20 AM, Susana Iraiis Delgado Rodriguez wrote: Hi Dave! Thank you for your suggestion I haven't prevent the problems you're describing, but I'm newbie in this stuff so, where should I repalce the code? Thank you again! 2010/11/24 Dave Angelda...@ieee.org On 01/-10/-28163 02:59 PM, Susana Iraiis Delgado Rodriguez wrote: snip wksht.row(row).write(1, filename) n = os.path.splitext(filename) p = ruta+'\\'+n[0]+'.prj' Use os.path.join() to combine these nodes. If you use an explicit backslash you can trigger two problems: 1) Your code won't be portable to other platforms 2) You could have trouble if one of the components already has a path separator. DaveA May I request that you put your response at the end, or directly after whatever you're responding to. What you did is called top-posting, and while it's common in private email, it's bad practice in these forums. It's also good to trim the parts that are no longer relevant, so the messages don't get too huge. I'm guilty of omitting that step way too often. You have an example of os.path.join() in your own code, a few lines up. But for this particular line, you could use p = os.path.join(ruta, n[0], .lprj) HTH DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python module structure directories
Hello, I use to have it under src/lib as follow: src/lib/python src/lib/tcl src/lib/c All *.py modules are in src/lib/python with all the possible modules hierarchy. Then, At build time I copy lib root directory in the install. (with all C code compiled). Then the startup bin executable do the following as Alan said: *if ! echo $PYTHON | grep PREFIX_PATH_TO_LIB/lib /dev/null ; then export PYTHONPATH=PREFIX_PATH_TO_LIB/lib:${PYTHONPATH} fi* The conditional statement is here just to not overload the environment variable. Best Regards Karim On 11/25/2010 02:23 AM, Alan Gauld wrote: Judy Chen jgc...@yahoo.com wrote I am very new to Python, I worked on C/C++ before. I would like to know is it a good practice to put Python development code under ../src/UI/foo.py ../src/businesslogic/bar.py, etc. Thats fine, especially if its a big project. src means source code and python is a type of source just as C is. or should we eliminate src' directory since it is not pythonic, or it very C/C++ like. Who says its not pythonic? src is a perfectly common name to use on Unix type systems for all types of source code. I was told that the above directory/file structure does not apply to Python, since Python's source code are objects. The source code is not really an object but becaiuse you can import any python file as a module, and modules are objects (once they are loaded) you might get away with saying that. But really, python source files are no different to any other source files when it comes to organising your file system for a project. Are there any standard for how Python source code to be structured? Not that I'm aware of. There are some standards for how to create packages which might restrict things a little but oprovided you have your PYHONPATH set up tio find the modules all should be well. And you might want to create a build script that moves the modules from src to lib in the production file system. But on a project, especially one with multiple programming languages, having all source files in one place is a definite plus IMHO. Another thing to do would be take a look at some of the Python projects on Sourceforge - DIA for example. See how they structure their code. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python module structure directories
Another possibility is to place you python library in the site package of your network installation, namely for my computer: *$HOME/build/python/src/Python-2.7.1rc1/Lib/site-packages* I build the latest release of python (rc1). But At build time I believe that the path to the libraries is hard-coded somewhere. Indeed, I provide the python v2.7.1rc1 package along with my application and I use PYTHONPATH to overcome this fact and point to the libraries of v2.7.1rc1 and PATH for python exec. Like that I am independent of the version of python present on a particular machine. Best Regards Karim On 11/25/2010 02:23 AM, Alan Gauld wrote: Judy Chen jgc...@yahoo.com wrote I am very new to Python, I worked on C/C++ before. I would like to know is it a good practice to put Python development code under ../src/UI/foo.py ../src/businesslogic/bar.py, etc. Thats fine, especially if its a big project. src means source code and python is a type of source just as C is. or should we eliminate src' directory since it is not pythonic, or it very C/C++ like. Who says its not pythonic? src is a perfectly common name to use on Unix type systems for all types of source code. I was told that the above directory/file structure does not apply to Python, since Python's source code are objects. The source code is not really an object but becaiuse you can import any python file as a module, and modules are objects (once they are loaded) you might get away with saying that. But really, python source files are no different to any other source files when it comes to organising your file system for a project. Are there any standard for how Python source code to be structured? Not that I'm aware of. There are some standards for how to create packages which might restrict things a little but oprovided you have your PYHONPATH set up tio find the modules all should be well. And you might want to create a build script that moves the modules from src to lib in the production file system. But on a project, especially one with multiple programming languages, having all source files in one place is a definite plus IMHO. Another thing to do would be take a look at some of the Python projects on Sourceforge - DIA for example. See how they structure their code. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Random Number Question
Alan Gauld wrote: import random It tries to import itself, which then tries to import itself, which then. infinite loop time... I think it's more like - look up module random in sys.modules - module random not found in sys.modules; locate the file random.py - file random.py found, create a new empty module object and put it into sys.modules - execute code loaded from random.py, encounter 'import random', look up random in sys.modules - module random found in sys.modules, bind it to the name 'random' in itself - continue execution of random.py code. This will succeed unless you try to access random.some_name for a name not already defined. In short: you are more likely to get an AttributeError than infinite recursion. $ cat module.py print importing, __name__ import module print module.yadda yadda = 42 $ python -c'import module' importing module Traceback (most recent call last): File string, line 1, in module File module.py, line 3, in module print module.yadda AttributeError: 'module' object has no attribute 'yadda' $ cat module2.py print importing, __name__ yadda = 42 import module2 print module2.yadda $ python -c'import module2' importing module2 42 While the latter works as in runs without error it still isn't a good idea. By the way, you can generalise the above to arbitrary circular imports where module a imports b which imports c which imports ... which imports z which imports a. Peter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] if syntax expression help
I have a user entered variable that I need to check to see if they entered one of the two legal values. But I only need to check this if one other fact is true. we have a variable called mode whose value is either add or edit based on how we where called. we have a userentry variable tied to an imput function. My current if statement looks like this: if ((userentry.lower != c or userentry.lower != i) and mode == add): do stuff else: do other stuff My problem is that my other stuff always executes even when I think my stuff should run. so what I want is two conditions tested simultaneously I think I have a syntax problem with the parenthesis because I can't find documentation where this is valid, but it doesn't error out so python doesn't choke on this. I know about nested ifs but I was trying to avoid them for elegance and easier reading. if mode is add then my userentry field needs good data, but if my mode is NOT add then I don't care about this data cause I'm not going to be using it. Ive tried to construct the logic such that when the if is true, its the stuff that runs, and when the if is false, its the other stuff that runs. Because of the NOT equal tos in the if a false is actually a passed test, and true is actually a failed test. Could someone please help me figure out the best way to say this in python. BTW its python31 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] if syntax expression help
Rance Hall wrote: I have a user entered variable that I need to check to see if they entered one of the two legal values. But I only need to check this if one other fact is true. we have a variable called mode whose value is either add or edit based on how we where called. we have a userentry variable tied to an imput function. My current if statement looks like this: if ((userentry.lower != c or userentry.lower != i) and mode == add): Hint: userentry = YADDA userentry.lower built-in method lower of str object at 0x7f94961f5ba0 userentry.lower() 'yadda' ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] if syntax expression help
Rance Hall ran...@gmail.com wrote My current if statement looks like this: if ((userentry.lower != c or userentry.lower != i) and mode == add): Peter has poinrted out one roblem but there is another. The or expression will always be true, consider: 1) The first term will only fail if the value is 'c' If the value is 'c; then the second trrrm will be True and the or expression will be True 2)The 2nd term will only fail if the value is 'i' If the value is 'i' then the first term will be True and the or expression will be True And since True and A is the same as A you ight as well just write: if mode == 'add': Based on your code I'm not actually sure what the correct logic should be, but I'm pretty sure its not what you have! -- Alan Gauld 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
[Tutor] for loops when there is only one row in the result - is there an alternative?
Im using the py-postgresql module (docs here: http://python.projects.postgresql.org/docs/1.0/) in a python 3.1 environment to connect to my database. so far everything is working, but I'm having trouble understanding the structure of the variable returned by a select statement Generally you have something like this: clientlist = get_clients() # where get_clients is a prepared sql statement. normally you would get the individual rows like this: for row in clientlist: do stuff which is great for a long list of results. But I'm running into issues when there are only 0 or 1 results in the set. if there are zero rows then I can do something like: if len(clientlist) == 0: do stuff I'm looking for a better way to access the row when there is just one row in the result. Say from a user login attempt, or a request to edit an existing client record. Is there a decent way to get direct access to the single row in the result set without having to go through the for loop for just one item? It likely helps to know exactly what variable type clientlist would be. I have no idea. What I can say is that once you do get the row result, you can refer to values in the row with syntax like row[columnname], but I'm honestly not sure if this is helpful information. Ive read the module docs looking for something interesting, but I can't seem to find this particular tidbit. If I have to do the for loop fine, but I just thought it looked a little ugly. Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] for loops when there is only one row in the result - is there an alternative?
Rance Hall wrote: Generally you have something like this: clientlist = get_clients() # where get_clients is a prepared sql statement. normally you would get the individual rows like this: for row in clientlist: do stuff which is great for a long list of results. But I'm running into issues when there are only 0 or 1 results in the set. If clientlist is empty, you don't need to explicitly test for it: clientlist = [] # nothing there for row in clientlist: ... print(Hello world) ... Notice that nothing gets printed. if there are zero rows then I can do something like: if len(clientlist) == 0: do stuff Generally one merely needs to check the list itself: if clientlist: ... although be warned that lazy lists, or iterators, are slightly different -- but if len(clientlist) works, then this should also work. I'm looking for a better way to access the row when there is just one row in the result. This should work: row, = clientlist # if one row only -- note the comma! row1, row2 = clientlist # if two rows row1, row2, row3 = clientlist # three rows If the comma in the first example is too subtle, try this: [row] = clientlist Another way which should work: row = clientlist[0] Say from a user login attempt, or a request to edit an existing client record. Is there a decent way to get direct access to the single row in the result set without having to go through the for loop for just one item? It likely helps to know exactly what variable type clientlist would be. I have no idea. type(clientlist) will tell you. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] List Changing Order
Tutors, I recall that the keys of dictionaries have arbitrary order, and may change over time. Is this true of lists? I can't find the answer from a simple Google search. Thank you! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List Changing Order
On Fri, Nov 26, 2010 at 1:47 PM, Corey Richardson kb1...@aim.com wrote: I recall that the keys of dictionaries have arbitrary order, and may change over time. Is this true of lists? I can't find the answer from a simple Google search. Thank you! items append to a list retain their order. cheers James -- -- James Mills -- -- Problems are solved by method ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List Changing Order
Corey Richardson wrote: Tutors, I recall that the keys of dictionaries have arbitrary order, and may change over time. Is this true of lists? I can't find the answer from a simple Google search. Thank you! Only if you re-arrange it yourself. list.sort(), list.reverse() and random.shuffle(list) explicitly change the list's order. You can also manually move items around, e.g.: list[3], list[5] = list[5], list[3] # swap items 3 and 5 but otherwise lists keep their order. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor