Dynamically create classes for spark-parser-framework

D

Diez B. Roggisch

Hi,

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

Of course I could create one big string, with placeholders for class-name,
doc-string contents and maybe even methodbody and evaluate it, but somehow
this strikes me as unelegant - first of all, I lose emacs indention support
for the code I write, and secondly I'd like the idea of manipulating only
as few as possible - I don't _need_ distinct classes, only instances with
differing parser-rules.

I'll also have to check if spark allows for second-time or delayed calling
of __init__, so that my altered rules get recognized - however, I think
that could be done, come hell or highwater.

Thanks for any suggestions,

Diez
 
P

Peter Otten

Diez said:
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):
.... def demo(self, x):
.... print "demo(%r)" % x
........ def __init__(self, func):
.... self.func = func
.... def __call__(self, *args):
.... self.func(*args)
....'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.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top