Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # dict2class.py -- designed mainly for json usage
- import types
- def dict2class(d):
- """
- Return a class that has same attributes/values and
- dictionaries key/value
- """
- # see if it is indeed a dictionary
- if type(d) != types.DictType:
- return None
- # define a dummy class
- class Dummy: 0
- c = Dummy
- # each key need to be given as a string variable
- for e in d.keys():
- c.__dict__[e] = d[e]
- return c
- employees = {}
- employees['Tom Katt'] = dict2class( { 'age': 17,
- 'title': 'Cashier' } )
- employees['Barb Wyre'] = dict2class( { 'age': 32,
- 'title': 'Manager' } )
- for id in employees:
- print id
- print employees[id].title
- print employees[id].age
- print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement