Christian Theune avatar Christian Theune committed b0966fe

Add tests and clean up coding style from ISO 3611-3 feature.

Comments (0)

Files changed (4)

 0.19 (unreleased)
 -----------------
 
-- Nothing changed yet.
+- Provide acess to historical country information (ISO 3166-3). Thanks to
+  @pferreir who provided the pull request.
 
 
 0.18 (2013-08-02)

src/pycountry/__init__.py

     xml_tags = 'iso_3166_entry'
 
 
+def choose_alpha2(record):
+    if hasattr(record, 'alpha2'):
+        return record.alpha2
+    return record.alpha4[:2]
+
+
 class HistoricCountries(CountriesBase):
     """Provides access to an ISO 3166-3 database
     (Countries that have been removed from the standard)."""
 
     # These fields are computed in a case-by-base basis
     # `alpha2` is not set in ISO-3166-3, so, we extract it from `alpha4`
-    generated_fields = dict(alpha2=lambda x: x.alpha2 if hasattr(x, 'alpha2') else getattr(x, 'alpha4')[:2],
-                            deleted=lambda x: hasattr(x, 'date_withdrawn'))
+
+    generated_fields = dict(
+        alpha2=choose_alpha2,
+        deleted=lambda x: hasattr(x, 'date_withdrawn'))
     xml_tags = ['iso_3166_entry', 'iso_3166_3_entry']
 
 
 
 
 countries = ExistingCountries(os.path.join(DATABASE_DIR, 'iso3166.xml'))
-historic_countries = HistoricCountries(os.path.join(DATABASE_DIR, 'iso3166.xml'))
+historic_countries = HistoricCountries(
+    os.path.join(DATABASE_DIR, 'iso3166.xml'))
 scripts = Scripts(os.path.join(DATABASE_DIR, 'iso15924.xml'))
 currencies = Currencies(os.path.join(DATABASE_DIR, 'iso4217.xml'))
 languages = Languages(os.path.join(DATABASE_DIR, 'iso639.xml'))

src/pycountry/db.py

     def __init__(self, filename):
         self.objects = []
         self.indices = {}
-        tags = [self.xml_tags] if isinstance(self.xml_tags, str) else self.xml_tags
 
-        self.data_class = type(self.data_class_name, (self.data_class_base,),
-                               {})
+        if isinstance(self.xml_tags, str):
+            tags = [self.xml_tags]
+        else:
+            tags = self.xml_tags
+
+        self.data_class = type(
+            self.data_class_name, (self.data_class_base,), {})
 
         f = open(filename, 'rb')
 
             if key in self.no_index:
                 continue
             else:
-                # Slightly horrible hack: to evaluate `key` at definition time of
-                # the lambda I pass it as a keyword argument.
+                # Slightly horrible hack: to evaluate `key` at definition time
+                # of the lambda I pass it as a keyword argument.
                 getter = lambda x, key=key: getattr(x, key, None)
             indices.append((key, getter))
 
             self.indices[key] = {}
 
         for obj in self.objects:
-            for name, rule in self.generated_fields.iteritems():
+            for name, rule in self.generated_fields.items():
                 value = rule(obj)
 
                 setattr(obj, name, value)

src/pycountry/tests/test_general.py

         'iso3166', pycountry.LOCALES_DIR, languages=['de'])
     german.install()
     assert __builtins__['_']('Germany') == 'Deutschland'
+
+
+def test_removed_countries():
+    ussr = pycountry.historic_countries.get(alpha2='SU')
+    assert isinstance(ussr, pycountry.db.Data)
+    assert ussr.alpha4 == u'SUHH'
+    assert ussr.alpha3 == u'SUN'
+    assert ussr.name == u'USSR, Union of Soviet Socialist Republics'
+    assert ussr.date_withdrawn == u'1992-08-30'
+    assert ussr.deleted
+    russia = pycountry.historic_countries.get(alpha2='RU')
+    assert isinstance(russia, pycountry.db.Data)
+    assert russia.name == u'Russian Federation'
+    assert not russia.deleted
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.