-1

I am trying to write a program for a GPA Calculator that lets the user input each Subject, with subcategories of subject name, final grade, and the credits it gives. I want this program to be able to be accessed over and over, and want to know a way to store data, and access it and be able to pull the data from the storage, as well as changing and adding new data. I want to know how to do this, so I can store the users, with username and password, and their gpa data.

    class User:
        """This class represents users in this program"""

        def __init__(self, username, password):
            self.username = username
            self.password = password

    class Subject:
        """This class represents what is needed to calculate gpa with classes"""

        def __init__(self, sub_name, final_grade, credit):
            self.sub_name = sub_name
            self.final_grade = final_grade
            self.credit = credit

    users = {}

    username = str(input("Username: "))
    password = str(input("Password: "))

    users[username] = User(username)
    users[password] = User(password)
  • 1
    Does this answer your question? [Best method of saving data](https://stackoverflow.com/questions/14509269/best-method-of-saving-data) – Tomerikoo Sep 21 '20 at 11:12

1 Answers1

0

The simplest and fastest way would be to use Python's builtin pickle module.

Here is an example:

import pickle

class User:
    def __init__(self, username, password):
        self.username = username
        self.password = password


user = User("Bryan", "132")

# Serializing the Python object into bytes using pickle
user_file = open("save_file_name.user", "wb")
pickle.dump(user, user_file)

# Deserializing the data into a Python object
user_file = open("save_file_name.user", "rb")
user = pickle.load(user_file)
print(user.username, user.password)  # Output: Bryan 132

This would work for any Python object (eg: dictionaries, lists, etc).

vrevolverr
  • 339
  • 3
  • 6
  • Thanks, this is really helpful, would there be a more advanced way that can store more, or more effectively that I can try to do in the future? I want to start with the pickle, and I want to work towards other ways of storing data. – Phasakorn Chivatxaranukul Sep 21 '20 at 11:33
  • You could look into databases. For your project, a document-oriented key-value database or similar should suffice in which data for each user can be stored as "documents" where there are "fields" and "values" for each field in each document. The database can be stored and managed locally (tinydb), or used via an online service (Firebase Firestore). If you are looking for a more sophisticated database, you could look into relational databases (MySQL, sqlite) – vrevolverr Sep 21 '20 at 13:01