<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8747417979827942680</id><updated>2011-11-30T14:25:49.943-08:00</updated><title type='text'>Code, mostly</title><subtitle type='html'>Code and similar I've found useful, mostly.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://codemostly.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://codemostly.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Terry</name><uri>http://www.blogger.com/profile/05697062323881183395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8747417979827942680.post-211987916508011877</id><published>2011-11-30T14:12:00.000-08:00</published><updated>2011-11-30T14:25:49.965-08:00</updated><title type='text'>Compiling QR reader for python / Ubuntu 11.10</title><content type='html'>Steps that apparently worked to get a QR code &lt;b&gt;reader&lt;/b&gt; compiled for python (2.7) un Ubuntu 11.10 64 bit as of 2011-11-30.&lt;br /&gt;
&lt;br /&gt;
(an alternative to the steps here might be this: &lt;a href="http://pyqrcode.sourceforge.net/"&gt;http://pyqrcode.sourceforge.net/&lt;/a&gt; but that didn't work easily for me in recent Ubuntus)&lt;br /&gt;
&lt;br /&gt;
These instructions: &lt;br /&gt;
&lt;a href="http://hi.baidu.com/paulau/blog/item/915e860ffbf7032c6059f34c.html"&gt;http://hi.baidu.com/paulau/blog/item/915e860ffbf7032c6059f34c.html&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
cover most of the steps, but lots of details need changing now vs. 2008.&lt;br /&gt;
&lt;br /&gt;
The PyQrcodec_Linux.tar.gz mentioned in various places on the web has disappeared from www.pedemonte.eu - in fact that whole domain is gone.  You can get it from &lt;a href="http://gentoo.mirrors.pair.com/distfiles/PyQrcodec_Linux.tar.gz"&gt;http://gentoo.mirrors.pair.com/distfiles/PyQrcodec_Linux.tar.gz&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
The instructions above say:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;sudo apt-get install g++
sudo apt-get install python-dev
sudo apt-get install libcv-dev libcvaux-dev
&lt;/pre&gt;&lt;br /&gt;
in addition you now need&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;sudo apt-get install libhighgui-dev
&lt;/pre&gt;&lt;br /&gt;
Untar and cd into the PyQrCodec folder.&lt;br /&gt;
&lt;br /&gt;
then edit setup.py and add &lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;extra_compile_args = ['-fpermissive'],&lt;/pre&gt;&lt;br /&gt;
before each of the "&lt;tt&gt;&lt;b&gt;sources = ...&lt;/b&gt;&lt;/tt&gt;" lines.&lt;br /&gt;
&lt;br /&gt;
then in the shell run (once and once only)&lt;br /&gt;
&lt;pre&gt;for file in $(grep -lre BEGIN ./*|grep -v svn); do sed -e "s/_BEGIN_/_CV_BEGIN_/;s/_END_/_CV_END_/" $file -i; done
&lt;/pre&gt;&lt;br /&gt;
now &lt;tt&gt;&lt;b&gt;python setup.py build&lt;/b&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;b&gt;sudo python setup.py install&lt;/b&gt;&lt;/tt&gt;&lt;br /&gt;
should work (with all kinds of warnings).&lt;br /&gt;
&lt;br /&gt;
The how to use example should be:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;import PyQrcodec
size, image = PyQrcodec.encode('www.example.com')
image.save('example.png')
status, text = PyQrcodec.decode('example.png')
print(text)
&lt;/pre&gt;&lt;br /&gt;
.encode() params are:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;Definition:     PyQrcodec.encode(string, image_width=400,
  case_sensitive=True, version=5,
  error_correction_level='QR_ECLEVEL_L',
  encoding_hint='QR_MODE_AN')
Docstring:
  Returns a PIL image of the QR code generated
  from the given string
&lt;/pre&gt;&lt;br /&gt;
Note: for http://upload.wikimedia.org/wikipedia/commons/thumb/9/9b/Wikipedia_mobile_en.svg/220px-Wikipedia_mobile_en.svg.png I had to flatten the image (replace the transparent background) for decode() to work.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8747417979827942680-211987916508011877?l=codemostly.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codemostly.blogspot.com/feeds/211987916508011877/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codemostly.blogspot.com/2011/11/compiling-qr-reader-for-python-ubuntu.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default/211987916508011877'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default/211987916508011877'/><link rel='alternate' type='text/html' href='http://codemostly.blogspot.com/2011/11/compiling-qr-reader-for-python-ubuntu.html' title='Compiling QR reader for python / Ubuntu 11.10'/><author><name>Terry</name><uri>http://www.blogger.com/profile/05697062323881183395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8747417979827942680.post-7798418538318326655</id><published>2011-06-21T07:40:00.000-07:00</published><updated>2011-06-21T07:40:10.980-07:00</updated><title type='text'>Can I delete that branch?  Check bzr branch relationships.</title><content type='html'>&lt;p&gt;If you use bazaar (bzr) and end up with several branches for the same project, you can end up wondering if one branch contains all the commits in another, e.g. you need to check that all the work done in a successful experimental branch has been moved into the trunk.&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;This small python program does that:&lt;/p&gt;&lt;br /&gt;
&lt;pre&gt;leo.repo&gt; bzrin free_layout trunk
Checking for commits of revs in 'free_layout' in 'trunk'
Status of 'free_layout':
unknown:
  .thumbnails/
  demo.jpg
  nohup.out
Status of 'trunk':
unknown:
  *.g1.dml
Counting revs in free_layout
6026 revs in free_layout
Counting revs in trunk
6683 revs in trunk
All revs in free_layout exist in trunk : OK
&lt;/pre&gt;&lt;br /&gt;
&lt;p&gt;`bzrin` checks that all the commits in the "free_layout" branch have been merged into the trunk - in this case they have, and you can safely delete "free_layout".&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;The code uses `subprocess` rather than the python bzr bindings to do its work, but it gets the job done and has proved very useful for tidying up a directory full of branches for various subprojects.&lt;/p&gt;&lt;br /&gt;
&lt;pre class="brush: python"&gt;#!/usr/bin/python
"""Check that the latest commit in bzr branch A exists in bzr branch B
"""

# bzrin2
# Author: Terry Brown
# Created: Mon Sep  8 12:18:21 CDT 2008

import subprocess, sys, os
import tempfile  # ? because subprocess.PIPE hangs in .wait() ?

def emit(s):
    sys.stdout.write(s)

def main():
    branch = tuple(sys.argv[1:3])
    emit("Checking for commits of revs in '%s' in '%s'\n" % branch)

    # show status
    for i in branch:
        emit("Status of '%s':\n" % i)
        cmd = subprocess.Popen(('bzr status '+i).split())
        cmd.wait()

    revs = []

    for i in branch:
        emit("Counting revs in %s\n" % i)
        revs.append(set())
        tmpFile, tmpName = tempfile.mkstemp()
        cmd = subprocess.Popen(('bzr log --show-ids --levels=0 '+i).split(),
            stdout = tmpFile)
        os.close(tmpFile)
        cmd.wait()
        source = file(tmpName)
        for line in source:
            content = line.strip()
            if content.startswith('revision-id:'):
                id_ = content.split(None,1)[1]
                while not line.strip() == 'message:':
                    line = source.next()
                line = source.next()
                msg = []
                while not line.strip().startswith('-'*10):
                    msg.append(line.strip())
                    try:
                        line = source.next()
                    except StopIteration:  # end of file
                        break
                revs[-1].add((id_, tuple(msg)))
        os.remove(tmpName)
        emit("%d revs in %s\n" % (len(revs[-1]), i))

    diff = revs[0].difference(revs[1])

    if not diff:
        emit ("All revs in %s exist in %s : OK\n" % branch)
    else:
        emit ("WARNING: %s contains revs NOT in %s\n" % branch)
        for i in diff:
            emit("%s\n%s\n" % (i[0], ''.join(['  '+m for m in i[1]])))
        emit ("WARNING: %s contains revs NOT in %s\n" % branch)

if __name__ == '__main__':
    main()
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8747417979827942680-7798418538318326655?l=codemostly.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codemostly.blogspot.com/feeds/7798418538318326655/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codemostly.blogspot.com/2011/06/can-i-delete-that-branch-check-bzr.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default/7798418538318326655'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default/7798418538318326655'/><link rel='alternate' type='text/html' href='http://codemostly.blogspot.com/2011/06/can-i-delete-that-branch-check-bzr.html' title='Can I delete that branch?  Check bzr branch relationships.'/><author><name>Terry</name><uri>http://www.blogger.com/profile/05697062323881183395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8747417979827942680.post-3822047975181048656</id><published>2010-10-28T14:04:00.000-07:00</published><updated>2010-10-28T14:04:54.270-07:00</updated><title type='text'>Use multiple cores for shell scripts in Ubuntu</title><content type='html'>So you want to use all your CPU's cores for some shell based batch processing task.  Seems there should already be an app for that, and there is, &lt;tt&gt;parallel&lt;/tt&gt;, in the &lt;tt&gt;more-utils&lt;/tt&gt; package in Ubuntu.  But it's not that easy to use the target file argument in a shell script.  (note: I think there may be more than one version of this utility, I'm referring to the one that ships with Ubuntu).&lt;br /&gt;
&lt;br /&gt;
For example, I wanted to use all cores for this operation:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: bash"&gt;for i in svg/*.svg; do f=$(basename $i); \
  inkscape-devel --without-gui \
  --export-background=white \
  --export-ps ps/${f%%svg}ps $i; \
  echo $f; done
&lt;/pre&gt;&lt;br /&gt;
So &lt;tt&gt;parallel&lt;/tt&gt; has an &lt;tt&gt;-i&lt;/tt&gt; flag which enables replacement of &lt;tt&gt;{}&lt;/tt&gt; with the target argument (&lt;tt&gt;$i&lt;/tt&gt; in the above) but only if it's surrounded by spaces and not quoted, hardly convenient for scripting.  This simple wrapper (saved in a file called &lt;tt&gt;task&lt;/tt&gt;, made executable and placed somewhere on your &lt;tt&gt;$PATH&lt;/tt&gt;) gets around that problem:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: bash"&gt;# helper for parallel
#
# usage: task 'shell-pattern' 'shell commands'

GLOB="$1"
shift
SCRIPT=$(mktemp)
echo "$@" &gt;"$SCRIPT"
chmod +x "$SCRIPT"
parallel "$SCRIPT" -- $GLOB
rm "$SCRIPT"
&lt;/pre&gt;&lt;br /&gt;
So now you can use &lt;tt&gt;$1&lt;/tt&gt; (not &lt;tt&gt;$i&lt;/tt&gt;) in your shell code without any complications.  The above example becomes:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: bash"&gt;task 'svg/*.svg' 'f=$(basename $1); inkscape-devel \
--without-gui --export-background=white --export-ps \
ps/${f%%svg}ps $1; echo $f'
&lt;/pre&gt;&lt;br /&gt;
...and running on four cores it's much quicker :-)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8747417979827942680-3822047975181048656?l=codemostly.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codemostly.blogspot.com/feeds/3822047975181048656/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codemostly.blogspot.com/2010/10/use-multiple-cores-for-shell-scripts-in.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default/3822047975181048656'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default/3822047975181048656'/><link rel='alternate' type='text/html' href='http://codemostly.blogspot.com/2010/10/use-multiple-cores-for-shell-scripts-in.html' title='Use multiple cores for shell scripts in Ubuntu'/><author><name>Terry</name><uri>http://www.blogger.com/profile/05697062323881183395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8747417979827942680.post-4584143747501526155</id><published>2010-05-26T09:21:00.000-07:00</published><updated>2010-05-26T09:23:49.272-07:00</updated><title type='text'>Loading SQL tables column by column</title><content type='html'>&lt;p&gt;&lt;b&gt;Goal:&lt;/b&gt; Load data copied from an PDF table into a RDMS table column by column, using SQL.&lt;br /&gt;
&lt;/p&gt;&lt;p&gt;Selecting and copy/pasting the whole PDF table at once didn't extract the data in clean or usable way, things got jumbled.  But selecting one column at a time (using &lt;a href="http://www.foolabs.com/xpdf/"&gt;xpdf&lt;/a&gt;) cleanly extracted the data in that column.  But how can you insert it into the table without messing up the ordering of each columns content?  OMG!  The Excel "reordering destroys data integrity" problem has come to SQL!  :-)  Anyway, given a table like this:&lt;br /&gt;
&lt;/p&gt;&lt;table border='1'&gt;&lt;tr&gt;&lt;td&gt;21&lt;/td&gt;&lt;td&gt;Ant&lt;/td&gt;&lt;td&gt;One&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;31&lt;/td&gt;&lt;td&gt;Bat&lt;/td&gt;&lt;td&gt;Two&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;76&lt;/td&gt;&lt;td&gt;Cat&lt;/td&gt;&lt;td&gt;Three&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;89&lt;/td&gt;&lt;td&gt;Dog&lt;/td&gt;&lt;td&gt;Four&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;p&gt;The following approach will work (from a postgres / psql session):&lt;br /&gt;
&lt;/p&gt;&lt;pre class="brush: sql"&gt;create table rescued_data (
  col1 int,
  col2 text,
  col3 text,
  ordering int
);

create temp sequence s;
create temp table col (val text);

\copy col from stdin
21
31
76
89
\.

insert into rescued_data (col1, ordering)
  select val::int, nextval('s') from col;

-- note need to match type with ::int in the above

select setval('s', 1, false);  -- reset the sequence
truncate col;

\copy col from stdin
Ant
Bat
Cat
Dog
\.

update rescued_data set col2 = val
  from (select val, nextval('s') as seq from col) as x
  where seq = ordering;

-- repeating above for next column

select setval('s', 1, false);  -- reset the sequence
truncate col;

\copy col from stdin
One
Two
Three
Four
\.

update rescued_data set col3 = val
  from (select val, nextval('s') as seq from col) as x
  where seq = ordering;

select * from rescued_data;

-- if necessary, you can
alter table rescued_data drop column ordering;

&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8747417979827942680-4584143747501526155?l=codemostly.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codemostly.blogspot.com/feeds/4584143747501526155/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codemostly.blogspot.com/2010/05/loading-sql-tables-column-by-column.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default/4584143747501526155'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default/4584143747501526155'/><link rel='alternate' type='text/html' href='http://codemostly.blogspot.com/2010/05/loading-sql-tables-column-by-column.html' title='Loading SQL tables column by column'/><author><name>Terry</name><uri>http://www.blogger.com/profile/05697062323881183395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8747417979827942680.post-4322627929325673378</id><published>2010-05-16T11:56:00.000-07:00</published><updated>2010-05-17T08:52:38.205-07:00</updated><title type='text'>Python/PyQt upgrade triggers strange bug</title><content type='html'>&lt;blockquote&gt;&lt;i&gt;Percy:&lt;/i&gt; Look, look, I just can't take the pressure of all these omens anymore!&lt;br /&gt;
&lt;div&gt;&lt;i&gt;Edmund:&lt;/i&gt; Percy...&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;i&gt;Percy:&lt;/i&gt; No, no, really, I'm serious! Only this morning in the courtyard I saw a horse with two heads and two bodies!&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;i&gt;Edmund:&lt;/i&gt; Two horses standing next to each other?&lt;br /&gt;
&lt;/div&gt;&lt;i&gt;Percy:&lt;/i&gt; Yes, I suppose it could have been.&lt;br /&gt;
&lt;div style='text-align:right'&gt;&lt;i&gt;Blackadder, "Witchsmeller Pursuivant"&lt;/i&gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;p&gt;Today I saw a bug with one head and two bodies. Upgrading from Ubuntu to 9.10 to 10.4 broke a tool bar button in &lt;a href="http://webpages.charter.net/edreamleo/front.html" target="_blank"&gt;Leo&lt;/a&gt;, the world's best code editor / project manager / note sorter.  The upgrade involved transitions from Python 2.6.4 -&gt; 2.6.5 and PyQt 4.6 -&gt; 4.7.2.  The forward and back browsing buttons supplied by Leo's nav_qt plugin stopped working.&lt;br /&gt;
&lt;/p&gt;&lt;p&gt;After Brain had been debugging, testing, googling, comparing etc. for over two hours, Intuition wanders past and says, "oh, ha, why not try&lt;br /&gt;
&lt;pre&gt;def __init__ (self,c):
         self.c = c
+        c._prev_next = self
         self.makeButtons()
&lt;/pre&gt;Sometimes, Brain doesn't like Intuition very much.&lt;br /&gt;
&lt;/p&gt;&lt;p&gt;Fortunately Brain was able to save some face, as&lt;br /&gt;
&lt;pre&gt;-        act_l = QtGui.QAction(icon_l, 'prev', ib_w)           
-        act_r = QtGui.QAction(icon_r, 'next', ib_w)           
+        act_l = QtGui.QAction(icon_l, 'prev', ib_w, triggered=self.clickPrev)   
+        act_r = QtGui.QAction(icon_r, 'next', ib_w, triggered=self.clickNext)  
&lt;/pre&gt;was also required.&lt;br /&gt;
&lt;/p&gt;&lt;p&gt;So it seems like the upgrade caused two changes which both had the same symptom, making debugging a challenge.  It seems like the plugin class instance or the actions it was creating are now being garbage collected where they weren't before.  The &lt;tt&gt;c._prev_next = self&lt;/tt&gt; would prevent the instance being collected, although it's unclear that it should also prevent the actions being collected.  You would think the GUI's links to the actions would be enough to protect them, so perhaps that bug body wasn't an old glitch going away, but a new one being introduced.  OTOH the gui must have a link to the actions, as it's able to trigger them.&lt;br /&gt;
&lt;/p&gt;&lt;p&gt;The &lt;tt&gt;triggered=self.clickPrev&lt;/tt&gt; addition presumably covers a change in the emission of 'clicked()' by QToolButton, or a change in default actions, or something.  Passing the parameter that way is a &lt;b&gt;Py&lt;/b&gt;Qt alternative to &lt;tt&gt;act_r.connect(act_r, QtCore.SIGNAL("triggered()"),self.clickNext)&lt;/tt&gt;, which would probably also have worked.&lt;br /&gt;
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8747417979827942680-4322627929325673378?l=codemostly.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codemostly.blogspot.com/feeds/4322627929325673378/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codemostly.blogspot.com/2010/05/pythonpyqt-upgrade-triggers-strange-bug.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default/4322627929325673378'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default/4322627929325673378'/><link rel='alternate' type='text/html' href='http://codemostly.blogspot.com/2010/05/pythonpyqt-upgrade-triggers-strange-bug.html' title='Python/PyQt upgrade triggers strange bug'/><author><name>Terry</name><uri>http://www.blogger.com/profile/05697062323881183395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8747417979827942680.post-5544418055553682568</id><published>2010-05-12T09:42:00.000-07:00</published><updated>2010-05-12T09:42:39.224-07:00</updated><title type='text'>zipas - ensure .zip file contains an outer wrapper folder</title><content type='html'>&lt;p&gt;A trivial &lt;tt class="docutils literal"&gt;bash&lt;/tt&gt; script to create a .zip file where everything's in&lt;br /&gt;
a top-level folder.  Saves creating a folder temporarily, and copying&lt;br /&gt;
/ linking things into it:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #408080; font-style: italic"&gt;#!/bin/sh&lt;/span&gt;

&lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;[&lt;/span&gt; @# -lt 2 &lt;span style="color: #666666"&gt;]&lt;/span&gt;; &lt;span style="color: #008000; font-weight: bold"&gt;then &lt;/span&gt;cat &lt;span style="color: #BA2121"&gt;&amp;lt;&amp;lt;EOF&lt;/span&gt;

&lt;span style="color: #BA2121"&gt;usage: $0 &amp;lt;wrapper-folder&amp;gt; &amp;lt;target files&amp;gt;&lt;/span&gt;

&lt;span style="color: #BA2121"&gt;e.g. zipas jul0907 *.c&lt;/span&gt;
&lt;span style="color: #BA2121"&gt;EOF&lt;/span&gt;
&lt;span style="color: #008000; font-weight: bold"&gt;else&lt;/span&gt;

&lt;span style="color: #008000; font-weight: bold"&gt;  &lt;/span&gt;&lt;span style="color: #19177C"&gt;PATHNAME&lt;/span&gt;&lt;span style="color: #666666"&gt;=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;$1&amp;quot;&lt;/span&gt;

  &lt;span style="color: #19177C"&gt;HEADNAME&lt;/span&gt;&lt;span style="color: #666666"&gt;=&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold"&gt;${&lt;/span&gt;&lt;span style="color: #19177C"&gt;PATHNAME&lt;/span&gt;%%/*&lt;span style="color: #008000; font-weight: bold"&gt;}&lt;/span&gt;  &lt;span style="color: #408080; font-style: italic"&gt;# i.e &amp;#39;foo&amp;#39; from &amp;#39;foo/bar/inner&amp;#39;&lt;/span&gt;

  &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;[&lt;/span&gt; -a &lt;span style="color: #BA2121"&gt;&amp;quot;$HEADNAME&amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;]&lt;/span&gt;; &lt;span style="color: #008000; font-weight: bold"&gt;then&lt;/span&gt;

&lt;span style="color: #008000; font-weight: bold"&gt;    &lt;/span&gt;&lt;span style="color: #008000"&gt;echo&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;ERROR: &amp;#39;$HEADNAME&amp;#39; exists&amp;quot;&lt;/span&gt;
  &lt;span style="color: #008000; font-weight: bold"&gt;else&lt;/span&gt;
&lt;span style="color: #008000; font-weight: bold"&gt;    &lt;/span&gt;&lt;span style="color: #008000"&gt;shift&lt;/span&gt;
&lt;span style="color: #008000"&gt;    &lt;/span&gt;mkdir -p &lt;span style="color: #19177C"&gt;$PATHNAME&lt;/span&gt;

    &lt;span style="color: #008000; font-weight: bold"&gt;for &lt;/span&gt;i in &lt;span style="color: #19177C"&gt;$@&lt;/span&gt;; &lt;span style="color: #008000; font-weight: bold"&gt;do&lt;/span&gt;
&lt;span style="color: #008000; font-weight: bold"&gt;      &lt;/span&gt;ln -s &lt;span style="color: #BA2121"&gt;&amp;quot;$PWD/$i&amp;quot;&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;$PATHNAME/$i&amp;quot;&lt;/span&gt;
    &lt;span style="color: #008000; font-weight: bold"&gt;done&lt;/span&gt;
&lt;span style="color: #008000; font-weight: bold"&gt;    &lt;/span&gt;zip -r &lt;span style="color: #19177C"&gt;$HEADNAME&lt;/span&gt;.zip &lt;span style="color: #19177C"&gt;$HEADNAME&lt;/span&gt;

    rm -rf &lt;span style="color: #BA2121"&gt;&amp;quot;$HEADNAME&amp;quot;&lt;/span&gt;
  &lt;span style="color: #008000; font-weight: bold"&gt;fi&lt;/span&gt;
&lt;span style="color: #008000; font-weight: bold"&gt;fi&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;p&gt;  posted at: 15:01 |&lt;br /&gt;
  path: &lt;a href="../bash.1" title="path"&gt;/code/bash&lt;/a&gt; |&lt;br /&gt;
  &lt;a href="zipas.html"&gt;permanent link to this entry&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8747417979827942680-5544418055553682568?l=codemostly.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codemostly.blogspot.com/feeds/5544418055553682568/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codemostly.blogspot.com/2010/05/zipas-ensure-zip-file-contains-outer.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default/5544418055553682568'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default/5544418055553682568'/><link rel='alternate' type='text/html' href='http://codemostly.blogspot.com/2010/05/zipas-ensure-zip-file-contains-outer.html' title='zipas - ensure .zip file contains an outer wrapper folder'/><author><name>Terry</name><uri>http://www.blogger.com/profile/05697062323881183395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8747417979827942680.post-1890183206677014980</id><published>2010-05-11T14:26:00.000-07:00</published><updated>2010-05-11T15:09:49.299-07:00</updated><title type='text'>Merging PDF files</title><content type='html'>&lt;p&gt;The python code at the bottom of this posting can be used to merge PDF files (via GhostScript (gs)).  In theory gs can do that by itself, in practice I found merging about 160 single page files into one resulted in strange characters appearing in some of the text.  The python code merges files two at a time, repeatedly, until all are merged.  Merging file 1 and 2, then that with 3, then that with 4, etc. may also work, but it becomes very slow for a large set of files.  The binary approach here is much faster.&lt;br /&gt;
&lt;/p&gt;&lt;p&gt;This code is &lt;strong&gt;just a quick hack&lt;/strong&gt;.  If you have a large pile of PDFs to merge and GhostScript is failing as described above, this could save your day.  It's invoked from the command line by:&lt;br /&gt;
&lt;/p&gt;&lt;pre&gt;python pdfmerge.py *.pdf&lt;/pre&gt;&lt;p&gt;and merges the files in the order listed, creating a lot of files called &lt;tt&gt;__XXXX.pdf&lt;/tt&gt; in the process.  The last &lt;tt&gt;__XXXX.pdf&lt;/tt&gt; file produced is your output, you should rename that one and delete the rest.  I did say it was just a quick hack :-)&lt;br /&gt;
&lt;/p&gt;&lt;pre class="brush: python"&gt;"""Merge pdfs using GhostScript (gs)

Work around for a bug in gs such that::
    
    gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER 
    -sOutputFile=foo.pdf *.pdf
    
produces odd text corruptions if *.pdf expands to a large number of files.

This program uses a binary merging approach which seems to avoid the bug.
"""
    
import sys
import os
import subprocess
from collections import defaultdict

pages = defaultdict(lambda:1)  # number of pages in each file

pdfs = sys.argv[1:]            # pdfs to merge, already ordered

idx = 0                        # sequence number for temporary pdfs

newpdfs = []                   # list of new pdfs to process

while pdfs or newpdfs:

    if not pdfs:  # pdfs list ends up empty whenever it starts of even length
        pdfs = newpdfs
        newpdfs = []

    if len(pdfs) == 1:
        # only one left, just add it to the end of the list for next iteration
        newpdfs.append(pdfs.pop(0))
        pdfs = newpdfs
        newpdfs = []
        if len(pdfs) == 1:  # we're done
            break

    pdf0 = pdfs.pop(0)  # pair of pdfs to merge
    pdf1 = pdfs.pop(0)

    assert os.path.isfile(pdf0)  # should both exist
    assert os.path.isfile(pdf1)

    idx += 1
    newpdf = "__pdf%04d.pdf" % idx

    pages[newpdf] = pages[pdf0] + pages[pdf1]

    newpdfs.append(newpdf)  # add new pdf to list for next iteration

    cmd = ("gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER "
    "-sOutputFile=%s %s %s" % (newpdf, pdf0, pdf1))

    # here's a lot of uneeded paranoia that arose when I was feeding in some
    # bad (0 byte) pdf files, doesn't hurt to leave it in

    print pdf0, pdf1, newpdf, pages[pdf0], pages[pdf1], \
        pages[newpdf], len(pdfs), len(newpdfs)
    print cmd

    proc = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, 
        stderr=subprocess.STDOUT)

    out,dummy = proc.communicate()

    print out
    print

    # the term 'Processing pages ' should occur twice, if both files were read
    procs = [i for i in out.split('\n') if i.startswith('Processing pages ')]

    assert len(procs) == 2
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8747417979827942680-1890183206677014980?l=codemostly.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codemostly.blogspot.com/feeds/1890183206677014980/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codemostly.blogspot.com/2010/05/merging-pdf-files.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default/1890183206677014980'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default/1890183206677014980'/><link rel='alternate' type='text/html' href='http://codemostly.blogspot.com/2010/05/merging-pdf-files.html' title='Merging PDF files'/><author><name>Terry</name><uri>http://www.blogger.com/profile/05697062323881183395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8747417979827942680.post-4670886229757851141</id><published>2009-08-15T13:41:00.000-07:00</published><updated>2010-10-16T07:57:14.711-07:00</updated><title type='text'>Syntax highlighting for Pyblosxom, S5, etc.</title><content type='html'>&lt;blockquote&gt;Oct. 2010 - note this also works for apps. like &lt;tt&gt;rst2s&lt;/tt&gt;5 - just make your own version of &lt;tt&gt;rst2s5&lt;/tt&gt; which includes the required module as shown below.  &lt;tt&gt;rst2s5&lt;/tt&gt; is a wrapper script of just a few lines, so it's easy to do.&lt;/blockquote&gt;&lt;br /&gt;
&lt;p&gt;After running around in circles a bit I've found it's quite easy to&lt;br /&gt;
get syntax highlighting in Pyblosxom if you're using the rst plugin.&lt;/p&gt;&lt;blockquote&gt;&lt;ol class="arabic"&gt;&lt;li&gt;&lt;p class="first"&gt;Install the rst plugin.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;Make sure pygments is installed on your system.  It's available as&lt;br /&gt;
a package for Ubuntu, so you can just do:&lt;/p&gt;&lt;pre class="literal-block"&gt;sudo apt-get install python-pygments
&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;Get &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;rst-directive.py&lt;/span&gt;&lt;/tt&gt; from the &lt;a class="reference external" href="http://dev.pocoo.org/projects/pygments/"&gt;pygments&lt;/a&gt; distribution (or from the&lt;br /&gt;
end of this article).  It's&lt;br /&gt;
in the 'external' folder.  It may not be included in the package&lt;br /&gt;
for your system, but you can get the file by itself by browsing&lt;br /&gt;
the pygments site.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;Rename it &lt;tt class="docutils literal"&gt;rst_directive.py&lt;/tt&gt; because that seems more sensible,&lt;br /&gt;
and put it in you plugins folder.  You would think you would need&lt;br /&gt;
to import it from the rst plugin, but you don't, because&lt;br /&gt;
Pyblosxom will have imported it just because it's in the plugins&lt;br /&gt;
folder.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;Edit rst_directive.py and change:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;INLINESTYLES &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000"&gt;False&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;to&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;INLINESTYLES &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000"&gt;True&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Doing so causes pygments to use &lt;tt class="docutils literal"&gt;&amp;lt;span &lt;span class="pre"&gt;style=&amp;quot;color:&lt;/span&gt; #BA2121&amp;quot;&amp;gt;&lt;/tt&gt;&lt;br /&gt;
&lt;br /&gt;
instead of &lt;tt class="docutils literal"&gt;&amp;lt;span &lt;span class="pre"&gt;class=&amp;quot;comment&amp;quot;&amp;gt;&lt;/span&gt;&lt;/tt&gt; etc., but it saves a lot of&lt;br /&gt;
trouble trying to ensure the CSS file is available.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;p&gt;That's it.  &lt;em&gt;Now&lt;/em&gt; in your rst you can use the &lt;cite&gt;sourcecode&lt;/cite&gt; directive:&lt;/p&gt;&lt;br /&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;That&amp;#39;s it.  &lt;span style="font-style: italic"&gt;*Now*&lt;/span&gt; in your rst you can use the &lt;span style="color: #19177C"&gt;`sourcecode`&lt;/span&gt; directive:

.. &lt;span style="color: #008000; font-weight: bold"&gt;sourcecode:&lt;/span&gt; python

  import foo  # get access to foo
  foo.bar(&amp;quot;test&amp;quot;) or raise SystemExit
&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;to  syntax highlight rst (above), python (below), and all the other languages and markups pygments knows.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;foo&lt;/span&gt;  &lt;span style="color: #408080; font-style: italic"&gt;# get access to foo&lt;/span&gt;

foo&lt;span style="color: #666666"&gt;.&lt;/span&gt;bar(&lt;span style="color: #BA2121"&gt;&amp;quot;test&amp;quot;&lt;/span&gt;) &lt;span style="color: #AA22FF; font-weight: bold"&gt;or&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;raise&lt;/span&gt; &lt;span style="color: #D2413A; font-weight: bold"&gt;SystemExit&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a class="reference external" href="http://bel-epa.com/bloxom/pylons/firstpost.html"&gt;Graham Higgins&lt;/a&gt;'s post may have set me off on the right track here, at least by making me aware of pygments.&lt;/p&gt;&lt;div class="section" id="syntax-highlighting-on-external-files"&gt;&lt;h4&gt;Syntax highlighting on external files&lt;/h4&gt;&lt;br /&gt;
&lt;p&gt;To make pygment's &lt;cite&gt;sourcecode&lt;/cite&gt; directive even more useful I added a&lt;br /&gt;
quick tweak to allow inclusion of a file like this:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;.. &lt;span style="color: #AA22FF; font-weight: bold"&gt;sourcecode&lt;/span&gt;:: &lt;span style="color: #008000; font-weight: bold"&gt;python&lt;/span&gt;
  &lt;span style="color: #0000FF; font-weight: bold"&gt;:filename:&lt;/span&gt; &lt;span style="color: #0000FF"&gt;../tlog/plugins/rst_directive.py&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In this form the inline content is ignored and the content of&lt;br /&gt;
&lt;br /&gt;
&lt;cite&gt;filename&lt;/cite&gt; is formatted instead.&lt;/p&gt;&lt;p&gt;Here's the complete, modified, &lt;cite&gt;rst_directive.py&lt;/cite&gt; (original from &lt;cite&gt;external&lt;/cite&gt;&lt;br /&gt;
folder on pygments Trac site).  Put it in your&lt;br /&gt;
plugins directory.  Modifications are:&lt;/p&gt;&lt;blockquote&gt;&lt;ul class="simple"&gt;&lt;li&gt;&lt;tt class="docutils literal"&gt;INLINESTYLES = True&lt;/tt&gt;&lt;/li&gt;
&lt;li&gt;the &lt;tt class="docutils literal"&gt;if &lt;span class="pre"&gt;'filename'...&lt;/span&gt;&lt;/tt&gt; block&lt;/li&gt;
&lt;li&gt;setting &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;...options['filename']&lt;/span&gt; = directives.path&lt;/tt&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/blockquote&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #408080; font-style: italic"&gt;# -*- coding: utf-8 -*-&lt;/span&gt;
&lt;span style="color: #BA2121; font-style: italic"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span style="color: #BA2121; font-style: italic"&gt;    The Pygments reStructuredText directive&lt;/span&gt;
&lt;span style="color: #BA2121; font-style: italic"&gt;    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&lt;/span&gt;

&lt;span style="color: #BA2121; font-style: italic"&gt;    This fragment is a Docutils_ 0.4 directive that renders source code&lt;/span&gt;
&lt;span style="color: #BA2121; font-style: italic"&gt;    (to HTML only, currently) via Pygments.&lt;/span&gt;

&lt;span style="color: #BA2121; font-style: italic"&gt;    To use it, adjust the options below and copy the code into a module&lt;/span&gt;
&lt;span style="color: #BA2121; font-style: italic"&gt;    that you import on initialization.  The code then automatically&lt;/span&gt;
&lt;span style="color: #BA2121; font-style: italic"&gt;    registers a ``sourcecode`` directive that you can use instead of&lt;/span&gt;
&lt;span style="color: #BA2121; font-style: italic"&gt;    normal code blocks like this::&lt;/span&gt;

&lt;span style="color: #BA2121; font-style: italic"&gt;        .. sourcecode:: python&lt;/span&gt;

&lt;span style="color: #BA2121; font-style: italic"&gt;            My code goes here.&lt;/span&gt;

&lt;span style="color: #BA2121; font-style: italic"&gt;    If you want to have different code styles, e.g. one with line numbers&lt;/span&gt;
&lt;span style="color: #BA2121; font-style: italic"&gt;    and one without, add formatters with their names in the VARIANTS dict&lt;/span&gt;
&lt;span style="color: #BA2121; font-style: italic"&gt;    below.  You can invoke them instead of the DEFAULT one by using a&lt;/span&gt;

&lt;span style="color: #BA2121; font-style: italic"&gt;    directive option::&lt;/span&gt;

&lt;span style="color: #BA2121; font-style: italic"&gt;        .. sourcecode:: python&lt;/span&gt;
&lt;span style="color: #BA2121; font-style: italic"&gt;            :linenos:&lt;/span&gt;

&lt;span style="color: #BA2121; font-style: italic"&gt;            My code goes here.&lt;/span&gt;

&lt;span style="color: #BA2121; font-style: italic"&gt;    Look at the `directive documentation`_ to get all the gory details.&lt;/span&gt;

&lt;span style="color: #BA2121; font-style: italic"&gt;    .. _Docutils: http://docutils.sf.net/&lt;/span&gt;
&lt;span style="color: #BA2121; font-style: italic"&gt;    .. _directive documentation:&lt;/span&gt;
&lt;span style="color: #BA2121; font-style: italic"&gt;       http://docutils.sourceforge.net/docs/howto/rst-directives.html&lt;/span&gt;

&lt;span style="color: #BA2121; font-style: italic"&gt;    :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.&lt;/span&gt;
&lt;span style="color: #BA2121; font-style: italic"&gt;    :license: BSD, see LICENSE for details.&lt;/span&gt;

&lt;span style="color: #BA2121; font-style: italic"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

&lt;span style="color: #408080; font-style: italic"&gt;# Options&lt;/span&gt;
&lt;span style="color: #408080; font-style: italic"&gt;# ~~~~~~~&lt;/span&gt;

&lt;span style="color: #408080; font-style: italic"&gt;# Set to True if you want inline CSS styles instead of classes&lt;/span&gt;
INLINESTYLES &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000"&gt;True&lt;/span&gt;

&lt;span style="color: #008000; font-weight: bold"&gt;from&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;pygments.formatters&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; HtmlFormatter


&lt;span style="color: #408080; font-style: italic"&gt;# The default formatter&lt;/span&gt;
DEFAULT &lt;span style="color: #666666"&gt;=&lt;/span&gt; HtmlFormatter(noclasses&lt;span style="color: #666666"&gt;=&lt;/span&gt;INLINESTYLES)

&lt;span style="color: #408080; font-style: italic"&gt;# Add name -&amp;gt; formatter pairs for every variant you want to use&lt;/span&gt;
VARIANTS &lt;span style="color: #666666"&gt;=&lt;/span&gt; {
    &lt;span style="color: #BA2121"&gt;&amp;#39;linenos&amp;#39;&lt;/span&gt;: HtmlFormatter(noclasses&lt;span style="color: #666666"&gt;=&lt;/span&gt;INLINESTYLES, linenos&lt;span style="color: #666666"&gt;=&lt;/span&gt;&lt;span style="color: #008000"&gt;True&lt;/span&gt;),
}



&lt;span style="color: #008000; font-weight: bold"&gt;from&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;docutils&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; nodes
&lt;span style="color: #008000; font-weight: bold"&gt;from&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;docutils.parsers.rst&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; directives

&lt;span style="color: #008000; font-weight: bold"&gt;from&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;pygments&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; highlight

&lt;span style="color: #008000; font-weight: bold"&gt;from&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;pygments.lexers&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; get_lexer_by_name, TextLexer

&lt;span style="color: #008000; font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #0000FF"&gt;pygments_directive&lt;/span&gt;(name, arguments, options, content, lineno,
                       content_offset, block_text, state, state_machine):

    &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;#39;filename&amp;#39;&lt;/span&gt; &lt;span style="color: #AA22FF; font-weight: bold"&gt;in&lt;/span&gt; options:
        &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; options[&lt;span style="color: #BA2121"&gt;&amp;#39;filename&amp;#39;&lt;/span&gt;]:
            content &lt;span style="color: #666666"&gt;=&lt;/span&gt; [line&lt;span style="color: #666666"&gt;.&lt;/span&gt;rstrip(&lt;span style="color: #BA2121"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #BB6622; font-weight: bold"&gt;\n&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;#39;&lt;/span&gt;) &lt;span style="color: #008000; font-weight: bold"&gt;for&lt;/span&gt; line &lt;span style="color: #AA22FF; font-weight: bold"&gt;in&lt;/span&gt; &lt;span style="color: #008000"&gt;file&lt;/span&gt;(options[&lt;span style="color: #BA2121"&gt;&amp;#39;filename&amp;#39;&lt;/span&gt;])] 
        &lt;span style="color: #008000; font-weight: bold"&gt;del&lt;/span&gt; options[&lt;span style="color: #BA2121"&gt;&amp;#39;filename&amp;#39;&lt;/span&gt;] 

    &lt;span style="color: #008000; font-weight: bold"&gt;try&lt;/span&gt;:
        lexer &lt;span style="color: #666666"&gt;=&lt;/span&gt; get_lexer_by_name(arguments[&lt;span style="color: #666666"&gt;0&lt;/span&gt;])
    &lt;span style="color: #008000; font-weight: bold"&gt;except&lt;/span&gt; &lt;span style="color: #D2413A; font-weight: bold"&gt;ValueError&lt;/span&gt;:
        &lt;span style="color: #408080; font-style: italic"&gt;# no lexer found - use the text one instead of an exception&lt;/span&gt;

        lexer &lt;span style="color: #666666"&gt;=&lt;/span&gt; TextLexer()

    &lt;span style="color: #408080; font-style: italic"&gt;# take an arbitrary option if more than one is given&lt;/span&gt;
    formatter &lt;span style="color: #666666"&gt;=&lt;/span&gt; options &lt;span style="color: #AA22FF; font-weight: bold"&gt;and&lt;/span&gt; VARIANTS[options&lt;span style="color: #666666"&gt;.&lt;/span&gt;keys()[&lt;span style="color: #666666"&gt;0&lt;/span&gt;]] &lt;span style="color: #AA22FF; font-weight: bold"&gt;or&lt;/span&gt; DEFAULT
    parsed &lt;span style="color: #666666"&gt;=&lt;/span&gt; highlight(&lt;span style="color: #BA2121"&gt;u&amp;#39;&lt;/span&gt;&lt;span style="color: #BB6622; font-weight: bold"&gt;\n&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;join(content), lexer, formatter)
    &lt;span style="color: #008000; font-weight: bold"&gt;return&lt;/span&gt; [nodes&lt;span style="color: #666666"&gt;.&lt;/span&gt;raw(&lt;span style="color: #BA2121"&gt;&amp;#39;&amp;#39;&lt;/span&gt;, parsed, format&lt;span style="color: #666666"&gt;=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;#39;html&amp;#39;&lt;/span&gt;)]

pygments_directive&lt;span style="color: #666666"&gt;.&lt;/span&gt;arguments &lt;span style="color: #666666"&gt;=&lt;/span&gt; (&lt;span style="color: #666666"&gt;1&lt;/span&gt;, &lt;span style="color: #666666"&gt;0&lt;/span&gt;, &lt;span style="color: #666666"&gt;1&lt;/span&gt;)
pygments_directive&lt;span style="color: #666666"&gt;.&lt;/span&gt;content &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;1&lt;/span&gt;

pygments_directive&lt;span style="color: #666666"&gt;.&lt;/span&gt;options &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000"&gt;dict&lt;/span&gt;([(key, directives&lt;span style="color: #666666"&gt;.&lt;/span&gt;flag) &lt;span style="color: #008000; font-weight: bold"&gt;for&lt;/span&gt; key &lt;span style="color: #AA22FF; font-weight: bold"&gt;in&lt;/span&gt; VARIANTS])

pygments_directive&lt;span style="color: #666666"&gt;.&lt;/span&gt;options[&lt;span style="color: #BA2121"&gt;&amp;#39;filename&amp;#39;&lt;/span&gt;] &lt;span style="color: #666666"&gt;=&lt;/span&gt; directives&lt;span style="color: #666666"&gt;.&lt;/span&gt;path

directives&lt;span style="color: #666666"&gt;.&lt;/span&gt;register_directive(&lt;span style="color: #BA2121"&gt;&amp;#39;sourcecode&amp;#39;&lt;/span&gt;, pygments_directive)

&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8747417979827942680-4670886229757851141?l=codemostly.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codemostly.blogspot.com/feeds/4670886229757851141/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codemostly.blogspot.com/2009/08/syntax-highlighting-for-pyblosxom.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default/4670886229757851141'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8747417979827942680/posts/default/4670886229757851141'/><link rel='alternate' type='text/html' href='http://codemostly.blogspot.com/2009/08/syntax-highlighting-for-pyblosxom.html' title='Syntax highlighting for Pyblosxom, S5, etc.'/><author><name>Terry</name><uri>http://www.blogger.com/profile/05697062323881183395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
