Object-Oriented Programming
Terms
- Object-Oriented Programming (OOP)
- class is a blueprint for creating an Object (a data structure)
- a class contains a collection of data (Attributes) and are pre-fixed using self, and a collection of Functions/Procedures (Methods)
- an Object is an Instance of the Class and many Objects can be created from the same class
- all methods in the Class become part of the Object, methods are accessed using dot notation (object.method())
- @ 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"))
- observe all instance data (self._name, self.email ...) are prefixed with "", this convention allows setters and getters to work with more natural variable name (name, email ...)
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="Grace Wang", uid="gracew", password="Grace123", dob=date(2005,9,2), classOf="2024")
print(u2)
###### 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")
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="Grace Wang", uid="gracew", password="Grace123", phone="111-111-1111", email="gracewang187@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")