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
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
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
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>
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.

The project also provides an iphone simulator (app
store)
which displays a live stream from Toronto.
More images can be found on
flickr
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:
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"))
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}']
A small python script to export your twitter friends.
The script can export the raw screen_names or export an Friend of a Friend
(foaf) RDF file.
If you want to use the foaf type export you need to install the python RDF
library and have a valid twitter user account to access the API. The
python-twitter library is needed anyway.
Usage:
python twitterFriends.py user
python twitterFriends.py -u yourusername -p yourpassword -t foaf user
You can download the script here:
twitterFriends.py.
guess-language is a nice python
script to determine the language of some text. Very useful:
import guess_language
import twitter
api = twitter.Api()
from collections import defaultdict
def guess(username, restrict=None):
#download tweets
tl = api.GetUserTimeline(username, count=200)
d = defaultdict(int)
for tweet in tl:
if restrict is None:
lang = guess_language.guessLanguage(tweet.GetText())
else:
lang = guess_language.guess_language.check(tweet.GetText(), restrict)
d[lang] += 1
return sorted(d.iteritems(),key=lambda x: x[1], reverse=True)
def action_guess(username=""):
"twitter screenname"
print "twitter language staistics for %s:" % username
for lang, anz in guess(username):
print "%-8s %s" %(lang, anz)
Example output for http://twitter.com/chrismarquardt
twitter language staistics for chrismarquardt:
en 129
de 67
UNKNOWN 4