[Patches] [ python-Patches-1281573 ] String formatting character for str.join

SourceForge.net noreply at sourceforge.net
Thu Sep 8 21:27:01 CEST 2005


Patches item #1281573, was opened at 2005-09-04 07:40
Message generated for change (Settings changed) made by tjreedy
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1281573&group_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: Core (C code)
Group: Python 2.5
Status: Closed
>Resolution: Postponed
Priority: 5
Submitted By: Nick Coghlan (ncoghlan)
Assigned to: Nobody/Anonymous (nobody)
Summary: String formatting character for str.join

Initial Comment:
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.

----------------------------------------------------------------------

Comment By: Nick Coghlan (ncoghlan)
Date: 2005-09-05 08:22

Message:
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.

----------------------------------------------------------------------

Comment By: Nick Coghlan (ncoghlan)
Date: 2005-09-04 08:26

Message:
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


----------------------------------------------------------------------

Comment By: Nick Coghlan (ncoghlan)
Date: 2005-09-04 07:53

Message:
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. . .

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1281573&group_id=5470


More information about the Patches mailing list