This weekend, I created library to aid developers in creating neat little embedded DSLs when using python, without having to do any complex parsing or anything like that. The resulting DSLs look a bit more like english than python does.
The idea for this was inspired by some ruby stuff that I’ve been using lately. I’ve been using ruby quite a bit lately, and while I am still not a huge fan of the language, I do like the idea of easy to code up DSLs that I can use to populate objects without too much effort. Since I wanted a DSL for a python project I was working on, I played with a few ideas and ended up with this tool.First, create some very basic python objects, and a file with the DSL to pass to dsltool. Here’s a sample recipe DSL for baking yummy cookies.
author = 'Mom' level = 'Easy' with Instructions('Yummy cookies'): preheat_oven = '375F' with Ingredients: add += '2 1/4 cups', 'flour' add += '1 tsp', 'baking soda' add += '1 tsp', 'salt' add += '1 cup', 'butter' add += '3/4 cup', 'sugar' add += '3/4 cup', 'brown sugar' add += '1 tsp', 'vanilla extract' add += '2', 'eggs' add += '2 cups', 'chocolate chips' add += '1 cup', 'nuts'
And the implementation to parse that dsl is super simple:
import dsltool class Ingredients(object): def __init__(self): self._items =  @property def add(self): return dsltool.add_to_list(self._items) @add.setter def add(self, value): self._items = value class Instructions(object): preheat_oven = None ingredients = Ingredients steps = Steps def __init__(self, name): self.name = name class Recipe(object): author = None level = None def __init__(self): self.instructions = Instructions if __name__ == '__main__': obj = dsltool.parse_dsl_file('cookies.pydsl', Recipe)
pip install pydsltool
Let me know if you find it useful!