-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdomain_modeling.py
79 lines (65 loc) · 3.77 KB
/
domain_modeling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Domain Modeling with Classes Exercise
# In this exercise, you'll be modeling the domain of a pet store based on the Swagger Petstore API.
# The pet store has the following entities:
# - Pet: Represents a pet in the store. Each pet has an ID, name, status, category, and tags.
# - Category: Represents a category of pets. Each category has an ID and name.
# - Tag: Represents a tag associated with a pet. Each tag has an ID and name.
# The relationships between the entities are as follows:
# - A pet belongs to one category.
# - A pet can have multiple tags.
# - A tag can be associated with multiple pets.
# Your task is to create the necessary classes to model this domain and establish the relationships between them.
# Step 1: Create the Category class
# - The Category class should have an `id` attribute (integer) and a `name` attribute (string).
# - Implement an `__init__` method to initialize the `id` and `name` attributes.
# - Implement a `__repr__` method to provide a string representation of the Category object.
# Step 2: Create the Tag class
# - The Tag class should have an `id` attribute (integer) and a `name` attribute (string).
# - Implement an `__init__` method to initialize the `id` and `name` attributes.
# - Implement a `__repr__` method to provide a string representation of the Tag object.
# Step 3: Create the Pet class
# - The Pet class should have the following attributes:
# - `id` (integer): The unique identifier of the pet.
# - `name` (string): The name of the pet.
# - `status` (string): The status of the pet (e.g., "available", "pending", "sold").
# - `category` (Category): The category the pet belongs to.
# - `tags` (list of Tag objects): The tags associated with the pet.
# - Implement an `__init__` method to initialize the attributes of the Pet class.
# - Implement a `__repr__` method to provide a string representation of the Pet object.
# - Implement an `add_tag` method that allows adding a Tag object to the `tags` list.
# Step 4: Create sample objects and establish relationships
# - Create a few sample Category objects.
# - Create a few sample Tag objects.
# - Create a few sample Pet objects, associating them with categories and tags.
# Step 5: Test the relationships
# - Print the string representation of each Pet object, including its category and tags.
# - Verify that the relationships between pets, categories, and tags are properly established.
# (optional) Step 6: Refactor the Pet.status attribute to use an Enum
# - Refactor the `status` attribute of the Pet class to use an Enum for the possible pet statuses ("available", "pending", "sold").
# - Update the Pet class to use the Enum for the `status` attribute.
# - Update the sample Pet objects to use the Enum for the `status` attribute.
# (optional) Step 7: Add type hints to the classes
# - Add type hints to the class attributes and method signatures to specify the expected types.
# (optional) Step 8: Add docstrings to the classes and methods
# - Add docstrings to the classes and methods to describe their purpose and functionality.
# (optional) Step 8: Pet.remove_tag method
# - Implement a `remove_tag` method in the Pet class that removes a tag from the list of tags based on the tag's ID.
# Example usage:
# category1 = Category(1, "Dogs")
# category2 = Category(2, "Cats")
#
# tag1 = Tag(1, "Fluffy")
# tag2 = Tag(2, "Playful")
#
# pet1 = Pet(1, "Buddy", "available", category1)
# pet1.add_tag(tag1)
# pet1.add_tag(tag2)
#
# pet2 = Pet(2, "Whiskers", "pending", category2)
# pet2.add_tag(tag1)
#
# print(pet1)
# print(pet2)
# Expected output:
# Pet(id=1, name='Buddy', status='available', category=Category(id=1, name='Dogs'), tags=[Tag(id=1, name='Fluffy'), Tag(id=2, name='Playful')])
# Pet(id=2, name='Whiskers', status='pending', category=Category(id=2, name='Cats'), tags=[Tag(id=1, name='Fluffy')])