[Python-ideas] proposal to add rowexpr as a keyword

Steve Howell showell30 at yahoo.com
Tue May 29 14:07:48 CEST 2007


ABSTRACT:

I propose the following syntax:

   rowexpr: convert_to_euros(salary) > 50000 and
deptno = 10

Rowexpr returns a function object that can be called
with one argument, row, and the items "salary" and
"deptno" are used to derefence row.

RATIONALE:

A lot of time people write Python code that works with
other small interpreters, such as SQL engines, math
libraries, etc., that have an expression syntax of
their own.  To use SQL as an example, I may want my
SQL engine to use this Python expression:

   where convert_to_euros(salary) > 50000 and deptno =
10

To interact with SQL engines, I could imagine code
like the following:
 
  sql(..., lambda row: convert_to_euros(row['salary']
> 50000 and row['deptno'] = 10) # not concise

  sql(..., sql_parse('convert_to_euros(salary) > 50000
and deptno = 10') # requires sophisticated library

  sql(..., [and_op,
[ge_op(func_call_op(convert_to_euros, 'salary'),
50000), eq_opt(itemgetter_op('deptno'), 10)]) #
UGLY!!!



SOLUTION:

Let the Python interpreter build expression objects
for you.  You write:

   rowexpr: convert_to_euros(salary) > 50000 and
deptno = 10

Python translates that into the same bytecode as:

lambda row: convert_to_euros(row['salary'] > 50000 and
row['deptno'] == 10

Benefits:

    1) Allows smaller code.
    2) Having Python translate the rowexpr creates an
opportunity for better error messages, catching more
things at startup time, etc.
    3) In the particular case of SQL, it enable the
development methodology that you might start off by
writing code that processes data in memory, but with
mostly SQL-like syntax, and then switch over to a real
SQL database as scalability becomes a bigger concern.

COMMON OBJECTIONS:

   1) More syntax, of course.

   2) Folks used to SQL would still need to adjust to
differences between Python expressions (==) and SQL
(=).

   3) In the case of SQL, people may already be happy
enough with current tools, and of course, interpreters
external to Python can cache expressions, etc.

OPEN QUESTIONS:

Would a rowexpr be limited to dictionaries, or could
it also try to access object attributes?




       
____________________________________________________________________________________Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
http://answers.yahoo.com/dir/?link=list&sid=396545469



More information about the Python-ideas mailing list