Am a ruby guy basically, and got into a situation where I need to make a small dsl in py as follows, I know in ruby following is doable, am looking for exactly same in py
from_a_dsl_file = "
take_this 'abc'
and_process_it
and_give_me_the_output
"
class A
def take_this abc
end
def and_process_it
end
def and_give_me_the_output
'desired'
end
end
A.new.instance_eval from_a_dsl_file
# => 'desired'
Any hints, or great to have a working example please
thanks in advance
As I understand it, in Ruby there are some tricky things you can do with function calls that don’t require parentheses:
In Ruby, that could be a function call where function
xis called withyfor the argument.Well, in Python, we don’t have those tricks; if you are calling a function, you need parentheses after the function name. So, I don’t think you will have much luck trying to play games with
eval()for this.Better is just to write a parser that parses the language for you and figures out what the language is trying to do. For that, Python has a particularly good library:
pyparsinghttp://pyparsing.wikispaces.com/
P.S. Just doing a Google search for “Python domain specific language” finds some good links, many in StackOverflow. Here is the best one I found:
Mini-languages in Python
EDIT: Okay, you asked for an example and here you go. This is my first-ever program in PyParsing and it was pretty easy. I didn’t even read the documentation; I just worked from the examples in a presentation I found on the web.
Here’s the URL of the presentation: http://www.ptmcg.com/geo/python/confs/TxUnconf2008Pyparsing.html