[Python-ideas] Why shouldn't Python be better at implementing Domain Specific Languages?

David Mertz mertz at gnosis.cx
Thu Aug 30 22:04:13 EDT 2018


On Thu, Aug 30, 2018 at 9:41 PM Guido van Rossum <guido at python.org> wrote:

> On Fri, Aug 31, 2018 at 3:19 AM, Michael Selik <mike at selik.org> wrote:
>
>> On Thu, Aug 30, 2018 at 5:31 PM James Lu <jamtlu at gmail.com> wrote:
>>
>>> It would be nice if there was a DSL for describing neural networks
>>> (Keras).
>>>
>>> model.add(Dense(units=64, activation='relu', input_dim=100))
>>> model.add(Dense(units=10, activation='softmax'))
>>>
>>>
>> Why not JSON or XML for cross-language compatibility?
>>
>
> Presumably because those are even harder to read and write for humans.
>

Guido is absolutely right (as usual) that JSON or XML would be *vastly*
harder to read than that very clean Python code.

That said, if you wanted a "DSL", the perfect choice would be YAML.  The
description above could look like this:

>>> model = '''
... model:
...   - layer: Dense
...     units: 64
...     activation: relu
...     input_dim: 100
...   - layer: Dense
...     units: 10
...     activation: softmax
... '''
>>> yaml.load(model)
{'model': [{'layer': 'Dense', 'units': 64, 'activation': 'relu',
'input_dim': 100}, {'layer': 'Dense', 'units': 10, 'activation':
'softmax'}]}


This format could easily be either a string within a Python program or an
external file with the definition.  YAML is already well supported in
Python, all you'd need to do is write a little wrapper to translate the
description above into the actual Keras API calls, which would be pretty
easy.


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180830/5b495901/attachment.html>


More information about the Python-ideas mailing list