2012-05-18 11:49:25 +00:00
|
|
|
class Person(object):
|
2012-05-18 05:33:35 +00:00
|
|
|
_people = [
|
|
|
|
{
|
|
|
|
'name': {
|
|
|
|
'first': 'Hammy',
|
|
|
|
'last': 'Spammy',
|
|
|
|
},
|
|
|
|
'age': 42,
|
|
|
|
'id': 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': {
|
|
|
|
'first': 'Sampers',
|
|
|
|
'last': 'Dancer',
|
|
|
|
},
|
|
|
|
'age': 38,
|
|
|
|
'id': 2
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._index()
|
|
|
|
|
|
|
|
def _index(self):
|
|
|
|
self._people_by_id = dict(((p['id'], p) for p in self._people))
|
|
|
|
|
|
|
|
def getall(self):
|
|
|
|
return self._people
|
|
|
|
|
|
|
|
def get(self, person_id):
|
|
|
|
return self._people_by_id[person_id]
|
|
|
|
|
|
|
|
def add(self, person):
|
|
|
|
if None in (person.get('name'), person.get('age')):
|
|
|
|
raise ValueError('Missing required fields!: {}'.format(person))
|
|
|
|
|
|
|
|
person['id'] = len(self._people) + 1
|
|
|
|
self._people.append(person)
|
|
|
|
self._index()
|
|
|
|
return person['id']
|
|
|
|
|