Last active
November 3, 2017 05:48
-
-
Save spyoungtech/14fe720c0b75054e58be57d24d3892a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Contains the User, Category, and Recipe classes""" | |
from types import SimpleNamespace | |
class DB(dict): | |
def query(self, **query_params): | |
results = [] | |
for key, obj in self.items(): | |
if all(getattr(obj, attr) == value for attr, value in query_params.items()): | |
results.append(obj) | |
return results | |
def update(self, key, **update_values): | |
obj = self[key] | |
for attr, value in update_values.items(): | |
if not hasattr(obj, attr): | |
raise AttributeError('{} object has no attribute {}'.format(type(obj), value)) | |
setattr(obj, attr, value) | |
return obj | |
def insert(self, key, obj): | |
if key in self: | |
raise ValueError('{} already exists'.format(key)) | |
self[key] = obj | |
return obj | |
def get(self, **attributes): | |
""" | |
overrides builtin get | |
""" | |
results = self.query(**attributes) | |
if not results: | |
raise ValueError('No results matching query parameters') | |
if len(results) > 1: | |
raise ValueError('Not a unique result') | |
return results[0] | |
def bulk_insert(self, *items): | |
for item in items: | |
self.insert(item) | |
class UserDatabase(DB): | |
def insert(self, user): | |
key = user.email | |
return super().insert(key, user) | |
class CategoryDatabase(DB): | |
def insert(self, category): | |
key = category.name | |
return super().insert(key, category) | |
class RecipeDatabase(DB): | |
def insert(self, recipe): | |
key = recipe.name | |
super().insert(key, recipe) | |
class User(SimpleNamespace): | |
def __init__(self, email, first_name, last_name): | |
super().__init__(email=email, first_name=first_name, last_name=last_name) | |
@property | |
def recipes(self): | |
results = recipes.query(user=self) | |
return results | |
class Category(SimpleNamespace): | |
def __init__(self, name, category_recipes=None): | |
super().__init__(name=name) | |
if category_recipes is None: | |
category_recipes = [] | |
for recipe in category_recipes: | |
if not isinstance(recipe, Recipe): | |
raise ValueError('recipes must be instance of Recipe') | |
recipe.category = self | |
if recipe.name not in recipes: | |
recipes.insert(recipe) | |
@property | |
def recipes(self): | |
return recipes.query(category=self) | |
class Recipe(SimpleNamespace): | |
def __init__(self, name, text='', category=None, user=None): | |
self.name = name | |
self.text = text | |
if category is None: | |
self._category = None | |
elif isinstance(category, str): | |
self._category = categories[category] | |
elif isinstance(category, Category): | |
self._category = category | |
else: | |
raise ValueError('category must be an instance of Category or a string of an existing category name') | |
if user is None: | |
self.user = None | |
elif isinstance(user, User): | |
self.user = user | |
elif isinstance(user, str): | |
self.user = users[user] | |
else: | |
raise ValueError('user must be an instance of User or a string of an existing user email') | |
@property | |
def category(self): | |
return self._category | |
@category.setter | |
def category(self, value): | |
if isinstance(value, str): | |
value = categories[value] | |
if not isinstance(value, Category): | |
raise ValueError('Category must be an instance of Category or a string of an existing category name') | |
self._category = value | |
users = UserDatabase() | |
categories = CategoryDatabase() | |
recipes = RecipeDatabase() | |
bob_ross = User('bob.ross@example.com', 'Bob', 'Ross') | |
bob_martin = User('bob.martin@example.com', 'Bob', 'Martin') | |
alice_cooper = User('alice@example.com', 'Alice', 'Cooper') | |
alice_walker = User('alice.walker@example.com', 'Alice', 'Walker') | |
john_smith = User('john.smith@example.com', 'John', 'Smith') | |
john_two = User('john@example.com', 'John', 'Smith') | |
users.bulk_insert(bob_ross, bob_martin, alice_cooper, alice_walker, john_smith, john_two) | |
italian = Category(name='Italian') | |
deserts = Category(name='Deserts') | |
categories.bulk_insert(italian, deserts) | |
pasta = Recipe(name='Pasta', text='1. Boil Water\n2.???\n3. enjoy', category=italian, user=bob_ross) | |
ice_cream = Recipe(name='Ice cream', text='1.Get Cream\n2. Add Ice', category=deserts,user=alice_cooper) | |
recipes.bulk_insert(pasta, ice_cream) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment