Skip to content

Commit

Permalink
refactor: move listify from MemoryBackend to MemoryBackend
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed May 3, 2024
1 parent cc9ed33 commit 851b383
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
16 changes: 10 additions & 6 deletions canaille/backends/memory/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from canaille.backends import Backend


def listify(value):
return value if isinstance(value, list) else [value]


class MemoryBackend(Backend):
indexes: Dict[str, Dict[str, Any]] = None
"""Associates ids and states."""
Expand Down Expand Up @@ -79,7 +83,7 @@ def query(self, model, **kwargs):
ids = {
id
for attribute, values in kwargs.items()
for value in model.serialize(model.listify(values))
for value in model.serialize(listify(values))
for id in self.attribute_index(model, attribute).get(value, [])
}

Expand All @@ -104,7 +108,7 @@ def fuzzy(self, model, query, attributes=None, **kwargs):
if any(
query.lower() in value.lower()
for attribute in attributes
for value in model.listify(instance._state.get(attribute, []))
for value in listify(instance._state.get(attribute, []))
if isinstance(value, str)
)
]
Expand Down Expand Up @@ -163,7 +167,7 @@ def index_save(self, instance):

# update the index for each attribute
for attribute in instance.attributes:
attribute_values = instance.listify(instance._state.get(attribute, []))
attribute_values = listify(instance._state.get(attribute, []))
for value in attribute_values:
self.attribute_index(instance.__class__, attribute).setdefault(
value, set()
Expand All @@ -178,7 +182,7 @@ def index_save(self, instance):
mirror_attribute_index = self.attribute_index(
model, mirror_attribute
).setdefault(instance.id, set())
for subinstance_id in instance.listify(instance._state.get(attribute, [])):
for subinstance_id in listify(instance._state.get(attribute, [])):
# add the current objet in the subinstance state
subinstance_state = self.index(model)[subinstance_id]
subinstance_state.setdefault(mirror_attribute, [])
Expand All @@ -195,7 +199,7 @@ def index_delete(self, instance):

# update the mirror attributes of the submodel instances
for attribute in instance.attributes:
attribute_values = instance.listify(old_state.get(attribute, []))
attribute_values = listify(old_state.get(attribute, []))
for value in attribute_values:
self.attribute_index(instance.__class__, attribute)[value].remove(
instance.id
Expand All @@ -221,7 +225,7 @@ def index_delete(self, instance):

# update the index for each attribute
for attribute in instance.attributes:
attribute_values = instance.listify(old_state.get(attribute, []))
attribute_values = listify(old_state.get(attribute, []))
for value in attribute_values:
if (
value in self.attribute_index(instance.__class__, attribute)
Expand Down
4 changes: 0 additions & 4 deletions canaille/backends/memory/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ def __init__(self, *args, **kwargs):
def __repr__(self):
return f"<{self.__class__.__name__} id={self.id}>"

@classmethod
def listify(cls, value):
return value if isinstance(value, list) else [value]

@classmethod
def serialize(cls, value):
if isinstance(value, list):
Expand Down

0 comments on commit 851b383

Please sign in to comment.