Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unicode support #135

Merged
merged 5 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cf_units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,10 @@ def __init__(self, unit, calendar=None):
else:
unit = str(unit).strip()

if six.PY2:
# Ensure that the string is a unicode object.
unit = six.text_type(unit.decode('utf8'))

if unit.lower().endswith(' utc'):
unit = unit[:unit.lower().rfind(' utc')]

Expand All @@ -831,7 +835,7 @@ def __init__(self, unit, calendar=None):
else:
category = _CATEGORY_UDUNIT
try:
ut_unit = _ud.parse(_ud_system, unit.encode('ascii'), UT_ASCII)
ut_unit = _ud.parse(_ud_system, unit.encode('utf-8'), UT_UTF8)
except _ud.UdunitsError as e:
self._propogate_error('Failed to parse unit "%s"' % unit, e)
if _OP_SINCE in unit.lower():
Expand Down Expand Up @@ -1893,7 +1897,7 @@ def utime(self):
if self.is_long_time_interval():
interval = self.origin.split(' ')[0]
emsg = ('Time units with interval of "months", "years" '
'(or singular of these) cannot be processed, got {!r}.')
'(or singular of these) cannot be processed, got "{!s}".')
raise ValueError(emsg.format(interval))

#
Expand Down
4 changes: 2 additions & 2 deletions cf_units/tests/integration/test_date2num.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) British Crown Copyright 2016 - 2018, Met Office
# (C) British Crown Copyright 2016 - 2019, Met Office
#
# This file is part of cf-units.
#
Expand Down Expand Up @@ -73,7 +73,7 @@ def test_long_time_interval(self):
# This test should fail with an error that we need to catch properly.
unit = 'years since 1970-01-01'
date = datetime.datetime(1970, 1, 1, 0, 0, 5)
exp_emsg = 'interval of "months", "years" .* got \'years\'.'
exp_emsg = 'interval of "months", "years" .* got "years".'
with six.assertRaisesRegex(self, ValueError, exp_emsg):
date2num(date, unit, self.calendar)

Expand Down
8 changes: 4 additions & 4 deletions cf_units/tests/test_coding_standards.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) British Crown Copyright 2013 - 2018, Met Office
# (C) British Crown Copyright 2013 - 2019, Met Office
#
# This file is part of cf-units.
#
Expand Down Expand Up @@ -52,9 +52,9 @@


LICENSE_RE_PATTERN = re.escape(LICENSE_TEMPLATE).replace(r'\{YEARS\}', '(.*?)')
# Add shebang possibility to the LICENSE_RE_PATTERN
LICENSE_RE_PATTERN = r'(\#\!.*\n)?' + LICENSE_RE_PATTERN
LICENSE_RE = re.compile(LICENSE_RE_PATTERN, re.MULTILINE)
SHEBANG = r'(\#\!.*\n)?'
ENCODING = r'(\# \-\*\- coding\: .* \-\*\-\n)?'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pelson You might want to account for white space after the trailing -*- ...?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh. Happy if you want me to, but why be generous on this? Do it right, or fix it is my opinion 😉

LICENSE_RE = re.compile(SHEBANG + ENCODING + LICENSE_RE_PATTERN, re.MULTILINE)


# Guess cf_units repo directory of cf_units - realpath is used to mitigate
Expand Down
12 changes: 12 additions & 0 deletions cf_units/tests/test_unit.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# (C) British Crown Copyright 2010 - 2019, Met Office
#
# This file is part of cf-units.
Expand Down Expand Up @@ -74,6 +75,17 @@ def test_unsupported_calendar(self):
with six.assertRaisesRegex(self, ValueError, 'unsupported calendar'):
Unit('hours since 1970-01-01 00:00:00', calendar='wibble')

def test_unicode_valid(self):
# Some unicode characters are allowed.
u = Unit('m²')
assert u.symbol == 'm2'

def test_unicode_invalid(self):
# Not all unicode characters are allowed.
msg = '[UT_UNKNOWN] Failed to parse unit "ø"'
with self.assertRaises(ValueError, msg=msg):
Unit('ø')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice...



class Test_modulus(unittest.TestCase):

Expand Down