def test_gen_kwargs_with_underscore(): gson = """ { "gen_kwarg_with_underscore" : { "nested": gaussian(0, 1, draws=1, random_seed=42) } } """ genson.loads(gson)
def test_gaussian_random_seed(): genson.set_global_seed(42) gson = \ """ { "gaussian_random_seed" : gaussian(0, 1, draws=2) } """ gen = genson.loads(gson) vals = [val['gaussian_random_seed'] for val in gen] assert_equal(vals[0], 0.4967141530112327) assert_equal(vals[1], -0.13826430117118466) genson.set_global_seed(None) gen.reset() vals = [val['gaussian_random_seed'] for val in gen] assert_not_equal(vals[0], 0.4967141530112327) assert_not_equal(vals[1], -0.13826430117118466) genson.set_global_seed(42) gen.reset() gen = genson.loads(gson) vals = [val['gaussian_random_seed'] for val in gen] assert_equal(vals[0], 0.4967141530112327) assert_equal(vals[1], -0.13826430117118466)
def test_multi_gen_kwargs(): gson = """ { "multi_gen_kwargs_bug" : { "nested": gaussian(0, 1, draws=1, rseed=42) } } """ genson.loads(gson)
def test_unary(): g = genson.loads('{ "p" : sin(-1) } ') assert( g['p'].args[0] == -1) g = genson.loads('{ "p" : sin(+1) } ') assert( g['p'].args[0] == 1)
def parse_and_report_error(doc): try: genson.loads(doc) except Exception as e: print("Caught exception: %s" % e) return print("Did not catch exception")
def test_dumps(): gson = """ { "test0": 4, "test1" : <0,1,2>, "test2" : { "nested": gaussian(0,1,draws=1) }, "test3" : <"a", "b", uniform(0,1)>, ("test4", "test5") : (0, 1), ("test6", "test7") : 1, ("test8","test9") : <("d", "e"), ("f", "g")>, "testA": {"another_nested" : root.test5, "parent_test" : parent.test5}, "testB": this.test5, "testC": this.test2.nested, "test_with_underscores": 4, "testD": this.test_with_underscores, "testE": sin(4), "testF": sin(this.testE), "testG": 10, "testExpr": 2.2*this.testG + (10 / sin(this.testA.another_nested)), "testZ": 10 } """ gen = genson.loads(gson) print genson.dumps(gen) print genson.dumps(gen, pretty_print=True)
def test_gaussian_random_seed(): gson = \ """ { "gaussian_random_seed" : gaussian(0, 1, draws=2, random_seed=42) } """ vals = [val['gaussian_random_seed'] for val in genson.loads(gson)] assert_equal(vals[0], 0.4967141530112327) assert_equal(vals[1], -0.13826430117118466)
def test_gaussian_uniform_seed(): gson = \ """ { "uniform_random_seed" : uniform(0, 1, draws=2, random_seed=42) } """ vals = [val['uniform_random_seed'] for val in genson.loads(gson)] assert_equal(vals[0], 0.3745401188473625) assert_equal(vals[1], 0.9507143064099162)
def test_root_reference(): s = """ { "b": { "c": 2 * root.a }, "a": 1 } """ g = genson.loads(s) d = g.next() assert(d['b']['c'] == 2 * d['a'])
def test_parent_reference(): s = """ { "a": 1, "b": { "c": 2 * parent.a } } """ g = genson.loads(s) d = g.next() assert(d['b']['c'] == 2 * d['a'])
def test_array_references(): g = genson.loads('{"a":[uniform(0,1),1,2], "b": this.a.1 }') d = g.next() assert(d['a'][1] == d['b'])
"b": grid(1, 2), "c": choice([6, 7, 8], draws = 1000) } } """, """ { "a": [ { "b": choice([1, 2, 3, 4], draws=1000), "c": choice([5, 6], draws = 1000) } ] } """ ] print print "NOTE: Each stanza below should contain five instances:" for i, spec_str in enumerate(specs): print "**********" print "Stanza %s:" % (i+1) son_iterator = genson.loads(spec_str) for j, spec in enumerate(son_iterator): if j >= 5: break print "Instance %s" % (j+1) print spec print
def test_gaussian_random_seed_multi_call(): gson = '{"param0": gaussian(0, 1, draws=10, random_seed=42)}' r1 = [e for e in genson.loads(gson)] r2 = [e for e in genson.loads(gson)] assert_equal(r1, r2)
def test_main(): testdata = """ { "test0": 4, "test1" : <0,1,2>, "test2" : { "nested": gaussian(0,1,draws=1) }, "test3" : <"a", "b", uniform(0,1)>, ("test4", "test5") : (0, 1), ("test6", "test7") : 1, ("test8","test9") : <("d", "e"), ("f", "g")>, "testA": {"another_nested" : root.test5, "parent_test" : parent.test5}, "testB": this.test5, "testC": this.test2.nested, "test_with_underscores": 4, "testD": this.test_with_underscores, "testE": sin(4), "testF": sin(this.testE), "testG": 10, "testExpr": 2.2*this.testG + (10 / sin(this.testA.another_nested)), "testZ": 10 } """ testdata2 = """ { "test0": 4, "test1" : <0,1,2>, "test3" : <"a", "b", uniform(0,1)>, ("test4", "test5") : (0, 1), ("test6", "test7") : 1, ("test8","test9") : <("d", "e"), ("f", "g")> } """ test_broken1 = """ { // no key 4, "test1" : 5 } """ test_broken2 = """ { // no value "test0" :, "test1" : 5 } """ test_broken3 = """ { // no comma "test0" : 17 "test1" : 5 } """ tic = time.time() genson.loads(testdata2) toc = time.time() - tic print("Simple example parse time: %s" % toc) tic = time.time() son_iterator2 = genson.loads(testdata) toc = time.time() - tic print("Complex example parse time: %s" % toc) for d in son_iterator2: print d print print "Again, but from a formatted file:" with open(path.join(my_path, 'test_basic.gson')) as f: son_file_iterator = genson.load(f) for d in son_file_iterator: print d parse_and_report_error(test_broken1) parse_and_report_error(test_broken2) parse_and_report_error(test_broken3)
def test_trailing_comma(): # currently this fails, which is a slight shame testdata2 = """{ "test0": 4, } """ list(genson.loads(testdata2))
def test_outermost_lists(): for spec, gt in specs: it = genson.loads(spec) gv = [it.next() for _ in xrange(5)] yield assert_equals, gv, gt
def test_recursion_problem(): # this one is a problem with the default recursion depth genson_string = '''{ "a": choice( [ [ {}, { "a": 0, "b": { "a": {"b": [ { "c": choice([0,1]) } ] } } } ] ]) } ''' # this one is okay genson_string2 = '''{ "a": [ [ {}, { "a": 0, "b": { "a": {"b": [ { "c": choice([0,1]) } ] } } } ] ] } ''' # this one is okay genson_string3 = '''{ "a": choice([ [ { "a": 0 } ] ] ) } ''' # this one is okay genson_string4 = '''{ "a": choice([ [ { "a": choice([{"a": 0}, 3]) } ] ] ) } ''' # this one is okay genson_string5 = '''{ "a": choice( [ [ {}, { "a": 0, "b": { "a": [ { "c": choice([0,1]) } ] } } ] ]) } ''' # this one is okay genson_string6 = '''{ "a": choice( [ [ {}, { "a": 0, "b": { "a": {"b": [ { "c": 1 } ] } } } ] ]) } ''' # this one is a problem genson_string7 = '''{ "a": choice( [ [ {}, { "a": {"b": [ { "c": choice([0,1]) } ] } } ] ]) } ''' genson.loads(genson_string7) genson.loads(genson_string6) genson.loads(genson_string5) genson.loads(genson_string4) genson.loads(genson_string3) genson.loads(genson_string2) genson.loads(genson_string)
def test_binary(): g = genson.loads('{ "p" : gaussian(1+1,1) } ') assert( g['p'].mean == 2) assert( g['p'].stdev == 1)