Nov-04-2021, 06:22 PM
(This post was last modified: Nov-04-2021, 10:58 PM by deanhystad.)
Instead of a user list you should have a user dictionary.
This is a quick and ugly example:
This is a quick and ugly example:
import sys import json from dataclasses import dataclass @dataclass class Account(): firstName:str lastName:str email:str username:str password:str class AccountManagement: def __init__(self): self.PendingUsers = {} def CreateAccount(self, user_id, firstName, lastName, email, username, password): self.PendingUsers[user_id] = Account(firstName, lastName, email, username, password) def WriteFile(self): users = {id: user.__dict__ for id, user in self.PendingUsers.items()} json.dump(users, sys.stdout) accounts = AccountManagement() accounts.CreateAccount(1, "a", "b", "c", "d", "e") accounts.CreateAccount(2, "A", "B", "C", "D", "E") accounts.WriteFile()I think there are packages that make dataclasses serializable. Using one of those is better than my __dict__ trick. You might also want to look at pydantic which makes a dataclass like thing that is serializeable to/from json.