Issue 1281573: String formatting character for str.join (original) (raw)

Created on 2005-09-04 11:40 by ncoghlan, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
format_join.diff ncoghlan,2005-09-04 11:40 String format character for joining iterables
format_join.2.diff ncoghlan,2005-09-04 12:26 Fix seg fault when handling generators
Messages (4)
msg48702 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2005-09-04 11:40
Current string formatting doesn't provide an easy way to say "put the contents of this sequence here, using this string as the separator". The only generic way is to use the '%s' formatting character, and then put "sep.join(map(str, seq))" in the appropriate place. This isn't so obvious with lists and tuples, as their default string representations are usually good enough. When trying to add debug statements for iterators, however, the current behaviour is a pain. This patch adds a '%j' (for 'join) formatting character that allows a separator string to be specified between the percent symbol and the format character. For example, "%'\n'j" would indicate that a newline should be used to separate items in the supplied iterable. At this point (initial posting) the patch doesn't support Unicode objects, and doesn't have any tests or documentation - it is solely a proof of concept.
msg48703 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2005-09-04 11:53
Logged In: YES user_id=1038590 Hmm, looks like this version has a few problems dealing with generators, so avoid it unless you don't mind the odd seg fault. . .
msg48704 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2005-09-04 12:26
Logged In: YES user_id=1038590 2nd version attached which fixed some problems with handling generators by cleaning up the reference counting and not trying to get too fancy when having to convert non-strings. Tested with the following simple test cases: $ ./python -c "print '%j' % range(10)" 0123456789 $ ./python -c "print '%j' % (x for x in range(10))" 0123456789 $ ./python -c "print '%j' % (x*x for x in range(10))" 0149162536496481 $ ./python -c "print '%\" \"j' % (x*x for x in range(10))" 0 1 4 9 16 25 36 49 64 81
msg48705 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2005-09-05 12:22
Logged In: YES user_id=1038590 'twas a silly idea. I still thinking printing an arbitrary sequence of non-strings is harder than it should be, but this isn't the answer.
History
Date User Action Args
2022-04-11 14:56:12 admin set github: 42340
2005-09-04 11:40:17 ncoghlan create