

How do we import a module in Python if we don’t know the module name ahead of time? Typically, when we’re writing code, we know the module name we want to import and just enter import module1.īut with our DSL, we have the module name as the first item in a list as a string value.

Now that we have these values, we can call the function in our module with its arguments.īut this brings up a new challenge. These token values, in the list “parts”, represent the module name, function name and function arguments. With just a few lines of code, we were able to get a list of tokens from a line in our source file. If you run into errors, check the Python FAQ. For Windows, it should work with a default Python installation. You can do this from your shell by running chmod +x dsl1.py. This will allow you to run your application as a command-line command. If you’re using macOS or Linux, remember to make “dsl1.py” executable if it’s not already. So the first version of our DSL will look like this: Since the users need to be able to run our Python code, they need to be able to specify the module name, function name and any arguments the function might accept. What’s the simplest version we could make? Let’s make this more concrete by deciding what the first version of our DSL will be able to do.
#Python dsl strongpassword data analysis full
If you ever get stuck, you can find the full source code for this tutorial on GitHub.

I want to show you how easy it is to use Python to write a DSL that you can adapt for your own use in your projects.Įven if you don’t have a direct use for a DSL today, you may pick up some new ideas or bits of the language that you haven’t seen before. We’re going to write a simple DSL in Python that’s generic in nature that uses other Python source files to do some work. A DSL doesn’t have to be complex or involve studying parser theory and abstract syntax trees. This isn’t a subject reserved for advanced programmers. Perhaps you haven’t heard of a DSL before. Writing a Domain Specific Language (DSL) may sound difficult-like something that’s really hard and should only be done by advanced programmers. Any other line starts with the module name, then the function name followed by its arguments, separated by spaces.Īs you’ll see in the course of this tutorial, even a simple language like this can offer a lot of flexibility and make your Python applications “scriptable.” What You’ll Learn in This Tutorial With the DSL you’ll create in this tutorial you’ll be able to call Python functions and pass arguments to them using a syntax that resembles assembly language.īlank lines or comment lines that start with “#” are ignored, just like Python. # This is a comment module1 add 1 2 module2 sub 12 7 module1 print_results
