Skip to content

Commit

Permalink
WIP exploring EnumFormat as base class
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrecamilleri committed Jan 14, 2025
1 parent 95d894d commit 3175163
Showing 1 changed file with 44 additions and 43 deletions.
87 changes: 44 additions & 43 deletions src/frformat/new_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,38 @@ def is_sorted(cls) -> bool:
return True


class BaseFormat(CustomStrFormat):
def __init__(self, data: Union[FrozenSet, None], options: Options = Options()):
self._options = options
self._data = data
def _new_enum_format(
class_name: str,
name: str,
description: str,
valid_data: FrozenSet[str],
) -> Type:
class EnumFormat(CustomStrFormat):
metadata = Metadata(name, description)

normalized_extra_values = {
normalize_value(e, self._options) for e in self._options.extra_valid_values
}
def __init__(self, options: Options = Options()):
self._options = options
self._data = valid_data

if self._data is None:
raise ValueError("There is no data")
normalized_extra_values = {
normalize_value(e, self._options)
for e in self._options.extra_valid_values
}

self._normalized_values = {
normalize_value(e, self._options) for e in self._data
}.union(normalized_extra_values)
if self._data is None:
raise ValueError("There is no data")

def is_valid(self, value: str) -> bool:
normalized_value = normalize_value(value, self._options)
return normalized_value in self._normalized_values
self._normalized_values = {
normalize_value(e, self._options) for e in self._data
}.union(normalized_extra_values)

def is_valid(self, value: str) -> bool:
normalized_value = normalize_value(value, self._options)
return normalized_value in self._normalized_values

EnumFormat.__name__ = class_name
EnumFormat.__qualname__ = class_name
return EnumFormat


def new(
Expand All @@ -57,8 +70,9 @@ def new(
description: str,
valid_data: Union[VersionedSet, FrozenSet[str]],
) -> Type:
def get_geo_data(cog: Union[Millesime, str]) -> Union[FrozenSet, None]:
if isinstance(valid_data, VersionedSet):
if isinstance(valid_data, VersionedSet):

def get_geo_data(cog: Union[Millesime, str]) -> FrozenSet:
try:
cog = Millesime(cog)
except ValueError:
Expand All @@ -69,36 +83,23 @@ def get_geo_data(cog: Union[Millesime, str]) -> Union[FrozenSet, None]:
f"No data available for geographical code: {cog.value}"
)
return cog_data
return None

class GeoFormat(BaseFormat):
def __init__(self, cog: Union[Millesime, str], options: Options = Options()):
super().__init__(get_geo_data(cog), options)

data = get_geo_data(cog)
if data is None:
raise ValueError(f"Geographical data for cog '{cog}' is not available.")

self._normalized_values = {
normalize_value(val, self._options) for val in data
}

metadata = Metadata(name, description)

def get_enum_data() -> Union[FrozenSet, None]:
return valid_data if isinstance(valid_data, FrozenSet) else None

class EnumFormat(BaseFormat):
def __init__(self, options: Options = Options()):
super().__init__(get_enum_data(), options)
class GeoFormat:
def __init__(
self, cog: Union[Millesime, str], options: Options = Options()
):
data = get_geo_data(cog)
specific_enum_format_cls = _new_enum_format(
class_name, name, description, data
)
self._enum_format = specific_enum_format_cls(options)

metadata = Metadata(name, description)
def is_valid(self, value: str) -> bool:
return self._enum_format.is_valid(value)

if isinstance(valid_data, VersionedSet):
GeoFormat.__name__ = class_name
GeoFormat.__qualname__ = class_name
return GeoFormat

EnumFormat.__name__ = class_name
EnumFormat.__qualname__ = class_name
return EnumFormat
else:
return _new_enum_format(class_name, name, description, valid_data)

0 comments on commit 3175163

Please sign in to comment.