2008-01-30

Tea time

Ordered various teas from Théhuone and picked them up from post office today. Two oolongs (dong fang mei ren & bao zhong), ama-cha, toasted and plain maté (too bad I don't have that drinking device that they use in América Latina) and ready made mint tea mixture. Should be enough for a couple of months!

Théhuone delivery was prompt and packaging execellent.

2008-01-27

mod_python

So, the Twisted engine turned out to be a bit more complicated than necessary for my needs. Maybe later..

A more conservative approach is needed, hence Apache/mod_python. To get it running in Ubuntu (assuming you have Apache already installed), do:

sudo apt-get install libapache2-mod-python

sudo apt-get install libapache2-mod-python-doc

sudo emacs /etc/apache2/sites-enabled/000-default

(Or whatever editor one is using.)

..and inside <Directory /var/www/> insert these lines:

AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On

Exit Emacs and restart Apache:

sudo /etc/init.d/apache2 restart

Done.

Ok, not quite. The above enables mod_python Publisher handler which operates at a high level. Instead of, or in addition to above, put something like this into Apache config:

<Directory /var/www/python>
        AddHandler mod_python .py
        PythonHandler python
        PythonDebug On
</Directory>

Now you have more control over the response:

from mod_python import apache

def handler(req):
        import time

        req.content_type = "text/plain"
        req.headers_out['CacheControl'] = "no-cache"
        req.headers_out['Expires'] = "-1"
        req.headers_out['This-is-my-own-wacky-and-useless-header'] = "Pangalactic Gargleblaster"
        req.write(str(time.time()))

        return apache.OK

2008-01-24

REST Describe & Compile

Create WADL documents from "example" HTTP requests; generate code from WADL documents in PHP, Ruby, Python and Java (C# seems to be on its' way). Quiz: in which of those languages the generated code is shortest? :))

See also author's blog and speech on YouTube.

2008-01-21

VectorMagic

Nice piece of software (online) from Stanford University Artificial Intelligence Laboratory. Tested it out, below is the original greyscale bitmap image (click for source JPEG):

Update: Vector Magic is now at vectormagic.com.

And here's the vectorized result (click for PDF):

Note: the VectorMagic FAQ has this note:

"What are the white lines I see in some results?
This is a rendering problem in most anti-aliased vector renderers (Illustrator, CorelDRAW, Xara Xtreme, Ghostscript) where the background appears to "shine through" between region boundaries. We're looking into possible workarounds."

Apple Preview seems to have this problem too. When printed with an HP b/w laser there are no lines.

And here's another, a picture of some cucumber-tzatziki snacks (photo by KK):

The vectorized result (again, click for PDF):

VectorMagic outputs (at least) EPS, SVG and PNG.

2008-01-09

Using Threads in Twisted

This Twisted thingy is seriously hard stuff to understand... In addition to that threading doc (post link), see howtos about deferreds and async programming.

What I'm trying to do here is plug Berkeley DB XML into a Twisted.Web2 application. This involves delegating DB access to worker threads outside the Twisted event loop ("reactor" pattern). A lot more stuff to figure out before coding starts...

2008-01-07

Berkeley DB XML Python basics

In an earlier post a C++ snippet can be found where a DB XML container was created (or opened if already exists) and a document read from stdin was put into that container. That same snippet done in Python is pretty much identical:

from bsddb3.db import *
from dbxml import *

mgr = XmlManager(DBXML_ALLOW_EXTERNAL_ACCESS)
uc = mgr.createUpdateContext()

try:
        cont = mgr.openContainer("testcontainer.dbxml", DB_CREATE|DBXML_ALLOW_VALIDATION, XmlContainer.WholedocContainer)
        doc = mgr.createDocument()
        input = mgr.createStdInInputStream()
        doc.setContentAsXmlInputStream(input)
        cont.putDocument(doc, uc, DBXML_GEN_NAME)

except XmlException, inst:
        print "XmlException (", inst.ExceptionCode,"): ", inst.What
        if inst.ExceptionCode == DATABASE_ERROR:
                print "Database error code:",inst.DBError

Berkeley DB XML Python module building

A glitch appears when trying to build out of the box as instructed in src/python/README. Minor tweaking with types was necessary in the dbxml_python_wrap.cpp file. Here's output from diff(1):


2058c2058
<     const char *doc = (((PyCFunctionObject *)obj) -> m_ml -> ml_doc);
---
>     char *doc = (((PyCFunctionObject *)obj) -> m_ml -> ml_doc);
3001c3001
<     char *cstr; Py_ssize_t len;
---
>     char *cstr; int len;
36695c36695
<       const char *c = methods[i].ml_doc;
---
>       char *c = methods[i].ml_doc;
36699c36699
<         const char *name = c + 10;
---
>         char *name = c + 10;


This same problem was discussed in DB XML forums (which, for some reason, seem to be off line a lot of time; what's wrong, Oracle?). After those minor modifications, build went as advertised:


cd bsddb3-4.5.0

python setup.dbxml.py build

sudo python setup.dbxml.py install

cd ..

python setup.py build

sudo python setup.py install

After the build these basic tests passed:


>>> from bsddb3.db import *
>>> from dbxml import *
>>> print version()
(4, 5, 20)
>>> mgr=XmlManager()
>>> print mgr.get_version_string()
Oracle: Berkeley DB XML 2.3.10: (January 30, 2007)

Running the tests (python examples.py test) also passed just fine. Great!

2008-01-03

Basic Twisted.Web2 on Ubuntu

sudo apt-get install python-twisted sudo apt-get install python-twisted-web2 wget http://twistedmatrix.com/projects/web2/documentation/examples/intro/simple.py twistd -ny simple.py And then point your web browser to http://<server address>:8080 — and go tweak the code.

Twisted.Web2 docs can be found at: http://twistedmatrix.com/projects/web2/documentation/.

See the Wikipedia article for overview of Twisted.