Python Forum
py2 to py3 manual conversion, dictonaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
py2 to py3 manual conversion, dictonaries
#1
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)
#2
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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
#3
(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?
#4
(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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
#5
(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
#6
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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
#7
(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...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

#8
(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.
#9
(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.


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked Manual Splines? Power_Broker 4 1,103 Nov-08-2022, 01:49 AM
Last Post: Power_Broker
  Regex Subdomain Validation & Input website manual rtzki 1 3,220 Sep-16-2018, 03:10 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020