anstmichaels / emopt

A suite of tools for optimizing the shape and topology of electromagnetic structures.
BSD 3-Clause "New" or "Revised" License
97 stars 41 forks source link

python 2+3 dual compatibility #6

Closed sbyrnes321 closed 5 years ago

sbyrnes321 commented 5 years ago

In case you're interested...

This allows emopt to run in both python 2 and python 3. At least I think so ... I haven't yet thoroughly checked it by running all the examples. I will add more commits here if I find any other issues when I do that.

The specific changes are:

(1) added from __future__ import division, print_function, absolute_import to every python file, so that py2 and py3 have mostly the same behavior & bugs; (2) print --> print(); (3) xrange --> range [it has no performance impact in python 2 for small ranges]; (4) repair floor integer divisions to keep the same behavior (at least the ones I can find! Sorry if I introduced bugs by missing any!); (5) implicit relative imports converted to explicit relative imports; (6) open(...'wb') --> open(...'w') somewhere in install.py for py3 compatibility.

I did not edit the installation instructions. You just replace "python" with "python3" and "pip" with "pip3" etc. The only thing that wasn't obvious to me is that, while you can run install.py in python 3, you do still have to have python 2 installed on the system---one of the libraries needs access to python 2 in order to compile itself.

anstmichaels commented 5 years ago

Python 3 compatibility is definitely on my list of things to do, but there are a few more major changes I want to make before launching into it (and potentially breaking things). For the meantime, though, this will be a good experiment to see how much effort it will take, so I appreciate it.

sbyrnes321 commented 5 years ago

Cool, good luck!

I added two more commits last night, but after those, at least the two example files I tried were running and optimizing beautifully in Python 3.

Well, sort of. There is one other bug ... I get an error whenever the program tries to emit a warning message, some kind of bad function call in the code that displays warning messages. But I'm currently thinking that this is something wonky in my configuration, and that it's not related to emopt or Python 3. Not 100% sure. When I comment out the things that make warning messages, everything else seems to work.

anstmichaels commented 5 years ago

Actually, I override the behavior of the warnings module. It is possible that the process for doing so has changed in Python 3, which could be causing an error. You could try deleting the line

warnings.showwarning = _warning_message

in misc.py and also changing the function warning_message to use print instead of warnings.warn and see if that fixes the errors. If it does, it just means that the way warnings are handled needs to be reworked (and I am sure there is a cleaner way to do it than I currently am).

sbyrnes321 commented 5 years ago

Thanks for that tip! The warning problem had a simple fix, see https://github.com/anstmichaels/emopt/pull/6/commits/03868748ff3b60b0af525f92ca50322c4e24031f .

I think everything is working now ... at least your example files that I've tried so far have all been optimizing and plotting beautifully.

anstmichaels commented 5 years ago

Just an update: I have just pushed a new version that support that add compatibility for both python 2.7 and python 3.X. I ended up using futurize which pretty much took care of the conversion without issues. I am going to close this pull request for now. Thanks for the input!