-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_gen.py
51 lines (40 loc) · 1.15 KB
/
json_gen.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
# generate a JSON file for testing purposes
import sys
import json
import string
import random
import numpy as np
SCALE = 512
def gen_string(_):
size = np.random.geometric(0.01)
return ''.join([
random.choice(string.ascii_lowercase + string.digits)
for _ in range(size)
])
def gen_number(_):
return random.randint(1, 2**20)
def gen_key(_):
size = np.random.geometric(0.1)
return ''.join([
random.choice(string.ascii_lowercase + string.digits)
for _ in range(size)
])
def gen_value(depth):
typ = random.choices(
['number', 'string', 'array', 'object'],
[1, 1, 1, 1])[0]
return globals()[f'gen_{typ}'](depth + 1)
def gen_array(depth):
size = int(np.random.binomial(2 * SCALE / (2 ** (depth - 1)), 0.5))
return [
gen_value(depth + 1) for _ in range(size)
]
def gen_object(depth):
size = int(np.random.binomial(2 * SCALE / (2 ** (depth - 1)), 0.5))
return {
gen_key(depth + 1) : gen_value(depth + 1) for _ in range(size)
}
def gen_json(depth=0):
return gen_object(depth + 1)
with open(sys.argv[1], 'w') as f:
json.dump(gen_json(), f)