More questions on Python strings

MRAB python at mrabarnett.plus.com
Sun Aug 31 14:19:28 EDT 2014


On 2014-08-31 18:37, Dennis E. Evans wrote:
>
>    Hi
>
> I have a function that reads some meta data from a database and builds a default order by and where clause for a table.
>
> some details,
>
>    rows is a list of pyOdbc.Row and will look like this
>    [1, 'ColumnName', 3, 5]
> there will be one to n elements
>
> EmptyString, defaultColumn, defaultColumnParaMarker,
> KeyLabelPos and a couple others are all constants stings or intergers defined in another module.
>
>
> this is the function :
>
>    def ReadPkColumns(self) :
>
>      # fetch all the rows, this is the second result set in the call
>      # this will be a very small result set, typically a PK will have 1,2 or 3 columns
>      # on rare occasions a PK can have 4 or more
>      rows = self.cursor.fetchall()
>
>      # if there are no rows just assign an empty string
>      # this is probably an error in the db
>      if (len(rows) <= 0) :
>        self.PkOrderBy = EmptyString
>        self.PkWhereClause = EmptyString
>      elif (len(rows) == 1) :
>      # if there is one column then assign it no loops
>        self.PkOrderBy = defaultColumn.format(Ali=self.alias, ColLabel = rows[0][KeyLabelPos])
>        self.PkWhereClause = defaultColumnParaMarker.format(Ali=self.alias, ColLabel = rows[0][KeyLabelPos])
>      else :
>        # two or more columns build a list of strings formatted
>        # as needed for the order by and where clause
>
>        # create an empty list
>        cols = []
>        # add the formatted columns to the list
>        for oneRow in rows :
>          cols.append(defaultColumn.format(Ali=self.alias, ColLabel = oneRow[KeyLabelPos]))
>        # and build the order by clause form the formatted strings
>        self.PkOrderBy = CommaSpace.join(cols)
>
>        # clear the cols list and fill again for use with the where clause
>        cols.clear()
>        for oneRow in rows :
>          cols.append(defaultColumnParaMarker.format(Ali=self.alias, ColLabel = oneRow[KeyLabelPos]))
>        self.PkWhereClause = andConjunction.join(cols)
>
>      return
> # ------------------------------------------------------------------------------
>
>
> my question is about this block
>        # create an empty list
>        cols = []
>        # add the formatted columns to the list
>        for oneRow in rows :
>          cols.append(defaultColumn.format(Ali=self.alias, ColLabel = oneRow[KeyLabelPos]))
>        # and build the order by clause form the formatted strings
>        self.PkOrderBy = CommaSpace.join(cols)
>
>        # clear the cols list and fill again for use with the where clause
>        cols.clear()
>
>    I create an empty list and then fill it with some formatted strings and then assign to the order by instance member, clear the list and repeat for the where clause member.
>
>    I tried a different things using the join statement and the rows instance to avoid the need to create the list of string (cols) but that resulted in various syntax and run time errors.
>
>    Is the a way to build the strings with out using the intermediate list?
>
>    the end result needs to look like this,
>
>    self.OrderBy = "tableAlias.ColumnOne, tableAlias.ColumnTwo, ..."
>
>    self.WhereClause = "(tableAlias.ColumnOne = ?) and (tableAlias.ColumnTwo = ?) and ..."
>
You could use a generator comprehension:

        self.PkOrderBy = 
CommaSpace.join(defaultColumn.format(Ali=self.alias, 
ColLabel=oneRow[KeyLabelPos]) for oneRow in rows)

Does that make the code clearer? I don't think so. Or faster? Not
enough to be noticeable.

Incidentally, there's no need to treat the single-row case specially:

 >>> ' and '.join(['first', 'second', 'third'])
'first and second and third'
 >>> ' and '.join(['first', 'second'])
'first and second'
 >>> ' and '.join(['first'])
'first'




More information about the Python-list mailing list