[Python-3000] Implementing % formatting in terms of str.format()

Rodrigo Bernardo Pimentel rbp at isnomore.net
Sat Apr 12 16:35:23 CEST 2008


On Sat, Apr 12 2008 at 10:49:43AM BRT, Nick Coghlan <ncoghlan at gmail.com> wrote:
> skip at pobox.com wrote:
> >     >> Is there a 2-to-3 fixer for % format?  I scanned the fixes directly
> >     >> quickly but didn't see anything obvious.
> > 
> >     Benjamin> I believe the only reason that % is even in 3.0 is that a 2to3
> >     Benjamin> fixer couldn't be easily written for it.
> > 
> > I find that kind of hard to believe (that it should be terribly difficult to
> > write a fixer, at least given a % operator with a string literal LHS and
> > either a tuple or dict RHS or a call to locals() or globals()).
> 
> That's exactly the problem though - while a 2to3 fixer can be written 
> for a tiny subset of formatting calls (those that meet the constraints 
> you gave), the vast majority are out of luck without some major type 
> inferencing additions to 2to3. Given the expression "x % y", 2to3 hasn't 
> got a clue whether it needs to do anything unless it somehow knows the 
> types of x and y.

I have submitted a GSoC proposal which might make a % fixer somewhat
useful. It was suggested by Collin Winter and, in a nutshell, is giving 2to3
fixers a way to say how confident they are on a certain fix, and then users
may specify a confidence threshold below which they want to manually
intervene to make a decision on whether to apply, skip or edit the
fix. The full proposal is at http://isnomore.net/2to3 .

With that in place, a % filter wouldn't need to be exact, but it could apply
some heuristics to rank how confident it is that "x % y" bears translating.

I'll try to implement that even if my GSoC isn't approved, and, from this
thread, I think a confidence-ranked % fixer would be a nice usage example,
so I'll probably try to write one.


        rbp
-- 
Rodrigo Bernardo Pimentel <rbp at isnomore.net> | GPG: <0x0DB14978>


More information about the Python-3000 mailing list