Пример #1
0
class RFA(clcsv.SheetWrapper):
    EMPTY_STRING_AS_NONE = True

    COERCE = {
        "Constituency Name": dataobj.to_unicode(),
        "Region": dataobj.to_unicode(),
        "Country": dataobj.to_unicode(),
        "Constituency ID": dataobj.to_unicode(),
        "Constituency Type": dataobj.to_unicode()
    }

    DEFAULT_COERCE = [dataobj.to_int()]
Пример #2
0
class SpeciesSheet(clcsv.SheetWrapper):

    HEADERS = {
        # main identifying field
        u'Species ID': u'species_id',

        # fields in the species sheet
        u'Kingdom': u'kingdom',
        u'Phylum': u'phylum',
        u'Class': u'species_class',
        u'Order': u'order',
        u'Family': u'family',
        u'Genus': u'genus',
        u'Species': u'species',
        u'Authority': u'authority',
        u'Infraspecific rank': u'infraspecific_rank',
        u'Infraspecific name': u'infraspecific_name',
        u'Infraspecific authority': u'infraspecific_authority',
        u'Stock/subpopulation': u'subpopulation',
        u'Synonyms': u'synonyms',
        u'Common names (Eng)': u'common_names_eng',
        u'Common names (Fre)': u'common_names_fre',
        u'Common names (Spa)': u'common_names_spa',
        u'Red List status': u'red_list_status_code',
        u'Red List criteria': u'red_list_criteria',
        u'Red List criteria version': u'red_list_criteria_version',
        u'Year assessed': u'year_assessed',
        u'Population trend': u'population_trend',
        u'Petitioned': u'petitioned',
    }

    COERCE = {
        "synonyms": synonym,
        "common_names_eng": common_name,
        "red_list_criteria_version": dataobj.to_float(),
        "year_assessed": dataobj.to_int(),
        "petitioned": petition,
    }

    DEFAULT_COERCE = [dataobj.to_unicode()]

    IGNORE_VALUES = {}

    EMPTY_STRING_AS_NONE = True

    def __init__(self, path):
        super(SpeciesSheet, self).__init__(path)
Пример #3
0
    def test_02_construct(self):
        struct = {
            "fields": {
                "one": {
                    "coerce": "unicode"
                }
            },
            "objects": ["three", "four", "five"],
            "lists": {
                "six": {
                    "contains": "whatever"
                },
                "seven": {
                    "contains": "field",
                    "coerce": "integer"
                },
                "eight": {
                    "contains": "object"
                },
                "nine": {
                    "contains": "object"
                }
            },
            "required": ["one"],
            "structs": {
                # Note there's no structure for three, so it can be anything
                "four": {
                    "fields": {
                        "alpha": {
                            "coerce": "integer"
                        }
                    }
                },
                "nine": {
                    "fields": {
                        "beta": {
                            "coerce": "integer"
                        }
                    }
                }
            }
        }

        coerce = {"unicode": dataobj.to_unicode(), "integer": dataobj.to_int()}

        obj = {"one": "hello"}
        new = dataobj.construct(obj, struct, coerce)
        assert new["one"] == u"hello"
        assert isinstance(new["one"], unicode)

        # try adding a disallowed field
        obj["two"] = "world"

        with self.assertRaises(dataobj.DataStructureException):
            try:
                new = dataobj.construct(obj, struct, coerce)
            except dataobj.DataStructureException as e:
                print e.message
                raise e

        # try not providing a required field
        obj = {}
        with self.assertRaises(dataobj.DataStructureException):
            try:
                new = dataobj.construct(obj, struct, coerce)
            except dataobj.DataStructureException as e:
                print e.message
                raise e

        # try an unvalidated sub-object
        obj = {"one": "hello"}
        obj["three"] = {"something": "here"}
        new = dataobj.construct(obj, struct, coerce)
        assert new["three"]["something"] == "here"

        # and a validated sub-object
        obj["four"] = {"alpha": "4"}
        new = dataobj.construct(obj, struct, coerce)
        assert new["four"]["alpha"] == 4

        # and a field that should be an object but isn't
        obj["five"] = "something here"
        with self.assertRaises(dataobj.DataStructureException):
            try:
                new = dataobj.construct(obj, struct, coerce)
            except dataobj.DataStructureException as e:
                print e.message
                raise e

        # a list where the list contains directive is broken
        del obj["five"]
        obj["six"] = ["6"]
        with self.assertRaises(dataobj.DataStructureException):
            try:
                new = dataobj.construct(obj, struct, coerce)
            except dataobj.DataStructureException as e:
                print e.message
                raise e

        # successfully coerce a list of field values
        del obj["six"]
        obj["seven"] = ["1", "1", "2", "3"]
        new = dataobj.construct(obj, struct, coerce)
        assert new["seven"] == [1, 1, 2, 3]

        # faile to coerce a list of field values
        obj["seven"] = ["a", "b", "walton-upon-thames"]
        with self.assertRaises(dataobj.DataStructureException):
            try:
                new = dataobj.construct(obj, struct, coerce)
            except dataobj.DataStructureException as e:
                print e.message
                raise e

        # list is supposed to contain an object but doesn't
        del obj["seven"]
        obj["eight"] = ["not an object"]
        with self.assertRaises(dataobj.DataStructureException):
            try:
                new = dataobj.construct(obj, struct, coerce)
            except dataobj.DataStructureException as e:
                print e.message
                raise e

        # list contains an object but no validation required on its structure
        obj["eight"] = [{"an": "object"}]
        new = dataobj.construct(obj, struct, coerce)
        assert new["eight"][0]["an"] == "object"

        # list contains an object which fails to validate
        obj["nine"] = [{"beta": "whatever"}]
        with self.assertRaises(dataobj.DataStructureException):
            try:
                new = dataobj.construct(obj, struct, coerce)
            except dataobj.DataStructureException as e:
                print e.message
                raise e

        # list contains an object that does validate
        obj["nine"] = [{"beta": 9}]
        new = dataobj.construct(obj, struct, coerce)
        assert new["nine"][0]["beta"] == 9
