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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.