Crazy Python literate programming + DSL idea

Posted on

Was in the middle of writing a DSL for unit testing a somewhat complex piece of code and then thought of extending Python syntax with (noop) keywords such as just, even, only, but, again etc. This would allow one to write intention-oriented code:

expect_condition(some_param=3, other_param=4)
expect_condition(some_param=now 10, other_param=still 4)
expect_condition(some_param=again 3, other_param=still 4)
but not expect_condition(some_param=20, other_param=20)
and not even expect_condition(some_param=200, other_param=200)

…or even some ad-hoc extensible syntax, which would allow to add such literate markers anywhere in the code.
—Should be easy to add to the (CPython or PyPy) parser.

The next level would be to do metaprogramming assertions… i.e. if you have other_param=4 and then other_param=still 5, a custom analyser would be able to detect the flaw; something called an Intention Mismatch Detector!

Could be a potential start to a magic but (arguably) Pythonic alternative to Ruby’s more uncontrolled DSL writing power.

Leave a Reply

Your email address will not be published.