[Python-ideas] "While" suggestion

Roman Susi rnd at onego.ru
Tue Jul 29 20:42:47 CEST 2008


Andrew Akira Toulouse wrote:
> Point taken -- it's a very valid point, but at the same time building an
> unnecessary list isn't really all that good-looking. Being able to do
> query-like things is very useful, and I'd prefer to do it as close to
> the Python core as possible, rather than (for example) creating a
> in-memory sqlite database or something. LINQ, I think, somewhat
> validates this point of view. I feel that an integrated way to query
> data would be a useful addition. List comprehensions seem to be the
> natural Pythonic way to do this, but if you can think of something better...

What I am not sure about is that Python needs special syntactic
provisions for "query-like" things even though its temptative and I am
sure a lot of use cases can be facilitated (not only SQL queries but
Prolog-like, SPARQL-like, etc, etc). Because it makes the language
unnecessarily complex.

Python already has dict/list literals + list comprehensions. And
universal functional syntax could be used for the same things if only
Python easier "laziness", because all those proposed untils and while
could be then simply realized as functions.

The below is not syntax proposition, but illustrates what I mean:

my_qry = select(` from(t1=`table1), ` where(`t1.somefield < 5), ...)

Those things after backticks function select can eval if needed.

Similarly, there could be triple backtick for lazy expressions as a whole:

qry = ```select(from(t2=table), where(...), ... ```

which could be then evaluated accordind to some domain-specific language
(Python syntax for domain specific language):

res = sqlish(qry)

One obvious usage is regular expressions. Right now ``` is just r"""
for them. And sqlish() above is re.compile().

In short, I do not like ad hoc additions to the language (even inline if
 was imho a compromise), but more generic features (syntactic support for
embedded declarative/query  languages) like simple lazy evaluation. (PEP
312 "Simple implicit lambda" co-authored by me is just one example
toward same goal of simpler laziness.)

Please, do not understand me wrong. I am not against adding features you
want/need and many people who design ORMs and the likes will be glad to
see in Python. I am against making particular syntactic changes to
Python to enable those features instead of thinking on more generic
feature which would cover many cases. And I think that feature boils
down to something spiritually like Lisp's quote.

The syntax above reuses backtick dropped by Py3k to mean not just a
string to evaluate but a closure. Its possible to do those things with
lambdas even now but the readability will suffer to the degree of
unpythonic code. Also, its possible to do the same (and there are a lot
of examples like PyParsing) with specially crafted classes which
overload all operations to perform their logic in a lazy evaluation
fashion (that is, function calls are not really function calls any more
but data structure specifiers/builders).

Sorry for going too far from the original topic. I just wanted to point
out that IMHO the while/until feature is not quite good for Python as
very specific syntactic addition. I do not believe GvR will ever make
list comprehensions complete sql with wheres, groupings, orderbys,
havings, limits, even though for (SQL's from) and if (SQL's where) are
there.

Regards,
Roman

> On Tue, Jul 29, 2008 at 3:15 AM, Roman Susi <rnd at onego.ru
> <mailto:rnd at onego.ru>> wrote:
> 
>     hi!
> 
>     Jacob Rus wrote:
>     > Andrew Akira Toulouse wrote:
> 
>     ...
> 
>     > Personally, I'd rather have this broken up than in one gigantic line
>     > that looks like:
>     >
>     >     result = [int(line) for line in f if evenP(int(line))
>     >               until int(line) == 0 with open(filename) as f]
> 
> 
>     I have not followed the thread, but the line above looks cool and
>     terrible at the same time.
> 
>     I can't think what other operator can be squeezed into expression, maybe
>     try-except, eh?
> 
>         result = [int(line) for line in f try if evenP(int(line))
>                   until int(line) == 0 except ValueError("blabla")
>                   with open(filename) as f]
> 
> 
>     I am not sure if this kind of SQLness should enter Python syntax at
>     all...
> 
>     Regards,
>     Roman
> 
>     >
>     > As for:
>     >
>     >>     tokenchecks = [token for regex,token in match_tok until
>     >> regex.match(s)]     return tokenchecks[-1]
>     >
>     > This is much more readably expressed as:
>     >
>     > for regexp, token in match_tok:
>     >     if regexp.match(s):
>     >         return token
>     >
>     >
>     > Cheers,
>     > Jacob Rus
>     >
>     > _______________________________________________
>     > Python-ideas mailing list
>     > Python-ideas at python.org <mailto:Python-ideas at python.org>
>     > http://mail.python.org/mailman/listinfo/python-ideas
>     >
>     >
>     >
> 
>     _______________________________________________
>     Python-ideas mailing list
>     Python-ideas at python.org <mailto:Python-ideas at python.org>
>     http://mail.python.org/mailman/listinfo/python-ideas
> 
> 
> !DSPAM:488f4b7828356531321995!




More information about the Python-ideas mailing list