Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #77369
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| 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; 'anyway.': 0.05; 'mrab': 0.05; 'subject:Python': 0.06; 'intermediate': 0.07; 'list?': 0.07; 'table.': 0.07; 'clause': 0.09; 'literal': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'def': 0.12; 'bind': 0.16; 'col': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:More': 0.16; 'subject:questions': 0.16; 'so.': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'helper': 0.24; 'second': 0.26; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'skip:( 20': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'another': 0.32; 'comment': 0.34; 'skip:d 20': 0.34; 'could': 0.34; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'skip:c 50': 0.60; 'skip:t 30': 0.61; 'name': 0.63; 'reads': 0.68; 'default': 0.69; 'introduce': 0.78; "'first'": 0.84; '(always': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: More questions on Python strings |
| Date | Mon, 01 Sep 2014 01:03:47 +0200 |
| Organization | None |
| References | <e008fcef-6d95-4123-a2e1-eeac5fd54ba3@googlegroups.com> <54036730.6010500@mrabarnett.plus.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bd8677.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.13670.1409526247.18130.python-list@python.org> (permalink) |
| Lines | 67 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1409526247 news.xs4all.nl 2977 [2001:888:2000:d::a6]:33885 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:77369 |
Show key headers only | View raw
MRAB wrote:
> 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.
>> 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'
The same goes for the no-rows case:
>>> " and ".join([])
''
You might also introduce a helper function to reduce code redundancy:
def ReadPkColumns(self) :
rows = self.cursor.fetchall()
def build_clause(sep, template):
return sep.join(
template.format(Ali=self.alias, ColLabel=row[KeyLabelPos])
for row in rows)
self.PkOrderBy = build_clause(CommaSpace, defaultColumn)
self.PkWhereClause = build_clause(andConjunction,
defaultColumnParaMarker)
General observations: don't microoptimize (always col = [] instead of
col.clear()) and don't comment your code to death ;)
Also: CommaSpace instead of the literal ", " string? When you bind it to
another value like
CommaSpace = ";\t"
you will also have to change the name to SemicolonTabulator anyway.
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
More questions on Python strings "Dennis E. Evans" <dennisearlevans@gmail.com> - 2014-08-31 10:37 -0700 Re: More questions on Python strings MRAB <python@mrabarnett.plus.com> - 2014-08-31 19:19 +0100 Re: More questions on Python strings Peter Otten <__peter__@web.de> - 2014-09-01 01:03 +0200
csiph-web