By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,285 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,285 IT Pros & Developers. It's quick & easy.

Language Work Benches in Py

P: n/a
Hi,
I recently read Martin Fowler's article on language workbenches and
domain specific
languages(http://www.martinfowler.com/articles...orkbench.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.blogsp...hands-in.html).

I also made a screen cast of the same
(http://openenterpriseafrica.com/neo/...ython.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)

Sep 3 '05 #1
Share this Question
Share on Google+
1 Reply


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

#DSL data
#1234567890123456789012345678901234567890123456789 01234567890,

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)

Sep 7 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.