Language Work Benches in Py

Discussion in 'Python' started by yoda, Sep 4, 2005.

  1. yoda

    yoda Guest

    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)
    yoda, Sep 4, 2005
    #1
    1. Advertising

  2. yoda

    yoda Guest

    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)
    yoda, Sep 7, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Spur
    Replies:
    4
    Views:
    2,098
  2. Ed
    Replies:
    24
    Views:
    970
    Dimitri Maziuk
    Mar 27, 2006
  3. DaveInSidney
    Replies:
    0
    Views:
    396
    DaveInSidney
    May 9, 2005
  4. chaitu
    Replies:
    2
    Views:
    778
  5. pabbu
    Replies:
    8
    Views:
    705
    Marc Boyer
    Nov 7, 2005
Loading...

Share This Page