Dynamically create classes for spark-parser-framework
__peter__ at web.de
Mon Dec 15 20:59:40 CET 2003
Diez B. Roggisch wrote:
> I've got to create some spark-based parsers during runtime. For people not
> familiar with spark, a parser looks like this:
> class MixFixParser(spark.GenericParser):
> def __init__(self, start='p_start'):
> spark.GenericParser.__init__(self, start)
> def p_rules(_, args):
> p_start ::= p_op p_start
> p_start ::=
> p_op ::= p_start lbracket p_start rbracket
> return args
> The framework looks for methods beginning with p_ and inspects its
> docstring for grammar rules.
> I hope that the body of my p_rules will be uniform, so until now I'd like
> to have one paser baseclass like MixFixParser above, and then modify the
> docstring accordingly - only for one specified instance!
> My first attempts failed due to
> AttributeError: 'instancemethod' object attribute '__doc__' is read-only
If a writable __doc__ really is sufficient (which I doubt, while I know
nothing about spark):
>>> class T:
... def demo(self, x):
... print "demo(%r)" % x
>>> class Method:
... def __init__(self, func):
... self.func = func
... def __call__(self, *args):
>>> t = T()
>>> t.demo = Method(t.demo)
>>> t.demo.__doc__ = "so what"
>>> t.demo.__doc__ = "something else"
Maybe you can expand on this, e. g. doing self.method = Method(self.method)
for every method starting with "p_" in the constructor.
More information about the Python-list