1

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 :)

Mark
  • 90,562
  • 7
  • 108
  • 148
Pepe
  • 59
  • 6

1 Answers1

0

Ok, this makes me feel silly, but I have already found a solution as I've gone back to test some things based on this answer:

Setting a class attribute with a given name in python while defining the class

class ManagementSystem:
  employee_data = get_employee_data()
  for employee in employee_data:
    vars()[employee] = Employee()
  def __init__(self):
   for employee in self.employee_data:
     setattr(self, employee, employee)

I believe that I was previously attempting to set attributes outside what is actually a class attribute (I am creating instance attributes):

'william' is a class attribute so storing the descriptor instance will handle behavior - bob cannot follow the same behavior and is just an instance of the 'Employee' class, therefore when I attempt to set bob it is overwritten

Still, if there is a more efficient way to do this (or my understanding seems incomplete), I would still very much appreciate improvements upon my given answer/explaination,

Thank you :)

Pepe
  • 59
  • 6