Language Work Benches in Py

Y

yoda

Hi,
I recently read Martin Fowler's article on language workbenches and
domain specific
languages(http://www.martinfowler.com/articles/languageWorkbench.html).
I then had the pleasure of reading Rainer Jowsig's implementation of
the sample in Lisp(http://lispm.dyndns.org/news?ID=NEWS-2005-07-08-1).

The lisp code was so sexy that I was inspired to write a sample in
Python. I'm relatively new to coding in Python so I'd love any barbs,
comments or criticisms about the code. You can find my article here :
(http://billionairebusinessman.blogspot.com/2005/09/drop-that-schema-and-put-your-hands-in.html).

I also made a screen cast of the same
(http://openenterpriseafrica.com/neo/blogs/010905/dsl-in-python.wmv.bz2).
Unfortunately, I had to make it using a windows machine so it's encoded
as wmv. (If anyone finds it useful and is inspired to encode it in a
more palatable format e.g. mov, I'd be honoured to create a torrent and
host it)
 
Y

yoda

I realize that I forgot to post the sample code:). Below is my
implementation:

#DSL data
#123456789012345678901234567890123456789012345678901234567890,

dsldata=(
'SVCLFOWLER 10101MS0120050313.........................',
'SVCLHOHPE 10201DX0320050315........................',
'SVCLTWO x10301MRP220050329..............................',
'USGE10301TWO x50214..7050329...............................')

#Class mappings
Mappings={'svcl':{
(4,18):'CustomerName',
(19,23):'CustomerID',
(24,27) :'CallTypeCode',
(28,35) : 'DateOfCallString'},
'usge':{(4,8) :'CustomerID',
(9,22):'CustomerName',
(30,30):'Cycle',
(31,36): 'ReadDate'}}


def generateClass(data):
'generate the class and instance with attributes'

className = data[:4].lower() #1)
mappingData= Mappings[className] #2)
class Klass:pass #3)
Klass. __name__=className #4)
# print Klass

for key in mappingData.keys(): #5)
fielddata=data[key[0]:key[1]]
print 'actual data->',fielddata
setattr(Klass,mappingData[key],fielddata) #6)
return Klass


def parseData():
'parse the data and generate a list of objects'
classes= [generateClass(item) for item in dsldata]
return classes

def printKlassData(Klass):
print Klass
for key in Klass.__dict__:
print ('attr->%s value->%s')%(key,Klass.__dict__[key])

if __name__=='__main__':
for Klass in parseData():
printKlassData (Klass)
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top