0

I'm converting python2.7 scripts to python3. 2to3 makes these kinds of suggestions:

 result = result.split(',')
 syslog_trace("Result : {0}".format(result), False, DEBUG)
- data.append(map(float, result))
+ data.append(list(map(float, result)))
 if (len(data) > samples):
 data.pop(0)
 syslog_trace("Data : {0}".format(data), False, DEBUG)
 # report sample average
 if (startTime % reportTime < sampleTime):
- somma = map(sum, zip(*data))
+ somma = list(map(sum, list(zip(*data))))
 # not all entries should be float
 # 0.37, 0.18, 0.17, 4, 143, 32147, 3, 4, 93, 0, 0
 averages = [format(sm / len(data), '.3f') for sm in somma]

I'm sure the makers of Python3 did not want to do it like that. At least, it gives me a "you must be kidding" feeling.

Is there a more pythonic way of doing this?

Dimitris Fasarakis Hilliard
162k35 gold badges282 silver badges265 bronze badges
asked Aug 7, 2016 at 10:45
5
  • 2
    List comprehension, maybe. [float(x) for x in result] Commented Aug 7, 2016 at 10:49
  • @zondo : thanks. I'll investigate list comprehension. Does that also work for the map(sum, zip(*data))? Commented Aug 7, 2016 at 10:59
  • 2
    Yes. Use [sum(d) for d in zip(*data)] Commented Aug 7, 2016 at 11:01
  • 1
    That's the "quick fix" mentioned in the docs, which also suggests using list comps instead. Commented Aug 7, 2016 at 11:10
  • 2
    2to3 is intended to translate Python 2 code into equivalent Python 3 code. The next step is to make it well-written Python 3, but an automatic tool won't do it for you! Commented Aug 7, 2016 at 15:07

2 Answers 2

2

What's wrong with the unfixed somma?

2to3 cannot know how somma is going to be used, in that case, as a generator in the next line to compute averages it is OK and optimal, no need to convert it as a list.

That's the genius of python 3 list to generator changes: most people used those lists as generators already, wasting precious memory materializing lists they did not need.

 # report sample average
 if (startTime % reportTime < sampleTime):
 somma = map(sum, zip(*data))
 # not all entries should be float
 # 0.37, 0.18, 0.17, 4, 143, 32147, 3, 4, 93, 0, 0
 averages = [format(sm / len(data), '.3f') for sm in somma]

Of course the first statement, unconverted, will fail since we append a generator whereas we need a list. In that case, the error is quickly fixed.

If left like this: data.append(map(float, result)), the next trace shows something fishy: 'Data : [<map object at 0x00000000043DB6A0>]', that you can quickly fix by cnverting to list as 2to3 suggested.

2to3 does its best to create running Python 3 code, but it does not replace manual rework or produce optimal code. When you are in a hurry you can apply it, but always check the diffs vs the old code like the OP did.

The -3 option of latest Python 2 versions print warnings when an error would be raised using Python 3. It's another approach, better when you have more time to perform your migration.

answered Aug 7, 2016 at 14:49
Sign up to request clarification or add additional context in comments.

Comments

1

I'm sure the makers of Python3 did not want to do it like that

Well, the makers of Python generally don't like seeing Python 2 being used, I've seen that sentiment being expressed in pretty much every recent PyCon.

Is there a more pythonic way of doing this?

That really depends on your interpretation of Pythonic, list comps seem more intuitive in your case, you want to construct a list so there's no need to create an iterator with map or zip and then feed it to list().

Now, why 2to3 chose list() wrapping instead of comps, I do not know; probably easiest to actually implement.

answered Aug 7, 2016 at 13:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.