Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #12101 > unrolled thread
| Started by | Roy Smith <roy@panix.com> |
|---|---|
| First post | 2011-08-23 09:29 -0700 |
| Last post | 2011-08-24 11:24 +1000 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2011-08-23 09:29 -0700 |
| Subject | truncating strings |
| Message-ID | <a24faeb6-ebe4-41bf-8c20-9cd46547a3bd@b14g2000vbg.googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2011-08-23 10:27 -0700 |
| Message-ID | <mailman.362.1314120465.27778.python-list@python.org> |
| In reply to | #12101 |
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
[toc] | [prev] | [next] | [standalone]
| From | Seebs <usenet-nospam@seebs.net> |
|---|---|
| Date | 2011-08-23 20:52 +0000 |
| Message-ID | <slrnj584bv.4ta.usenet-nospam@guild.seebs.net> |
| In reply to | #12101 |
On 2011-08-23, 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:
> logger.error("FAILED: '%s{50}', '%s', %s, %s" % (message,
> route, params, e.code))
> does anything like this exist?
%.50s
-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2011-08-23 15:02 -0700 |
| Message-ID | <mailman.368.1314135952.27778.python-list@python.org> |
| In reply to | #12116 |
Seebs wrote:
> On 2011-08-23, 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:
>
>> logger.error("FAILED: '%s{50}', '%s', %s, %s" % (message,
>> route, params, e.code))
>
>> does anything like this exist?
>
> %.50s
That's not working in 2.7 or 3.2.
~Ethan~
[toc] | [prev] | [next] | [standalone]
| From | Seebs <usenet-nospam@seebs.net> |
|---|---|
| Date | 2011-08-23 22:00 +0000 |
| Message-ID | <slrnj588mv.96g.usenet-nospam@guild.seebs.net> |
| In reply to | #12118 |
On 2011-08-23, Ethan Furman <ethan@stoneleaf.us> wrote:
> Seebs wrote:
>> On 2011-08-23, Roy Smith <roy@panix.com> wrote:
>>> logger.error("FAILED: '%s{50}', '%s', %s, %s" % (message,
>>> route, params, e.code))
>>> does anything like this exist?
>> %.50s
> That's not working in 2.7 or 3.2.
Huh.
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print "%.5s" % ("hello there, truncate me!")
hello
-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2011-08-23 16:44 -0700 |
| Message-ID | <mailman.371.1314142071.27778.python-list@python.org> |
| In reply to | #12120 |
Seebs wrote:
> On 2011-08-23, Ethan Furman <ethan@stoneleaf.us> wrote:
>> Seebs wrote:
>>> On 2011-08-23, Roy Smith <roy@panix.com> wrote:
>>>> logger.error("FAILED: '%s{50}', '%s', %s, %s" % (message,
>>>> route, params, e.code))
>
>>>> does anything like this exist?
>
>>> %.50s
>
>> That's not working in 2.7 or 3.2.
>
> Huh.
>
> Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
> [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> print "%.5s" % ("hello there, truncate me!")
> hello
Ah -- that's only part of it -- the OP wants '...' to print as well. :)
~Ethan~
[toc] | [prev] | [next] | [standalone]
| From | Seebs <usenet-nospam@seebs.net> |
|---|---|
| Date | 2011-08-24 04:21 +0000 |
| Message-ID | <slrnj58uue.k7o.usenet-nospam@guild.seebs.net> |
| In reply to | #12123 |
On 2011-08-23, Ethan Furman <ethan@stoneleaf.us> wrote: > Ah -- that's only part of it -- the OP wants '...' to print as well. :) Ohhhh. Hmm. That's harder. I can't think of a pretty way, so I think I'd probably write a "prettytrunc(string, len)" or something similar. -s -- Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! I am not speaking for my employer, although they do rent some of my opinions.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-08-24 11:24 +1000 |
| Message-ID | <4e5452be$0$29984$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #12120 |
Seebs wrote:
> Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
> [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> print "%.5s" % ("hello there, truncate me!")
> hello
Well, whadda you know, I learned something new :)
In any case, this doesn't solve the OP's problem, as he wants to truncate
the input string, and append '...' if and only if it were truncated.
The right solution is to wrap the functionality in a function. It's not
hard, and is elegant. Not everything needs to be a built-in.
# Untested.
def truncate(s, maxwidth=50):
if len(s) <= maxwidth:
return s
s = s[:maxwidth - 3]
return s + '...'
--
Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web