swcarpentry / DEPRECATED-bc

DEPRECATED: This repository is now frozen - please see individual lesson repositories.
Other
299 stars 383 forks source link

[meta] Require Python3 for tools at bin #591

Closed rgaiacs closed 9 years ago

rgaiacs commented 10 years ago

When working at #578 I had some issues when testing my script with Python2 due UTF-8. Is acceptable to require the users of some tools at bin to use Python3?

gvwilson commented 10 years ago

What were the issues that came up? And is it possible to make the script 2-and-3-compatible? G

rgaiacs commented 10 years ago

What were the issues that came up?

Traceback (most recent call last):
  File "bin/update-team.py", line 68, in <module>
    write_team(contribs)
  File "bin/update-team.py", line 57, in write_team
    file_.write(u"<tr>\n<td>{0}</td>".format(contributor))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)

The script is available at https://github.com/r-gaia-cs/bc/blob/team/bin/update-team.py

And is it possible to make the script 2-and-3-compatible?

Probably. I just need to figure out how write unicode char to file. The unicode came from subprocess.check_output.

jiffyclub commented 10 years ago

What type is contributor when this errors under Python 2?

Here's an example that writes unicode to a file with Python 2 & 3 assuming that the input is bytes:

from __future__ import print_function
import tempfile

s = b'\xe0\xa5\x90'  # om
to_write = u'{}'.format(s.decode('utf-8'))

print(to_write)

file_ = tempfile.TemporaryFile()
file_.write(to_write.encode('utf-8'))

The key there is decoding the bytes to unicode, doing stuff, then encoding back to bytes before writing it out.

rgaiacs commented 10 years ago

What type is contributor when this errors under Python 2?

str

Thanks for the example but I didn't solve the problem yet.

jiffyclub commented 10 years ago

My example should work if contributor is str in Python 2, but it will not work if contributor is also str in Python 3, it needs to be bytes in both places.

gvwilson commented 9 years ago

We will switch to Python 3 after the lesson repo has exploded for both teaching and tools.