On Thu, May 2, 2013 at 6:20 PM, Arnon Marcus <[email protected]> wrote:
> I agree.
>
> Here is a more concrete explanation.
>
> Given the following tables:
>
> db.define_table('Continent', Field('Name', 'string'))
> db.define_table('Country', Field('Name', 'string'), Field('Continent',
> db.Continent))
> db.define_table('City', Field('Name', 'string'), Field('Country',
> db.Country))
>
> Using an ORM, you could do something like:
>>>> Country(Name='France').City.list
> [<City Name:Paris>, <City Name:Nice>]

Sorry, but what should do .list() ?
it is a query? it is pre-fetched or cached?
What about if I need the cities of countries where main language is
not Spanish, and population is above 1 millon?

Please, note that you are mixing a declarative query syntax (DAL),
with an imperative one (ORM)

> Using the DAL, the best you might get is:
>>>> [city for city in db.City.Country.select() if city.Country.Name ==
>>>> ''France']
> [<Row Name:Paris>, <Row Name:Nice>]

I would like a elegant declarative syntax similar to LINQ, but in python:

[city for db.City.ALL in db.City, db.Country if db.Country.Name ==
'France' and db.Country.id == db.City.country]

Sadly, there is NO possible way to archive a pythonic list
comprenhension syntax that query the objects in the server side AFAIK
Some libraries uses dis (python disassembler) and other internal &
dirty python hacks to do something similar, please, see:

http://www.aminus.net/geniusql/

http://www.aminus.org/blogs/index.php/2008/04/22/linq-in-python?blog=2

http://www.aminus.net/geniusql/chrome/common/doc/trunk/managing.html

Note that web2py is much closer to the "pythonic expressions", but
without the early / late binding and other issues described there ;-)

>
> In an ORM on-top of the DAL, I would like to be able to do something like:
>
> @ORM(db.City)
> class City:
>     pass
>
> @ORM(db.Country)
> class Country:
>     pass
>
> @ORM(db.Continent)
> class Continent:
>     pass
>
>>>> europe = Continent(Name='Europe')
>>>> france = Country(Name='France', Continent=europe)
>>>> paris = City(Name='Paris', Country=france)
>>>> nice = City(Name='Nice', Country=france)
>>>>
>>>> europe.Country(Name='France') is france
> True

This would require caching or storing previous queried record in
memory (something like a singleton), or "is" will not work as you are
expecting (it is for checking "identity")...
That could be also archived hacking "Row", but you should use == in
python for this kind of comparision (equality)

>
> This would be so intuitive and easy to use...

For you that are creating it, but I will not understand it in a first
sight, so I prefer the DAL syntax that is a bit verbose but more
uniform
(at least I know what I'm doing in each step, or can get a SQL book
and see how to "pythonize" the expression)
A ORM is not more intuitive and will have a higher learning curve, and
no formal / logical / math justification theory  BTW

I think that a more elegant way for web2py would be research how to
put bussiness rules (and not only "tables"), in the models.
This could be done right now to some extent with virtual fields &
predefined queries (we already have represent, field validators, etc).
Implementing "stored procedures" and "triggers" in web2py also could
be interesting (and you could win a lot of speed), for example,
PostgreSQL supports PL/Python for that.

Best regards,

Mariano Reingart
http://www.sistemasagiles.com.ar
http://reingart.blogspot.com

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to