Skip to content

Commit

Permalink
Move filter enum generation to function
Browse files Browse the repository at this point in the history
  • Loading branch information
koldakov committed Jan 3, 2024
1 parent bab07fc commit c60d145
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions app/repositories/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def generate_inverted_enum[T: Type[Enum]](
return Enum(name, {f"NOT_{i.name}": f"!{i.value}" for i in [*proto_enum]})


def generate_filter_enum[T: Type[Enum]](
name: str,
enums: List[T],
) -> T:
unpacked_enums: [List[T]] = [val for _e in enums for val in _e]
return Enum(name, {e.name: to_camel(e.value) for e in unpacked_enums})


class CharacterStatus(Enum):
ALIVE = "ALIVE"
DEAD = "DEAD"
Expand All @@ -57,15 +65,12 @@ class CharacterStatus(Enum):
)


CharacterStatusFilter = Enum(
CharacterStatusFilter = generate_filter_enum(
"CharacterStatusFilter",
{
i.name: to_camel(i.value)
for i in [
*CharacterStatus,
*CharacterInvertedStatus,
]
},
[
CharacterStatus,
CharacterInvertedStatus,
],
)


Expand All @@ -81,15 +86,12 @@ class CharacterGender(Enum):
)


CharacterGenderFilter = Enum(
CharacterGenderFilter = generate_filter_enum(
"CharacterGenderFilter",
{
i.name: to_camel(i.value)
for i in [
*CharacterGender,
*CharacterInvertedGender,
]
},
[
CharacterGender,
CharacterInvertedGender,
],
)


Expand All @@ -109,15 +111,12 @@ class CharacterSpecies(Enum):
)


CharacterSpeciesFilter = Enum(
CharacterSpeciesFilter = generate_filter_enum(
"CharacterSpeciesFilter",
{
i.name: to_camel(i.value)
for i in [
*CharacterSpecies,
*CharacterInvertedSpecies,
]
},
[
CharacterSpecies,
CharacterInvertedSpecies,
],
)


Expand Down

0 comments on commit c60d145

Please sign in to comment.