forked from dimagi/jsonobject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_generator.py
75 lines (62 loc) · 2.36 KB
/
sample_generator.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
from __future__ import absolute_import
from __future__ import unicode_literals
import random
import six
from six.moves import range
def generate_object_type(jo=None):
"""
jo can be either jsonobject (default)
or couchdbkit_shim for comparison with couchdbkit
"""
if jo is None:
from . import jsonobject as jo
WORDS = 'dog cat elephant river candle stripe pin plum'.split()
leaf_types = {
jo.StringProperty: six.text_type,
jo.IntegerProperty: int,
}
used_phrases = set()
def get_phrase(n=2):
phrase = tuple(random.sample(WORDS, n))
if phrase in used_phrases or len(set(phrase)) < n:
phrase = get_phrase()
else:
used_phrases.add(phrase)
return phrase
def generate_class_name(words=None):
words = words or get_phrase()
return ''.join([s.title() for s in words])
def generate_property_name(words=None):
words = words or get_phrase()
return '_'.join(words)
def generate_list_name(words=None):
words = words or get_phrase()
return generate_property_name(words) + 's'
object_types = []
def generate_object_type(words=None):
class_name = generate_class_name(words)
n_properties = random.choice(list(range(5)))
dct = {}
for _ in range(n_properties):
words = get_phrase()
property_type = random.choice([
jo.ObjectProperty,
jo.ListProperty,
Ellipsis
])
if property_type is jo.ObjectProperty:
property_name = generate_property_name(words)
dct[property_name] = jo.ObjectProperty(generate_object_type(words))
elif property_type is jo.ListProperty:
property_name = generate_list_name(words)
dct[property_name] = jo.ListProperty(generate_list_type(words))
object_type = type(jo.JsonObject)(class_name, (jo.JsonObject,), dct)
object_types.append(object_type)
return object_type
def generate_list_type(words=None):
property_type = random.choice([jo.ObjectProperty, Ellipsis])
if property_type is jo.ObjectProperty:
return generate_object_type(words)
else:
return random.choice(list(leaf_types.values()))
return generate_object_type(), object_types