[Python-Dev] PEP 30XZ: Simplified Parsing

Steve Holden steve at holdenweb.com
Fri May 4 21:00:38 CEST 2007


skip at pobox.com wrote:
>     Trent> But if you don't want the EOLs? Example from some code of mine:
> 
>     Trent>      raise MakeError("extracting '%s' in '%s' did not create the "
>     Trent>                      "directory that the Python build will expect: "
>     Trent>                      "'%s'" % (src_pkg, dst_dir, dst))
> 
>     Trent> I use this kind of thing frequently. Don't know if others
>     Trent> consider it bad style.
> 
> I use it all the time.  For example, to build up (what I consider to be)
> readable SQL queries:
> 
>     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,))
> 
> I would be disappointed it string literal concatention went away.
> 
Tripe-quoted strings are much easier here, and SQL is insensitive to the 
newlines and additional spaces. Why not just use

     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,))

It also gives you better error messages from most database back-ends.

I realise it makes the constants slightly longer, but if that's an issue 
I'd have thought people would want to indent code with tabs and not spaces.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com        squidoo.com/pythonology
tagged items:         del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------



More information about the Python-Dev mailing list