The Python Oracle

Are there technical reasons a Ruby DSL like RSpec couldn't be rewritten in Python?

This video explains
Are there technical reasons a Ruby DSL like RSpec couldn't be rewritten in Python?

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Beneath the City Looping

--

Chapters
00:00 Question
02:36 Accepted answer (Score 10)
05:24 Answer 2 (Score 5)
05:59 Answer 3 (Score 5)
06:33 Answer 4 (Score 2)
07:00 Thank you

--

Full question
https://stackoverflow.com/questions/7079...

Question links:
[DSL]: http://en.wikipedia.org/wiki/Domain-spec...
[RSpec]: http://rspec.info/
[Running away from RSpec]: http://permalink.gmane.org/gmane.comp.py...
[RSpec]: http://rspec.info/
[RSpec]: http://rspec.info/
[Running away from RSpec]: http://permalink.gmane.org/gmane.comp.py...

Accepted answer links:
[Jasmine]: http://pivotal.github.com/jasmine/
[Pyccuracy]: https://github.com/heynemann/pyccuracy
[doctest]: http://docs.python.org/library/doctest.h...
[Twill]: http://twill.idyll.org/
[reStructuredText]: http://docutils.sourceforge.net/rst.html
[Sphinx]: http://sphinx.pocoo.org/

Answer 2 links:
[https://gist.github.com/2029866]: https://gist.github.com/2029866

Answer 3 links:
[pyparsing]: http://pyparsing.wikispaces.com/
[Parsley]: https://pypi.python.org/pypi/Parsley

Answer 4 links:
[https://github.com/nestorsalceda/mamba]: https://github.com/nestorsalceda/mamba
[https://github.com/jaimegildesagredo/exp...]: https://github.com/jaimegildesagredo/exp...
[https://github.com/jmvrbanac/Specter]: https://github.com/jmvrbanac/Specter
[http://specter.readthedocs.io/en/latest/...]: http://specter.readthedocs.io/en/latest/...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #ruby #rspec #dsl

#avk47



ACCEPTED ANSWER

Score 10


If I had to point out one great difficulty for creating a Python RSpec, it would be the lack of a good syntax in Python for creating anonymous functions (as in JavaScript) or blocks (as in Ruby). The only option for a Python programmer is to use lambdas, which is not an option at all because lambdas just accept one expression. The do ... end blocks used in RSpec would have to be written as a function before calling describe and it, as in the example below:

def should_do_stuff():
    # ...
it("should do stuff", should_do_stuff)

Not so sexy, right?

There are some difficulties in creating the should methods, but I bet it would be a smaller problem. Actually, one does not even need to use such an unusual syntax—you could get similar results (maybe even better, depending on your taste) using the Jasmine syntax, which can be trivially implemented.

That said, I feel that Python syntax is more focused on efficiently representing the usual program components such as classes, functions, variables, etc. It is not well suited to be extended. I, for one, think that a good Python program is one where I can see objects, and functions, and variables, and I understand what each one of these elements do. Ruby programmers, OTOH, seem to seek for a more prose-like style, where a new language is defined for a new problem. It is a good way of doing things, too, but not a Pythonic way. Python is good to represent algorithms, not prose.

Sometimes it is a draconian limit. How could one use BDD for example? Well, the usual way of pushing these limits in Python is to effectively write your own DSL, but it should REALLY be another language. That is what Pyccuracy is, for example: another language for BDD. A more mainstream example is doctest. (Actually, if I would write some BDD Python library, I would write it based on doctest.) Another example of Python DSL is Twill. And yet another example is reStructuredText, used in Sphinx.

Summarizing: IMHO the hardest barrier to DSLs in Python is the lack of a flexible syntax for creating anonymous functions. And it is not a fault*: Python is not fond of having its syntax heavily explored anyway—it is considered to make code less clear in the Python universe. If you want a new syntax in Python you are well advised to write your own language, or at least it is the way I feel.

* Or maybe it is - I have to confess that I miss anonymous functions. However, I recognize that they would be hard to implement elegantly given the Python semantic indentation.




ANSWER 2

Score 5


One of Ruby's strengths is in the creation of DSLs. However the reasons given for it being difficult in python can be sidestepped. For example you can easily subclass the builtin types, e.g:

>>> class myint(int):  pass
>>> i = myint(5)
>>> i
5

If I were going to create a DSL in python I'd use pyparsing or Parsley and something like the above behind the scenes, optimizing the syntax for the problem, not the implementation language.




ANSWER 3

Score 5


I set out on an attempt to implement something like rspec in Python.

I got this:

with It('should pass') as test:
    test.should_be_equal(1, 1)

source: https://gist.github.com/2029866

(thoughts?)

EDIT: My answer to your question is that the lack of anonymous blocks prevents a Ruby DSL like RSpec from being rewritten in Python but you can get a close approximation using with statements.




ANSWER 4

Score 1


I think this is what you are looking for. Yes, we made the "impossible" in python "sure" is an utility belt for expressive python tests, created by Gabriel Falcão