truncating strings

R

Roy Smith

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?
 
C

Chris Rebert

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. "...").
'spam'

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

Cheers,
Chris
 
S

Seebs

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.hello

-s
 
E

Ethan Furman

Seebs said:
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~
 
S

Steven D'Aprano

Seebs said:
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.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 + '...'
 
S

Seebs

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top