Skip to content

Instantly share code, notes, and snippets.

@LucasDondo
Created May 17, 2023 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucasDondo/538d68495f82ab12180486cdfe63ded8 to your computer and use it in GitHub Desktop.
Save LucasDondo/538d68495f82ab12180486cdfe63ded8 to your computer and use it in GitHub Desktop.
How I used a single DB instead of distributing data inside classes.
class Address:
''' An attempt to represent an address. '''
def __init__(self, street, city, state, country):
''' Initialize main attributes. '''
self.street = street
self.city = city
self.state = state
self.country = country
def __str__(self):
''' Returns a nicely formatted address. '''
lines = [self.street, self.city, self.state, self.country]
return ', '.join(lines).title()

::: mermaid classDiagram

class Employee{
    +String         name
    +Int            id
    +IRole          role
    +IPayrollPolicy payroll_policy
    +Address        address
}

class IRole{
    +work()
}
<<interface>> IRole
Employee *-- IRole

class ManagerRole{
    +work()
}
ManagerRole --|> IRole

class DeveloperRole{
    +work()
}
DeveloperRole --|> IRole

class BackEndDeveloperRole{
    +work()
}
BackEndDeveloperRole --|> DeveloperRole

class FrontEndDeveloperRole{
    +work()
}
FrontEndDeveloperRole --|> DeveloperRole

class DesignerRole{
    +work()
}
DesignerRole --|> IRole

class SalespersonRole{
    +work()
}
SalespersonRole --|> IRole

class Address{
    +String street
    +String city
    +String state
    +String country
}
Employee *-- Address

class IPayrollPolicy{
    +Int payroll()
}
<<interface>> IPayrollPolicy
Employee *-- IPayrollPolicy

class SalaryPolicy{
    +Int salary
    +Int payroll()
}
SalaryPolicy --|> IPayrollPolicy

class CommissionPolicy{
    +Int commission_per_sale
    +Int sales_made
    +Int payroll()
}
CommissionPolicy --|> SalaryPolicy

class HourlyPolicy{
    +Int hour_rate
    +Int hours_worked
    +Int payroll()
}
HourlyPolicy --|> IPayrollPolicy

:::

