[Python-ideas] Assignment decorators (Re: The Descriptor Protocol...)
Cesare Di Mauro
cesare.di.mauro at gmail.com
Fri Mar 11 07:38:30 CET 2011
2011/3/11 MRAB <python at mrabarnett.plus.com>
>
> Talking about different semantics, I had an (admittedly vague) idea
> about passing the body of a def statement to a builder as a string at
> runtime, allowing 'foreign' code to be embedded more easily.
>
> In a statement of the form:
>
> def name(...) from builder:
> ...
>
> the body of the def wouldn't be parsed by Python at compile time, but
> would, as I said, be passed to the builder as a string at runtime. The
> builder would be able to parse the string, compiling it to a callable
> object with hooks into Python so that it could access variables, call
> functions, etc, when it was actually executed.
>
> For example, this:
>
> c.execute("""insert into stocks values (?, ?, ?, ?, ?)""",
> ('2006-01-05', 'BUY', 'RHAT',100, 35.14))
>
> could become (assuming that sqlite3 had a magic __compile__ function):
>
> def insert(date, trans, symbol, qty, price) from sqlite3:
> insert into stocks values (date, trans, symbol, qty, price)
>
> insert('2006-01-05', 'BUY', 'RHAT',100, 35.14)
>
> When run, the SQL code would fetch the values of the parameters as
> needed.
>
> Actually, a closer match to the original code would be:
>
> c.insert('2006-01-05', 'BUY', 'RHAT',100, 35.14)
>
> Clearly it's a method, so the instance would be passed as 'self',
> something like this:
>
> def insert(self, date, trans, symbol, qty, price) from sqlite3:
> insert into stocks values (date, trans, symbol, qty, price)
>
> but what happens to 'self' within the definition? It's not referred to
> in the SQL statement itself.
I already do something like this:
DB.stocks += '2006-01-05', 'BUY', 'RHAT',100, 35.14
DB.stocks -= ID == 123
DB.stocks[ID == 123] = qty == qty + 1, price == 46.51
print DB.stocks[date,
'COUNT(*)'].GroupBy[date].OrderBy[date].Where[date['2011-01-01' :
'2011-02-28']].List()
with Python 2.5+.
So there's no need for a new syntax to implement such things in a "pythonic"
way.
However some changes can improve ORM-like code. For example, len() always
checks that __len__ returns an integer, but "relaxing" it can help a
bit(actually I revert to something like this: symbol.len()).
Cesare
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110311/3a1dd4fa/attachment.html>
More information about the Python-ideas
mailing list