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

Solution: solutions/wrapper1.py

Go to next step