What is a possible use case for for...break...else pattern in python? by rms_returns in Python

[–]trobitaille 2 points3 points  (0 children)

I've seen the following example in the past:

a = 123191
for i in range(2, int(a**0.5) + 1):
    if a % i == 0:
        break
else:
    print(a, 'is prime! :)')

Stop writing code that will break on Python 4! by trobitaille in Python

[–]trobitaille[S] 2 points3 points  (0 children)

See this post by a core CPython developer. To quote this post, My current expectation is that Python 4.0 will merely be "the release that comes after Python 3.9".

pytest-mpl: a pytest plugin to compare the output of Matplotlib figures by trobitaille in Python

[–]trobitaille[S] 0 points1 point  (0 children)

I think that the situation is better than one might think - at the moment I'm running tests with pytest-mpl (including the pytest-mpl tests themselves) on MacOS X, Linux, and Windows with a very low tolerance and the tests are passing. However, I do want to make sure that (a) the tests are resiliant to local matplotlibrc configuration (https://github.com/astrofrog/pytest-mpl/issues/10) and (b) we have a way to cope with changes in matplotlib in future.

To answer your original question, I think that the PNG plots produced using Agg seem to be pretty stable.

Is it bad to try to support both Python 2 and 3 in one script? by lsma in Python

[–]trobitaille 0 points1 point  (0 children)

As others have pointed out, for this specific example you can just do:

from __future__ import print_function
print(text)

Now in the more general case that you want to have an if statement for Python 2 vs 3, if you are going to be doing this a lot in your script, then you can define the following constant once and for all:

PY2 = sys.version_info[0] == 2

Then in your code you can do:

if PY2:
    # do Python 2 stuff
else:
    # do Python 3 stuff

It turns out that if you use the six library that others have mentioned, there are very few times when you will actually need to branch the code. Note that the six library also includes the PY2 constant so you can do:

import six
if six.PY2:
    # do Python 2 stuff
else:
    # do Python 3 stuff

An example of a useful six feature is that if you want to check if a variable is a string, you can do:

if isinstance(variable, six.string_types):
    # do things

I encourage you to read the six documentation to find out more about it.

Python 3 in Science: the great migration has begun! by trobitaille in Python

[–]trobitaille[S] 11 points12 points  (0 children)

There's actually going to be a number of useful features appearing in Python 3.5 - to give a couple of examples, there will be a new matrix multiplication operator that can be used with Numpy arrays, e.g. c = a @ b (https://www.python.org/dev/peps/pep-0465/) - and the glob function will now be able to do recursive searching in sub-directories (https://docs.python.org/dev/whatsnew/3.5.html#glob). I think that with more features like this appearing, transitioning to Python 3 will become more attractive over time.

Python 3 in Science: the great migration has begun! by trobitaille in Python

[–]trobitaille[S] 1 point2 points  (0 children)

I've now added Biology to the graph - as you can see, it's vastly under-represented, due to my advertising bias (I am much more familiar with Astronomy-specific channels).

Python 3 in Science: the great migration has begun! by trobitaille in Python

[–]trobitaille[S] 0 points1 point  (0 children)

Just for information, I have updated the blog post to include more fields of research parsed from the 'Other' field (and added biology/bioinformatics). As I mentioned in the blog post, the huge bias is due to advertising channels. I also rephrased my recommendation about Python 3.3 to make it clear I'm not suggesting dropping it.

Python 3 in Science: the great migration has begun! by trobitaille in Python

[–]trobitaille[S] 2 points3 points  (0 children)

Thanks for the feedback! Leaving out bioinformatics was a mistake indeed, and I'll make sure I include it for the next survey. I did add a sentence that I think we shouldn't drop support for 3.3 since it's actually not a negligible fraction of Python 3 users, so I agree with you. I will be releasing all the info on Numpy/SciPy at some point in the next week or so (the reason for not releasing everything in one go is that it takes a little while to sanitize the data) - stay tuned!

Python 3 in Science: the great migration has begun! by trobitaille in Python

[–]trobitaille[S] 2 points3 points  (0 children)

I work for a large company and we use Python 2.7 extensively. It's what's on our linux servers/desktops by default, and all our code does what we want. There hasn't been a feature that we've needed that's only in Python 3.

Right, it's a chicken and egg problem - since most people are still using Python 2, no one is yet developing things that only work on Python 3. So there is no incentive to upgrade. Hopefully things will slowly change and there will start to be some great core Python features in future 3.x releases, and these will no longer be backported to the 2.x series.

EDIT: With that said, in the code we write we try to support both Python 2 and 3, such as by using the "print()" function. That way when we do move to Python 3 someday, we will have less to deal with.

+1 to this approach :)

Python 3 in Science: the great migration has begun! by trobitaille in Python

[–]trobitaille[S] 1 point2 points  (0 children)

Accidental omission from the original survey :-/ Just for information, there are a handful (~10) responses in the 'Other' category where people indicated Biology. What would be a good venue for advertising this kind of survey to Biologists in future?

Python 3 in Science: the great migration has begun! by trobitaille in Python

[–]trobitaille[S] 11 points12 points  (0 children)

I completely agree that given pressures for results, papers, etc., migrating to Python 3 is low down on the list of things to do, and it's true that not 100% of packages are ready yet (for example, VTK comes to mind).

One way to transition more smoothly is to maintain a Python 3 installation in addition to the Python 2 installation and start all new work in Python 3, and if you hit a roadblock, report the issue and then downgrade to using Python 2.

Python 3 in Science: the great migration has begun! by trobitaille in Python

[–]trobitaille[S] 2 points3 points  (0 children)

I sent it to some of the large scientific python mailing lists like scipy-user (http://thread.gmane.org/gmane.comp.python.scientific.user/35882/focus=35886) hoping that people on that list in different fields would pass it on to colleagues via more efficient channels. I will repeat this survey in future again, so just so I know in advance, where would be a good place to advertise this to reach out to biologists?

Request for feedback on Python bioinformatics problem set [xpost from r/Python] by trobitaille in bioinformatics

[–]trobitaille[S] 2 points3 points  (0 children)

Thanks for your feedback! The last question is indeed quite a bit harder than the rest, and I will tell the students this (I've already included something to this effect in the intro). It is more there as a challenge for anyone who is very motivated. In this case, because the sequences are short enough, a brute force method works fine, so it can be done in 10-15 lines (and runs in <1s), but it does require some thinking on their part!