From bc3cd919482360b01a4eec17be83c582d0c3ac98 Mon Sep 17 00:00:00 2001 From: sarrabah Date: Thu, 23 Jan 2025 17:20:37 +0100 Subject: [PATCH] Delete new_geo method --- src/frformat/formats/canton.py | 2 +- src/frformat/formats/code_commune_insee.py | 2 +- src/frformat/formats/code_pays.py | 4 +- src/frformat/formats/code_region.py | 4 +- src/frformat/formats/commune.py | 2 +- src/frformat/formats/departement.py | 2 +- src/frformat/formats/numero_departement.py | 2 +- src/frformat/formats/pays.py | 2 +- src/frformat/formats/region.py | 2 +- src/frformat/set_format.py | 73 ++++++------------- ...r_with_cog.py => test_insee_geo_format.py} | 12 ++- 11 files changed, 43 insertions(+), 64 deletions(-) rename src/tests/{test_geo_fr_with_cog.py => test_insee_geo_format.py} (95%) diff --git a/src/frformat/formats/canton.py b/src/frformat/formats/canton.py index e456e6f..520466d 100644 --- a/src/frformat/formats/canton.py +++ b/src/frformat/formats/canton.py @@ -10,4 +10,4 @@ canton_versioned_data.add_version(Millesime.M2023, CANTON_COG_2023) canton_versioned_data.add_version(Millesime.M2024, CANTON_COG_2024) -Canton = set_format.new_geo("Canton", name, description, canton_versioned_data) +Canton = set_format.new("Canton", name, description, canton_versioned_data) diff --git a/src/frformat/formats/code_commune_insee.py b/src/frformat/formats/code_commune_insee.py index 036d117..4aa6858 100644 --- a/src/frformat/formats/code_commune_insee.py +++ b/src/frformat/formats/code_commune_insee.py @@ -17,6 +17,6 @@ Millesime.M2024, CODES_COMMUNES_INSEE_COG_2024 ) -CodeCommuneInsee = set_format.new_geo( +CodeCommuneInsee = set_format.new( "CodeCommuneInsee", name, description, code_commune_insee_versioned_data ) diff --git a/src/frformat/formats/code_pays.py b/src/frformat/formats/code_pays.py index 5044a07..80f3964 100644 --- a/src/frformat/formats/code_pays.py +++ b/src/frformat/formats/code_pays.py @@ -15,7 +15,7 @@ code_pays_IS02_versioned_data.add_version(Millesime.M2023, CODES_PAYS_ISO2_COG_2023) code_pays_IS02_versioned_data.add_version(Millesime.M2024, CODES_PAYS_ISO2_COG_2024) -CodePaysISO2 = set_format.new_geo( +CodePaysISO2 = set_format.new( "CodePaysISO2", name, description, code_pays_IS02_versioned_data ) @@ -27,6 +27,6 @@ code_pays_IS03_versioned_data.add_version(Millesime.M2023, CODES_PAYS_ISO3_COG_2023) code_pays_IS03_versioned_data.add_version(Millesime.M2024, CODES_PAYS_ISO3_COG_2024) -CodePaysISO3 = set_format.new_geo( +CodePaysISO3 = set_format.new( "CodePaysISO3", name, description, code_pays_IS03_versioned_data ) diff --git a/src/frformat/formats/code_region.py b/src/frformat/formats/code_region.py index 4d960ac..7f5b6f5 100644 --- a/src/frformat/formats/code_region.py +++ b/src/frformat/formats/code_region.py @@ -34,6 +34,4 @@ code_region_versioned_data.add_version(Millesime.M2023, CODES_REGIONS_COG_2023) code_region_versioned_data.add_version(Millesime.M2024, CODES_REGIONS_COG_2024) -CodeRegion = set_format.new_geo( - "CodeRegion", name, description, code_region_versioned_data -) +CodeRegion = set_format.new("CodeRegion", name, description, code_region_versioned_data) diff --git a/src/frformat/formats/commune.py b/src/frformat/formats/commune.py index f85587f..048c80a 100644 --- a/src/frformat/formats/commune.py +++ b/src/frformat/formats/commune.py @@ -13,4 +13,4 @@ commune_versioned_data.add_version(Millesime.M2023, COMMUNES_COG_2023) commune_versioned_data.add_version(Millesime.M2024, COMMUNES_COG_2024) -Commune = set_format.new_geo("Commune", name, description, commune_versioned_data) +Commune = set_format.new("Commune", name, description, commune_versioned_data) diff --git a/src/frformat/formats/departement.py b/src/frformat/formats/departement.py index c276302..697d255 100644 --- a/src/frformat/formats/departement.py +++ b/src/frformat/formats/departement.py @@ -13,6 +13,6 @@ departement_versioned_data.add_version(Millesime.M2023, DEPARTEMENTS_COG_2023) departement_versioned_data.add_version(Millesime.M2024, DEPARTEMENTS_COG_2024) -Departement = set_format.new_geo( +Departement = set_format.new( "Departement", name, description, departement_versioned_data ) diff --git a/src/frformat/formats/numero_departement.py b/src/frformat/formats/numero_departement.py index 81f2a14..c4e0381 100644 --- a/src/frformat/formats/numero_departement.py +++ b/src/frformat/formats/numero_departement.py @@ -24,6 +24,6 @@ Millesime.M2024, NUMEROS_DEPARTEMENTS_COG_2024 ) -NumeroDepartement = set_format.new_geo( +NumeroDepartement = set_format.new( "NumeroDepartement", name, description, numero_departement_versioned_data ) diff --git a/src/frformat/formats/pays.py b/src/frformat/formats/pays.py index e0e50e5..771c71f 100644 --- a/src/frformat/formats/pays.py +++ b/src/frformat/formats/pays.py @@ -11,4 +11,4 @@ pays_versioned_data = VersionedSet[Millesime]() pays_versioned_data.add_version(Millesime.M2024, PAYS_COG_2024) -Pays = set_format.new_geo("Pays", name, description, pays_versioned_data) +Pays = set_format.new("Pays", name, description, pays_versioned_data) diff --git a/src/frformat/formats/region.py b/src/frformat/formats/region.py index c6d1235..7928295 100644 --- a/src/frformat/formats/region.py +++ b/src/frformat/formats/region.py @@ -12,4 +12,4 @@ region_versioned_data.add_version(Millesime.M2023, REGIONS_COG_2023) region_versioned_data.add_version(Millesime.M2024, REGIONS_COG_2024) -Region = set_format.new_geo("Region", name, description, region_versioned_data) +Region = set_format.new("Region", name, description, region_versioned_data) diff --git a/src/frformat/set_format.py b/src/frformat/set_format.py index 277e824..dd69e9d 100644 --- a/src/frformat/set_format.py +++ b/src/frformat/set_format.py @@ -6,8 +6,6 @@ - GenericSetFormat creates a validator with valid data passed on the fly - `new` creates specialized versions where data is tied to the class -- `new_geo` creates an even more specialized version for geographical data - from INSEE """ from enum import Enum @@ -46,6 +44,28 @@ def is_valid(self, value: str) -> bool: V = TypeVar("V", bound="Version") +@total_ordering +class Millesime(Enum): + """Millesime class implements the `Version` protocol methods.""" + + M2023 = "2023" + M2024 = "2024" + LATEST = "latest" + + def __eq__(self, other) -> bool: + return self.value == other.value + + def __lt__(self, other) -> bool: + return self.value < other.value + + def get_id(self) -> str: + return self.value + + @classmethod + def is_sorted(cls) -> bool: + return True + + def new( class_name: str, name: str, @@ -86,52 +106,3 @@ def __init__(self, options: Options = Options()): specialized_set_format.metadata = Metadata(name, description) return specialized_set_format - - -################################ -# Insee Geo format ############# -################################ - - -@total_ordering -class Millesime(Enum): - """Millesime class implements the `Version` protocol methods.""" - - M2023 = "2023" - M2024 = "2024" - LATEST = "latest" - - def __eq__(self, other) -> bool: - return self.value == other.value - - def __lt__(self, other) -> bool: - return self.value < other.value - - def get_id(self) -> str: - return self.value - - @classmethod - def is_sorted(cls) -> bool: - return True - - -def new_geo( - class_name: str, name: str, description: str, valid_data: VersionedSet[Millesime] -) -> Type: - """A set format specialized on Insee geographical data, versioned by - the Millesime enum. - - The main difference is that the __init__ function takes a "cog" parameter for the version, - which means "Code officiel géographique" (Official Geographical Code). - - """ - VersionedSetFormat = new(class_name, name, description, valid_data) - - original_init = VersionedSetFormat.__init__ - - def new_init(self, cog: Union[Millesime, str], options=Options()): - original_init(self, cog, options) # type: ignore - - setattr(VersionedSetFormat, "__init__", new_init) - - return VersionedSetFormat diff --git a/src/tests/test_geo_fr_with_cog.py b/src/tests/test_insee_geo_format.py similarity index 95% rename from src/tests/test_geo_fr_with_cog.py rename to src/tests/test_insee_geo_format.py index e3b3606..34caaf1 100644 --- a/src/tests/test_geo_fr_with_cog.py +++ b/src/tests/test_insee_geo_format.py @@ -14,6 +14,13 @@ class ValidatorTest: + """ + This class tests all INSEE geographical formats, versioned by the Millesime enum. + + The __init__ function takes a cog parameter for the version, + which refers to the "Code officiel géographique" (Official Geographical Code). + """ + def __init__(self, cog, validTestCases, invalidTestCases, formatClass): self.cog = cog self.validTestCases = validTestCases @@ -215,7 +222,10 @@ def test_all_validators_with_cog(): for tc in test_cases: validatorTest = ValidatorTest( - tc["cog"], tc["validTestCases"], tc["invalidTestCases"], tc["formatClass"] + tc["cog"], + tc["validTestCases"], + tc["invalidTestCases"], + tc["formatClass"], ) validatorTest.run_all_tests()