Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'elif': 0.05; 'subject:Python': 0.06; 'assign': 0.07; 'column': 0.07; 'intermediate': 0.07; 'list?': 0.07; 'table.': 0.07; 'string': 0.09; '[1,': 0.09; 'clause': 0.09; 'rows': 0.09; 'rows,': 0.09; 'def': 0.12; 'columns': 0.16; 'fetch': 0.16; 'formatted': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'loops': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'set,': 0.16; 'subject:More': 0.16; 'subject:questions': 0.16; 'elements': 0.16; 'so.': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'errors.': 0.24; 'question': 0.24; 'second': 0.26; 'defined': 0.27; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'member.': 0.30; 'statement': 0.30; 'skip:( 20': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'probably': 0.32; 'run': 0.32; 'another': 0.32; 'skip:d 20': 0.34; 'could': 0.34; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'module.': 0.36; 'two': 0.37; 'list': 0.37; 'clear': 0.37; 'skip:o 20': 0.38; 'needed': 0.38; 'to:addr :python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'skip:c 50': 0.60; 'more': 0.64; 'different': 0.65; 'details,': 0.68; 'reads': 0.68; 'default': 0.69; 'repeat': 0.74; "'first'": 0.84; 'resulted': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=Uv7tNoAB c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=u9EReRu7m0cA:10 a=t67aTNSlDKkA:10 a=8G5_lP3-SXsA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=3G_rzmaUlkpnt-V-3VEA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett:2500 Date: Sun, 31 Aug 2014 19:19:28 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: More questions on Python strings References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 98 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409509178 news.xs4all.nl 2865 [2001:888:2000:d::a6]:40602 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77353 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'