Introduction
CRUD (Create, Read, Update, Delete) operations are fundamental in software development, allowing us to manage data effectively. In this beginner-friendly blog post, we are going to dive into the world of CRUD operations using Python, focusing on Zodiac signs because or doesn't check their horoscope? By the end of this journey, you'll have a solid understanding of how to manipulate Zodiac sign data using Python.
1. Creating Zodiac Signs:
To create Zodiac signs, we'll start by defining a dictionary to store the signs' names and date ranges.
zodiac_signs = {
'Aries': (3, 21, 4, 19),
'Taurus': (4, 20, 5, 20),
'Gemini': (5, 21, 6, 20),
# Add more signs here...
}
2. Reading Zodiac Signs:
Let's implement a function to read Zodiac signs based on a provided date.
def find_zodiac_sign(month, day):
for sign, (start_month, start_day, end_month, end_day) in zodiac_signs.items():
if (month == start_month and day >= start_day) or (month == end_month and day <= end_day):
return sign
return "Unknown"
This function iterates through the dictionary and returns the Zodiac sign that matches the provided date.
3. Updating Zodiac Signs:
Let's say we want to update the date range for a Zodiac sign. We can simply modify the values in the dictionary.
def update_zodiac_sign(sign, start_month, start_day, end_month, end_day):
if sign in zodiac_signs:
zodiac_signs[sign] = (start_month, start_day, end_month, end_day)
print(f"{sign} sign updated successfully!")
else:
print(f"Sign '{sign}' not found.")
4. Deleting Zodiac Signs:
To delete a Zodiac sign, we'll define a function to remove it from our dictionary.
def delete_zodiac_sign(sign):
if sign in zodiac_signs:
del zodiac_signs[sign]
print(f"{sign} sign deleted successfully!")
else:
print(f"Sign '{sign}' not found.")
5. Implementing CLI Interface:
Now, let's create a simple CLI interface using input()
to interact with our CRUD operations.
def main():
while True:
print("1. Read Zodiac Sign")
print("2. Update Zodiac Sign")
print("3. Delete Zodiac Sign")
print("4. Exit")
choice = input("Enter your choice: ")
if choice == "1":
month = int(input("Enter birth month (1-12): "))
day = int(input("Enter birth day (1-31): "))
print(f"Zodiac sign: {find_zodiac_sign(month, day)}")
elif choice == "2":
sign = input("Enter sign to update: ")
start_month = int(input("Enter new start month: "))
start_day = int(input("Enter new start day: "))
end_month = int(input("Enter new end month: "))
end_day = int(input("Enter new end day: "))
update_zodiac_sign(sign, start_month, start_day, end_month, end_day)
elif choice == "3":
sign = input("Enter sign to delete: ")
delete_zodiac_sign(sign)
elif choice == "4":
print("Exiting program.")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
Conclusion:
We just build a CLI application that performs CRUD operations on Zodiac Signs using Python learned in Phase 3. I decided to do CRUD because I really enjoyed this part of the project. I was able to use ipdb set_trace() to debug and run my CLI file to see my tables in SQL explorer.
Happy Coding
/\___/\
\ -.- /
) (
/ _ \
.'-🦋 🦋-'.
(___/ \___)