truncating strings
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? -- http://mail.python.org/mailman/listinfo/python-list
Re: truncating strings
On Tue, Aug 23, 2011 at 9:29 AM, Roy Smith r...@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 -- http://mail.python.org/mailman/listinfo/python-list
Re: truncating strings
On 2011-08-23, Roy Smith r...@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-nos...@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. -- http://mail.python.org/mailman/listinfo/python-list
Re: truncating strings
Seebs wrote: On 2011-08-23, Roy Smith r...@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~ -- http://mail.python.org/mailman/listinfo/python-list
Re: truncating strings
On 2011-08-23, Ethan Furman et...@stoneleaf.us wrote: Seebs wrote: On 2011-08-23, Roy Smith r...@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-nos...@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. -- http://mail.python.org/mailman/listinfo/python-list
Re: truncating strings
Seebs wrote: On 2011-08-23, Ethan Furman et...@stoneleaf.us wrote: Seebs wrote: On 2011-08-23, Roy Smith r...@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~ -- http://mail.python.org/mailman/listinfo/python-list
Re: truncating strings
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: truncating strings
On 2011-08-23, Ethan Furman et...@stoneleaf.us wrote: Ah -- that's only part of it -- the OP wants '...' to print as well. :) O. 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-nos...@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. -- http://mail.python.org/mailman/listinfo/python-list
[issue1735418] file.read() truncating strings under Windows
Antoine Pitrou pit...@free.fr added the comment: It's a bug in the program's logic. The program assumes that the file pointer will have advanced by the same number of bytes as were returned by read(), but it is false when opened in text mode ('r') since text mode under Windows will convert Windows newlines ('\r\n') into C newlines ('\n'). Also, please note this is a feature of Windows itself, *not* of Python. That's why you don't see it happening on e.g. Mac OS X. And that's why the fix, short of changing the program's logic, is to open in binary mode ('rb'). -- resolution: - invalid status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1735418 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1735418] file.read() truncating strings under Windows
Daniel Diniz aja...@gmail.com added the comment: Is this valid? -- nosy: +ajaksu2, benjamin.peterson, pitrou stage: - test needed type: - behavior versions: +Python 2.6, Python 3.1 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1735418 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Bugs-1735418 ] file.read() truncating strings under Windows
Bugs item #1735418, was opened at 2007-06-12 00:19 Message generated for change (Comment added) made by cgkanchi You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1735418group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: None Group: None Status: Open Resolution: None Priority: 5 Private: No Submitted By: cgkanchi (cgkanchi) Assigned to: Nobody/Anonymous (nobody) Summary: file.read() truncating strings under Windows Initial Comment: On Python 2.4.4 and 2.5.1 under Windows, file.read() fails to read a varying number of characters from the last line(s) of text files when asked to read more than 800 characters from near the end of the file. For example, if the last word of a 500kb file is superlative, file.read() might output erlative. The file pointer at this stage is very close (a few words at most) to the end of the file. I ran into this problem while writing a program to split .txt ebooks into smaller files so that my ancient iPod could handle them. The behaviour is identical on both 2.4.4 and 2.5.1 under Windows, but does not appear under Mac OS X. I was unable to test it under Linux. To test the bug, I used various books from http://gutenberg.org . The one primarily used was Pride and Prejudice by Jane Austen. -- Comment By: cgkanchi (cgkanchi) Date: 2007-06-14 17:59 Message: Logged In: YES user_id=1814873 Originator: YES (e) To have tell() on the same level with read(), try the unbuffered mode by specifying bufsize=0 in open(), http://docs.python.org/lib/built-in-funcs.html This does not work either. There is no change in the behaviour of the program. (a) I would not trust tell(). Calculate the absolute position and use seek(). That defeats the purpose of having native string handling in python. It means I have to do things the C way. Therefore, it is a bug in the implementation. (b) Just from the documentation to Python's file-like objects I can assume that read() and tell() belong to different levels of API. The read() function has this in its documentation: Note that this method may call the underlying C function fread() more than once in an effort to acquire as close to size bytes as possible. http://docs.python.org/lib/bltin-file-objects.html That should not make any difference whatsoever. The tell() function's documentation refers to stdio's ftell(). This hints that tell() will return the position of the fread() buffer's end, not the read()'s end. Again, irrelevant. (c) It also appears that by adding 1 to the current position - unget size you are skipping the space character itself. This is by design. I didn't want the space. Functionally, it makes no difference. (d) The rfind() might return -1 if the search fails. This is by design as well, when there are no spaces in the remaining file, i.e., the file pointer is on the last word, a return value of -1 causes read() to read till EOF. I did however find the solution in the python docs, but it is a workaround rather than a fix for a very obvious bug. tell() Return the file's current position, like stdio's ftell(). Note: On Windows, tell() can return illegal values (after an fgets()) when reading files with Unix-style line-endings. Use binary mode ('rb') to circumvent this problem. Cheers, cgkanchi -- Comment By: Ilguiz Latypov (ilgiz) Date: 2007-06-12 15:51 Message: Logged In: YES user_id=281701 Originator: NO (e) To have tell() on the same level with read(), try the unbuffered mode by specifying bufsize=0 in open(), http://docs.python.org/lib/built-in-funcs.html -- Comment By: Ilguiz Latypov (ilgiz) Date: 2007-06-12 15:47 Message: Logged In: YES user_id=281701 Originator: NO This is your coding bug. (a) I would not trust tell(). Calculate the absolute position and use seek(). (b) Just from the documentation to Python's file-like objects I can assume that read() and tell() belong to different levels of API. The read() function has this in its documentation: Note that this method may call the underlying C function fread() more than once in an effort to acquire as close to size bytes as possible. http://docs.python.org/lib/bltin-file-objects.html The tell() function's documentation refers to stdio's ftell(). This hints that tell() will return the position of the fread() buffer's end, not the read()'s end. (c) It also appears that by adding 1 to the current position - unget size you are skipping the space character itself. (d) The rfind() might return -1 if the search fails. -- You can respond by visiting: https://sourceforge.net
[ python-Bugs-1735418 ] file.read() truncating strings under Windows
Bugs item #1735418, was opened at 2007-06-11 20:19 Message generated for change (Comment added) made by ilgiz You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1735418group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: None Group: None Status: Open Resolution: None Priority: 5 Private: No Submitted By: cgkanchi (cgkanchi) Assigned to: Nobody/Anonymous (nobody) Summary: file.read() truncating strings under Windows Initial Comment: On Python 2.4.4 and 2.5.1 under Windows, file.read() fails to read a varying number of characters from the last line(s) of text files when asked to read more than 800 characters from near the end of the file. For example, if the last word of a 500kb file is superlative, file.read() might output erlative. The file pointer at this stage is very close (a few words at most) to the end of the file. I ran into this problem while writing a program to split .txt ebooks into smaller files so that my ancient iPod could handle them. The behaviour is identical on both 2.4.4 and 2.5.1 under Windows, but does not appear under Mac OS X. I was unable to test it under Linux. To test the bug, I used various books from http://gutenberg.org . The one primarily used was Pride and Prejudice by Jane Austen. -- Comment By: Ilguiz Latypov (ilgiz) Date: 2007-06-12 11:47 Message: Logged In: YES user_id=281701 Originator: NO This is your coding bug. (a) I would not trust tell(). Calculate the absolute position and use seek(). (b) Just from the documentation to Python's file-like objects I can assume that read() and tell() belong to different levels of API. The read() function has this in its documentation: Note that this method may call the underlying C function fread() more than once in an effort to acquire as close to size bytes as possible. http://docs.python.org/lib/bltin-file-objects.html The tell() function's documentation refers to stdio's ftell(). This hints that tell() will return the position of the fread() buffer's end, not the read()'s end. (c) It also appears that by adding 1 to the current position - unget size you are skipping the space character itself. (d) The rfind() might return -1 if the search fails. -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1735418group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Bugs-1735418 ] file.read() truncating strings under Windows
Bugs item #1735418, was opened at 2007-06-11 20:19 Message generated for change (Comment added) made by ilgiz You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1735418group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: None Group: None Status: Open Resolution: None Priority: 5 Private: No Submitted By: cgkanchi (cgkanchi) Assigned to: Nobody/Anonymous (nobody) Summary: file.read() truncating strings under Windows Initial Comment: On Python 2.4.4 and 2.5.1 under Windows, file.read() fails to read a varying number of characters from the last line(s) of text files when asked to read more than 800 characters from near the end of the file. For example, if the last word of a 500kb file is superlative, file.read() might output erlative. The file pointer at this stage is very close (a few words at most) to the end of the file. I ran into this problem while writing a program to split .txt ebooks into smaller files so that my ancient iPod could handle them. The behaviour is identical on both 2.4.4 and 2.5.1 under Windows, but does not appear under Mac OS X. I was unable to test it under Linux. To test the bug, I used various books from http://gutenberg.org . The one primarily used was Pride and Prejudice by Jane Austen. -- Comment By: Ilguiz Latypov (ilgiz) Date: 2007-06-12 11:51 Message: Logged In: YES user_id=281701 Originator: NO (e) To have tell() on the same level with read(), try the unbuffered mode by specifying bufsize=0 in open(), http://docs.python.org/lib/built-in-funcs.html -- Comment By: Ilguiz Latypov (ilgiz) Date: 2007-06-12 11:47 Message: Logged In: YES user_id=281701 Originator: NO This is your coding bug. (a) I would not trust tell(). Calculate the absolute position and use seek(). (b) Just from the documentation to Python's file-like objects I can assume that read() and tell() belong to different levels of API. The read() function has this in its documentation: Note that this method may call the underlying C function fread() more than once in an effort to acquire as close to size bytes as possible. http://docs.python.org/lib/bltin-file-objects.html The tell() function's documentation refers to stdio's ftell(). This hints that tell() will return the position of the fread() buffer's end, not the read()'s end. (c) It also appears that by adding 1 to the current position - unget size you are skipping the space character itself. (d) The rfind() might return -1 if the search fails. -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1735418group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Bugs-1735418 ] file.read() truncating strings under Windows
Bugs item #1735418, was opened at 2007-06-12 00:19 Message generated for change (Tracker Item Submitted) made by Item Submitter You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1735418group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: None Group: None Status: Open Resolution: None Priority: 5 Private: No Submitted By: cgkanchi (cgkanchi) Assigned to: Nobody/Anonymous (nobody) Summary: file.read() truncating strings under Windows Initial Comment: On Python 2.4.4 and 2.5.1 under Windows, file.read() fails to read a varying number of characters from the last line(s) of text files when asked to read more than 800 characters from near the end of the file. For example, if the last word of a 500kb file is superlative, file.read() might output erlative. The file pointer at this stage is very close (a few words at most) to the end of the file. I ran into this problem while writing a program to split .txt ebooks into smaller files so that my ancient iPod could handle them. The behaviour is identical on both 2.4.4 and 2.5.1 under Windows, but does not appear under Mac OS X. I was unable to test it under Linux. To test the bug, I used various books from http://gutenberg.org . The one primarily used was Pride and Prejudice by Jane Austen. -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1735418group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com