Frame Hack Recipe: Generating properties on a wrapper class

Step 1

Implement the properties function, using a frame hack. Feel free to use the eval function.

import sys
from employee import Employee

def properties(**kwargs):
    """Implement this function"""

class PyEmployee(object):
    def __init__(self, **kwargs):
        self.e = Employee()
        for k, v in kwargs.items():
            setattr(self, k, v)

    properties(
        given  = 'GivenName',
        family = 'FamilyName',
        birth =  'DateOfBirth',
    )


e = PyEmployee(given='Feihong', family='Hsu', birth='2007-11-15')

print e.given, e.e.GetGivenName()

e.given = 'Horatio'     # change given name through the property
print e.given, e.e.GetGivenName()

Expected output:

Feihong Feihong
Horatio Horatio

Hints:

  1. Show hint
  2. Show hint
  3. Show hint
  4. Show hint
  5. Show hint

Solution: wrapper1.py

Go to next step