[Tutorial]: Development of an institutions data collection back-end using python 3.6(Part 2)
image source
Repository: Python, Open Source Repository
Software: For software download python 3.0 compatible with your OS here
Difficulty : Basic
What you will learn:
In this tutorial you will learn how to
Develop a Data Management system backend.
Modify our class by defining new functions for our program.
Store the data we've collected in files and set systems to update this files
For my previous posts in the python tutorial series
-Click Here* Creating a simple calculator using python 3.8(cpython)
Creating an encryption key(Cryptography)
- Part 1
- Part 2
- Part 3
- Part 4
Curriculum for this series
Part 1 of this series
Note: For best understanding go through the codes written in the previous series of this curriculum. You can find the code in the link above as it will not be included in this tutorial.
Tutorial
In the last tutorial we defined a class student
which accepts the student data we require . Developed it's __init__
function and defined a student_summary
function that presents this data in a simplified form.
In this tutorial we shall edit our inline code to include new fixed data and create new functions to copy this data file which depending on the institution and sensitivity may be encrypted or not. In this case we will be using a text file which will contain all the data collected for storage.
Go back to your __init__
function and add new values to create a text file in the name of the student data you have input. The new __init__
function should look like this.
def __init__(self,name,gender, age, major):
self.name = name
self.gender = gender
if gender == "Male" or "male":
self.gen = "He"
elif gender == "Female" or "female":
self.gen = "She"
else:
self.gen = "It"
self.age = age
time = datetime.datetime.now()
self.tor = str(time)
self.major = major
self.tuition = 50000
tuit = str(self.tuition)
self.student_data = open(self.name, "w+")
data = [self.gender, self.age, self.tor, self.major, tuit]
for dat in data:
self.student_data.write(dat)
self.student_data.close()
In the lines of code below, we set the process to create a text document containing all the student data that has been input into our system.
self.student_data = open(self.name, "w+")
data = [self.gender, self.age, self.tor, self.major, tuit]
for dat in data:
self.student_data.write(dat)
self.student_data.close()
Access to this data can be limited by password encrypting it with external tools that can be downloaded or by customized encryption methods created by the institution. In this case we do not encrypt our files.
To open a new file we used
self.student_data = open(self.name, "w+")
And at the end of the code we have to close our text file by using the
self.student_data.close()
At this point run your code and see that you don't have any errors. Now that we have updated our program to storing all our data we have to update the functions. This means that any function we write must be able to change the data stored in the text file for that student. So anytime we define a new function and run that function it will update the data within the text document.
Let's define a function to manage tuition. We will call this function update_tuition
. This function will update the pending tuition of the student either by adding to it or reducing from it.
def update_tuition(self,paid):
if paid == int(-paid):
self.tuition -= paid
if paid == int(+paid):
self.tuition += paid
x = open(self.name,"a")
x.write(str(self.tuition))
x.close()
return self.tuition
This function takes two arguments. self
and paid
. The self arguments is used in class functions to signify that the function runs for an instance of the class. Run this function outside the class by using:
print(yalz. update_tuition(+5000))
If we run this on our output console we see that the tuition which was 50,000 at the beginning is now 55,000.
Our code runs perfectly with no errors and also updates our text document. You could try to define a different function of your choice but make sure that whatever function updates the text document which would keep track of all the change in data.
You can find the Proof of work done in
GITHUB
Thank you for your contribution.
Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post, click here.
Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]
Noted with thanks, working towards perfection.
Hey @yalzeee
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!
Hello! I find your post valuable for the wafrica community! Thanks for the great post! @wafrica is now following you! ALWAYs follow @wafrica and use the wafrica tag!