Notes

  • Object-Oriented Programming (OOP) uses classes as a blueprint for creating an Object which is then used like many other Python variables
  • Classes collect data, functions and procedures
  • An object is a part of a class and there can be many objects from one class
    • each object also collects its own data
  • @ decorators: allow access to instance data without the use of functions
  • @property decorator (aka getter): enables developers to reference/get instance data in a shorthand fashion (object.name versus object.get_name())
  • @name.setter decorator (aka setter): enables developers to update/set instance data in a shorthand fashion (object.name = "John" versus object.set_name("John"))
  • All instance data (self._name, self.email ...) are prefixed with ""

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

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
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):
        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
    
    #CLASS OF
    @property
    def classOf(self):
        return self._classOf
    
    @classOf.setter
    def classOf(self, classOf):
        self._classOf = classOf
    
    @property
    def name(self):
        return self._name
    
    # a setter function, allows name to be updated after initial object creation
    @name.setter
    def name(self, name):
        self._name = name
    
    # a getter method, extracts email from object
    @property
    def uid(self):
        return self._uid
    
    # a setter function, allows name to be updated after initial object creation
    @uid.setter
    def uid(self, uid):
        self._uid = uid
        
    # check if uid parameter matches user id in object, return boolean
    def is_uid(self, uid):
        return self._uid == uid
    
    # dob property is returned as string, to avoid unfriendly outcomes
    @property
    def dob(self):
        dob_string = self._dob.strftime('%m-%d-%Y')
        return dob_string
    
    # dob should be have verification for type date
    @dob.setter
    def dob(self, dob):
        self._dob = dob
        
    # age is calculated and returned each time it is accessed
    @property
    def age(self):
        today = date.today()
        return today.year - self._dob.year - ((today.month, today.day) < (self._dob.month, self._dob.day))
    
    # dictionary is customized, removing password for security purposes
    @property
    def dictionary(self):
        dict = {
            "name" : self.name,
            "uid" : self.uid,
            "dob" : self.dob,
            "age" : self.age,
            "class_of": self.classOf
        }
        return dict
    
    # update password, this is conventional setter
    def set_password(self, password):
        """Create a hashed password."""
        self._password = generate_password_hash(password, method='sha256')

    # check password parameter versus stored/encrypted password
    def is_password(self, password):
        """Check against hashed password."""
        result = check_password_hash(self._password, password)
        return result
    
    # output content using json dumps, this is ready for API response
    def __str__(self):
        return json.dumps(self.dictionary)
    
    # output command to recreate the object, uses attribute directly
    def __repr__(self):
        return f'User(name={self._name}, uid={self._uid}, password={self._password},dob={self._dob}, class_of={self._classOf})'
    

if __name__ == "__main__":
    u1 = User(name='Thomas Edison', uid='toby', password='123toby', dob=date(1847, 2, 11), classOf="1915")
    print(u1) 
    u2 = User(name="Claire Chen", uid="clu", password="123clu", dob=date(2006,8,19), classOf="2024")
    print(u2)
{"name": "Thomas Edison", "uid": "toby", "dob": "02-11-1847", "age": 175, "class_of": "1915"}
{"name": "Claire Chen", "uid": "clu", "dob": "08-19-2006", "age": 16, "class_of": "2024"}
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import date
import json

class Login:    

    def __init__(self, name, uid, password, phone, email):
        self._name = name    # variables with self prefix become part of the object, 
        self._uid = uid
        self._phone = phone
        self.set_password(password)
        self._email = email
    
    #CLASS OF
    @property
    def email(self):
        return self._email
    
    @email.setter
    def email(self, email):
        self._email = email

    @property
    def phone(self):
        return self._phone
    
    @phone.setter
    def phone(self, phone):
        self._phone = phone
    
    @property
    def name(self):
        return self._name
    
    # a setter function, allows name to be updated after initial object creation
    @name.setter
    def name(self, name):
        self._name = name
    
    # a getter method, extracts email from object
    @property
    def uid(self):
        return self._uid
    
    # a setter function, allows name to be updated after initial object creation
    @uid.setter
    def uid(self, uid):
        self._uid = uid
        
    # check if uid parameter matches user id in object, return boolean
    def is_uid(self, uid):
        return self._uid == uid

    # dictionary is customized, removing password for security purposes
    @property
    def dictionary(self):
        dict = {
            "name" : self.name,
            "uid" : self.uid,
            "phone": self.phone,
            "email": self.email
        }
        return dict
    
    # update password, this is conventional setter
    def set_password(self, password):
        """Create a hashed password."""
        self._password = generate_password_hash(password, method='sha256')

    # check password parameter versus stored/encrypted password
    def is_password(self, password):
        """Check against hashed password."""
        result = check_password_hash(self._password, password)
        return result
    
    # output content using json dumps, this is ready for API response
    def __str__(self):
        return json.dumps(self.dictionary)
    
    # output command to recreate the object, uses attribute directly
    def __repr__(self):
        return f'User(name={self._name}, uid={self._uid}, password={self._password},phone={self._phone}, class_of={self._email})'
    

u1 = Login(name="Claire Chen", uid="clu", password="123clu", phone="000.000.0000", email="clairechengmail@gmail.com")
print(u1)
    ###### For reference to see raw form ################
    # 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": "Claire Chen", "uid": "clu", "phone": "000.000.0000", "email": "clairechengmail@gmail.com"}