Developing a Simple User-Friendly Application with Object-Oriented Mapping in Python
Hello, I hope you are all doing well. In this blog, I want to share a fun project I created for my phase-3 section at Flatiron School. For this project, I built a small application using one-to-many relationship class models, with patients on the many side and hospitals on the one side. First, I will show you my backend, and then I'll show you my frontend.
Backend
For the backend of my project, I'll start by introducing my __init__
file. In this file, sqlite3
is imported, and a connector and a cursor are created so I can implement OOM on my classes.
After that’s created. In my hospital.py
file there is a class which initializes a name and city attribute. For the attributes I set property and setter functions for the attributes. After That’s done I started creating my initialize and class methods. Here’s a few of them below.
My save()
method uses SQL to create a new hospital row and save it in my database. Once saved, I add it to my all
dictionary, which stores all the class objects.
Now, let's move on to my create()
class method. This method creates a new hospital object, saves the row into the database, and then returns the object.
The delete()
method uses SQL to delete the row based on the ID. Then, the method removes the object from my all
class dictionary.
In the above code snippet, I will discuss some of the functions created. The first one is the instance_from_db()
method, which checks if a hospital exists in the class's all dictionary. If it does, the object's attributes are updated. Otherwise, it creates a new object, sets the object's ID, and adds it to the class's all dictionary.
My get_all()
method fetches all the hospitals from the database, converts each row into a hospital object, and returns a list of all the hospitals.
The find_by_id()
and find_by_name()
methods return the object if the ID or name matches. If found, the row is converted into a hospital object.
Patient.py
My patient file has almost all the above functionality so I wont share everything. Although, I will share my class attributes with you. These attributes include a name, age, illness, and hospital_id to reference a specific hospital row’s id.
Frontend
The front end includes two simple files the cli.py
file and the helpers.py
file.