I have a database class that stores the objects in database.py:
class Database(dict):
def get_objects_by_object_type(self, object_type):
# get a list of objects based on object type
db = Database()
and then I have these two classes in models.py:
class IdentifiableObject(object):
def __init__(self, object_id):
self.object_id = object_id
self.object_type = self.__class__.__name__.lower()
@classmethod
def get_object_type(cls):
return f"{cls.__name__.lower()}"
class Ingredient(IdentifiableObject):
def __init__(self, object_id, unsuitable_diets=[]):
super(Ingredient, self).__init__(object_id=object_id)
self.unsuitable_diets = unsuitable_diets
How can I get objects by type: for example, if I pass an object of type ingredient it should get all ingredients and return it.
# Ingredient.get_object_type() is equal to 'ingredient'
ingredients = db.get_objects_by_object_type(object_type=Ingredient.get_object_type())