This repository was archived by the owner on Aug 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproducer.py
More file actions
50 lines (34 loc) · 1.34 KB
/
producer.py
File metadata and controls
50 lines (34 loc) · 1.34 KB
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
from random import randint
from faker import Faker
from connect import init
# Embedded documents are called through globals function so they are required.
from services.rabbitmq.models import Contact, Email, PhoneNumber
def main() -> None:
'''
Generate a few contacts and put them in different queues depending on the
type of contact information used.
'''
if not init() or not (data := init(True)):
return
connection, channel, initial_types = data
fake = Faker()
for _ in range(randint(10, 20)):
fields = {'fullname': fake.name(), 'address': fake.address()}
types = [*initial_types]
if (targets := randint(0, 3)):
for target, field in enumerate(('phone_number', 'email')):
if targets != target + 1:
words = [word.title() for word in field.split('_')]
method = getattr(fake, field)
fields[field] = globals()[''.join(words)](value=method())
else:
types.remove(field)
contact = Contact(**fields).save()
if not targets:
continue
message = str(contact.id).encode()
for type in types:
channel.basic_publish(f'{type}_exchange', f'{type}_queue', message)
connection.close()
if __name__ == '__main__':
main()