-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdata.py
57 lines (49 loc) · 1.9 KB
/
data.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import random
from barnum import gen_data
import addressbook_pb2
# Barnum generates US data but that's ok for the example
names = [gen_data.create_name() for _ in range(0, 15)]
phones = [gen_data.create_phone() for _ in range(0, 30)]
postcodes = [gen_data.create_city_state_zip() for _ in range(0, 15)]
streets = [gen_data.create_street() for _ in range(0, 30)]
contacts = []
for name in names:
address = {}
# Simulate the fact that postcode are optionals
if random.choice([True, False]):
address['postcode'] = random.choice(postcodes)[0]
address['address_lines'] = random.sample(streets, random.randint(0, 2))
phone_numbers = []
for _ in range(0, random.randint(0, 2)):
phone_numbers.append({
'type': random.choice(['MOBILE', 'LANDLINE']),
'number': random.choice(phones)
})
contacts.append({
'first_name': name[0],
'last_name': name[1],
'address': address,
'phone_numbers': phone_numbers
})
def get_protobuf_data():
"""Need to protobuf-ize the data"""
pb_contacts = []
for contact in contacts:
address =addressbook_pb2.Address()
if contact['address'].get('address_lines'):
address.address_lines.extend(contact['address']['address_lines'])
if 'postcode' in contact['address']:
address.postcode = contact['address']['postcode']
pb_contacts.append(addressbook_pb2.Contact(
first_name=contact['first_name'],
last_name=contact['last_name'],
address=address,
phone_numbers=[
addressbook_pb2.Phone(
type=addressbook_pb2.MOBILE if num['type'] == 'MOBILE' else addressbook_pb2.LANDLINE,
number=num['number']
)
for num in contact['phone_numbers']
]
))
return addressbook_pb2.AddressBook(contacts=pb_contacts)