Some python syntax that I'm not getting
Hello. Been studying Python for about a week now. I did a quick read of the tutorial in the manual and I'm reading Programming Python by Mark Lutz. I'm still getting used to the Python syntax, but I'm able to pretty much follow what is being said. But tonight Lutz was talking about implementing a database on a website and threw out this piece in his code: key That last bit is the bit that throws me: %(keys)s He explains this so: "The only feat of semimagic it relies on is using a record's attribute dictionary (__dict__) as the source of values when applying string formatting to the HTML reply template string in the last line of the script. Recall that a %(key)code replacement target fetches a value by key from a dictionary: >>> D = {'say': 5, 'get': 'shrubbery'} >>> D['say'] 5 >>> S = '%(say)s => %(get)s' % D >>> S '5 => shrubbery' " Hm... I understand how D['say'] gets you 5, But I still don't understand the line after the 5. How is the character 's' some special code? And I don't get what is going on with the % character. I'm used to it's use in c-style formatting, but this just seems so bizarre. I can tell that the key is being replaced by it's value in the string, but I don't know how that is being done. TIA -- http://mail.python.org/mailman/listinfo/python-list
python from any command line?
Hi folks. I'm learning Python from the Mark Lutz Book, Programming Python 3rd edition. He seems to be able to invoke the Python interpreter from any command line prompt. C:\temp>python C:\PP3rdEd\examples>python C:\PP3rdEd\Examples\PP3E\System>cd Whereas I am only able to invoke it when the command line is pointing to the directory where the executable resides. Currently: C:\Python25 Is there a way to set this so I can also invoke it from any command line prompt? Thankyou very much. -- http://mail.python.org/mailman/listinfo/python-list
Re: python from any command line?
On Dec 9, 8:54 am, Adonis Vargas <[EMAIL PROTECTED]> wrote: > waltbrad wrote: > > Hi folks. I'm learning Python from the Mark Lutz Book, Programming > > Python 3rd edition. > > > He seems to be able to invoke the Python interpreter from any command > > line prompt. > > > C:\temp>python > > > C:\PP3rdEd\examples>python > > > C:\PP3rdEd\Examples\PP3E\System>cd > > > Whereas I am only able to invoke it when the command line is pointing > > to the directory where the executable resides. Currently: > > > C:\Python25 > > > Is there a way to set this so I can also invoke it from any command > > line prompt? > > > Thankyou very much. > > The Python executable must be found in your system path, that is why you > are unable to just simply type python at anytime at a command prompt. > Simple fix: > > click Start > Control Panel > System > Advanced > Environment Variables > (bottom right) > > Then you will have two list boxes one for user environment variables > another for system variables. I prefer placing the path in the System > environment variables section that way it is available for all accounts > on the system (provided you have more than 1 user on it). > > In the System list locate the Path variables, select edit variable and > append ;C:\Python25 to it. And thats it reopen a new command prompt the > type python and it should just fire up. > > Hope this helps. > > Adonis Vargas Got it, Adonis. Thanks much. -- http://mail.python.org/mailman/listinfo/python-list
shelve.open call gives error
Working through the Mark Lutz book Programming Python 3rd Edition. A couple of modules in the "Preview" chapter give me errors. Both on a shelve.open call: Pretty simple code, (2nd example): =code begin= import shelve from people import Person, Manager bob = Person('Bob Smith', 42, 3, 'sweng') sue = Person('Sue Jones', 45, 4, 'music') tom = Manager('Tom Doe', 50, 5) db = shelve.open('class-shelve') db['bob'] = bob db['sue'] = sue db['tom'] = tom db.close() code end Error message Traceback (most recent call last): File "make_db_classes.py", line 9, in db = shelve.open('class-shelve') File "C:\PYTHON25\lib\shelve.py", line 225, in open return DbfilenameShelf(filename, flag, protocol, writeback) File "C:\PYTHON25\lib\shelve.py", line 209, in __init__ Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback) File "C:\PYTHON25\lib\anydbm.py", line 83, in open return mod.open(file, flag, mode) File "C:\PYTHON25\lib\dbhash.py", line 16, in open return bsddb.hashopen(file, flag, mode) File "C:\PYTHON25\lib\bsddb\__init__.py", line 306, in hashopen d.open(file, db.DB_HASH, flags, mode) bsddb.db.DBError: (5, 'Input/output error') ++End Error message I've looked on the errata pages of his website and used Google to find someone else who ran into this problem. Found someone else who had a similar error but no explanation was given. No other reader of the book seems to have had the problem. I'm using 2.5.1 I thought maybe the file had to exist before it could be opened, so I created one with that name, but no dice. A different but similar set of errors. I also thought maybe there had to be a second argument like 'a', 'r' or 'w'. That doesn't work either. This is the first chapter of the book. It's an overview of the language features. So, this is pretty perplexing. Any help would be appreciated. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: shelve.open call gives error
On Feb 8, 5:29 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Fri, 08 Feb 2008 06:36:53 -0200, waltbrad <[EMAIL PROTECTED]> > escribió: > > > > > Working through the Mark Lutz book Programming Python 3rd Edition. > > > A couple of modules in the "Preview" chapter give me errors. Both on a > > shelve.open call: > > > Pretty simple code, (2nd example): > > =code begin= > > import shelve > > from people import Person, Manager > > > bob = Person('Bob Smith', 42, 3, 'sweng') > > sue = Person('Sue Jones', 45, 4, 'music') > > tom = Manager('Tom Doe', 50, 5) > > > db = shelve.open('class-shelve') > > db['bob'] = bob > > db['sue'] = sue > > db['tom'] = tom > > db.close() > > code end > > shelve uses the anydbm module; anydbm tries to select the best database > module available, but apparently fails in your system. If you are just > learning Python, I don't think it's worth trying to fix it; instead, let's > force anydbm to use the fallback module dumbdbm (implemented in pure > python, slow, but bullet-proof, or at least arrow-proof :) ) > > Add these two lines at the start of your script: > > import dumbdbm, anydbm > anydbm._defaultmod = dumbdbm > > Remove the class-shelve.* files, if any, before running it. > > -- > Gabriel Genellina Thanks to all for the help, but the only advice that worked was Gabriel's. I actually tried to look into the issue of fixing it, but I'm just too much of a novice. I put the two lines in a seperate module that I import to save typing. But as I gain experience I'd like to return to this issue and try to fix it. Can you give me advice on how to go about that? I'm working on a win98 system. I have python on a linux system, (Kubuntu) and winxp but it's more convenient right now for me to use the 98 laptop. -- http://mail.python.org/mailman/listinfo/python-list
I cannot evaluate this statement...
The script comes from Mark Lutz's Programming Python. It is the second line of a script that will launch a python program on any platform. import os, sys pyfile = (sys.platform[:3] == 'win' and 'python.exe') or 'python' Okay, run on a win32 machine, pyfile evaluates to python.exe That makes sense. Because the first condition is true and 'python.exe' is true. So the next comparison is 'python.exe' or 'python' Well, python.exe is true. So that value is returned to pyfile. Now. Run this on linux. The first condition evaluates sys.platform[:3] == 'win' as false. So, the next comparison should be 'False' or 'python' -- This is because 'and' returns the first false value. But, again, on linux pyfile evaluates to python.exe Where am I going wrong. And when will this statment make pyfile evaluate to 'python' ? -- http://mail.python.org/mailman/listinfo/python-list
questions about named pipe objects...
I'm proceeding slowly though the Lutz book "Programming Python". I'm in the section on named pipes. The script he uses has two functions: one for the child the other for the parent. You start the parent then the child: python pipefifo.py #starts the parent file /tmp/pipefifo # shows that the file is a named pipe python pipefifo.py -child # starts a child process and writes to the pipe. This is done between two command windows - the first for the parent and the second for the child. Now, the child's write loop is infinite. So, I used Ctrl-C and stopped the process. But the parent's read loop is also infinite and without and exception, so it keeps reading from the pipe after the child is shutdown even though the lines are empty. So, I had to shut that down also. I then wanted to start the child process first and see what happened when I ran the parent. Well that works but the reads come out in random order. This got me wondering about the pipe file itself. So I tried to open it with leafpad and found that I couldn't. I guess these files can only be opened by processes? Okay. But exactly when does the system brand this file as a named pipe and how? -- http://mail.python.org/mailman/listinfo/python-list
Re: questions about named pipe objects...
On Mar 17, 1:50 pm, waltbrad <[EMAIL PROTECTED]> wrote: > > I then wanted to start the child process first and see what happened > when I ran the parent. Well that works but the reads come out in > random order. Well, I take that back. I accidentally had two 'parent' processes open. So, the reads were sequential but split between two windows. -- http://mail.python.org/mailman/listinfo/python-list
Re: questions about named pipe objects...
On Mar 17, 1:59 pm, Jeff Schwab <[EMAIL PROTECTED]> wrote: > > A Unix fifo is only nominally a file. It's really just a convenient way > of referring to an in-memory object. > mkfifo f > some_prog > f & > cat f > > Is semantically equivalent to: > > some_prog | cat > > If you want to peruse the data in your editor, use a regular file, not a > fifo. Have the writer (producer, "child" in your example) open it in > append mode. Okay. I get it. That's interesting. Wish Lutz explained that. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Running a python program as main...
Stumbling through Mark Lutz's "Programming Python 3rd", he gives an example of a program that will automatically configure environment settings and launch other programs. Then he gives an example of running this program. On his command line he types: C:\...\PP3E>Launcher.py and this begins the program. Doesn't work for me. I have to type: C:\...\PP3E>python Launcher.py Is this a typo on his part or has he configured his settings in such a way that the command line will automatically associate the extension with the program? (If so, he didn't mention this in his book). -- http://mail.python.org/mailman/listinfo/python-list
uninstall before upgrade?
I want to upgrade from 2.5 to 2.6. Do I need to uninstall 2.5 before I do that? If so, what's the best way to uninstall it? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
PYTHONPATH in Windows
PYTHONPATH is a concept I've never been able to get straight. I can't see the difference between this and just setting paths in the Windows environment variables. So, for the longest time I just never worried about it. Now, I'm going through James Bennett's "Practical Django Projects" and the issue raises it's head again. We need to set up PYTHONPATH so that python can find the directories for import statements. Can this just be done through the environment variables? Bennett says: "On Windows, the setup is a bit more involved, largely because Windows, unlike UNIX-based systems, isn’t as friendly to command-line– based programs. In the Control Panel’s System area, under the Advanced tab, you can set environment variables. The PYTHONPATH variable should already be set up with the initial value Python provided, and you can add new directories to it (directories in the list should be separated with semicolons)." But no PYTHONPATH variable shows up in my environment settings. This website: http://www.imladris.com/Scripts/PythonForWindows.html says you need to alter PYTHONPATH in the windows directory: " Now that you've taught Windows to find the python executable in the python install directory, you'll need to tell it how to find your python scripts saved in folders other than that one; otherwise, your python import statements will fail because they won't know where to look for the custom modules you wish to import. Possible module locations are specified by the PYTHONPATH environment variable, which is stored in the Windows registry. "To augment PYTHONPATH, run regedit and navigate to KEY_LOCAL_MACHINE \SOFTWARE\Python\PythonCore and then select the folder for the python version you wish to use. Inside this is a folder labelled PythonPath, with one entry that specifies the paths where the default install stores modules. Right-click on PythonPath and choose to create a new key. You may want to name the key after the project whose module locations it will specify; this way, you can easily compartmentalize and track your path modifications. "Your new key will have one string value entry, named (Default). Right- click on it and modify its value data; this should be text in the same format as the Path environment variable discussed above--absolute directory paths, separated by semicolons. If one project will use modules from several directories, add them all to this list. (Don't bother attempting to add more string value entries to your new key, or to the original PythonPath key, since they will be ignored.) Once these new registry entries are in place, your scripts' import statements should work fine." I don't know when this was written, they refer to WIN 2000 but not XP. Is this correct? Do I go into the registry and create a key and type the path into it's string value? I get pretty cautious about playing around with the registry. -- http://mail.python.org/mailman/listinfo/python-list
Re: PYTHONPATH in Windows
On Nov 29, 1:39 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > waltbrad schrieb: > > > > > PYTHONPATH is a concept I've never been able to get straight. I can't > > see the difference between this and just setting paths in the Windows > > environment variables. So, for the longest time I just never worried > > about it. > > > Now, I'm going through James Bennett's "Practical Django Projects" and > > the issue raises it's head again. We need to set up PYTHONPATH so > > that python can find the directories for import statements. Can this > > just be done through the environment variables? Bennett says: > > > "On Windows, the setup is a bit more involved, largely because > > Windows, unlike UNIX-based systems, isn’t as friendly to command-line– > > based programs. In the Control Panel’s System area, under the Advanced > > tab, you can set environment variables. The PYTHONPATH variable should > > already be set up with the initial value Python provided, and you can > > add new directories to it (directories in the list should be separated > > with semicolons)." > > > But no PYTHONPATH variable shows up in my environment settings. This > > website: > > >http://www.imladris.com/Scripts/PythonForWindows.html > > > says you need to alter PYTHONPATH in the windows directory: > > > " Now that you've taught Windows to find the python executable in the > > python install directory, you'll need to tell it how to find your > > python scripts saved in folders other than that one; otherwise, your > > python import statements will fail because they won't know where to > > look for the custom modules you wish to import. Possible module > > locations are specified by the PYTHONPATH environment variable, which > > is stored in the Windows registry. > > > "To augment PYTHONPATH, run regedit and navigate to KEY_LOCAL_MACHINE > > \SOFTWARE\Python\PythonCore and then select the folder for the python > > version you wish to use. Inside this is a folder labelled PythonPath, > > with one entry that specifies the paths where the default install > > stores modules. Right-click on PythonPath and choose to create a new > > key. You may want to name the key after the project whose module > > locations it will specify; this way, you can easily compartmentalize > > and track your path modifications. > > > "Your new key will have one string value entry, named (Default). Right- > > click on it and modify its value data; this should be text in the same > > format as the Path environment variable discussed above--absolute > > directory paths, separated by semicolons. If one project will use > > modules from several directories, add them all to this list. (Don't > > bother attempting to add more string value entries to your new key, or > > to the original PythonPath key, since they will be ignored.) Once > > these new registry entries are in place, your scripts' import > > statements should work fine." > > > I don't know when this was written, they refer to WIN 2000 but not > > XP. Is this correct? Do I go into the registry and create a key and > > type the path into it's string value? > > > I get pretty cautious about playing around with the registry. > > Both result in the same - an environment-variable being added. > > That no shows up when you do that for the first time isn't too much > surprising - it doesn't exist, but you can create it. > > However, I would strongly recommend *not* relying on a PYTHONPATH-variable. > > Instead, go & install virtualenv. Then create a virtualenv for you > django-project, and install django into it (however that is accomplished). > > Then, go & install your codebase for your app into it as well. > Preferably as egg-link. Should be easy enough with a minimal setup.py > like this: > > from setuptools import setup, find_packages > setup( > name = "HelloWorld", > version = "0.1", > packages = find_packages(), > ) > > The advantage of this approach is that you can have several versions of > your software installed in distinct VEs, for example for > debugging/patching a released version, without having to meddle with the > PYTHONPATH everytime. > > The important thing to remember ist just to always either activate the > proper VE, or use the full path to the python executable inside it. > > Diez This just sounds like you're making everything even more complicated. Am I going to have to uninstall Python and Django and