42 lines
951 B
Python
42 lines
951 B
Python
class Person(object):
|
|
_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']
|
|
|