semi-concatenated strings

Steve Holden sholden at holdenweb.com
Sun Jun 2 14:08:41 EDT 2002


"Skip Montanaro" <skip at pobox.com> wrote in message
news:mailman.1022874291.30340.python-list at python.org...
>     >> rows = self.executesql(("select cities.city, state, country"
+
>     >>                         "  from cities, venues, events, addresses"
+
>     >>                         "  where cities.city like %s"
+
>     >>                         "    and events.active = 1"
+
>     >>                         "    and venues.address = addresses.id"
+
>     >>                         "    and addresses.city = cities.id"
+
>     >>                         "    and events.venue = venues.id"),
>     >>                        (city,))
>
>     Grant> I dunno...I don't know anything about SQL, but your code looks
>     Grant> sortta Pythonic.
>
> Yeah it does.  It serves two purposes.  One it makes it clear to me where
> the various clauses of the select statement begin ("from ...", "where
...").
> It also guarantees I have at least one space between the different
> fragments.  This:
>
>     ("select cities.city, state, country"
>      "from cities, venues, events, addresses"
>      "where cities.city like %s"
>      "and events.active = 1"
>      "and venues.address = addresses.id"
>      "and addresses.city = cities.id"
>      "and events.venue = venues.id")
>
> might appear correct, but there are no spaces between the end of one
> substring and the start of the next...
>
I would personally much prefer to see this written as

    ("""select cities.city, state, country
     from cities, venues, events, addresses
     where cities.city like %s
         and events.active = 1
         and venues.address = addresses.id
         and addresses.city = cities.id
         and events.venue = venues.id""",
    (city,))

Perhaps the reason that Python's conventional wisdom states "there should be
oine obvious way to do it" is because, given alternatives, people like you
and me will spend endless time debating which is to be preferred!

regards
 Steve
--
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------








More information about the Python-list mailing list