Giter Site home page Giter Site logo

Comments (8)

thesamovar avatar thesamovar commented on May 23, 2024

I think this is a really good idea. We're going to have to deal with Python 3 compatibility at some point, and now that we're rewriting Brian from scratch it seems like it would be mad not to take the opportunity to do it now. I say let's get on this as soon as possible, and enforce writing code in a way that will work on both 2/3 where possible, or easily modifiable to 3 via 2to3 where not possible.

from brian2.

mstimberg avatar mstimberg commented on May 23, 2024

I agree, I'll implement the 2to3 thing soon and then we can include it in the automatic testing. As far as I can see, it should not be much of an issue, now that we dropped Python < 2.6 support. We could try to write code that directly works with both Python 2 and 3, but I'm not sure it's worth the effort (we would need a lot of "from future import ..."). There are many things in the code that are translated very easily with 2to3 (e.g. "iteritems()" --> "items()", "xrange()" --> "range()", etc.). I think the only major thing we would have to take care of manually is to make the clear distinction between byte and unicode strings -- I guess basically all of the strings we have now (group names, etc.) should be unicode strings.

from brian2.

thesamovar avatar thesamovar commented on May 23, 2024

We should probably add something to the developers guide with things to watch out for. I guess with 2to3 we develop always in 2.x and generate a 3.x version, right?

I haven't used 3 at all, so I don't know what the issues are with unicode strings, etc. They're the default in 3, right? If so, I guess we use them too unless we find a strong reason not to.

from brian2.

mstimberg avatar mstimberg commented on May 23, 2024

We should probably add something to the developers guide with things to watch out for

You mean, something like the bottom of https://github.com/brian-team/brian2/blob/master/docs_sphinx/developer/guidelines/style.rst ;-) ?

I guess with 2to3 we develop always in 2.x and generate a 3.x version, right?

Exactly.

I haven't used 3 at all, so I don't know what the issues are with unicode strings, etc. They're the default in 3, right? If so, I guess we use them too unless we find a strong reason not to.

Yes, unicode is default in 3, whereas byte strings are default in 2, so we'll have to write u"string" in python 2. The distinction is messy in python 2 (and that was one of the main reasons for python 3 in the first place), you can use them interchangeably very often whereas python 3 makes a clear difference. But the only case where this is an issue for us will be when writing to/reading from files, I think.

from brian2.

mstimberg avatar mstimberg commented on May 23, 2024

Ok, I started a branch and pull request #26 for this issue. There was a slight complication that I did not anticipate: For the "write the default preferences to a file" mechanism we have to import brian2 in setup.py, which obviously fails for Python3 because setup.py is run before the 2to3 fixer converted Python code from 2 to 3. The solution is not that complicated though: We overwrite the install_data command to insert the installation directory in the path so that it imports brian2 from there. This seems to work fine, but there are some issues in our code that 2to3 cannot deal with currently.
One example (I'll add them to the developer docs soon): 2to3 automatically converts operator.isNumberType(obj) into isinstance(obj, numbers.Number), but in the units system we do from operator import isNumberType and therefore this replacement does not work. But in this particular case, we can use the isinstance variant anyway, since it works since Python 2.6

from brian2.

thesamovar avatar thesamovar commented on May 23, 2024

I never liked isNumberType anyway. ;)

from brian2.

mstimberg avatar mstimberg commented on May 23, 2024

Hah, so it turns out that the above example is a good one because it shows the limitations of 2to3... operator.isNumberType returns True for a numpy array but isinstance(obj, numbers.Number) does not... This shows how important it is to have a test suite with good coverage!

from brian2.

mstimberg avatar mstimberg commented on May 23, 2024

Closed with the merge of #26 (commit 6c3a160).

from brian2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.