Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Getting started with PyXPCOM, Part 3

Implementing XPCOM components in Python

Return to article

import anydbm
from xpcom import components
class DbHashFactory:
    _com_interfaces_ = components.interfaces.DbHashFactory
    _reg_clsid_ = "{11f39cd7-b40c-42b6-9eaa-b1082d9fcdb1}"
    _reg_contractid_ = "Python.DbHashFactory"
    #_reg_registrar_ = Setup()  #Function that would be called at registration time
    _reg_desc_ = "Factory for persistent hash implementations"
    def open(self, fname):
        dbObj = anydbm.open(fname, "w")
        db = DbHash(fname, dbObj)
        return db
    def create(self, fname):
        dbObj = anydbm.open(fname, "n")
        db = DbHash(fname, dbObj)
        return db
RESERVED_PREFIX = "_pydbhash_"
class DbHash:
    _com_interfaces_ = components.interfaces.DbHash
    _reg_clsid_ = "{b4eec552-ebed-4039-94b8-1a2d5dd2db35}"
    _reg_contractid_ = "Python.DbHash"
    _reg_desc_ = "Persistent hash implementation"
    def __init__(self, fname, dbObj):
        #This is where we initialize any general state
        self._fname = fname
        self._db = dbObj
        pass
    def keys(self):
        keys = self._db.keys()
        rp = RESERVED_PREFIX
        rpl = len(RESERVED_PREFIX)
        filtered_keys = [ k for k in keys if k[:rpl] != rp]
        return filtered_keys
    def set(self, key, value):
        self._db[key] = value
        return
    def lookup(self, key):
        if self._db.has_key(key):
            return self._db[key]
        else:
            return None
    def has_key(self, key):
        return self._db.has_key(key)
    #Attributes
    def get_fname(self):
        return self._fname
    def get_comment(self):
        return self.lookup(RESERVED_PREFIX + "comment")
    def set_comment(self, comment):
        self.set(RESERVED_PREFIX + "comment", comment)
        retu
        

Return to article