Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12108

Re: truncating strings

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'width': 0.05; 'override': 0.07; '%s"': 0.09; 'received:209.85.160.174': 0.09; 'received :mail-gy0-f174.google.com': 0.09; 'truncate': 0.09; 'am,': 0.12; 'roy': 0.16; "\xc2\xa0i'm": 0.16; '\xc2\xa0if': 0.16; 'cc:addr :python-list': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; '>>>': 0.18; 'wrap': 0.18; 'cc:no real name:2**0': 0.20; 'seems': 0.20; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'tue,': 0.23; 'similar,': 0.23; 'aug': 0.24; 'specify': 0.24; 'string': 0.26; 'bugs': 0.28; 'message-id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; 'lines': 0.30; 'skip:\xc2 20': 0.30; '(e.g.': 0.31; 'chris': 0.32; 'certainly': 0.32; 'this.': 0.32; 'does': 0.32; 'there': 0.33; 'received:209.85.160': 0.35; 'skip:" 20': 0.35; 'anything': 0.36; 'but': 0.37; 'something': 0.37; 'could': 0.38; 'some': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'define': 0.39; 'maximum': 0.63; 'skip:\xc2 10': 0.74; 'sender:addr:chris': 0.84; 'url:rebertia': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=Ui2YNU45fUDzspNcE3pVlnr8qREvfsOIfz18aQyYVPk=; b=ECWL8Pgt1Sa3NJC2+MAXXkImq8qv4pkn+Rc2Wr1pAYpZLsSm8toPxRKfLcYMT3Lv1U 6ojlhl7qXGfmQAeAVm9vt4P6irKIExKA+CsINmkbHmMnot4kmLf0Cqddf9twPqPOXNNR YxTlq7/1EF6QyOLBmOlD+Ru8WhJcflsk8k+gs=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <a24faeb6-ebe4-41bf-8c20-9cd46547a3bd@b14g2000vbg.googlegroups.com>
References <a24faeb6-ebe4-41bf-8c20-9cd46547a3bd@b14g2000vbg.googlegroups.com>
Date Tue, 23 Aug 2011 10:27:42 -0700
X-Google-Sender-Auth n35LShb3lo_OU7hwTWymiShNPcY
Subject Re: truncating strings
From Chris Rebert <clp2@rebertia.com>
To Roy Smith <roy@panix.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.362.1314120465.27778.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314120465 news.xs4all.nl 23976 [2001:888:2000:d::a6]:50850
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12108

Show key headers only | View raw


On Tue, Aug 23, 2011 at 9:29 AM, Roy Smith <roy@panix.com> wrote:
> I want to log a string but only the first bunch of it, and add "..."
> to the end if it got truncated.  This certainly works:
>
>          log_message = message
>          if len(log_message) >= 50:
>            log_message = log_message[:50] + '...'
>          logger.error("FAILED: '%s', '%s', %s, %s" % (log_message,
> route, params, e.code))
>
> but it bugs me that there should be some cleaner way to do this.  I'm
> fantasizing about something along the lines of:
>
>          logger.error("FAILED: '%s{50}', '%s', %s, %s" % (message,
> route, params, e.code))
>
> does anything like this exist?

You can specify a maximum width to truncate the string to, but I don't
see any built-in way to add an elision indication (e.g. "...").

>>> "%.4s" % "spam and eggs"
'spam'
>>> "{:.4s}".format("spam and eggs")
'spam'

You could define something to wrap strings and override __format__()
or similar, but that seems like overkill.

Cheers,
Chris
--
http://rebertia.com

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

truncating strings Roy Smith <roy@panix.com> - 2011-08-23 09:29 -0700
  Re: truncating strings Chris Rebert <clp2@rebertia.com> - 2011-08-23 10:27 -0700
  Re: truncating strings Seebs <usenet-nospam@seebs.net> - 2011-08-23 20:52 +0000
    Re: truncating strings Ethan Furman <ethan@stoneleaf.us> - 2011-08-23 15:02 -0700
      Re: truncating strings Seebs <usenet-nospam@seebs.net> - 2011-08-23 22:00 +0000
        Re: truncating strings Ethan Furman <ethan@stoneleaf.us> - 2011-08-23 16:44 -0700
          Re: truncating strings Seebs <usenet-nospam@seebs.net> - 2011-08-24 04:21 +0000
        Re: truncating strings Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-24 11:24 +1000

csiph-web