Python Forum

Full Version: py2 to py3 manual conversion, dictonaries
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am wondering if my conversion on this dictionary was done correctly. Take a look at the way this was written in py2:

  def new_item(self,raw_string):

    the_type=raw_string.split(",")[0]

    if self.constructors.has_key(the_type):
      return self.constructors[the_type](raw_string)
    else:
      return None
I want to say with the changes from py2 to py3 that the new method should be:

  def new_item(self,raw_string):

    the_type=raw_string.split(",")[0]

    if the_type in self.constructors:
      return the_type in self.constructors[raw_string]
    else:
      return None
If it helps the initialization for constructors is:

self.constructors={"None":None}
The problem for me is that I am not sure what was meant by the py2 return statement:

return self.constructors[the_type](raw_string)
Obviously, self.constructors dictionary has functions as its values

self.constructors[the_type](raw_string)
retrieves function corresponding to the_type (unfortunate name, data_type, maybe?) and calls it with an argument raw_string. Looks like you are breaking the functionality by assuming that Python2 code did something wrong.

Migration from 2 to 3 is mostly about interfaces returning generator-like objects instead of lists and some streamlining of APIs - I mostly remember reduction in dict methods. I would recommend to do conversion by proper tools. 2to3 used to work rather well
(Jun-06-2018, 04:34 PM)volcano63 Wrote: [ -> ]Obviously, self.constructors dictionary has functions as its values

self.constructors[the_type](raw_string)
retrieves function corresponding to the_type (unfortunate name, data_type, maybe?) and calls it with an argument raw_string. Looks like you are breaking the functionality by assuming that Python2 code did something wrong.

Migration from 2 to 3 is mostly about interfaces returning generator-like objects instead of lists and some streamlining of APIs - I mostly remember reduction in dict methods. I would recommend to do conversion by proper tools. 2to3 used to work rather well

So I actually do not need to convert that is what your saying? It should work in py3 the way it was written in py2?
(Jun-06-2018, 05:40 PM)Vysero Wrote: [ -> ]So I actually do not need to convert that is what your saying? It should work in py3 the way it was written in py2?
That's exactly what I said
(Jun-06-2018, 07:04 PM)volcano63 Wrote: [ -> ]
(Jun-06-2018, 05:40 PM)Vysero Wrote: [ -> ]So I actually do not need to convert that is what your saying? It should work in py3 the way it was written in py2?
That's exactly what I said

Actually, exactly what you said was: "Looks like you are breaking the functionality by assuming that Python2 code did something wrong.", you then went on to say: "Migration from 2 to 3 is mostly about interfaces returning generator-like objects instead of lists and some streamlining of APIs - I mostly remember reduction in dict methods. I would recommend to do conversion by proper tools. 2to3 used to work rather well" which can easily mislead someone into thinking that the code is broken and that I should be using a tool to fix it rather than trying to fix it manually. So I did use a tool to fix it (as suggested) and that portion of the code was not changed which is the only way I was able to come to the conclusion that what you actually were saying was that there was nothing wrong with the code.

Please read the following very, very thoroughly believe me you need the advice:
https://markmanson.net/how-to-give-advice
You need an advice in manners and a lesson in gratitude - but I will just add you to ignore list, second in 10 minutes.What a night Naughty



Getting an accusation of being
Quote:extremely rude
from an ungrateful pompous ass feels me with pride Dance
(Jun-06-2018, 07:14 PM)Vysero Wrote: [ -> ]that portion of the code was not changed which is the only way I was able to come to the conclusion that what you actually were saying was that there was nothing wrong with the code.
The sole fact that you *think* it needs to be translated to py3 suggest you shoudn't do it manually as you don't know what you are doing. And that is also confirmed by how you translated it...
(Jun-06-2018, 07:49 PM)buran Wrote: [ -> ]
(Jun-06-2018, 07:14 PM)Vysero Wrote: [ -> ]that portion of the code was not changed which is the only way I was able to come to the conclusion that what you actually were saying was that there was nothing wrong with the code.
The sole fact that you *think* it needs to be translated to py3 suggest you shoudn't do it manually as you don't know what you are doing. And that is also confirmed by how you translated it...

Your not fit to be a moderator. This is going up the chain.
(Jun-06-2018, 08:06 PM)Vysero Wrote: [ -> ]Your not fit to be a moderator. This is going up the chain.
We stop here,Thread closed.
Star a new thread if you have problem with suggestions,and we try to keep it polite here.