Inheriting module-level attributes, methods in derived module

Jeff Kowalczyk jtk at yahoo.com
Tue Jul 30 12:20:48 EDT 2002


I need to make a product that derives from Formulator (MyFormulator),
but allows me to selectively override certain methods. For example,
the only thing I need to change in FieldRegistry is the
initializeFieldForm() method. The FieldRegistry singleton should be
distinct from the original Formulator's one, so I can register a
different set of fields.

http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/formulator/Formulator/

Products\Formulator\FieldRegistry.py
-----------------------------------------------
(...)
class FieldRegistry:
    """A registry of fields, maintaining a dictionary with
    the meta_type of the field classes as key and the field class as
    values. Updates the Form as necessary as well.
    """
    def __init__(self):
        """Initializer of FieldRegistry.
        """
        self._fields = {}
(...)
# initialize registry as a singleton
FieldRegistry = FieldRegistry()

def initializeFieldForm(field_class):
    """Initialize the properties (fields and values) on a particular
    field class. Also add the tales and override methods.
    (...)
----------------------------------------------

How would  be set up? I'm new to all this, and formulator is a
complicated heirarchy to begin with. Do I need to derive a new
FieldRegistry to modify a module-level method, or to ensure that
I have a distinct singleton instance?

Products\MyFormulator\FieldRegistry.py
----------------------------------------------
from Products.Formulator.FieldRegistry import FieldRegistry
as BaseFieldRegistry

class FieldRegistry(BaseFieldRegistry):
    pass

# initialize registry as a singleton - Do I need this?
FieldRegistry = FieldRegistry()

def initializeFieldForm(field_class):
    """My new Doc string"""
    PossiblyCallBaseInitializeFieldForm(()
    # How do I? Namespace refs this FieldRegistry singleton?
    (write my own follow-on or complete replacement code here)
---------------------------------------------

Gives me errors like:
  File C:\(...)\Products\MyFormulator\FieldRegistry.py, line 3, in ?
TypeError: __init__() takes exactly 1 argument (4 given)

Or, an alternate way I was thinking it might need to be:
----------------------------------------------
from Products.Formulator.FieldRegistry import FieldRegistry
as BaseFieldRegistry

# initialize registry as a singleton
FieldRegistry = BaseFieldRegistry.FieldRegistry()

def initializeFieldForm(field_class):
    """Initialize the properties (fields and values) on a particular
    field class. Also add the tales and override methods.
----------------------------------------------
File C:\(...)\Products\Formulator3\FieldRegistry.py, line 4, in ?
AttributeError: FieldRegistry instance has no attribute 'FieldRegistry'

I guess I just don't grok module imports yet.

Thanks for any advice, I just need to have a few key concepts spelled
out for me, and I'll be off and running here. A couple more questions
to follow...








More information about the Python-list mailing list