This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_docs.py
156 lines (138 loc) · 7.49 KB
/
gen_docs.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import random
import json
from optparse import OptionParser
from microcouch import MicroCouch
names = ["Isabella", "Sophia", "Emma", "Olivia", "Ava", "Emily", "Abigail",
"Madison", "Chloe", "Mia", "Addison", "Elizabeth", "Ella", "Natalie",
"Samantha", "Alexis", "Lily", "Grace", "Hailey", "Alyssa", "Lillian",
"Hannah", "Avery", "Leah", "Nevaeh", "Sofia", "Ashley", "Anna",
"Brianna", "Sarah", "Zoe", "Victoria", "Gabriella", "Brooklyn",
"Kaylee", "Taylor", "Layla", "Allison", "Evelyn", "Riley","Amelia",
"Khloe", "Makayla", "Aubrey", "Charlotte", "Savannah", "Zoey", "Bella",
"Kayla", "Alexa", "Jacob", "Ethan", "Michael", "Jayden", "William",
"Alexander", "Noah", "Daniel", "Aiden", "Anthony", "Joshua", "Mason",
"Christopher", "Andrew", "David", "Matthew", "Logan", "Elijah",
"James", "Joseph", "Gabriel", "Benjamin", "Ryan", "Samuel", "Jackson",
"John", "Nathan", "Jonathan", "Christian", "Liam", "Dylan", "Landon",
"Caleb", "Tyler", "Lucas", "Evan", "Gavin", "Nicholas", "Isaac",
"Brayden", "Luke", "Angel", "Brandon", "Jack", "Isaiah", "Jordan",
"Owen", "Carter","Connor", "Justin"]
def gen_doc(schema):
doc = {}
for field in schema.keys():
doc[field] = schema[field]()
return doc
def options():
parser = OptionParser()
parser.add_option("-u", "--url", default="http://localhost:5984/test",
help="Url of the database - don't put password in it [default %default]")
parser.add_option("-s", "--schema", default="schema.json",
help="JSON file describing the schema of documents to generate [default %default]")
parser.add_option("-n","--number", default=10, type="int",
help="Number of docs to generate [default %default]")
opts, args = parser.parse_args()
return opts, args
def ipsum(nlines):
#This text is under public domain
#Lorem ipsum
#Cicero
ipsum_text = u"""lorem ipsum dolor sit amet consetetur sadipscing elitr sed
diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat
sed diam voluptua at vero eos et accusam et justo duo dolores et ea rebum
stet clita kasd gubergren no sea takimata sanctus est lorem ipsum dolor sit
amet lorem ipsum dolor sit amet consetetur sadipscing elitr sed diam nonumy
eirmod tempor invidunt ut labore et dolore magna aliquyam erat sed diam
voluptua at vero eos et accusam et justo duo dolores et ea rebum stet clita
kasd gubergren no sea takimata sanctus est lorem ipsum dolor sit amet lorem
ipsum dolor sit amet consetetur sadipscing elitr sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat sed diam voluptua
at vero eos et accusam et justo duo dolores et ea rebum stet clita kasd
gubergren no sea takimata sanctus est lorem ipsum dolor sit amet
duis autem vel eum iriure dolor in hendrerit in vulputate velit esse
molestie consequat vel illum dolore eu feugiat nulla facilisis at vero eros
et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril
delenit augue duis dolore te feugait nulla facilisi lorem ipsum dolor sit
amet consectetuer adipiscing elit sed diam nonummy nibh euismod tincidunt ut
laoreet dolore magna aliquam erat volutpat
ut wisi enim ad minim veniam quis nostrud exerci tation ullamcorper suscipit
lobortis nisl ut aliquip ex ea commodo consequat duis autem vel eum iriure
dolor in hendrerit in vulputate velit esse molestie consequat vel illum
dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio
dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te
feugait nulla facilisi
nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet
doming id quod mazim placerat facer possim assum lorem ipsum dolor sit amet
consectetuer adipiscing elit sed diam nonummy nibh euismod tincidunt ut
laoreet dolore magna aliquam erat volutpat ut wisi enim ad minim veniam quis
nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea
commodo consequat
duis autem vel eum iriure dolor in hendrerit in vulputate velit esse
molestie consequat vel illum dolore eu feugiat nulla facilisis
at vero eos et accusam et justo duo dolores et ea rebum stet clita kasd
gubergren no sea takimata sanctus est lorem ipsum dolor sit amet lorem ipsum
dolor sit amet consetetur sadipscing elitr sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat sed diam voluptua at vero
eos et accusam et justo duo dolores et ea rebum stet clita kasd gubergren no
sea takimata sanctus est lorem ipsum dolor sit amet lorem ipsum dolor sit
amet consetetur sadipscing elitr at accusam aliquyam diam diam dolore
dolores duo eirmod eos erat et nonumy sed tempor et et invidunt justo labore
stet clita ea et gubergren kasd magna no rebum sanctus sea sed takimata ut
vero voluptua est lorem ipsum dolor sit amet lorem ipsum dolor sit amet
consetetur sadipscing elitr sed diam nonumy eirmod tempor invidunt ut labore
et dolore magna aliquyam erat
consetetur sadipscing elitr sed diam nonumy eirmod tempor invidunt ut labore
et dolore magna aliquyam erat sed diam voluptua at vero eos et accusam et
justo duo dolores et ea rebum stet clita kasd gubergren no sea takimata
sanctus est lorem ipsum dolor sit amet lorem ipsum dolor sit amet consetetur
sadipscing elitr sed diam nonumy eirmod tempor invidunt ut labore et dolore
magna aliquyam erat sed diam voluptua at vero eos et accusam et justo duo
dolores et ea rebum stet clita kasd gubergren no sea takimata sanctus est
lorem ipsum dolor sit amet lorem ipsum dolor sit amet consetetur sadipscing
elitr sed diam nonumy eirmod tempor invidunt ut labore et dolore magna
aliquyam erat sed diam voluptua at vero eos et accusam et justo duo dolores
et ea rebum stet clita kasd gubergren no sea takimata sanctus est lorem
ipsum dolor sit amet"""
# Done this way in case I decide to do words/paragraphs/sentences
lorem_text = lorem_text.replace('\n\n','\n')
lines = lorem_text.split('\n')
return lines[:nlines]
def bind_function(field):
"""
Return the appropriate (lambda) function for the given field
"""
t = field['type']
try:
if t == 'int':
return lambda: random.randint(field['min'], field['max'])
elif t == 'float':
return lambda: field['min'] + (field['max'] * random.random())
elif t == 'string':
return lambda: ''.join(random.choice(string.letters) for i in xrange(random.randint(1, 15))).title()
elif t == 'ipsum':
return lambda: ipsum(field['lines'])
elif t == 'choice':
return lambda: random.choice(field['values'])
elif t == 'bool':
return lambda: bool(random.randint(0,1))
elif t == 'name':
return lambda: random.choice(names)
else:
print 'Unknown field type, exiting'
sys.exit(1)
except KeyError, k:
print k
sys.exit(1)
if __name__ == "__main__":
opts, args = options()
schema = json.load(open(opts.schema))
function_schema = {}
for field in schema:
function_schema[field] = bind_function(schema[field])
docs = []
microcouch = MicroCouch(opts.url)
for d in xrange(opts.number):
docs.append(gen_doc(function_schema))
if len(docs) > 500:
microcouch.push(docs)
docs = []
microcouch.push(docs)