[Baypiggies] Discussion for newbies/beginner night talks

Doug Landauer zia at cruzio.com
Sat Feb 10 09:27:04 CET 2007


For what it's worth, here's my version.  It was inspired by Hal 
Fulton's "margin" method from his Ruby book.  It works at runtime, and 
isn't as strict as the cookbook example that Michael Bernstein 
mentioned ( 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/145672 ), but 
it's short and a bit easier to use (less cluttered-looking at usage 
site) than Chad's more efficient compile-time version.

import re
undent_pat = re.compile( r"(?m)^\s*\S(.*)$", re.M  )
def undent (str):
     return undent_pat.sub( r'\1', str.rstrip() )

# Example:

def getCustomerInfo(cust_id):
     sql  = undent( '''\
             |select customers.name as name,
             |        sum(invoices.amount) as amount_total,
             |        blah as blah
             |   from customers
             |   etc...
             ''')

Maybe I ought to add it to that cookbook entry.

  -- Doug L.


On Feb 9, 2007, at 5:43 PM, Chad Netzer wrote:

> n 2/9/07, Dennis Reinhardt <DennisR at dair.com> wrote:
>>
>> To preserve SQL *and* Python structuring, I would write this as:
>>
>> class MyBizobj(...):
>>      def getCustomerInfo(self, cust_id):
>>         sql  = ""
>>         sql += "select customers.name as name,\r\n"
>>         sql += "        sum(invoices.amount) as amount_total,\r\n"
>>         sql += "        blah as blah\r\n"
>>         sql += "   from customers\r\n"
>>         sql += "   left join invoices\r\n"
>>         sql += "     on invoices.cust_id = customers.id\r\n"
>>         sql += "  where customers.id = ?\r\n"
>>          self.cur.execute(sql, (cust_id,))
>
> Another alternative (which does the concatenation at compile time) is:
>
> class MyBizobj(...):
>     def getCustomerInfo(self, cust_id):
>         sql  = (
>             "select customers.name as name,\r\n"
>             "        sum(invoices.amount) as amount_total,\r\n"
>             "        blah as blah\r\n"
>             "   from customers\r\n"
>             "   etc..."
>         )
>
> Chad
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>



More information about the Baypiggies mailing list