I have a function which gets data from a spreadsheet and processes the data into a dictionary
def get_employee_data(path=None):
# load excel file from 'path'
# return processed data as dictionary
return {
'bob':{'id':'12039123','age':90,'occupation':'manager'},
'john':{'id':'43434433','age':66,'occupation':'janitor'},
'hannah':{'id':'48484839','age':1,'occupation':'proffesional hamster'},
}
the init method iterates over the dictionary to set class attributes where the 'attribute name' is set based on the employee name - I want this to store my descriptor class, 'Employee'
class Employee:
def __set_name__(self, owner, name):
self.public_name = name
self.private_name = '_' + name
def __get__(self, obj, objType=None):
print('GET...')
return getattr(obj, self.private_name)
def __set__(self, obj, value):
print('SET...')
setattr(obj, self.private_name, value)
class ManagementSystem:
employee_data = get_employee_data()
william = Employee() # test works as expected
def __init__(self):
for employee in self.employee_data:
setattr(self, employee, Employee())
self.william = 'william' # test works as expected
With the same design, I aim to create another descriptor to handle access to employ details, where said descriptor will be assigned within the 'Employee' class
class EmployeeDetail:
# example implementation:
# ms.bob.id
pass
For testing I have created a class attribute for ManagementSystem 'william' which works as expected, but accessing an attribute 'bob' instantiated using setattr() behaves differently
>>> ms = ManagementSystem()
SET...
>>> ms.william
GET...
'william'
>>> ms.william = 'walliamson'
SET...
>>> ms.bob
<__main__.Employee object at 0x7f7f01a54e20>
>>> ms.bob = 'bob'
>>> ms.bob
'bob'
I have some understanding as to why this may be, but have not been able to find a solution to this problem.
Thank you for help as always :)