0

I'm looking for a way to convert obsolete % string formatting operator to format() function in python3 code.

ugly = 'Exception: the %s is over %+f' % (t, value)
good = 'Exception: the {} is over {:+f}'.format(e, value)

Sure, I use autopep8 on save (via PyDev), but it seems that there is no option for turning on desirable conversion. The closest is an W690 option:

W690 - Fix various deprecated code (via lib2to3).

But it does not work, as well as explicit run of lib2to3. Any solutions are welcome; better if the tool can be integrated into eclipse / pydev, like autopep8.

martineau
  • 119,623
  • 25
  • 170
  • 301
Sergey Belash
  • 1,433
  • 3
  • 16
  • 21
  • 1
    May be it should be asked in a different way, like this: http://stackoverflow.com/questions/2625294/how-do-i-autoformat-some-python-code-to-be-correctly-formatted ? – Sergey Belash Oct 04 '16 at 09:13
  • 1
    `%` string interpolation isn't obsolete. But if you _really_ want to replace it with something new then why bother with `format` when you can use f-strings? Eg, `f'Exception: the {t} is over {value:+f}'` – PM 2Ring Oct 04 '16 at 09:52
  • @cdarke I agree that it is not obsolete; the `format` is just my preference: I do believe that it makes code more readable, and easier for i18n. @2Ring it worth bothering unless python 3.6 is alpha released; but for sure, I'm waiting for the f-string feature, it looks awesome – Sergey Belash Oct 04 '16 at 10:26
  • @SergeyBelash: I have been playing with f-strings this morning (I have 3.6.0b). The syntax is similar in some ways to Ruby, but the formatting makes them more powerful. – cdarke Oct 04 '16 at 12:26
  • @SergeyBelash: I was doing something unrelated and realised that the `%` formatting has no alternative for bytes objects. Bytes objects don't have a `.format()` method. So `%` is certainly not obsolete. – cdarke Oct 04 '16 at 13:38

0 Answers0