[Python-ideas] Proposal: Query language extension to Python (PythonQL)

Michel Desmoulin desmoulinmichel at gmail.com
Sat Mar 25 15:43:06 EDT 2017


Hello,

I've been following PythonQL with interest. I like the clever hack using
Python encoding. It's definitely not something I would recommend to do
for an inclusion in Python as it hijack the Python encoding method,
which prevent your from... well choosing an encoding. And requires to
have a file.

However, I find the idea great for the demonstration purpose. Like LINQ,
the strength of your tool is the integrated syntax. I myself found it
annoying to import itertools all the time. I eventually wrote a wrapper
so I could use slicing on generators, callable in slicing, etc for this
very reason.

However, I have good news.

When the debate about f-strings was on this list, the concept has been
spitted in several parts. The f-string currently implemented in Python
3.6, and an more advanced type of string interpolation: the i-string
from PEP 501 (https://www.python.org/dev/peps/pep-0501/) that is still
to be implemented.

The idea of the i-string was to allow something like this:

mycommand = sh(i"cat {filename}")
myquery = sql(i"SELECT {column} FROM {table};")
myresponse = html(i"<html><body>{response.body}</body></html>")

Which would then pass an object to sql/sh/html() with the string, the
placeholders and the variable context then allow it to do whatever you
want. Evaluation of the i-string would of course be lazy.

So while I don't thing PythonQL can be integrated in Python the way it
is, you may want to champion PEP 501. This way you will be able to
provide a PQL hook allowing you to do something like:

pql(i"""select (x, sum_y)
           for x in range(1,8),
               y in {stuff}
           where x % 2 == 0 and y % 2 != 0 and x > y
           group by x
           let sum_y = sum(y)
           where sum_y % 2 != 0
""")

Granted, this is not as elegant as your DSL, but that would make it
easier to adopt anywhere: repl, ipython notebook, files in Python with a
different encoding, embeded Python, alternative Python implementations
compiled Python, etc.

Plus the sooner we start with i-string, the sooner editors will
implement syntax highlighting for the popular dialects.

This would allow you to spread the popularity of your tool and maybe
change the way it's seen on this list.


More information about the Python-ideas mailing list