Home API Notes

Hacks

  • Add new attributes/variables to the Class. Make class specific to your CPT work.

  • Add classOf attribute to define year of graduation

  • Add setter and getter for classOf
  • Add dob attribute to define date of birth
  • This will require investigation into Python datetime objects as shown in example code below
  • Add setter and getter for dob
  • Add instance variable for age, make sure if dob changes age changes
  • Add getter for age, but don't add/allow setter for age
  • Update and format tester function to work with changes
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import date
import json

class User:    

    def __init__(self, name, uid, password, dob, classOf, make):
        self._name = name    # variables with self prefix become part of the object, 
        self._uid = uid
        self.set_password(password)
        self._dob = dob
        self._classOf = classOf
        self._make = make

    @property
    def name(self):
        return self._name
    
    @name.setter
    def name(self, name):
        self._name = name
    
    @property
    def uid(self):
        return self._uid
    
    @uid.setter
    def uid(self, uid):
        self._uid = uid
        
    def is_uid(self, uid):
        return self._uid == uid
    
    @property
    def dob(self):
        dob_string = self._dob.strftime('%m-%d-%Y')
        return dob_string
    
    @dob.setter
    def dob(self, dob):
        if type(dob) == date:
            self._dob = dob

    #classOf
    @property
    def classOf(self):
        return self._classOf
    
    @classOf.setter
    def classOf(self, classOf):
        self._classOf = classOf

    # Getter for car make
    @property
    def make(self):
        return self._make

    # Adding a setter function for make
    @make.setter
    def make(self, make):
        self._make = make

    @property
    def age(self):
        today = date.today()
        return today.year - self._dob.year - ((today.month, today.day) < (self._dob.month, self._dob.day))
    
    @property
    def dictionary(self):
        dict = {
            "name" : self.name,
            "uid" : self.uid,
            "dob" : self.dob,
            "age" : self.age,
            "classOf" : self.classOf,
            "make" : self.make
        }
        return dict
    
    def set_password(self, password):
        """Create a hashed password."""
        self._password = generate_password_hash(password, method='sha256')

    def is_password(self, password):
        """Check against hashed password."""
        result = check_password_hash(self._password, password)
        return result
    
    def __str__(self):
        return json.dumps(self.dictionary)
    
    def __repr__(self):
        return f'User(name={self._name}, uid={self._uid}, password={self._password},dob={self._dob}, classOf={self._classOf}, make={self._make})'
    
def tester(users, uid, psw):
    result = ""
    for user in users:

        if user.uid == uid and user.is_password(psw):  
            print("* ", end="")
            result = user
        print(user)
    return result

# Adding a second user
if __name__ == "__main__":
    u1 = User(name='Thomas Edison', uid='toby', password='123toby', dob=date(1847, 2, 11) , classOf=1865 , make="Ford")   
    u2 = User(name='Ethan Tran', uid='ethan', password='ethan123', dob=date(2007, 5, 19) , classOf=2025 , make="BMW")
    users = [u1, u2]

# Output shows a star or "*" due to the tester using the correct password
    tester(users, u2.uid, "ethan123")
    print("JSON ready string:\n", u1, "\n") 
    print("Raw Variables of object:\n", vars(u1), "\n") 
    print("Raw Attributes and Methods of object:\n", dir(u1), "\n")
    print("Representation to Re-Create the object:\n", repr(u1), "\n") 
{"name": "Thomas Edison", "uid": "toby", "dob": "02-11-1847", "age": 175, "classOf": 1865, "make": "Ford"}
* {"name": "Ethan Tran", "uid": "ethan", "dob": "05-19-2007", "age": 15, "classOf": 2025, "make": "BMW"}
JSON ready string:
 {"name": "Thomas Edison", "uid": "toby", "dob": "02-11-1847", "age": 175, "classOf": 1865, "make": "Ford"} 

Raw Variables of object:
 {'_name': 'Thomas Edison', '_uid': 'toby', '_password': 'sha256$PRhIHZBTIUef4gJZ$8d8568942cb27d88636af7a201be31f751afb271fccace59c0b6ab77c78db143', '_dob': datetime.date(1847, 2, 11), '_classOf': 1865, '_make': 'Ford'} 

Raw Attributes and Methods of object:
 ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_classOf', '_dob', '_make', '_name', '_password', '_uid', 'age', 'classOf', 'dictionary', 'dob', 'is_password', 'is_uid', 'make', 'name', 'set_password', 'uid'] 

Representation to Re-Create the object:
 User(name=Thomas Edison, uid=toby, password=sha256$PRhIHZBTIUef4gJZ$8d8568942cb27d88636af7a201be31f751afb271fccace59c0b6ab77c78db143,dob=1847-02-11, classOf=1865, make=Ford) 

Start a class design for each of your own Full Stack CPT sections of your project

  • Use new code cell in this notebook
  • Define init and self attributes
  • Define setters and getters
  • Make a tester
import json

class Car:
    def __init__(self, make, model, price, year):
        # Adding instance attributes
        self._make = make
        self._model = model
        self._price = price
        self._year = year

    # Add getters and setters for make, model, price, year
    @property
    def make(self):
        return self._make
    
    @make.setter
    def make(self, make):
        self._make = make

    @property
    def model(self):
        return self._model
    
    @model.setter
    def model(self, model):
        self._model = model

    @property
    def price(self):
        return self._price
    
    @price.setter
    def price(self, price):
        self._price = price

    @property
    def year(self):
        return self._year
    
    @year.setter
    def year(self, year):
        self._year = year

    #Calculating Depreciation
    def calculate_depreciation(self, years):
        depreciation = 0.9**years * self._price
        return depreciation

    @property
    def dictionary(self):
        dict = {
            "make" : self.make,
            "model" : self.model,
            "price" : self.price,
            "year" : self.year,
        }
        return dict 

    def __str__(self):
        return json.dumps(self.dictionary)

if __name__ == "__main__":
    car1 = Car(make="Lexus", model="IS 500", price="$58,000", year="2022")
    print(car1)
{"make": "Lexus", "model": "IS 500", "price": "$58,000", "year": "2022"}