Python and Jongo

For some reason or another, I've always found Python support for RDBMS lacking behind Java, specially in the ORM area. Just imagine, there are no officially supported Python drivers to connect to an Oracle database and you must install the Oracle client... ugh!

Anyway, by having a Jongo server connected to your database you don't have to worry about installing any Python modules, you'll only need a standard Python 2.4+ installation with simplejson or Python 2.5+ which already support JSON.

Let's see, for example, an application which accesses the CIDB database and the Car table:

import jongo

class Car(jongo.JongoModel):
    def __init__(self, id=None, model=None, maker=None, fuel=None, transmission=None, year=None):
        jongo.JongoModel.__init__(self)
        self.id = id
        self.idCol = "cid"
        self.model = model
        self.maker = maker
        self.fuel = fuel
        self.transmission = transmission
        self.year = year

class CarStore(jongo.JongoStore):
    def __init__(self):
        jongo.JongoStore.__init__(self)
        self.model = Car
        self.proxy = jongo.Proxy("localhost:8080","cidb","car", Car)

if __name__ == '__main__':
    carstore = CarStore()
    carstore.load()
    for car in carstore.data:
        print car

    c1 = Car(None, "206cc", "Peugeot", "Gasoline", "Manual", 2008)
    carstore.add(c1)
    carstore.sync()

    c1 = carstore.getAt(carstore.count() - 1)
    c1.set('model', "206")
    c1.set('maker', "PPegoushn")
    carstore.update(c1)
    carstore.sync()

    # We need to refresh the object since it has changed after the sync
    c1 = carstore.getAt(carstore.count() - 1)
    carstore.remove(c1)
    carstore.sync()

Since this is Python, the code is pretty straightforward and self documented, right? For more information and examples on working with Python and Jongo check this page.

No hay comentarios:

Publicar un comentario