Language Work Benches in Py

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yoda

    #1

    Language Work Benches in Py

    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)

  • yoda

    #2
    Re: Language Work Benches in Py

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

    #DSL data
    #12345678901234 567890123456789 012345678901234 567890123456789 0,

    dsldata=(
    'SVCLFOWLER 10101MS01200503 13............. ............',
    'SVCLHOHPE 10201DX03200503 15............. ...........',
    'SVCLTWO x10301MRP220050 329............ ............... ...',
    'USGE10301TWO x50214..7050329 ............... ............... .')

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


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

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

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


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

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

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

    Comment

    Working...