diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-02-18 15:50:43 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-02-18 15:50:43 +0100 |
commit | cc37287f27f591004b79436cb46d842f852e8da0 (patch) | |
tree | 90ba61f83d0062c4b5487827526f77f71d582dde /ishtar_common/data_importer.py | |
parent | fcb00f3d1f09f86f9819a4128f43594c5712bd4b (diff) | |
download | Ishtar-cc37287f27f591004b79436cb46d842f852e8da0.tar.bz2 Ishtar-cc37287f27f591004b79436cb46d842f852e8da0.zip |
Importer: INSEE code formater
Diffstat (limited to 'ishtar_common/data_importer.py')
-rw-r--r-- | ishtar_common/data_importer.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index b2dd0d5a0..ffe6c221d 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -269,6 +269,35 @@ class FloatFormater(Formater): 'value': value}) +class InseeFormater(Formater): + """ + Formater for "code INSEE" (statistic institute ine France) + The syntax "CodeINSEE-Year" is accepted (Ishtar trick) in order to manage + old INSEE (year is the date of creation) + """ + ERROR = _(u"\"{value}\" is not an appropriate INSEE code") + + def format(self, value): + value = value.strip() + exp = value.split('-') + code = exp[0] + try: + int(code) + except ValueError: + raise ValueError(unicode(self.ERROR).format(value)) + while len(code) < 5: + code = "0" + code + if len(exp) > 2: + raise ValueError(unicode(self.ERROR).format(value)) + elif len(exp) == 1: + return code + try: + datetime.datetime.strptime(exp[1], '%Y') + except ValueError: + raise ValueError(unicode(self.ERROR).format(value)) + return code + u"-" + exp[1] + + class YearFormater(Formater): def format(self, value): value = value.strip() |