MS Word mail merge automation
I'm trying to do invoke the mail merge functionality of MS Word from a
Python script. The situation is that I have a template Word document,
and a record that I've generated in Python, and I want to output a new
Word .doc file with the template filled in with the record I've
generated.
(To refresh your memory, in Word a mailmerge is achieved by a) under
Tools -> Letters and Mailings, check off Show Mail Merge Toolbar; b)
open a document with template-style variables in the form of
<>; c) on Toolbar select Open Data Source and select
appropriate Access or Excel or CSV file (with column headers
corresponding to the FIELD_NAME's in your template variables); and then
d) on Toolbar select Merge To New Document to create a new document
with the template variables replaced with the value from the
corresponding column in the data source - here, you can make one
document per row of data source, or just one document for a given row.
Don't forget to save the new document.)
Using various online sources*, I have been able to piece together all
but (what I hope is) the final missing piece, viz., the name of the
method that corresponds to "Merge to New Document" command from within
the Word interface.
Here is the basic code, if anyone can provide the missing piece I (and
others, I suspect) would appreciate it:
import os, win32com.client
doc_template_name = os.path.abspath('template.doc')
data_source_name = os.path.abspath('record23.csv')
doc_final_name = os.path.abspath('record23.doc')
app = win32com.client.Dispatch("Word.Application")
doc = app.Documents.Open(doc_template_name)
#attach data source to template
doc.MailMerge.OpenDataSource(data_source_name)
#merge to new document - THIS RAISES ATTRIBUTE ERROR, HOW TO FIX?
new_doc = doc.MailMerge.MergeToNewDocument()
#save out result
new_doc.SaveAs(doc_final_name)
#cleanup
doc.Close()
new_doc.Close()
app.Quit()
*I found some information here:
http://64.233.161.104/search?q=cache:V-xpWKigqVQJ:coderforums.com/archive/topic/1514-1.html+win32com+merge+to+new+document&hl=en
and here:
http://www.brunningonline.net/simon/blog/archives/001299.html
as well as here:
http://www.win32com.de/index.php?option=com_content&task=category§ionid=7&id=86&Itemid=192
I also have the Hammond and Robinson book on Python on Win32 but it
hasn't helped me to discover the method name.
--
http://mail.python.org/mailman/listinfo/python-list
Re: MS Word mail merge automation
I was finally able to get things working right, so I thought I'd stick
an example here for posterity.
"""An example of a MS Word mail merge using the COM interface.
In order for this script to work you must first run the COM Makepy
utility and select
"Microsoft Word 10.0 Object Library (8.2)" or whatever your version of
Word is.
The template must also be set up with merge fields corresponding to the
data source.
"""
import os, win32com.client
doc_template_name = os.path.abspath('template.doc')
data_source_name = os.path.abspath('record23.csv')
doc_final_name = os.path.abspath('record23.doc')
app = win32com.client.Dispatch("Word.Application")
doc_template = app.Documents.Open(doc_template_name)
mm = doc_template.MailMerge
#attach data source to template
mm.OpenDataSource(data_source_name)
#merge just one record - this step may be redundant
mm.DataSource.FirstRecord = 1
mm.DataSource.LastRecord = 1
#send the merge result to a new document
mm.Destination = win32com.client.constants.wdSendToNewDocument
#merge
mm.Execute()
#apparently app.Documents is like a stack
doc_final = app.Documents[0]
#save our new document
doc_final.SaveAs(doc_final_name)
#cleanup
doc_final.Close()
doc_template.Close()
app.Quit()
--
http://mail.python.org/mailman/listinfo/python-list
Re: Lambda evaluation
Here's another one:
>>> d = {}
>>> for x in [1,2,3]:
... d[x] = (lambda z: lambda y: y * z) (x)
...
>>> d[1](3)
3
>>> d[2](3)
6
--
http://mail.python.org/mailman/listinfo/python-list
Bug in COM Makepy utility? (ActivePython 2.4)
When I use the COM Makepy utility on one computer with WindowsXP,
ActivePython 2.3 and I select the library Microsoft Word 10.0 Object
Library (8.2), things work fine.
When I have WindowsXP, ActivePython 2.4 (build 247) and Microsoft Word
11.0 Object Library (8.3), then I get the following SyntaxError, and on
two different computers I tested this. More on the error below, but has
anyone else had this problem? Will there soon be a newer build of
ActivePython? I've never used the standard Python distribution with
manually installed win32all package, just because its so easy to deploy
ActivePython. Should I consider switching, now that I'm on the topic?
>>> Generating to
>>> C:\Python24\lib\site-packages\win32com\gen_py\00020905---C000-0046x0x8x3\__init__.py
Building definitions from type library...
Generating...
Importing module
Failed to execute command:
from win32com.client import makepy;makepy.main()
Traceback (most recent call last):
File
"C:\Python24\Lib\site-packages\pythonwin\pywin\framework\toolmenu.py",
line 103, in HandleToolCommand
exec "%s\n" % pyCmd
File "", line 1, in ?
File "C:\Python24\lib\site-packages\win32com\client\makepy.py", line
363, in main
GenerateFromTypeLibSpec(arg, f, verboseLevel = verboseLevel,
bForDemand = bForDemand, bBuildHidden = hiddenSpec)
File "C:\Python24\lib\site-packages\win32com\client\makepy.py", line
274, in GenerateFromTypeLibSpec
gencache.AddModuleToCache(info.clsid, info.lcid, info.major,
info.minor)
File "C:\Python24\Lib\site-packages\win32com\client\gencache.py",
line 555, in AddModuleToCache
mod = _GetModule(fname)
File "C:\Python24\Lib\site-packages\win32com\client\gencache.py",
line 634, in _GetModule
mod = __import__(mod_name)
File
"C:\Python24\lib\site-packages\win32com\gen_py\00020905---C000-0046x0x8x3\__init__.py",
line 2831
'{00020960---C000-0046}' : 'Pane',
'{00020961---C000-0046}' : 'Windows',
^
SyntaxError: invalid syntax
I don't entirely understand the error (I'm rather ignorant of the whole
process Makepy is doing, come to think of it...). First of all, when I
load the file with the alleged syntax error into Scite, the line where
the caret is pointing to is actually 2838, not line 2831. Further, I
cannot detect any syntax errors based on visual inspection. In fact,
when I copy/paste lines 2830-2840 into a new script and put "d = {"
before the lines and "}" after the lines, it is syntactically valid
python that executes without complaint. I don't know how "dynamically"
this file is being generated, but somethin' ain't right.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Bug in COM Makepy utility? (ActivePython 2.4)
After exploring the bug database I discovered that this bug has been reported since March, and appears to derive from a bug in Python itself. http://bugs.activestate.com/show_bug.cgi?id=38052 It apparently only happens when the code generated by Makepy is really big (which it is for Word or Excel). So my options are to downgrade from Python 2.4.1 to 2.3.5 or to use Late Binding instead of Early Binding (as described in Hammond and Robinson, Ch. 12). The easiest path will be to use Early Binding, because as far as I can tell the only significant changes to code will be that I have to use integer literals instead of named constants. In my case this means replacing 'win32com.client.constants.wdSendToNewDocument' with '0' (zero). -- http://mail.python.org/mailman/listinfo/python-list
Changing console text color
Hello, I've been trying to change the text color on a windows console program I've been working on with no luck. I should mention that I'm a novice so please dummy up your replies. Thanks-in-Advance Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing console text color
Thank you. I appreciate the help. Steve Larry Bates wrote: > You can use this module to control console windows background and > text colors. > > http://www.effbot.org/zone/console-index.htm > > -Larry Bates > > Steve M wrote: >> Hello, >> >> I've been trying to change the text color on a windows >> console >> program I've been working on with no luck. I should mention that >> I'm a novice so please dummy up your replies. >> >> Thanks-in-Advance >> Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: Advice on this code
output.write(wrkmisc(ifile,2582)) output.write(wrkviol(ifile,2774,16)) output.write(wrkaccid(ifile,3270,16)) output.write(wrkmisc2(ifile,3638)) output.write(wrkcov(ifile,3666,6)) output.write(wrklp(ifile,4314,7)) output.write(wrkai(ifile,4909,6)) output.write(wrkmisc3(ifile,5707)) Ummm... yuck? First of all, this program is very hard to understand to somebody who hasn't written it. ALthough I've never heard of Quote Vista so maybe that's the problem. Anyway, if you want to integrate the first program with the second GUI program, you should convert the first program into a set of functions that can be called to do everything the first program does. So basically, take the first program and put everything that's not already in a function into one. That is, all the code that is at zero indentation, starting with creating the oConn connection, should be moved into functions. You can also write a big-mama function that calls those functions in such a way that it would have the exact same overall effect as the original script before you moved the code into functions. You can call this big-mama function 'main'. Now you can integrate it with the GUI program by importing the first module from the GUI program and binding the functions to the appropriate buttons. For example you could bind main to the Start button you described. Also, in the first file, you can put the following lines at the bottom, and it will allow you to run the original script directly to achieve the same result, while still being able to import it into a second program and re-use just the functions. if __name__ == '__main__': main() -- http://mail.python.org/mailman/listinfo/python-list
Re: Well written open source Python apps
Here is an article discussing the coding style of BitTorrent. http://www.onlamp.com/pub/a/python/2003/7/17/pythonnews.html Maybe that code is worth looking at. -- http://mail.python.org/mailman/listinfo/python-list
Re: Queue question
According to my "Python in a Nutshell": q.get(block=True) is the signature, so, as you use it above, the call will hang until something is on the queue. If block is false and the queue is empty, q.get() will raise the exception Empty. q.get_nowait is apparently synonymous with q.get(block=False) q.not_empty, if it existed, I expect would be true just in case there was at least one item in the queue. But according to my book there is q.empty and q.full, which is true when the queue has the maximum allowed number of items (a value specified when the queue is created). Also, I don't think you can rely on q.empty in the way you may expect. For example, another thread can empty the queue between the time you test whether q.empty is false and the time you call q.get. -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem Pythoncard tutorial
I'd say that the tutorial text (by Dan Shafer) and the file starter1.py are not in sync. The word 'self' doesn't appear in the source file, it isn't a keyword (just a convention) or a literal, and it isn't imported by wildcard or some other trickery. So just by inspection you can tell that the name isn't defined at the point you are trying to use it, and so using the name anywhere other than as the target of an assignment (is that called an 'lvalue' ?) will cause a NameError. I think the problem begins around where the tutorial says: - Here's the important part to focus on: def on_menuFileExit_select(self, event): pass - Based on the indentation and other clues, that was probably supposed to be a method of the Minimal class, but it doesn't exist in the starter1.py file. Just stick it in (as a method of that class) and things should work. FYI here is the version of starter1.py from my installed PythonCard 0.8.1: #!/usr/bin/python """ __version__ = "$Revision: 1.6 $" __date__ = "$Date: 2004/05/05 16:53:23 $" """ from PythonCard import model class Minimal(model.Background): pass if __name__ == '__main__': app = model.Application(Minimal) app.MainLoop() -- http://mail.python.org/mailman/listinfo/python-list
Re: Python for .NET and IronPython
I was under the impression that IronPython is like CPython and Jython, namely an implementation of the Python language. So in that sense it is exactly like normal Python, although I don't know how convenient it is to deploy. I was also under the impression that Python for .NET is like an API wrapper thingy, analagous to the win32com package that wraps that interface and allows you to call functions and stuff provided by the .NET API. It is not at all an implementation of Python. I am confident that we will learn shortly whether I'm wrong. -- http://mail.python.org/mailman/listinfo/python-list
Re: convert COM obj to integer
I don't know exactly what a COM object is, but those aren't them. The
win32com package takes care of converting everything to Python types.
The excel call returns a tuple of tuples. That is, the outer tuple is
the sequence of rows, and each such row is itself a tuple with one
member per column requested. Since you only request one column, it is a
one-item-long tuple, also called a 1-tuple. That is demonstrated by the
result of print'ing the list.
By the way, you shouldn't use 'list' as a name because it is also the
name of a built-in function. And it isn't a list anyway, it's a tuple.
Now, each number is in fact already a primitive Python object of type
float. (The asterisk is a unicode string.) So you want to convert the
floats into integers, and it looks like you want to round rather than
truncate.
table = xlApp.ActiveWorkbook.ActiveSheet.Range("Q13:Q36")
converted_values = []
for row in table:
value = row[0] #get the first (and only) item in the tuple
try:
value = round(value)
except TypeError: #value is not a float
value = None
else:
value = int(value) #turn the float into an int
converted_values.append(value)
print converted_values
By the way, if you wonder how I knew to catch the TypeError, I just
fired up the interactive Python interpreter, and typed this: round(u'*')
--
http://mail.python.org/mailman/listinfo/python-list
Re: So, Which Version is Suitable for Beginners
There is a new gratis VMWare player at http://www.vmware.com/download/player/ You can download an image http://www.vmware.com/vmtn/vm/browserapp.html that they call a Browser Appliance, but if I remember correctly it is Ubuntu. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using Which Version of Linux
Max wrote: > (Mark Shuttleworth, ... > really loves Python - he gave me quite a lot of money for using it). Please elaborate. -- http://mail.python.org/mailman/listinfo/python-list
Re: A Tcl/Tk programmer learns Python--any advice?
Even though its for any (not just Tcl) experienced programmer, and even though you've got all the appropriate resources, I'll mention that you should get a lot out of the book Dive Into Python, at http://www.diveintopython.org/ -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie - How do I import automatically?
The file C:\Python24\Lib\sitecustomize.py (which I think doesn't exist by default) executes every time Python starts. (This means not just your IDLE session but every time you run any Python script.) One use for this file is to invoke sys.setdefaultencoding because that name gets deleted during initialization and so is unavailable by the time your main script starts executing. You can also put in any other code, such as "from btools import *". You can also put your btools.py file in C:\Python24\Lib\site-packages folder and it will be available from any program (as opposed to requiring it in the current directory). -- http://mail.python.org/mailman/listinfo/python-list
Re: the PHP ternary operator equivalent on Python
Another way to simulate the ternary operator is this: a = (quantity > 90 and "It is very huge") or "The value is correct" You have to be careful of semantics of 'and' and 'or'. But in this case I wonder why you don't just test whether quantity is greater than 90 and assign the corresponding value to a, e.g., : if quantity > 90: a = "It is huge" else: a = "Time for tea." Clarity is a virtue, and simulating ternary operator doesn't always serve that end. -- http://mail.python.org/mailman/listinfo/python-list
Re: Dive into Python PDF
You should buy the book in hardcopy, that one looks fine. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with mass remove in text file
First, in your intro you say you want to remove all strings of the form "f=n;" where n can be 0-14. So you want to remove "f=0;" and "f=1;" and ... Later, you appear to be trying to remove "f=;" which may be what you want but it doesn't match your described intentions. Second, the formatting (whitespace) is all messed up on your post (at least in googroups), so its not entirely clear what the program is supposed to do. Anyway, why do you say the program doesn't work? Does it generate an error when you try to run it? Or is it doing what you say and not what you mean? I'm guessing that it will give (provided it doesn't end on a SyntaxError) at least the following error: TypeError: replace() takes at least 2 arguments (1 given) This is because the line: data = data.replace(x) doesn't use enough arguments for the replace() method. You need to say what to replace it with. Since you want to remove whatever string it is, you can use the empty string as the replacement value: data = data.replace(x, '') #That's two single quotes Also, unless you want to make a regular expression, you might have to do a replace() call on data 15 times, sort of like this: for i in range(15): text_to_remove = "f=%s;" % i data = data.replace(text_to_remove, '') -- http://mail.python.org/mailman/listinfo/python-list
Re: PY2EXE => Is there a way to go backwards? EXE2PY
>I have the executable of a script that I wrote, that has been erased. >Is there any way to retrieve the uncompiled python script from the >executable that was created with py2exe? You're gonna need a case of 20-weight ball bearings and several quarts of antifreeze. Preferably Quakerstate. No, better make it Penzoil. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to run python script in background after i logout
Harlin Seritt wrote: > I have a remote linux server where I can only access it via ssh. I have > a script that I need to have run all the time. I run like so: > > python script.py & > > It runs fine. When I log off ssh I notice that the script died when I > logged off. How do I make sure it stays running? You might also check out the extremely cool screen program. It lets you have multiple virtual terminal sessions from one connection, and detach them all and logout, then login later and re-attach them. I typically use 'screen -D -R' which will re-attach if there is a set of sessions to re-attach, or otherwise start a new one. Then you can have a dedicated window for your script.py (you don't even need to run it in the background of the shell with '&') and you can just detach the screen before you logout. Later you can log back in, reattach, check for any output (you can use print statements for debug info, etc.). Since it can be tricky getting started, I'll tell you briefly, there is a command key, which you use to send commands to the screen program. Anything other than command key will be passed through to whatever program is running, e.g. the bash shell or whatever. The default command key is ctrl-a. So you would do 'ctrl-a c' to create a new virtual window, 'ctrl-a 1', 'ctrl-a 2', etc. to switch between virtual windows, and 'ctrl-a d' to detach your session. This brings you back to your original ssh login shell. Incidentally, if you do a 'ps aux' here you'll see one of the programs is 'SCREEN' owned by root; this is the process that is keeping alive all your other processes and that persists when you logout and allows you to reattach later. A couple of problems I've had are first, that ctrl-a is also the emacs command to go to the beginning of the line, which I use all the time. So I've sometimes rebound the screen command key (I've tried ctrl-[ since I dont' ever seem to use that for anything else, but I don't think it works entirely perfectly, especially in combination with the next problem.). Another is that when I use putty.exe from Windows for my ssh client, I can't get scroll-back buffers to work correctly with screen. (Screen is really powerful with its own scrollback buffers and screendumps and stuff but I don't have time to get into all that or even learn it sometimes. I wish I were more a master of it since its such a great program.) Another alternative is to daemonize your program, but I don't know how to do that off the top of my head. -- http://mail.python.org/mailman/listinfo/python-list
Re: Create a variable "on the fly"
PythonWin 2.3.5 (#62, Feb 9 2005, 16:17:08) [MSC v.1200 32 bit (Intel)] on win32. Portions Copyright 1994-2004 Mark Hammond ([EMAIL PROTECTED]) - see 'Help/About PythonWin' for further copyright information. >>> locals()['OSCAR'] = 'the grouch' >>> OSCAR 'the grouch' >>> -- http://mail.python.org/mailman/listinfo/python-list
Re: trying to parse non valid html documents with HTMLParser
>You were right, the HTMLParser of htmllib is more permissive. He just ignores the bad tags ! The HTMLParser on my distribution is a she. But then again, I am using ActivePython on Windows... -- http://mail.python.org/mailman/listinfo/python-list
Printing to printer
Hello, I'm having problems sending information from a python script to a printer. I was wondering if someone might send me in the right direction. I wasn't able to find much by Google TIA Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing to printer
Kristian Zoerhoff wrote: > On 8/11/05, Steve M <[EMAIL PROTECTED]> wrote: >> Hello, >> >>I'm having problems sending information from a python >> script to a printer. I was wondering if someone might send me >> in the right direction. I wasn't able to find much by Google > > Which platform? Directions will vary wildly. > Ooops, sorry forgot to mention I'm using Suse 9.0 and Python 2.3x TIA Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: Psyco & Linux
> First, I tried the usual "python setup.py install" but that did not work. How exactly did it fail? Perhaps you can paste the error output from this command. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python's Exception, and Capitalization
You might find the Python Style Guide to be helpful: http://www.python.org/doc/essays/styleguide.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing to printer
Larry Bates wrote: > I adapted some code from David Boddie into a Python class to write > directly to Linux print queues. I have used it in one project and > it worked just fine. I've attached a copy for your use. You are > free to use it as you wish, with no guarantees or warranties. > > Hope it helps. > > Larry Bates > > Steve M wrote: >> Hello, >> >> I'm having problems sending information from a python >> script to a printer. I was wondering if someone might send me >> in the right direction. I wasn't able to find much by Google >> >> TIA >> Steve Thank you, I'll give it a try. Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing to printer
Kristian Zoerhoff wrote: > On 8/11/05, Steve M <[EMAIL PROTECTED]> wrote: >> Kristian Zoerhoff wrote: >> >> > On 8/11/05, Steve M <[EMAIL PROTECTED]> wrote: >> >> Hello, >> >> >> >>I'm having problems sending information from a python >> >> script to a printer. I was wondering if someone might send me >> >> in the right direction. I wasn't able to find much by Google >> > >> > Which platform? Directions will vary wildly. >> > >> Ooops, sorry forgot to mention I'm using Suse 9.0 and Python 2.3x > > Assuming a local printer, you could just open the appropriate > device file (e.g. /dev/lp0) in write mode and write the text to it. > Another option would be to create a temp file, and then feed that > to the lpr or enscript commands via the subprocess module. > Thank you for your help. I try and work it out now that I have a direction. Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: Psyco & Linux
> c/codegen.h:19:3: #error "sorry -- I guess it won't work like that on 64-bits > machines" The first error output by gcc suggests the 64-bit OS might be the problem. But I don't actually know what that error means. -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterparse and ElementTree confusion
"when i attempted [to load 150MB xml file] my PC goes to lala land, theres much HDD grinding followed by "windows runnign low on virtual memory" popup after 10-15mins. Then just more grinding...for an hour before i gave up" I have had great success using SAX to parse large XML files. If you use care your memory use will peak at a low number no matter how much XML you chew through. -- http://mail.python.org/mailman/listinfo/python-list
Re: Binary Trees in Python
[diegueus9] Diego Andrés Sanabria <[EMAIL PROTECTED]> wrote: > Hello!!! > > I want know if python have binary trees and more? You might be interested that ZODB comes with some B-tree implementations. They can be used alone or you can persist them in the ZODB quite easily. http://www.zope.org/Wikis/ZODB/FrontPage -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Light Revisted?
I agree with you in part and disagree in part. I don't see the point to making the distribution any smaller. 10MB for the installer from python.org, 16MB for ActiveState .exe installer. How is 5MB "lightweight" while 10MB isn't? The Windows XP version of Java at java.com is 16+ MB, and the .NET framework is, well, I don't know how big, but I doubt it's much less than 10MB. Also I think it's a terrible idea to distribute without pieces of the standard library if the distribution is meant to be developed against for arbitrary applications. (If you just want a slimmed down Python for one specific application, use py2exe - I think it only includes whatever gets imported for your program to run.) And excluding zip while including Twisted? I don't get it. Besides, how do you know Twisted doesn't import XML-RPC? I could see an enlarged Python that includes Twisted along with the standard library, and in fact ActiveState's includes win32 stuff. There's another such mega-distribution around, the name of which escapes me at the moment. But removing zip from the standard library so you can save 20kb seems foolish. (Again, unless it is for one specific application, in which case py2exe should do the trick, although I could be wrong about that since I've never used it.) Now, what I do agree with is a Python that can be run from a folder without having to be installed on the system. That could have lots of benefits, if the details with pythonpath and whatever could be sorted out. For example I haven't upgraded to 2.4 yet because I have 20 different packages (e.g. SOAPpy, ZODB, whatever) installed on WinXP, many of which required me selecting my 2.3 installation when I installed them. I have no idea what will happen to all those if I run the ActiveState installer for 2.4. I seem even to remember reading that I have to un-install 2.3 before installing 2.4. I don't want to re-install those 20 packages. Anyway I haven't had time to research it and it isn't pressing, even though I'd like to start trying decorators and generator expressions. -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with Pythoncard
There are some Python packages that I prefer to get from the developer instead of from my Linux distribution (Debian). Usually it is because I intend to upgrade the package before, or more often than, my distribution releases a new version. (A likely scenario if you use Debian - zing!) So I download the source.tar.gz from the developer. I extract it into ~/src, change to the directory and run "python setup.py build". (Here you might recursively detour to install a dependency.) Then I change to the build/lib directory and in there is all of the modules. In build/scripts will be any included scripts. I copy the contents of build/lib to ~/python/lib and the scripts to ~/python/scripts. (Alternatively I could make symbolic links.) My PYTHONPATH environment variable includes ~/python/lib. (Alternatively you can modify sitecustomize.py to insert that directory into the sys.path but this could have system-wide implications.) You can also add ~/python/scripts to your command path. (I tend to manually type the path whenever I want to run something in there.) Now the package is available for import from any python program that I run. I can reproduce this on a system that I do not have root for but only a user shell account. It is easy to transfer the whole library to another computer just by copying the relevant directories. And if I want to upgrade I can download the new source tarball, build it, and change the symlinks or overwrite the old directory contents. I find this is a reasonably convenient way to have up-to-date packages from the Python world on my stable-but-not-entirely-state-of-the-art Debian system. I'm sure as time goes by I'll refine this system. Or maybe Python Eggs will solve most of the problems... -- http://mail.python.org/mailman/listinfo/python-list
Re: Mapping network drive on Linux
You can approximate it by doing this at the command prompt: # mkdir /z #mount //blah/data /z I assume 'blah' is the hostname for a Windows machine and 'data' is the name of a share on blah. You might need to install smbfs and/or use 'mount.smb' and/or use 'mount -t smbfs'. Of course this can all be done as a matter of Linux system administration and not as part of your python program. (To do it within Python, take the string you would type at the command prompt and call os.system() on it.) If my assumption is wrong then the answer depends on what you mean by 'map a network drive to a local drive'. -- http://mail.python.org/mailman/listinfo/python-list
Re: is there a better way to check an array?
You can check for membership in a list at least these ways: my_list.index(candidate) -returns the index into my_list of the first occurrence of candidate. Raises ValueError if candidate doesn't occur in my_list. my_list.find(candidate) -returns the index into my_list of the first occurrence of candidate. Returns -1 if candidate doesn't occur in my_list. candidate in my_list -returns True if candidate occurs in my_list The last is nicest (most pythonic?) if you don't actually need the index, which it seems you don't. That being said, you should consider the, what I believe is called, Complexity of your algorithm. In particular, you should recall that checking for membership in a list takes an amount of time proportional to the length of the list, while checking for membership in a set (or dictionary) takes a constant amount of time regardless of the size of the set. From what I see, it appears that you don't necessarily need to keep the notRequiredAry things in order, but that you really just need to kknow the set of things that are not required. If you are making this check a lot, for instance looping over a big list and checking for each member whether it is not required, you should consider using a set instead of a list. It might have the benefit of making your code more readable too. -- http://mail.python.org/mailman/listinfo/python-list
Re: encryption with python
>My goal is to combine two different numbers and encrypt them to create a new number that cann't be traced back to the originals. Here's one: def encrypt(x, y): """Return a number that combines x and y but cannot be traced back to them.""" return x + y -- http://mail.python.org/mailman/listinfo/python-list
Re: where is sys.path initialized?
The PYTHONPATH environment variable is good for that. For general
customizing beyond the path, you can make a file called:
C:\Python24\Lib\site-packages\sitecustomze.py
and it will be executed every time python runs. It might look like
this:
import sys
sys.path.insert(0, r'C:\Python24\mypython')
sys.setdefaultencoding('latin-1)
Where the tutorial says the sys.path is "initialized from the directory
containing the input script" I think the input script is the file whose
name is the argument to the Python interpreter. E.g., if you ran
"python c:\stuff\myscript.py" then the path is initialized relative to
c:\stuff. If you just run the interpreter in interactive mode, it's
current working directory when you run 'python'. To say that the path
is initialized "from the directory" I guess only matters if any of the
items in PYTHONPATH or the sys.path are not given as absolute pathnames
but rather relative. If my PYTHONPATH has 'mypython' in it with no
absolute qualifier then in the above scenario where I'm in, say, the
c:\windows directory and I run "python c:\stuff\myscript.py" it will be
c:\stuff\mypython that is added to the path. I think...
--
http://mail.python.org/mailman/listinfo/python-list
Re: How to protect Python source from modification
This is a heck of a can of worms. I've been thinking about these sorts of things for awhile now. I can't write out a broad, well-structured advice at the moment, but here are some things that come to mind. 1. Based on your description, don't trust the client. Therefore, "security", whatever that amounts to, basically has to happen on the server. The server should be designed with the expectation that any input is possible, from slightly tweaked variants of the normal messages to a robotic client that spews the most horrible ill-formed junk frequently and in large volumes. It is the server's job to decide what it should do. For example, consider a website that has a form for users to fill out. The form has javascript, which executes on the client, that helps to validate the data by refusing to submit the form unless the user has filled in required fields, etc. This is client-side validation (analagous to authentication). It is trivial for an attacker to force the form to submit without filling in required fields. Now if the server didn't bother to do its own validation but just inserted a new record into the database with whatever came in from the form submission, on the assumption that the client-side validation was sufficient, this would constitute a serious flaw. (If you wonder then why bother putting in client-side validation at all - two reasons are that it enhances the user experience and that it reduces the average load on the server.) 2. If you're moving security and business logic to the server you have to decide how to implement that. It is possible to rely solely on the RDBMS e.g., PostgreSQL. This has many consequences for deployment as well as development. FOr example, if you need to restrict actions based on user, you will have a different PgSQL user for every business user, and who is allowed to modify what will be a matter of PgSQL configuration. The PgSQL is mature and robust and well developed so you can rely on things to work as you tell them to. On the other hand, you (and your clients?) must be very knowledgeable about the database system to control your application. You have to be able to describe permissions in terms of the database. They have to be able to add new users to PgSQL for every new business user, and be able to adjust permissions if those change. You have to write code in the RDBMS procedural language which, well, I don't know a lot about it but I'm not to thrilled about the idea. Far more appealing is to write code in Python. Lots of other stuff. Imagine in contrast that user authentication is done in Python. In this scenario, you can have just a single PgSQL user for the application that has all access, and the Python always uses that database user but decides internally whether a given action is permitted based on the business user. Of course in this case you have to come up with your own security model which I'd imagine isn't trivial. You could also improve security by combining the approaches, e.g. have 3 database users for 3 different business "roles" with different database permissions, and then in Python you can decide which role applies to a business user and use the corresponding database user to send commands to the database. That could help to mitigate the risks of a flaw in the Python code. 3. You should therefore have a layer of Python that runs on the server and mediates between client and database. Here you can put authentication, validation and other security. You can also put all business logic. It receives all input with the utmost suspicion and only if everything is in order will it query the database and send information to the client. There is little or no UI stuff in this layer. To this end, you should check out Dabo at www.dabodev.com. This is an exciting Python project that I haven't used much but am really looking forward to when I have the chance, and as it becomes more developed. My impression is that it is useable right now. They basically provide a framework for a lot of stuff you seem to have done by hand, and it can give you some great ideas about how to structure your program. You may even decide to port it to Dabo. -- http://mail.python.org/mailman/listinfo/python-list
Re: O'Reilly book on Twisted
Does anybody know: Is this book fully up to date with Twisted 2.0? Does the book cover Nevow at all? Does the book cover general programming concepts related to concurrency? I'm reminded of those high quality articles about Deferreds and event programming by one of the Twisted developers? What is the relationship between the primary developers of Twisted and the book? -- http://mail.python.org/mailman/listinfo/python-list
Windows paths, Java, and command-line arguments, oh my!
I'm trying to invoke a Java command-line program from my Python program on Windows XP. I cannot get the paths in one of the arguments to work right. The instructions for the program describe the following for the command-line arguments: java -jar sforcedataloader.jar -Dsalesforce.config.dir=CONFIG_DIRECTORY They also give an example: java -Dsalesforce.config.dir=c:\config -jar sforcedataloader.jar If I type the example above at the cmd.exe command line the thing works (assuming I have the config file in c:\config). What doesn't work is these two lines: cmd = r'java -jar sforcedataloader.jar -Dc:\config' os.system(cmd) I have tried (not entirely systematically but pretty exhaustively) every combination of backslashes in the cmd string, e.g.: -Dc\:\\config -Dc:\\config -Dc\\:\config -Dc\\:\\config etc. No matter what I do, the program outputs that it cannot find the config file. I cannot tell whether this is a java thing (why are there three different styles for argument on the same command line? In addition to "-jar xxx" and "-Dxxx=yyy" you can also put "xxx=yyy" for some options... wth?), Windows lame cmd.exe shell (is that program invoked by Python's os.system function?), or something else that is messing up. It drivin me crazy though. (Come to think of it, Windows paths have been a persistent thorn in my side for two years of Python development at my company.) Anybody have any suggestions? p.s. 1. I would like to qualify the claim above that the example works at the command-line. I'm not completely certain exactly which form of invocation was successful at the command line, but at least one of them was and that one definitely didn't work from Python. 2. I have a work-around available to me, which is that the program will look for the configuration file in the current directory if the command-line option isn't specified. I'd much rather be able to specify a directory on the command line, so that I can have multiple simultaneous invocations, and so that I can have the configuration file not be in the directory where the Python program is, or alternatively not have to change my directory (since I don't fully appreciate the implications for other parts of my program - this thing runs asynchronously.) -- http://mail.python.org/mailman/listinfo/python-list
Re: Windows paths, Java, and command-line arguments, oh my!
Well, apparently I fried my brain trying to sort this out. There is a typo in my example code in the post but not in my real program. (I know it is a no-no when asking help on c.l.py but I simplified some details from the real code in order not to confuse the issues. Probably backfired by this point.) Below is the post with the error fixed and one sentence added (to clarify why the error in my original post really was not the problem). Thanks for any advice. --- I'm trying to invoke a Java command-line program from my Python program on Windows XP. I cannot get the paths in one of the arguments to work right. The instructions for the program describe the following for the command-line arguments: java -jar sforcedataloader.jar -Dsalesforce.config.dir=CONFIG_DIRECTORY They also give an example: java -Dsalesforce.config.dir=c:\config -jar sforcedataloader.jar If I type the example above at the cmd.exe command line the thing works (assuming I have the config file in c:\config). What doesn't work is these two lines: cmd = r'java -jar sforcedataloader.jar -Dsalesforce.config.dir=c:\config' os.system(cmd) I have tried (not entirely systematically but pretty exhaustively) every combination of backslashes in the cmd string, e.g.: -Dsalesforce.config.dir=c\:\\config -Dsalesforce.config.dir=c:\\config -Dsalesforce.config.dir=c\\:\config -Dsalesforce.config.dir=c\\:\\config etc. No matter what I do, the program outputs that it cannot find the config file. *For at least one variation of the cmd string, I can print the value of cmd and copy/paste it to the command line and the java program works successfully, while for this same cmd string the java program fails when invoked from Python.* I cannot tell whether this is a java thing (why are there three different styles for argument on the same command line? In addition to "-jar xxx" and "-Dxxx=yyy" you can also put "xxx=yyy" for some options... wth?), Windows lame cmd.exe shell (is that program invoked by Python's os.system function?), or something else that is messing up. It drivin me crazy though. (Come to think of it, Windows paths have been a persistent thorn in my side for two years of Python development at my company.) Anybody have any suggestions? p.s. 1. I would like to qualify the claim above that the example works at the command-line. I'm not completely certain exactly which form of invocation was successful at the command line, but at least one of them was and that one definitely didn't work from Python. 2. I have a work-around available to me, which is that the program will look for the configuration file in the current directory if the command-line option isn't specified. I'd much rather be able to specify a directory on the command line, so that I can have multiple simultaneous invocations, and so that I can have the configuration file not be in the directory where the Python program is, or alternatively not have to change my directory (since I don't fully appreciate the implications for other parts of my program - this thing runs asynchronously.) -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting tired with py2exe
What about PyInstaller that was announced the other day? The feature list looks great, and it appears the developers intend to maintain and enhance the program indefinitely. http://groups.google.com/group/comp.lang.python/browse_thread/thread/b487056b7b1f99bc/583da383c1749d9f?q=ANN&rnum=1&hl=en#583da383c1749d9f http://pyinstaller.hpcf.upr.edu/pyinstaller Feature highlights: * Packaging of Python programs into standard executables, that work on computers without Python installed. * Multiplatform: works under Windows, Linux and Irix. * Multiversion: works under any version of Python since 1.5. * Dual packaging mode: * Single directory: build a directory containing an executable plus all the external binary modules (.dll, .pyd, .so) used by the program. * Single file: build a single executable file, totally self-contained, which runs without any external dependency. * Support for automatic binary packing through the well-known UPX compressor. * Optional console mode (see standard output and standard error at runtime). * Selectable executable icon (Windows only). * Fully configurable version resource section in executable (Windows only). * Support for building COM servers (Windows only). -- http://mail.python.org/mailman/listinfo/python-list
Re: Windows paths, Java, and command-line arguments, oh my!
Thank you. I was able to fix it by putting the '-Dwhatever=x' bit before the '-jar y.jar' bit. I had no idea this could matter. Thanks all for the help. -- http://mail.python.org/mailman/listinfo/python-list
Tuple index
Hello, I'm trying to figure out the index position of a tuple member. I know the member name, but I need to know the members index position. I know that if I use the statement print tuple[4] that it will print the contents of that location. What I don't understand is if I know that foo is a member of tuple, how do I get foo's index position. Thanks-in-Advance Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: Tuple index
John Machin wrote:
>
> Steve M wrote:
>> Hello,
>>
>> I'm trying to figure out the index position of a tuple
> member.
>> I know the member name, but I need to know the members index
> position.
>
> Tuples, like lists, don't have members in the sense that they can be
> "named" like t.foo. The only way of referring to them is by index,
> t[4].
>
>> I
>> know that if I use the statement print tuple[4] that it will print
> the
>> contents of that location. What I don't understand is if I know that
> foo is
>> a member of tuple, how do I get foo's index position.
>
> You *can't* "know that foo is a member of tuple".
>
> Consider this:
>
>>>> foo = 'carol'
>>>> t = (123,456,789,'bob',foo,'ted')
>>>> t[4]
> 'carol'
>
> Is that what you mean by "foo is a member of t'? Well, it's not. foo is
> a reference to the string 'carol'. t[4] is also a reference to the
> string 'carol'.
>
> Now read on ...
>
>>>> foo = 'alice'
>>>> t
> (123, 456, 789, 'bob', 'carol', 'ted')
>>>> t[4]
> 'carol'
>>>>
>
> Now foo is a reference to the string 'alice'. Nothing to do with t,
> either before or now.
>
> Have you read the tutorial found at http://docs.python.org/tut/tut.html
> ?
I guess I explained my problem incorrectly. Let me try again.
tuple = ("fred", "barney", "foo")
I know that foo is an element of tuple, but what I need to know is what
the index of foo is, tuple[?]. Hopefully this explains what I'm trying
do do better. Sorry about the earlier confusion.
Steve
--
http://mail.python.org/mailman/listinfo/python-list
Re: Tuple index
Steven Bethard wrote:
> Steve M wrote:
>> I guess I explained my problem incorrectly. Let me try again.
>>
>> tuple = ("fred", "barney", "foo")
>>
>> I know that foo is an element of tuple, but what I need to know is what
>> the index of foo is, tuple[?].
>
> Larry Bates's solution is probably the best way to go here:
>
> py> t = ("fred", "barney", "foo")
> py> list(t).index("foo")
> 2
> py> t[2]
> 'foo'
>
> But note that if you're doing this often, you're probably using tuple
> for the wrong things. Check out:
>
> http://www.python.org/doc/faq
general.html#why-are-there-separate-tuple-and-list-data-types
>
> If you're iterating over the items of something and the items are all of
> the same type, you probably want a list, not a tuple.
>
> What's the use case in which you want to do this?
>
> STeVe
I'm actually doing this as part of an exercise from a book. What the program
is supposed to do is be a word guessing game. The program automaticly
randomly selects a word from a tuple. You then have the oportunity to ask
for a hint. I created another tuple of hints, where the order of the hints
correspond to the word order. I was thinking if I could get the index
position of the randomly selected word, I pass that to the hints tuple to
display the correct hint from the hints tuple. I'm trying to do it this way
as the book I'm using has not gotten to lists yet. As you may have guessed,
I'm just learning Python. I do appreciate your help, Thank you.
Steve
--
http://mail.python.org/mailman/listinfo/python-list
Re: Tuple index
Michael Hartl wrote:
> I actually find it strange that tuples don't have an index function,
> since finding the index doesn't involve any mutation. Anyone know why
> Python doesn't allow a statement like t.index('foo')?
>
> In any case, you can use the index method of list objects if you
> convert your tuple to a list first:
>
t = ("fred", "barney", "foo")
list(t).index("foo")
> 2
def index(a_tuple, element):
> ... return list(a_tuple).index(element)
> ...
t[index(t, "foo")]
> 'foo'
>
> (By the way, 'tuple' is a Python built-in type, so it's probably best
> to avoid using it as a variable name.)
>
>
> Michael
>
> --
> Michael D. Hartl, Ph.D.
> CTO, Quark Sports LLC
> http://quarksports.com/
The book I'm using to learn Python with has not gotten to lists yet, maybe
next chapter.
I knew tuple is a built in type, I was just trying to be clear, I guess I
just muddied the water a bit. Thank you for your help.
Steve
--
http://mail.python.org/mailman/listinfo/python-list
Re: Tuple index
John Machin wrote: > > Steve M wrote: >> I'm actually doing this as part of an exercise from a book. What the > program >> is supposed to do is be a word guessing game. The program automaticly >> randomly selects a word from a tuple. > > Care to tell us which book is using a tuple for this, but hasn't got to > lists yet? > > Cheers, > John Python Programming for the absoulte beginner by Michael Dawson Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: Tuple index
Steven Bethard wrote:
> Steve M wrote:
>> I'm actually doing this as part of an exercise from a book. What the
>> program is supposed to do is be a word guessing game. The program
>> automaticly randomly selects a word from a tuple. You then have the
>> oportunity to ask for a hint. I created another tuple of hints, where the
>> order of the hints correspond to the word order. I was thinking if I
>> could get the index position of the randomly selected word, I pass that
>> to the hints tuple to display the correct hint from the hints tuple. I'm
>> trying to do it this way as the book I'm using has not gotten to lists
>> yet.
>
> I'm guessing it also hasn't gotten to dicts yet either? Perhaps a
> somewhat more natural way of doing this would be something like:
>
> py> hints = dict(word1="here's hint 1!",
> ... word2="here's hint 2!",
> ... word3="here's hint 3!")
> py> words = list(hints)
> py> import random
> py> selected_word = random.choice(words)
> py> selected_word
> 'word3'
> py> print hints[selected_word]
> here's hint 3!
>
> That said, if you want to find the index of a word in a tuple without
> using list methods, here are a couple of possibilities, hopefully one of
> which matches the constructs you've seen so far:
>
> py> t = ("fred", "barney", "foo")
>
> py> for i, word in enumerate(t):
> ... if word == "barney":
> ... break
> ...
> py> i
> 1
>
> py> for i in range(len(t)):
> ... if t[i] == "barney":
> ... break
> ...
> py> i
> 1
>
> py> i = 0
> py> for word in t:
> ... if word == "barney":
> ... break
> ... i += 1
> ...
> py> i
> 1
>
> HTH,
>
> STeVe
Thanks Steve, I'll see if I can make that solution work for me.
Steve
--
http://mail.python.org/mailman/listinfo/python-list
Re: Version Number Comparison Function
I recently saw this: http://www.egenix.com/files/python/mxTools.html mx.Tools.verscmp(a,b) Compares two version strings and returns a cmp() function compatible value (<,==,> 0). The function is useful for sorting lists containing version strings. The logic used is as follows: the strings are compared at each level, empty levels defaulting to '0', numbers with attached strings (e.g. '1a1') compare less than numbers without attachement (e.g. '1a1' < '1). Keith wrote: > Is there a function for comparing version numbers? > > E.g. > > 0.1.0 < 0.1.2 > 1.876b < 1.876c > 3.2.2 < 3.4 > > Keith -- http://mail.python.org/mailman/listinfo/python-list
Function to log exceptions and keep on truckin
import sys, traceback
def e2str(id):
"""Return a string with information about the current exception. id
is arbitrary string included in output."""
exc = sys.exc_info()
file, line, func, stmt = traceback.extract_tb(exc[2])[-1]
return("%s: %s line %s (%s): %s" % (id, func, line, repr(stmt),
str(exc[1])))
This function returns a string containing useful information about the
current exception.
It takes as argument an arbitrary string that gets included in the
output. You can include anything else that might be useful in this
string, such as a loop counter or other local variable.
You can use this function to log anything you'd want to know about an
exception but continue running.
For example:
for i in interable:
try:
some_function(i)
except NonFatalError:
print e2str(str(i))
For some reason I've been into closures lately, so I like to have the
following code in a general utility module, from which I import *:
def _make_e2str():
"""This function creates a closure e2str."""
import sys, traceback
def e2str(id):
"""Return a string with information about the current
exception. id is arbitrary string included in output."""
exc = sys.exc_info()
file, line, func, stmt = traceback.extract_tb(exc[2])[-1]
return("%s: %s line %s (%s): %s" % (id, func, line, repr(stmt),
str(exc[1])))
return e2str
e2str = _make_e2str()
del _make_e2str #clean up the namespace...
--
http://mail.python.org/mailman/listinfo/python-list
Re: compile shebang into pyc file
I just happened across the page linked to below, and remembered this thread, and, well... here you go: http://www.lyra.org/greg/python/ Executable .pyc files Ever wanted to drop a .pyc file right into your web server's cgi-bin directory? Frustrated because the OS doesn't know what to do with a .pyc? (missing the #! line) Look no further! :-) Below is a tiny Bash script to do this. "cat" your .pyc onto the end of this and drop it wherever you need direct execution of that .pyc -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I load python script into Html ??
Man, I don't even know where to start. There is no way this will work if you don't have a web browser that can interpret Python. I don't know of one, and I don't think anybody wants one because using a browser that would execute arbitrary Python code provided by the server would be an obscene security mistake. Javascript is specifically designed for client-side scripting, whereas Python is not. You'll have to re-think what you wanted to accomplish, and if it was just "I prefer coding in Python to coding in Javascript" then you're basically SOL. Hope this helps! -- http://mail.python.org/mailman/listinfo/python-list
Re: How To Read Excel Files In Python?
"""Derived from _Python Programming on Win32_ by Mark Hammond and Andy
Robinson"""
import win32com.client
import win32com.client.dynamic
class Excel:
def __init__(self, filename=None):
self.xlApp =
win32com.client.dynamic.Dispatch('Excel.Application')
if filename:
self.filename = filename
self.xlBook = self.xlApp.Workbooks.Open(filename)
else:
self.xlBook = self.xlApp.Workbooks.Add()
self.filename = ''
def save(self, newfilename=None):
if newfilename:
self.filename = newfilename
self.xlBook.SaveAs(newfilename)
else:
self.xlBook.Save()
def close(self):
self.xlBook.Close(SaveChanges=0)
del self.xlApp
def show(self):
self.xlApp.Visible = 1
def hide(self):
self.xlApp.Visible = 0
def get_cell(self, sheet, row, col):
"get value of one cell"
sht = self.xlBook.Worksheets(sheet)
return sht.Cells(row, col).Value
def set_cell(self, sheet, row, col, value):
"set value of one cell"
sht = self.xlBook.Worksheets(sheet)
sht.Cells(row, col).Value = value
def get_range(self, sheet, row1, col1, row2, col2):
"return a 2d array (i.e. tuple of tuples)"
sht = self.xlBook.Worksheets(sheet)
return sht.Range(sht.Cells(row1, col1), sht.Cells(row2,
col2)).Value
def set_range(self, sheet, leftCol, topRow, data):
bottomRow = topRow + len(data) - 1
rightCol = leftCol + len(data[0]) - 1
sht = self.xlBook.Worksheets(sheet)
sht.Range(sht.Cells(topRow, leftCol), sht.Cells(bottomRow,
rightCol)).Value = data
--
http://mail.python.org/mailman/listinfo/python-list
Re: getaddrinfo not found on SCO OpenServer 5.0.5
In case you haven't heard Microsoft is suing SCO for stealing his Internet concepts and letters and numbers, so you should probably just ditch OpenServer and get Debian like all the smart people have done. I guess the quality of SCO software has declined over the last forty or fifty years and they had to have David Boies compile libsocket and that is probably why this broken symbol problem is happenig. I'm sorry if you cannot switch from the SCO platform, in which case this message may not be very helpful. Have a nice day! -- http://mail.python.org/mailman/listinfo/python-list
Re: Unicode question
Ben Edwards (lists) wrote:
> I am using python 2.4 on Ubuntu dapper, I am working through Dive into
> Python.
>
> There are a couple of inconsictencies.
>
> Firstly sys.setdefaultencoding('iso-8859-1') does not work, I have to do
> sys.setdefaultencoding = 'iso-8859-1'
When you run a Python script, the interpreter does some of its own
stuff before executing your script. One of the things it does is to
delete the name sys.setdefaultencoding. This means that by the time
even your first line of code runs that name no longer exists and so you
will be unable to invoke the function as in your first attempt.
The second attempt "sys.setdefaultencoding = 'iso-8859-1' " is creating
a new name under the sys namespace and assigning it a string. This will
not have the desired effect, or probably any effect at all.
I have found that in order to change the default encoding with that
function, you can put the command in a file called sitecustomize.py
which, when placed in the appropriate location (which is
platform-dependent), will be called in time to have the desired effect.
So the order of events is something like:
1. Invoke Python on myscript.py
2. Python does some stuff and then executes sitecustomize.py
3. Python deletes the name sys.setdefaultencoding, thereby making the
function that was so-named inaccessible.
4. Python then begins executing myscript.py.
Regarding the location of sitecustomize.py, on Windows it is
C:\Python24\Lib\sitecustomize.py.
My guess is that you should put it in the same directory as the bulk of
the Python standard library files. (Also in that directory is a
subdirectory called site-packages, where you can put custom modules
that will be available for import from any of your scripts.)
--
http://mail.python.org/mailman/listinfo/python-list
Re: newb question: file searching
[EMAIL PROTECTED] wrote: > I must ask, in the interest of learning, what is > > [file for file in files if file.endswith(extension)] > > actually doing? I know that 'file' is a type, but what's with the set > up and the brackets and all? Other people explained the list comprehension, but you might be confused about the unfortunate choice of 'file' as the name in this example. 'file' is a built-in as you remarked. It is allowed, but a bad idea, to use names that are the same as built-ins. Some people characterize this as shadowing the built-in. A similar, common case is when people use the name 'list' for a list object. They shouldn't. The example could have been written as: [f for f in files if f.endswith(extension)] -- http://mail.python.org/mailman/listinfo/python-list
Re: newb question: file searching
[EMAIL PROTECTED] wrote:
> Okay. This is almost solved. I just need to know how to have each
> entry in my final list have the full path, not just the file name.
from http://docs.python.org/lib/os-file-dir.html:
walk() generates the file names in a directory tree, by walking the
tree either top down or bottom up. For each directory in the tree
rooted at directory top (including top itself), it yields a 3-tuple
(dirpath, dirnames, filenames).
dirpath is a string, the path to the directory. dirnames is a list of
the names of the subdirectories in dirpath (excluding '.' and '..').
filenames is a list of the names of the non-directory files in dirpath.
Note that the names in the lists contain no path components. To get a
full path (which begins with top) to a file or directory in dirpath, do
os.path.join(dirpath, name).
So walk yields a 3-tuple, not just a filename. You seem to be somewhat
aware of this where you refer to files[2] in your list comprehension,
but I believe that is not constructed correctly.
Try this (untested):
def get_image_filepaths(target_folder):
"""Return a list of filepaths (path plus filename) for all images
in target_folder or any subfolder"""
import os
images = []
for dirpath, dirnames, filenames in os.walk(target_folder):
for filename in filenames:
normalized_filename = filename.lower()
if normalized_filename.endswith('.jpg') or
normalized_filename.endswith('.gif'):
filepath = os.path.join(dirpath, filename)
images.append(filepath)
return images
import os
images = get_image_filepaths(os.getcwd())
> Also, I've noticed that files are being found within hidden
> directories. I'd like to exclude hidden directories from the walk, or
> at least not add anything within them. Any advice?
Decide how you identify a hidden directory and test dirpath before
adding it to the images list. E.g., test whether dirpath starts with
'.' and skip it if so.
>
> Here's what I've got so far:
>
> def getFileList():
> import os
> imageList = []
> for files in os.walk(os.getcwd(), topdown=True):
> imageList += [file for file in files[2] if file.endswith('jpg')
> or
> file.endswith('gif')]
> return imageList
--
http://mail.python.org/mailman/listinfo/python-list
Re: iterate over a series of nodes in an XML file
I see you've had success with elementtree, but in case you are still
thinking about SAX, here is an approach that might interest you. The
idea is basically to turn your program inside-out by writing a
standalone function to process one myID node. This function has nothing
to do with SAX or parsing the XML tree. This function becomes a
callback that you pass to your SAX handler to call on each node.
import xml.sax
def myID_callback(data):
"""Process the text of one myID node - boil it, mash it, stick it
in a list..."""
print data
class MyHandler(xml.sax.ContentHandler):
def __init__(self, myID_callback):
#a buffer to collect text data that may or may not be needed
later
self.current_text_data = []
self.myID_callback = myID_callback
def characters(self, data):
"""Accumulate characters. startElement("myID") resets it."""
self.current_text_data.append(data)
def startElement(self, name, attributes):
if name == 'myID':
self.current_text_data = []
def endElement(self, name):
if name == 'myID':
data = "".join(self.current_text_data)
self.myID_callback(data)
filename = 'idlist.xml'
xml.sax.parse(filename, MyHandler(myID_callback))
--
http://mail.python.org/mailman/listinfo/python-list
Re: How to avoid a warning message box when sending email via Outlook
> [Dermot Doran] > > | I'm very new to using win32com! I just want to send an email > | message via Outlook. However, I keep getting an annoying > | message box (generated by Outlook) indicating that my program > | could be a virus. Does anybody know how to get around this? The users in our office have a little program called ClickYes.exe that sits in the system tray and automatically clicks yes when this pops up. Hardly elegant but it solves their problem. -- http://mail.python.org/mailman/listinfo/python-list
Re: processing a Very Large file
I'm surprised you didn't recommend to use ZODB. Seems like an ideal way to manage this large amount of data as a collection of Python objects... -- http://mail.python.org/mailman/listinfo/python-list
Re: Is Python suitable for a huge, enterprise size app?
This thread: http://mail.python.org/pipermail/python-dev/2005-January/051255.html discusses the problem with memory allocation in CPython. Apparently CPython is not good at, or incapable of, releasing memory back to the operating system. There are ways to compensate for this. I guess the comment about C modules was meant as one way to do so, either by reducing memory requirement in the first place (C data structures are more compact than Python) or else by allocating and freeing memory wholly in the C module, which perhaps does work as expected. The web page for Evan Jones does not obviously indicate the status of the improved memory allocator patch he was working on. I wonder if it is coming along. Incidentally, does anyone know the prospects for CPython to be made stackless? Say in 2.5 or 2.9? Or will that always be an independent project? -- http://mail.python.org/mailman/listinfo/python-list
Re: ZODB memory problems (was: processing a Very Large file)
class ExtendedTupleTable(Persistent):
def __init__(self):
self.interning = ObjectInterning()
# This Set stores all generated ExtendedTuple objects.
self.ets = Set() # et(s): ExtendedTuple object(s)
# This dictionary stores a mapping of elements to Sets of
# ExtendedTuples.
# eg: self.el2ets[3] = Set([(1,2,3), (3,4,5), (1,3,9)])
# self.el2ets[4] = Set([(3,4,5), (2,4,9)])
self.el2ets = {} # el: element of an ExtendedTuple object
###
Note: I might be wrong. I say this here instead of qualifying every
assertion below. Thank you.
If you want more fine-grained swapping-out to disk, you might want to
look at the classes provided by the BTrees modules that come with ZODB.
Built-in container classes like set and dictionary are effectively
opaque to ZODB - they have to be loaded into memory or out to disk as
one whole unit, container and contents. This is true for the Persistent
versions of the containers as well - these are special mostly because
they automatically detect when they are modified.
In order to have some contents of a container pickled out to disk and
others available in memory, you should use BTrees:
>>> root = get_zodb_root_container()
>>> from BTrees import IOBTree
>>> root['el2ets'] = el2ets = IOBTree.IOBTree()
>>> transaction.commit()
>>> el2ets[3] = Set([(1,2,3), (3,4,5), (1,3,9)])
>>> transaction.commit()
IOBTree means that its designed to have integer keys and arbitrary
object values. OOBTree means you can use arbitrary objects (e.g.
tuples) as keys. I read that you should avoid using instances of
subclasses of Persistent as keys in BTrees unless you are very careful
implementing __cmp__(); instead confine your keys to objects
constructed from immutable python types, e.g., strings, tuples, tuples
of strings, ...
If you break down the persistent chunks into small enough pieces and
use the transaction commit and abort appropriately (that takes some
experimenting - e.g., on a read-only loop through every element of a
large BTree, I was running out of memory until I called
transaction.abort() every loop), you should max out your memory usage
at some reasonable amount (determined by cache size) no matter how big
your BTree grows.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Pressing A Webpage Button
Do you actually need to 'press' the button? Or do you just need the effect that pressing the button would bring about (e.g., submitting a Google search query and receiving the results page)? If the latter, then you might want to search for, e.g., "html form get post" and check out some results. Pushing the button is often just loading a URL with parameters. For example, go to Google and type "html form get post" into the search box and press Submit. Now look at the URL you are visiting in your location bar, the URL of the search results. It will be something like: http://www.google.com/search?hl=en&q=html+form+get+post&btnG=Google+Search If you were to load that URL directly (without having gone to the Google homepage, typed "html form get post" in the text entry box and pressed submit) the exact same effect would happen. Filling in the box and clicking the submit button is just the user-friendly way of constructing that URL. -- http://mail.python.org/mailman/listinfo/python-list
Re: Overcoming herpetophobia (or what's up w/ Python scopes)?
This link seems to be about Closures in Python, but I am not feeling sharp enough at the moment to evaluate the correctness of the discussion. http://en.wikipedia.org/wiki/Python_programming_language#Closures As others have said, you might get more useful responses if you can elaborate upon what exactly you worry are shortcomings of Python compared to Perl. -- http://mail.python.org/mailman/listinfo/python-list
How to get the target of a Windows shortcut file
Below is some code adapted from something I think was written by Mark
Hammond. Originally I needed to create a Windows shortcut (link), and
this code does the trick, requiring only the target filename and the
desired shortcut name.
Now, I find I need to open a shortcut and extract the target filename,
and I don't have a clue how that is achieved. To be clear, I mostly
don't understand the gory (Windows API) details of this code. So, can
anyone show how to open an existing shortcut file (given its name) and
discover the name of the file to which it is a shortcut?
import os
from win32com.shell import shell
import pythoncom
# Get the shell interface.
sh = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None, \
pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
# Get an IPersist interface
persist = sh.QueryInterface(pythoncom.IID_IPersistFile)
target_of_link = os.path.abspath('target.doc')
link_name = 'shortcut_to_target.doc.lnk'
sh.SetPath(target_of_link)
persist.Save(link_name, 1)
--
http://mail.python.org/mailman/listinfo/python-list
Re: Can't get exclusive file lock when safely renaming a file
On Dec 6, 12:25 am, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > The rename works, but here is my problem: after getting what I thought > was an exclusive lock on the new file, but before calling os.rename(), I > can still over-write it from another process: > > $ echo "this comes from another process" > spam.txt > $ cat spam.txt > this comes from another process What happens if you try to delete spam.txt at this point instead of over-write it? -- http://mail.python.org/mailman/listinfo/python-list
Re: Good thread pool module
I believe "Python in a Nutshell" has a couple of clear examples using Queue and Threading, including one with a pool of worker threads that wait for entries in one queue and place results in another. Also you should look at the Python Cookbook, which probably includes the same or similar examples from the Nutshell book, since the author of that is an editor of Cookbook. http://aspn.activestate.com/ASPN/Cookbook/Python?kwd=Threads -- http://mail.python.org/mailman/listinfo/python-list
Re: Python types
I think it means that names, not objects, are weakly typed. So you can have: a = 4 a = 'hello' and there is no problem. The name 'a' doesn't have any type associated with it. This contrasts with strongly typed language like C where you declare the type of the name (variable) and the compiler objects at compile time if you attempt to assign a value of a different type. -- http://mail.python.org/mailman/listinfo/python-list
Re: tips for this exercise?
You have two lines inside the loop that ought to be outside the loop - the initial assignment to fiveNumbers and the return statement. Also, the line that appends new numbers to fiveNumbers is not quite correct - the append() method modifies the list in place, rather than return a new list. Here's one that does what you seem to want. It exploits the fact that a given number can't be in a set twice. It eliminates having to modify and recreate the numbers list, but incurs the risk of calling choice() more than five times; however each such extra call to choice is increasingly improbable... from random import choice numbers = range(1,54) def genNumbers(): fiveNumbers = set() while len(fiveNumbers) < 5: fiveNumbers.add(choice(numbers)) return list(fiveNumbers) -- http://mail.python.org/mailman/listinfo/python-list
Re: Memory limit to dict?
An alternative is to use ZODB. For example, you could use the BTree class for the outermost layers of the nested dict, and a regular dict for the innermost layer. If broken up properly, you can store apparently unlimited amount of data with reasonable performance. Just remember not to iterate over the entire collection of objects without aborting the transaction regularly. -- http://mail.python.org/mailman/listinfo/python-list
