forked from gnyers/python-tuesday
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddressbook.py
More file actions
41 lines (32 loc) · 969 Bytes
/
addressbook.py
File metadata and controls
41 lines (32 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from dataclasses import dataclass, field
from typing import List
from collections import Counter
@dataclass
class Person:
fname: str = ''
sname: str = ''
gender: str = ''
email: str = ''
def __getitem__(self, item):
res = getattr(self, item)
return res
@dataclass
class Addressbook:
name: str = 'My Addressbook'
_items: List[Person] = field(default_factory=list, init=False)
def __iter__(self):
return iter(self._items)
def add(self, person):
self._items.append(person)
def __len__(self):
return len(self._items)
fred = Person(fname='Fred', sname='Flintstone', gender='m',
email='fred@bedrock.place')
wilma = Person(fname='Wilma', sname='Flintstone', gender='f',
email='wilma@bedrock.place')
ab = Addressbook(name='The Flintstones')
ab.add(fred)
ab.add(wilma)
gender_data_iterator = map(lambda v: v['gender'], ab)
res = Counter(gender_data_iterator)
print(res)