Пример #4
0
 def add_measure(self, year, value):
     if value is None:
         value = 0
     self._set_single("year", year, coerce=dataobj.to_int())
     self._set_single("value", value, coerce=dataobj.to_float())
 def year_of_publication(self):
     return self._get_single("journalInfo.yearOfPublication",
                             dataobj.to_int())
 def month_of_publication(self):
     return self._get_single("journalInfo.monthOfPublication",
                             dataobj.to_int())
Пример #7
0
 def add_measure(self, year, value):
     if value is None:
         value = 0
     self._set_single("year", year, coerce=dataobj.to_int())
     self._set_single("value", value, coerce=dataobj.to_float())
Пример #8
0
    def test_02_construct(self):
        struct = {
            "fields" : {
                "one" : {"coerce" : "unicode"}
            },
            "objects" : [
                "three", "four", "five"
            ],
            "lists" : {
                "six" : {"contains" : "whatever"},
                "seven" : {"contains" : "field", "coerce" : "integer"},
                "eight" : {"contains" : "object"},
                "nine" : {"contains" : "object"}
            },
            "required" : ["one"],
            "structs" : {
                # Note there's no structure for three, so it can be anything
                "four" : {
                    "fields" : {
                        "alpha" : {"coerce" : "integer"}
                    }
                },
                "nine" : {
                    "fields" : {
                        "beta" : {"coerce" : "integer"}
                    }
                }
            }
        }

        coerce = {
            "unicode" : dataobj.to_unicode(),
            "integer" : dataobj.to_int()
        }

        obj = { "one" : "hello" }
        new = dataobj.construct(obj, struct, coerce)
        assert new["one"] == u"hello"
        assert isinstance(new["one"], unicode)

        # try adding a disallowed field
        obj["two"]  = "world"

        with self.assertRaises(dataobj.DataStructureException):
            try:
                new = dataobj.construct(obj, struct, coerce)
            except dataobj.DataStructureException as e:
                print e.message
                raise e

        # try not providing a required field
        obj = {}
        with self.assertRaises(dataobj.DataStructureException):
            try:
                new = dataobj.construct(obj, struct, coerce)
            except dataobj.DataStructureException as e:
                print e.message
                raise e

        # try an unvalidated sub-object
        obj = {"one" : "hello"}
        obj["three"] = {"something" : "here"}
        new = dataobj.construct(obj, struct, coerce)
        assert new["three"]["something"] == "here"

        # and a validated sub-object
        obj["four"] = {"alpha" : "4"}
        new = dataobj.construct(obj, struct, coerce)
        assert new["four"]["alpha"] == 4

        # and a field that should be an object but isn't
        obj["five"] = "something here"
        with self.assertRaises(dataobj.DataStructureException):
            try:
                new = dataobj.construct(obj, struct, coerce)
            except dataobj.DataStructureException as e:
                print e.message
                raise e

        # a list where the list contains directive is broken
        del obj["five"]
        obj["six"] = ["6"]
        with self.assertRaises(dataobj.DataStructureException):
            try:
                new = dataobj.construct(obj, struct, coerce)
            except dataobj.DataStructureException as e:
                print e.message
                raise e

        # successfully coerce a list of field values
        del obj["six"]
        obj["seven"] = ["1", "1", "2", "3"]
        new = dataobj.construct(obj, struct, coerce)
        assert new["seven"] == [1, 1, 2, 3]

        # faile to coerce a list of field values
        obj["seven"] = ["a", "b", "walton-upon-thames"]
        with self.assertRaises(dataobj.DataStructureException):
            try:
                new = dataobj.construct(obj, struct, coerce)
            except dataobj.DataStructureException as e:
                print e.message
                raise e

        # list is supposed to contain an object but doesn't
        del obj["seven"]
        obj["eight"]  = ["not an object"]
        with self.assertRaises(dataobj.DataStructureException):
            try:
                new = dataobj.construct(obj, struct, coerce)
            except dataobj.DataStructureException as e:
                print e.message
                raise e

        # list contains an object but no validation required on its structure
        obj["eight"] = [{"an" : "object"}]
        new = dataobj.construct(obj, struct, coerce)
        assert new["eight"][0]["an"] == "object"

        # list contains an object which fails to validate
        obj["nine"] = [{"beta" : "whatever"}]
        with self.assertRaises(dataobj.DataStructureException):
            try:
                new = dataobj.construct(obj, struct, coerce)
            except dataobj.DataStructureException as e:
                print e.message
                raise e

        # list contains an object that does validate
        obj["nine"] = [{"beta" : 9}]
        new = dataobj.construct(obj, struct, coerce)
        assert new["nine"][0]["beta"] == 9