Commits

Lars Yencken committed 2f25344

Initial commit and interface.

  • Participants

Comments (0)

Files changed (4)

+.DS_Store
+*.pyc
+*.pyo
+env

File globetrotter.py

+# -*- coding: utf-8 -*-
+#
+#  globetrotter.py
+#  globetrotter
+#
+
+"""
+Approximate country and language finding for pycountry.
+"""
+
+import pycountry
+
+def find_country(name):
+    "Find a country's information given an approximate name."
+    norm_name = _norm_countries[_norm_string(name)]
+    c = pycountry.countries.get(name=norm_name)
+    if not c:
+        raise KeyError(name)
+    return c
+
+def find_language(name):
+    "Find a language's information given an approximate name."
+    norm_query = _norm_string(name)
+    norm_name = _norm_languages.get(norm_query)
+    if norm_name:
+        l = pycountry.languages.get(name=norm_name)
+        assert l
+        return l
+
+    # try prefix matching
+    matches = [l for (n, l) in _norm_languages.iteritems() if
+            n.startswith(norm_query)]
+    if matches and len(matches) == 1:
+        return pycountry.languages.get(name=matches[0])
+
+    raise KeyError(name)
+
+def _norm_string(s):
+    return s.replace(' ', '').lower()
+
+_norm_countries = {_norm_string(c.name): c.name
+        for c in pycountry.countries.objects}
+
+_norm_languages = {_norm_string(l.name): l.name
+        for l in pycountry.languages.objects}
+

File requirements.txt

+pycountry==0.14.3
+wsgiref==0.1.2

File test_globetrotter.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+#  test_globetrotter.py
+#  globetrotter
+#
+
+import unittest
+import random
+
+import globetrotter
+
+class CountryTestCase(unittest.TestCase):
+    def test_exact(self):
+        fc = globetrotter.find_country
+        for c in ['Australia', 'Spain']:
+            self.assertEqual(fc(c).name, c)
+
+    def test_case_mangling(self):
+        fc = globetrotter.find_country
+        for c in ['Australia', 'Spain']:
+            cm = _mangle_case(c)
+            self.assertEqual(fc(cm).name, c)
+
+    def test_approx(self):
+        fc = globetrotter.find_country
+        pairs = [
+                ('Vietnam', 'Viet Nam'),
+            ]
+        for approx, name in pairs:
+            self.assertEqual(fc(approx).name, name)
+
+class LanguageTestCase(unittest.TestCase):
+    def test_exact(self):
+        fl = globetrotter.find_language
+        for l in ['English', 'French']:
+            self.assertEqual(fl(l).name, l)
+
+    def test_case_mangling(self):
+        fl = globetrotter.find_language
+        for l in ['English', 'French']:
+            ml = _mangle_case(l)
+            self.assertEqual(fl(ml).name, l)
+
+    def test_approx(self):
+        fl = globetrotter.find_language
+        pairs = [
+                ('Spanish', 'Spanish; Castilian'),
+            ]
+        for approx, name in pairs:
+            self.assertEqual(fl(approx).name, name)
+
+def _mangle_case(s):
+    if len(s) == 0:
+        return s
+
+    i = _mangle_case._r.choice(xrange(len(s)))
+    c = s[i].lower() if s[i].isupper() else s[i].upper()
+    return s[:i] + c + s[(i+1):]
+
+_mangle_case._r = random.Random(1001) # seed it manually
+
+def suite():
+    return unittest.TestSuite(map(unittest.makeSuite, [
+            CountryTestCase,
+            LanguageTestCase,
+        ]))
+
+if __name__ == '__main__':
+    unittest.TextTestRunner(verbosity=1).run(suite())