Jun 30
divs = ( line.strip() for line in open("tmp.html") if line.find(">DIV")>0 )
for line in divs:
print line
I started reading Apress Pro Python and immediately learned something new. I knew about Generators as a pattern for writing classes but I didn’t know you could make them on the fly and use in a similar way to List Comprehensions.
The above code lists all the lines in an HTML file which contain a DIV tag, while stripping the white space off the start and end of the lines. Previously I would have written it in a much more clunky way.
Yep. Nothing beats actually stepping away from the PC and reading a book.
Tagged with: apress • book • generator • programming • python
Apr 17
bash-2.05# ./configure
checking MACHDEP… sunos5
checking EXTRAPLATDIR…
checking for –without-gcc… no
checking for –with-cxx=… no
checking for c++… c++
checking for C++ compiler default output file name… configure: error: C++ compiler cannot create executables
See `config.log’ for more details.
If you get the above error, basically it means that your system is not set up at all for compilation. Try compiling the most basic C “hello world” application and see how far you get. In my case even the gnu assembler “as” wasn’t installed. The “configure” script checks your configuration but even it assumes that you have the minimal most basic of tools installed.
Jul 04
I was playing with ZSI today and installed the example script in my Apache cgi-bin folder to try to create a simple SOAP service on top of CGI
#!"C:\Documents and Settings\Stuart\My Documents\Python\Python23\python.exe"
def hello():
return "Hello, world"
def echo(*args):
return args
def average(*args):
sum = 0
for i in args: sum += i
return sum / len(args)
from ZSI import dispatch
dispatch.AsCGI()
On the client side I made a script to call this service:
import sys
from ZSI.client import Binding
IN, OUT = sys.stdin, sys.stdout
b = Binding( port="8765", url="/cgi-bin/soaptest.py", tracefile=OUT)
print b.echo( )
print b.echo( "1", "2" )
print b.hello()
a = b.average(range(1,11))
assert a == 5
Notice that the port is set to 8765. I am forwarding the SOAP calls through MindReef SoapScope using the command ssconfig -f 8765,localhost:80
At first the scripted failed at the fitst call as the request from the service had truncated XML. So, I went into ZSIs dispatch.py class and commented the line in the _CGISendXML() method where the Content-Length was being set. I’m not sure why the content length is causing a problem but it seems that it is a few characters too small and thus the reciever is only reading up to that point. I guess this might not happen on an English OS and may be related to me running the script on Japanese Windows XP.
Once I got the example running I had another problem. The example in the ZSI: The Zolera Soap Infrastructure
Release 1.5.1 documentation was expecting an arg list where really average just needs a list of numbers so I rewrote it as follows.
def average( integers ):
sum = 0
for i in integers:
sum += i
return sum / len(integers)
Later: Thinks, I need an easy way to format Python code in these postings..
May 16
“Cog is the Checkpointed Object Graph object database, providing semi-transparent persistence for large sets of interrelated Python objects. It handles automatic loading of objects on reference, and saving of modified objects back to disk. Reference counting is used to automatically remove no longer referenced objects from storage, and objects will be automatically be attached to the database if a persistent object references them.”
Looks like an interesting base for projects…if only I will be able to think of it when the need arises.
May 07
Simon Willison posts an interesting example of how to code the equivalent of switch statement in Python.
The example is really neat and syntactly cleaner than the example of a more usual switch statement, however anyone coming across that code for first time would most likely be mystifed by it. Not mystified in the same way as having to decode read-only Perl which uses all the tricks in one line, but mystified in that it is so simple but uses constructs like lambda statements that you don’t really need to know to understand most of the Python code you will ever encounter.
You can learn all the Java syntax that you will ever use in a couple of days however to master all of the Python syntax takes time. Most people don’t use it all but Simon’s example and a lot of the code in Dive into Python is a great example of how you can write concise Python using all the neat features of the language that beginners can easily overlook.
May 05
After getting PyBloxsom working on my local machine (XP with Apache) I went off in search of a way to download all my old LiveJournal entries locally. My long term plan is to migrate everything on my site to PyBloxsom and retire both LiveJournal and MT.
By chance,I found an interesting tool: Charm: A command line livejournal client written in Python which should allow me to archive old entries into a text files.
I have one minor problem with it right now. It expects “vi” to be in the path. Not a big deal for me as I have Vim in the path. In the end I made a copy of vi in my cygwin installation and named it vi.exe. From reading the code I could have set one or more environment variables to indicate which editor to use.
I had one more problem. On windows, the environment variable HOME is set to c:\Documents and Settings\YOURNAME. The spaces in this path cause some UNIX tools to get confused so I made a wrapper batch file and set it to the safe 8+3 DOS style path which can be found by doing a DIR /X on the long pathname.
After that Charm worked as expected.
May 04
While poking around various Python projects related to webcams I found an email threading module written in Python which implements JWZs threading algorithm.
May 04
After looking at a lot of webcam software, most of which basically seems to be a shell around a Windows DLL which does the hard work, I have decided to take a look at Video Capture “A Win32 Python Extension for Accessing Video Devices (e.g. a USB WebCam, a TV-Card, …)”. The example code looks really easy:
from VideoCapture import Device
cam = Device()
cam.saveSnapshot('image.jpg')
Jan 17
I was just trying to get some Python to work and decided to wrap it in a shell script in Cygwin on Windows XP so that I wouldn’t have to convert a batch file to a shell script later on when I copied it to a Unix server.
Of course, when I ran the script, Bash complained that the interprerter mentioned in the #! line wasn’t there. In a fit of non Unix compatibility I just replaced /usr/bin with the correct path of my Python exe on Windows.
I should have tested this before updating all my files as Bash choked on the spaces in the path to my Python exe. After some mad Googling to find out how to escape this properly I gave up and did what I should have done in the first place.
I went over to my Cygwin’s /usr/bin and made in a symbolic link to the Python exe in my Windows installation of Python. For some reason, I didn’t expect symbolic links made in Cygwin on Windows to work but it worked perfectly.
Also, as usual, I reversed the arguments for “ln” the first time I tried to make the link. It always seems backwards to me and the help text doesn’t make it obvious which is the source and which the destination.
What I found interesting was that I assumed that putting in the windows path would work the most quickly and it would be less effort to do a temporary fix which I would have to fix later than to try and use “ln”.
Aug 21
This is a really neat tool that one day you may find useful.
poptool.py is a python script that allows you to clean up your POP mailbox.
Usage is like this:
poptool.py -n -uUserName -Ppassword hostname --kill "subject.startswith('Obvious spam signature')"
The text after the –kill is Python code that is excuted to produce a boolean value whether or not to kill the mail in question. Note that you have full access to all the headers and can make the expression as complicated as you like.
n.b. -n means dry run…
Continue reading »