Skip to content

Commit

Permalink
Delete new_geo method
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarrabah committed Jan 23, 2025
1 parent dcafec8 commit bc3cd91
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/frformat/formats/canton.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion src/frformat/formats/code_commune_insee.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions src/frformat/formats/code_pays.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
)
4 changes: 1 addition & 3 deletions src/frformat/formats/code_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion src/frformat/formats/commune.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion src/frformat/formats/departement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 1 addition & 1 deletion src/frformat/formats/numero_departement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 1 addition & 1 deletion src/frformat/formats/pays.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion src/frformat/formats/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
73 changes: 22 additions & 51 deletions src/frformat/set_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit bc3cd91

Please sign in to comment.