Skip to content

Commit

Permalink
More flexibility for units (#43)
Browse files Browse the repository at this point in the history
Allows both the raised and plain notation to be used for powers in units.

Also add ha (hectare) as a known unit.
  • Loading branch information
lokal-profil authored Jun 27, 2017
1 parent ed80289 commit c200c33
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
18 changes: 18 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import mock

from wikidataStuff.helpers import (
get_unit_q,
is_number,
is_int,
is_pos_int,
Expand Down Expand Up @@ -241,3 +242,20 @@ def test_fill_cache_redirects_and_warns(self):
self.warning.assert_called_once_with(
'fill_cache is deprecated. Use fill_cache_wdqs instead.')
self.assertEqual(result, expected)


class TestGetUnitQ(unittest.TestCase):

"""Test get_unit_q()."""

def test_get_unit_q_empty(self):
self.assertEqual(get_unit_q(''), None)

def test_get_unit_q_match(self):
self.assertEqual(get_unit_q('km2'), 'Q712226')

def test_get_unit_q_no_match(self):
self.assertEqual(get_unit_q('foo'), None)

def test_get_unit_q_raised(self):
self.assertEqual(get_unit_q('km²'), 'Q712226')
10 changes: 9 additions & 1 deletion wikidataStuff/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,9 @@ def get_unit_q(unit):
"""
Given a unit abbreviation return the appropriate qid.
Powers can be given as either a plain number or a raised number, e.g.
km2 or km².
@param unit: the unit abbreviation to look up
@type unit: str
@return: Q-value of matching wikidata entry or None if not mapped
Expand All @@ -511,8 +514,13 @@ def get_unit_q(unit):
'km': 'Q828224',
'cm': 'Q174728',
'mm': 'Q174789',
'km²': 'Q712226'
'km2': 'Q712226',
'ha': 'Q35852'
}

# standardise input
unit = unit.replace('²', '2').replace('³', '3')

return units.get(unit)


Expand Down

0 comments on commit c200c33

Please sign in to comment.