-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_data.py
More file actions
executable file
·72 lines (58 loc) · 1.69 KB
/
generate_data.py
File metadata and controls
executable file
·72 lines (58 loc) · 1.69 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
import json
import random
import loremipsum
import names
AUTHOR_COUNT = 50
BOOK_COUNT = 200
MAX_WORDS_IN_TITLE = 6
MAX_AUTHORS_PER_BOOK = 4
def generate_book_title():
words = loremipsum.generate_sentence()[2][:-1].split()
title = ' '.join(words[:random.randrange(1, MAX_WORDS_IN_TITLE+1)])
if title[-1] == ',':
title = title[:-1] + '.'
elif title[-1] != '.':
title += '.'
return title
def get_random_author_id():
return random.randrange(1, AUTHOR_COUNT+1)
def get_authors():
is_multiply_authors = random.choice([True, False])
if is_multiply_authors:
authors_count = random.randrange(2, MAX_AUTHORS_PER_BOOK+1)
return list(set([
get_random_author_id() for i in range(authors_count)
])) # count of authors can be less then authors_count
else:
return [get_random_author_id()]
def main():
authors = []
author_set = set()
i = 1
while i <= AUTHOR_COUNT:
author_name = names.get_full_name()
if author_name not in author_set:
authors.append({
'id': i,
'name': author_name
})
author_set.add(author_name)
i += 1
books = []
book_set = set()
i = 1
while i <= BOOK_COUNT:
book_title = generate_book_title()
if book_title not in book_set:
books.append({
'id': i,
'title': book_title,
'authors': get_authors(),
})
book_set.add(book_title)
i += 1
data = {'authors': authors, 'books': books}
print json.dumps(data, indent=1)
if __name__ == "__main__":
main()