from employees import employees
for employee in employees:
print(employee)
print('Comments from the developer team:')
for employee in employees:
try:
print(f'- {employee.name} says: {employee.role.comment_on_coffee()}')
except AttributeError:
pass
[
{
"name" : "Adam Smith",
"id" : 0,
"role" : "manager",
"payroll_policy" : "salary",
"monthly_salary" : 2000,
"commission_rate": null,
"sales_made" : null,
"hourly_rate" : null,
"hours_worked" : null,
"address" : {
"street" : "The Smithies 840",
"city" : "Smithtown",
"state" : "North Virginia",
"country": "United States"
}
},
{
"name" : "Candelaria Candelarium",
"id" : 1,
"role" : "back_end_developer",
"payroll_policy" : "salary",
"monthly_salary" : 1000,
"commission_rate": null,
"sales_made" : null,
"hourly_rate" : null,
"hours_worked" : null,
"address" : {
"street" : "Candles 1700",
"city" : "Candum",
"state" : "South Virginia",
"country": "United States"
}
},
{
"name" : "Jhon Ivi",
"id" : 2,
"role" : "front_end_developer",
"payroll_policy" : "salary",
"monthly_salary" : 1000,
"commission_rate": null,
"sales_made" : null,
"hourly_rate" : null,
"hours_worked" : null,
"address" : {
"street" : "Candles 1700",
"city" : "Candum",
"state" : "South Virginia",
"country": "United States"
}
},
{
"name" : "Elon Misk",
"id" : 3,
"role" : "salesperson",
"payroll_policy" : "commission",
"monthly_salary" : 500,
"commission_rate": 10,
"sales_made" : 5,
"hourly_rate" : null,
"hours_worked" : null,
"address" : {
"street" : "Miska Muska 2050",
"city" : "Miskies",
"state" : "Somwhere On",
"country": "Mars"
}
},
{
"name" : "Duane D'Lec",
"id" : 4,
"role" : "designer",
"payroll_policy" : "hourly",
"monthly_salary" : null,
"commission_rate": null,
"sales_made" : null,
"hourly_rate" : 10,
"hours_worked" : 20,
"address" : {
"street" : "Dulce de Leche 177",
"city" : "Dulces",
"state" : "Buenos Aires",
"country": "Argentina"
}
}
]
# This is not really a mixin, because since I use it just for DeveloperRole, it
# could perfectly be just a DeveloperRole's method. But I wanted to show how
# mixins can be used to add functionality to multiple classes.
# I'll have to find a better example for mixins inside this company...
class DeveloperCommentsMixin:
''' An attempt to represent typical developer comments. '''
def comment_on_windows(self):
''' A comment we make on Windows. '''
return 'Windows sucks!'
def comment_on_coffee(self):
''' A comment we make on coffee. '''
return 'No coffee, no code!'
import json
from roles_system import RolesSystem
from payroll_system import PayrollSystem
from address import Address
class Employee:
''' An attempt to represent an employee. '''
def __init__(self, **data):
''' Initialize main attributes. '''
self.name = data['name'].title()
self.id = data['id']
self.role = RolesSystem.get_role(data['role'])
self.payroll_policy = PayrollSystem.get_policy(data['payroll_policy' ],
data['monthly_salary' ],
data['commission_rate'],
data['sales_made' ],
data['hourly_rate' ],
data['hours_worked' ],)
self.address = Address(**data['address'])
def __str__(self):
''' Returns a string that represents this employee. '''
return(f'\nName: {self.name}\n'
f'ID : {self.id}\n'
f'Works {self.role.work()}.\n'
f'Getting paid ${self.payroll_policy.payroll} USD this month.\n'
f'Lives at {self.address}.\n')
with open('db.json', 'r') as f:
db = json.load(f)
employees = []
for employee in db:
employees.append(Employee(**employee))
from developer_comments import DeveloperCommentsMixin
class RolesSystem:
''' An attempt to represent a roles system. '''
@staticmethod
def get_role(role):
''' Gets the role object. '''
if role == 'manager':
return ManagerRole()
elif role == 'developer':
return DeveloperRole()
elif role == 'back_end_developer':
return BackEndDeveloperRole()
elif role == 'front_end_developer':
return FrontEndDeveloperRole()
elif role == 'salesperson':
return SalespersonRole()
elif role == 'designer':
return DesignerRole()
class ManagerRole:
''' An attempt to represent a manager's role. '''
def work(self):
''' Returns a string that represents this person working. '''
return 'managing employees, setting goals, and reviewing results'
class DeveloperRole(DeveloperCommentsMixin):
''' An attempt to represent a developer's role. '''
def work(self):
''' Returns a string that represents this person working. '''
return("developing and enhancing continuously the company's digital "
"systems")
class BackEndDeveloperRole(DeveloperRole):
''' An attempt to represent a back-end developer's role. '''
def work(self):
''' Returns a string that represents this person working. '''
return("developing and enhancing continuously the company's back-end")
class FrontEndDeveloperRole(DeveloperRole):
''' An attempt to represent a front-end developer's role. '''
def work(self):
''' Returns a string that represents this person working. '''
return("developing and enhancing continuously the company's front-end")
class SalespersonRole:
''' An attempt to represent a salesperson's role. '''
def work(self):
''' Returns a string that represents this person working. '''
return "making people buy the company's products and/or services"
class DesignerRole:
''' An attempt to represent a designer's role. '''
def work(self):
''' Returns a string that represents this person working. '''
return 'making this company beautiful'
from developer_comments import DeveloperCommentsMixin
class RolesSystem:
''' An attempt to represent a roles system. '''
@staticmethod
def get_role(role):
''' Gets the role object. '''
if role == 'manager':
return ManagerRole()
elif role == 'developer':
return DeveloperRole()
elif role == 'back_end_developer':
return BackEndDeveloperRole()
elif role == 'front_end_developer':
return FrontEndDeveloperRole()
elif role == 'salesperson':
return SalespersonRole()
elif role == 'designer':
return DesignerRole()
class ManagerRole:
''' An attempt to represent a manager's role. '''
def work(self):
''' Returns a string that represents this person working. '''
return 'managing employees, setting goals, and reviewing results'
class DeveloperRole(DeveloperCommentsMixin):
''' An attempt to represent a developer's role. '''
def work(self):
''' Returns a string that represents this person working. '''
return("developing and enhancing continuously the company's digital "
"systems")
class BackEndDeveloperRole(DeveloperRole):
''' An attempt to represent a back-end developer's role. '''
def work(self):
''' Returns a string that represents this person working. '''
return("developing and enhancing continuously the company's back-end")
class FrontEndDeveloperRole(DeveloperRole):
''' An attempt to represent a front-end developer's role. '''
def work(self):
''' Returns a string that represents this person working. '''
return("developing and enhancing continuously the company's front-end")
class SalespersonRole:
''' An attempt to represent a salesperson's role. '''
def work(self):
''' Returns a string that represents this person working. '''
return "making people buy the company's products and/or services"
class DesignerRole:
''' An attempt to represent a designer's role. '''
def work(self):
''' Returns a string that represents this person working. '''
return 'making this company beautiful'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment