[Biopython-dev] Post Biopython 1.62 release, clean-up after dropping Python 2.5
Peter Cock
p.j.a.cock at googlemail.com
Fri Aug 30 14:22:26 UTC 2013
Thanks Sergei - that clarified things.
Unfortunately this doesn't just break our convenience __main__ trick for
running the doctests in any single module, it also breaks doing it via:
$ python run_tests.py doctest
This means we'd have to update the doctests to also use Python 3
style print functions... which may be premature (we'll need to do
this at some point though).
How about the less ambitious plan of replacing lines like this:
print variable
with:
print(variable)
This will be understood as a print function call on Python 3 (and work),
and will also work on Python 2 (without the future import) where it will
be parsed as redundant parentheses.
Note you can't use this trick where more than one variable is printed,
because then on Python 2 the brackets will create a tuple instead.
Peter
On Fri, Aug 30, 2013 at 2:28 PM, Sergei Lebedev <superbobry at gmail.com> wrote:
> Sure, a common pattern for a lot of BioPython modules seems to be:
>
> # +from __future__ import print_function
>
>
> def foo():
> """A docstring with print statement.
>
> >>> print "foo"
> foo
> """
> print "Running foo ..."
> # +print("Running foo ...")
>
>
> if __name__ == "__main__":
> import doctest
> doctest.testmod()
>
> where foo is some function, which uses print statement in its body. Since we
> want to switch from print statements to print function we replace print
> "Running foo ..." with a print() call and add from __future__ import ... to
> the beginning of the module.
>
> What happens if we try to run the doctests after we've switched to
> print_function?
>
> $ python /tmp/foo.py
> **********************************************************************
> File "/tmp/foo.py", line 7, in __main__.foo
> Failed example:
> print "foo"
> Exception raised:
> Traceback (most recent call last):
> File ".../doctest.py", line 1254, in __run
> compileflags, 1) in test.globs
> File "<doctest __main__.foo[0]>", line 1
> print "foo"
> ^
> SyntaxError: invalid syntax
> **********************************************************************
> 1 items had failures:
> 1 of 1 in __main__.foo
> ***Test Failed*** 1 failures.
>
> So, enabling print_function makes doctests using print statement fail with a
> SyntaxError, as shown by the example above. Thus, if we want to get rid of
> print statement in the code we have no other choice but to do the same it in
> the doctests.
>
> Sergei
>
>
>
> On August 30, 2013 at 5:14:14 PM, Peter Cock (p.j.a.cock at googlemail.com)
> wrote:
>
> On Fri, Aug 30, 2013 at 1:58 PM, Sergei Lebedev <superbobry at gmail.com>
> wrote:
>>> (8) Excluding doctests and the Tutorial, use print function
>>> rather than print statement. e.g. replace this:
>>
>> Unfortunately we cannot exclude doctests, because 'from __future__' import
>> is module wide, thus the 'doctest.testmod()' will raise a SyntaxError on
>> docstrings with print statement.
>>
>> Sergei
>
> Could you clarify this? Does this cause a problem via:
>
> [Tests]$ python run_tests.py doctest
>
> If you have a small example, copy & paste the "git diff" output here.
>
> Peter
More information about the Biopython-dev
mailing list