Source

Immutabase / demo1.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: eph


from immutabase import *


##### the burning stage #####

class Country(object):
    def __init__(self, name, abbr, code):
        self.name = name
        self.abbr = abbr
        self.code = code
    def __getstate__(self):
        return self.name, self.abbr, self.code
    def __setstate__(self, data):
        self.name, self.abbr, self.code = data
        self.name.rstrip('\0')

db_countries = Immutabase('countries.db', 'w', entry=InstanceEntry(cls=Country, fmt='25s3sI'))

db_countries['China'] = Country('China', 'CHN', 86)
db_countries['United States'] = Country('United States', 'USA', 1)
db_countries['Japan'] = Country('Japan', 'JPN', 81)

db_city2country = Immutabase('city2country.db', 'w', entry=ReferenceEntry(db_countries))
db_city2country.insert('China', 'Beijing')
db_city2country.insert('China', 'Shanghai')
db_city2country.insert('United States', 'New York')

db_city2country.close()
db_countries.close()


##### the query stage #####

db_countries = Immutabase('countries.db', entry=InstanceEntry(cls=Country, fmt='25s3sI'))
db_city2country = Immutabase('city2country.db', entry=ReferenceEntry(db_countries))

print db_city2country['Shanghai'].code
print db_city2country['New York'].abbr