URL Fetch API, MiniDom (Google App Engine)

Fetching stuff with the URL Fetch API is simple (especially if one has faith that the source is there and it will deliver inside GAE time limits):

from google.appengine.api import urlfetch
from xml.dom import minidom

def parse(url):
  r = urlfetch.fetch(url)
  if r.status_code == 200:
    return minidom.parseString(r.content)

As is accessing the resulting DOM with MiniDom. Here the source is an Atom feed:

import time

dom = parse(URL)
for entry in dom.getElementsByTagName('entry'):
  try:
    published = entry.getElementsByTagName('published')[0].firstChild.data
    published = time.strftime('%a, %d %b', time.strptime(published, '%Y-%m-%dT%H:%M:%SZ'))
  except IndexError, ValueError:
    pass
  …

Tagged with:

Categorised as:


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

Tagged with:

Categorised as:


pkg-config

CFLAGS_GTK = `pkg-config --cflags gtk+-2.0`
LIBS_GTK = `pkg-config --libs gtk+-2.0`

gcb-test: main.o
        $(CC) $(COMPILER_FLAGS) -o gcb-test main.o $(LIBS_GTK)

main.o: main.c
        $(CC) $(COMPILER_FLAGS) -c main.c -o main.o $(CFLAGS_GTK)

Tagged with:

Categorised as:


SQLite, prepare/step/finalize

rc =sqlite3_prepare( Db, qry_getnewid, -1, &stmt;, NULL);
if( SQLITE_OK !=rc)
  diediedie( "...");

rc =sqlite3_step( stmt);
if( SQLITE_ROW !=rc)
  {
    sqlite3_finalize( stmt);
    diediedie( "...");
  }

...

a->id =sqlite3_column_int( stmt, 0);

...

sqlite3_finalize( stmt);

Tagged with:

Categorised as:


SQLite, open db and exec sql

rc =sqlite3_open(DB_FILENAME, &Db;);
if( rc)
  diediedie( "...");

rc =sqlite3_exec( Db, tbldef_accounts, NULL, 0, &zErrMsg;);
if( SQLITE_OK !=rc)
  diediedie( zErrMsg);

...

sqlite3_close( Db);

Tagged with:

Categorised as: