OOP
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):
self._name = name # variables with self prefix become part of the object,
self._uid = uid
self.set_password(password)
self._dob = dob
self._classOf = 0
@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))
# sets the class of graduation to 18 years after date of birth
def classOf(self):
self._classOf = self._dob.year + 18
return self._classOf
# 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 graduation" : 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})'
if __name__ == "__main__":
u1 = User(name='Ryan McWeeny', uid='Ryan', password='Password', dob=date(2006, 3, 27))
u1.classOf()
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")
#print("class of graduation: ", u1.classOf())
from datetime import date
import json
class User:
def __init__(self, name, password, email):
self._name = name
self._password = password
self._email = email
# getter method for username
@property
def name(self):
return self._name
# setter method for username
@name.setter
def name(self, username):
self._name = name
# getter method for password
@property
def password(self):
return self._password
# setter method for password
@password.setter
def password(self, password):
self._password = password
# getter method for email
@property
def email(self):
return self._email
# setter method for email
@email.setter
def email(self, email):
self._email = email
# dictionary is customized, removing password for security purposes
@property
def dictionary(self):
dict = {
"name" : self.name,
"email" : self.email,
}
return dict
# 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(username={self._name}, password={self._password},email={self._email})'
if __name__ == "__main__":
u1 = User(name='Ryan', password='Password', email = 'ryanrob327@gmail.com')
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")