From 851b383117579dea4c293bafe5e157eed1139eef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89loi=20Rivard?= Date: Wed, 17 Apr 2024 15:27:49 +0200 Subject: [PATCH] refactor: move listify from MemoryBackend to MemoryBackend --- canaille/backends/memory/backend.py | 16 ++++++++++------ canaille/backends/memory/models.py | 4 ---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/canaille/backends/memory/backend.py b/canaille/backends/memory/backend.py index 6e567015..1fda6bf7 100644 --- a/canaille/backends/memory/backend.py +++ b/canaille/backends/memory/backend.py @@ -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.""" @@ -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, []) } @@ -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) ) ] @@ -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() @@ -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, []) @@ -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 @@ -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) diff --git a/canaille/backends/memory/models.py b/canaille/backends/memory/models.py index 6bfbaafa..9e05b8e2 100644 --- a/canaille/backends/memory/models.py +++ b/canaille/backends/memory/models.py @@ -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):