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: 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: References: Date: Tue, 23 Aug 2011 10:27:42 -0700 X-Google-Sender-Auth: n35LShb3lo_OU7hwTWymiShNPcY Subject: Re: truncating strings From: Chris Rebert To: Roy Smith 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Tue, Aug 23, 2011 at 9:29 AM, Roy Smith wrote: > I want to log a string but only the first bunch of it, and add "..." > to the end if it got truncated. =C2=A0This certainly works: > > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0log_message =3D message > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if len(log_message) >=3D 50: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0log_message =3D log_message[:50]= + '...' > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0logger.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. =C2=A0I'= m > fantasizing about something along the lines of: > > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0logger.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