Peter Hoffmann Code Blog

Random postings about python, code and social apps. If you want more, check out my friendfeed profile.
Jun 17
Permalink

This Week in Google with Chris Messina on OAuth and OpenID http://peter-hoffmann.com/2010/twig-chris-messina-oauth-openid.html

Comments (View)
Permalink
Comments (View)
Jan 09
Permalink

iterate over list by pairs

The following code will iterate over an list by pairs of elements:

from itertools import chain

def by_pairs(iterable):
    sequence = iter(iterable)
    previous = sequence.next()
    while True:
        current = sequence.next()
        yield previous, current
        previous = current
Comments (View)
Jan 01
Permalink

store json data with sqlobject in mysql database

Especially while developing the final database schema I find pleasant to store JSON data structures direct in the database. SQLObject has a PickleCol, but I has the downside, that serialized objects are binary based and thus not very well to debug.

The following codes need sqlobject as well as formencode:

from sqlobject import *
sqlhub.processConnection = connectionForURI("...")

import cjson
from formencode import validators


class JSONValidator(validators.Validator):

    def to_python(self, value, state):
        if value is None:
            return None
        try:
            return cjson.decode(value)
        except:
            raise validators.Invalid("expected a decodable JSON object in the JSONCol '%s', got %s %r instead" % (self.name, type(value), value), value, state)

    def from_python(self, value, state):
        if value is None:
            return None
        try:
            return cjson.encode(value)
        except:
            raise validators.Invalid("expected an encodable JSON object in the JSONCol '%s', got %s %r instead" % (self.name, type(value), value), value, state)


class SOJSONCol(SOStringCol):

    def createValidators(self):
        return [JSONValidator()] + super(SOJSONCol, self).createValidators()


class JSONCol(Col):
    baseClass = SOJSONCol


class Person(SQLObject):
    name = StringCol()
    data = JSONCol()

The code was adopted from http://openxcap.org/browser/xcap/interfaces/backend/sipthor.py

Comments (View)
Dec 27
Permalink

installing cloudkit on ubuntu 8.10

Installing cloudkit on ubuntu 8.10:

sudo aptitude install rubygems ruby1.8-dev libsqlite3-ruby libhttp-access2-ruby
sudo gem install hoe
sudo gem install sequel -v 2.6.0
sudo gem install thin
sudo gem install cloudkit

Create a configuration file config.ru

require 'cloudkit'
expose :notes, :projects

Start thin with:

/var/lib/gems/1.8/bin/thin -R config.ru start

For more Information about the api see http://getcloudkit.com/rest-api.html

Comments (View)
Oct 10
Permalink

vim toogle spell

Toggle spell language and on/off in vim

function! ToggleSpell()
    if &spell
        if &spelllang == "de"
            set spelllang=de,en
            echo "toggle spell" &spelllang
        elseif &spelllang == "de,en"
            set spelllang=en
            echo "toggle spell" &spelllang
        else
            set spell!
            echo "toggle spell off"
        endif
    else
        set spelllang=de
        set spell!
        echo "toogle spell" &spelllang
    endif
endfunction
map <F2> :call ToggleSpell()<CR>
Comments (View)
Oct 03
Permalink

blinkenlights project stereoscope in toronto

The latest blinkenlights project is to transform the Toronto City Hall into a big interactive computer matrix. The installation is part of the nuit blache.

blinkenlights

The project also provides an iphone simulator (app store) which displays a live stream from Toronto.

More images can be found on flickr

Comments (View)
Permalink

http pubsub

Tags: http pubsub messaging

I’m looking for some kind of http pubsub messaging server. Just found some blog posts but nothing I really like:

Comments (View)
Sep 28
Permalink

working with uuids on python

Tags: python uuid

Python has a module to generate different UUIDs like defined in RFC 4122. uuids include creation time. This recipe extracts the timestamp information:

def get_posixtime(uuid1):
     """Convert the uuid1 timestamp to a standard posix timestamp
    """
    assert uuid1.version == 1, ValueError('only applies to type 1')
    t = uuid1.time
    t = t - 0x01b21dd213814000
    t = t / 1e7
    return t

Another task is to sort uuids by creation time. The default cmp function seems not to do this:

...  
    int = ((time_low << 96L) | (time_mid << 80L) |
               (time_hi_version << 64L) | (clock_seq << 48L) | node)
def __cmp__(self, other):
    if isinstance(other, UUID):
        return cmp(self.int, other.int)
    return NotImplemented

But since uuids have time attribute, a list with them can be sorted with

from operator import attrgetter
lst = [..]
lst.sort(key=attrgetter("time"))
Comments (View)
Sep 23
Permalink

middleware to monitor python wsgi applications

I’m working on some RESTful python wsgi application. For debugging and monitoring I needed a way to see the request and response HTTP header without modifying the actual code.

So I posted the question to stackoverflow where Florian Boenisch came up with a nice solution.

from wsgiref.util import request_uri
import sys

def logging_middleware(application, stream=sys.stdout):
    def _logger(environ, start_response):
        stream.write('REQUEST\n')
        stream.write('%s %s\n' %(
            environ['REQUEST_METHOD'],
            request_uri(environ),
        ))

        for name, value in sorted(environ.items()):
            if name.startswith('HTTP_'):
                stream.write('    %s: %s\n' %(
                    name[5:].title().replace('_', '-'),
                    value,
                ))
        stream.flush()
        def _start_response(code, headers):
            stream.write('RESPONSE\n')
            stream.write('%s\n' % code)
            for data in sorted(headers):
                stream.write('    %s: %s\n' % data)
            stream.flush()
            start_response(code, headers)
        return application(environ, _start_response)
    return _logger

def application(environ, start_response):
    start_response('200 OK', [
        ('Content-Type', 'text/html')
    ])
    return ['Hello World']

if __name__ == '__main__':
    logger = logging_middleware(application)
    from wsgiref.simple_server import make_server
    httpd = make_server('', 1234, logger)
    httpd.serve_forever()

This middelware is is a example of accessing the response from the wrapped middleware.

The output will be something like this:

REQUEST
PUT http://localhost:5000/testdb/1
    Accept-Encoding: identity
    Host: localhost:5000
    User-Agent: Python-httplib2/$Rev: 196 $
RESPONSE
201 CREATED
    content-type: application/json
    ['{"rev": "1222169702", "ok": true, "id": "1"}']

REQUEST
GET http://localhost:5000/_all_dbs
    Accept-Encoding: compress, gzip
    Host: localhost:5000
    User-Agent: Python-httplib2/$Rev: 196 $
RESPONSE
200 OK
    Content-Type: text/plain; charset=utf-8
   ['["testdb"]']

REQUEST
DELETE http://localhost:5000/testdb
    Accept-Encoding: identity
    Host: localhost:5000
    User-Agent: Python-httplib2/$Rev: 196 $
RESPONSE
200 OK
    Content-Type: text/plain; charset=utf-8
    ['{"ok":true}']
Comments (View)