def ftest(self, text_o=text_o, text_0=text_0, text_1=text_1, text_2=text_2): vs = loads(text_o) t_0 = dumps(vs, sorted=1) t_1 = dumps(vs, pretty=1, braces=1, sorted=1) t_2 = dumps(vs, pretty=1, sorted=1) #toks = tokens(text_o) #t_3 = ''.join(toks) #print(t_0, t_1, t_2) #print(text_0, text_1, text_2) self.assertEqual(t_0, text_0) self.assertEqual(t_1, text_1) self.assertEqual(t_2, text_2)
def test_av(text): vals = loads(text) text1 = dumps(vals) text2 = dumps(vals, pretty=1) template = '''\ <table> <tr><tdstyle="background-color:#d0d0d0;"><b>Compact form</b></td></tr> <tr><td><pre>%s</pre></td></tr> <tr><td style="background-color:#d0d0d0;"><b>Expression formatted</b></td></tr> <tr><td><pre>%s</pre></td></tr> </table> ''' return HTML(template % (text1, text2))
def test_time9(self): v = loads('23:59:59-23:00')[0] self.assertEqual(type(v), time) self.assertIsInstance(v.tzinfo, tzinfo) s = dumps([v]) self.assertEqual(s, '^23:59:59-23') self.assertEqual(v.utcoffset().seconds/60, 60)
def test_usafe_5(self): vs = [] v = C() v.x = 1 v.y = lst = [] for i in range(3): w = D() w.z = i lst.append(w) vs.append(w) vs.append(v) text = axon.dumps(vs, crossref=1, pretty=1) # self.assertEqual(text, '''\ # &1 D: # z: 0 # &2 D: # z: 1 # &3 D: # z: 2 # C: # x: 1 # y: [*1 *2 *3]''') self.assertEqual(vs[-1].y[0], vs[0]) self.assertEqual(vs[-1].y[1], vs[1]) self.assertEqual(vs[-1].y[2], vs[2])
def test_vs_yaml_2(self): d = {} for j in range(50): lst = [] for i in range(100): lst.append({ random_string(8): random.random(), random_string(8): random.randint(1,99999), random_string(8): random_string(32), random_string(8): random.random(), random_string(8): random.randint(1,99999), random_string(8): random_string(32), }) d[random_string(8)] = lst yaml_text = yaml.dump(d, Dumper=yaml.CDumper) t0 = time.time() v = yaml.load(yaml_text, Loader=yaml.CLoader) dt_yaml = time.time() - t0 axon_text = axon.dumps([d], pretty=False) #print(axon_text) t0 = time.time() v = axon.loads(axon_text) dt_axon = time.time() - t0 print('axon:', dt_axon, 'yaml', dt_yaml)
def test_vs_json_2(self): lst = [] for i in range(4000): lst.append({ random_string(8): random.random(), random_string(8): random.randint(1,99999), random_string(8): random_string(32), random_string(8): random.random(), random_string(8): random.randint(1,99999), random_string(8): random_string(32), random_string(8): random.random(), random_string(8): random.randint(1,99999), random_string(8): random_string(32), random_string(8): random.random(), random_string(8): random.randint(1,99999), random_string(8): random_string(32), }) axon_text = axon.dumps(lst) gc.collect() gc.disable() t0 = time.time() v = axon.loads(axon_text) dt_axon = time.time() - t0 gc.enable() json_text = json.dumps(lst) gc.collect() gc.disable() t0 = time.time() v = json.loads(json_text) dt_json = time.time() - t0 gc.enable() print('axon:', dt_axon, 'json', dt_json)
def xml2axon(from_, to_=None, pretty=1, braces=0): ''' Convert from `XML` to `AXON`. :from: The path of input file with `XML` or `XML` string. :to: The path of output file with `XML`` (default is `None`). If `to` is valid path then result of convertion to `AXON` will write to the file. :result: If `to` is `None` then return string with `AXON`, else return `None`. ''' _text = from_.lstrip() if _text.startswith('<'): tree = etree.fromstring(from_) else: tree = etree.parse(from_) root = tree._root if to_ is None: return axon.dumps([root], pretty=pretty, braces=braces) else: axon.dump(to_, [root], pretty=pretty, braces=braces)
def test_time10(self): v = loads('23:59:59+3:15')[0] self.assertEqual(type(v), time) self.assertIsInstance(v.tzinfo, tzinfo) s = dumps([v]) self.assertEqual(s, '^23:59:59+03:15') self.assertEqual(v.utcoffset().seconds/60, 3*60+15)
def test_datetime8(self): v = loads('2010-01-01T23:59:59+23:00')[0] self.assertEqual(type(v), datetime) self.assertIsInstance(v.tzinfo, tzinfo) s = dumps([v]) self.assertEqual(s, '^2010-01-01T23:59:59+23') self.assertEqual(v.utcoffset().seconds/60, 23*60)
def test_datetime11(self): v = loads('2010-01-01T23:59:59-3:15')[0] self.assertEqual(type(v), datetime) self.assertIsInstance(v.tzinfo, tzinfo) s = dumps([v]) self.assertEqual(s, '^2010-01-01T23:59:59-03:15') self.assertEqual(v.utcoffset().seconds/60, 1440-3*60-15)
def test_vs_yaml_1(self): lst = [] for i in range(2000): lst.append({ random_string(8): random.random(), random_string(8): random.randint(1,99999), random_string(8): random_string(32), random_string(8): random.random(), random_string(8): random.randint(1,99999), random_string(8): random_string(32), random_string(8): random.random(), random_string(8): random.randint(1,99999), random_string(8): random_string(32), }) yaml_text = yaml.dump(lst, Dumper=yaml.CDumper) t0 = time.time() v = yaml.load(yaml_text, Loader=yaml.CLoader) dt_yaml = time.time() - t0 axon_text = axon.dumps(lst, pretty=False) t0 = time.time() v = axon.loads(axon_text) dt_axon = time.time() - t0 print('axon:', dt_axon, 'yaml', dt_yaml)
def test_usafe_3(self): v = E(1, 2, 3) text = axon.dumps([v]) v1 = axon.loads(text, mode='strict')[0] self.assertEqual(v.a, v1.a) self.assertEqual(v.b, v1.b) self.assertEqual(v.c, v1.c)
def test_str3(self): t = '''"\ abc\ def"''' v = loads(t)[0] self.assertEqual(v, 'abcdef') s = dumps([v]) self.assertEqual(s, '"abcdef"')
def test_1(self): from random import randint for i in range(10): btext = bytes([randint(1, 255) for i in range(256)]) text1 = dumps([btext]) # print('***', btext, '***') # print('***', text1, '***') btext1 = loads(text1)[0] self.assertEqual(btext, btext1)
def test_str6(self): t = '''"abfdsfdfd sdfdfdfdfsdfdwrerwrwerwe cvbnbnfrtt"''' v = loads(t)[0] self.assertEqual(v, '''abfdsfdfd sdfdfdfdfsdfdwrerwrwerwe cvbnbnfrtt''') s = dumps([v]) self.assertEqual(s, t)
def test_usafe_1(self): v = C() v.a = 1 v.b = 2 v.c = 3 text = axon.dumps([v]) #display(text) v1 = axon.loads(text, mode='strict')[0] self.assertEqual(v.a, v1.a) self.assertEqual(v.b, v1.b) self.assertEqual(v.c, v1.c)
def test_usafe_4(self): v = C() v.x = 1 v.y = lst = [] for i in range(3): w = D() w.z = i lst.append(w) text = axon.dumps([v]) #self.assertEqual(text, 'C{x:1 y:[D{z:0} D{z:1} D{z:2}]}') v1 = axon.loads(text, mode='strict')[0] self.assertEqual(v1.x, 1) self.assertEqual(len(v1.y), 3) self.assertEqual(all([type(z) is D for z in v1.y]), True) self.assertEqual(all([z.z==z1.z for z,z1 in zip(v1.y, lst)]), True)
def test_usafe_2(self): v = C() v.a = 1 v.b = 2 v.c = 3 w = D() w.a = 'a' w.b = [1,2] w.c = 2 text = axon.dumps([v, w]) v1, w1 = axon.loads(text, mode='strict') self.assertEqual(v.a, v1.a) self.assertEqual(v.b, v1.b) self.assertEqual(v.c, v1.c) self.assertEqual(w.a, w1.a) self.assertEqual(w.b, w1.b) self.assertEqual(w.c, w1.c)
def test_usafe_6(self): text = """\ graph { nodes: [ &1 node { 1 1} &2 node { 1 2} &3 node { 2 1} &4 node { 2 2}] edges: [ edge { *1 *2} edge { *1 *3} edge { *2 *3} edge { *1 *4} edge { *3 *4}]} """ obs = axon.loads(text, mode='strict') text2 = axon.dumps(obs, crossref=1, sorted=0) obs2 = axon.loads(text2, mode='strict') self.assertEqual(obs2[0].nodes[0] is obs2[0].edges[0].p1, True) self.assertEqual(obs2[0].nodes[1] is obs2[0].edges[0].p2, True) self.assertEqual(obs2[0].nodes[2] is obs2[0].edges[1].p2, True) self.assertEqual(obs2[0].nodes[2] is obs2[0].edges[2].p2, True) self.assertEqual(obs2[0].nodes[2] is obs2[0].edges[4].p1, True) self.assertEqual(obs2[0].nodes[3] is obs2[0].edges[3].p2, True) self.assertEqual(obs2[0].nodes[3] is obs2[0].edges[4].p2, True)
def test_vs_json_7(self): lst = [] for i in range(5000): lst.extend([ random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), random.random(), ]) lst = [lst] axon_text = axon.dumps(lst) gc.collect() gc.disable() t0 = time.time() v = axon.loads(axon_text) dt_axon = time.time() - t0 gc.enable() json_text = json.dumps(lst) gc.collect() gc.disable() t0 = time.time() v = json.loads(json_text) dt_json = time.time() - t0 gc.enable() print('axon:', dt_axon, 'json', dt_json)
def make_test(data): t0 = time.time() axon_text = axon.dumps([data]) dt_axon_dump = time.time() - t0 axon_times_dump.append(dt_axon_dump) t0 = time.time() v = axon.loads(axon_text) dt_axon_load = time.time() - t0 axon_times_load.append(dt_axon_load) t0 = time.time() json_text = json.dumps(data) dt_json_dump = time.time() - t0 json_times_dump.append(dt_json_dump) t0 = time.time() v = json.loads(json_text) dt_json_load = time.time() - t0 json_times_load.append(dt_json_load) if use_yaml: t0 = time.time() yaml_text = yaml.dump(data, Dumper=yaml.CDumper) dt_yaml_dump = time.time() - t0 yaml_times_dump.append(dt_yaml_dump) t0 = time.time() v = yaml.load(json_text, Loader=yaml.CLoader) dt_yaml_load = time.time() - t0 yaml_times_load.append(dt_yaml_load) else: dt_yaml_dump, dt_yaml_load = float('nan'), float('nan') print('Dump:: axon: %.3f json: %.3f yaml: %.3f' % (dt_axon_dump, dt_json_dump, dt_yaml_dump)) print('Load:: axon: %.3f json: %.3f yaml: %.3f' % (dt_axon_load, dt_json_load, dt_yaml_load))
def test_vs_json_5(self): lst = [] for i in range(5000): lst.extend([ random_string(8), random_string(8), random_string(32), random_string(8), random_string(32), random_string(32), random_string(8), random_string(8), random_string(8), random_string(8), random_string(32), random_string(8), random_string(8), random_string(32), random_string(32), random_string(8), random_string(32), random_string(32), random_string(8), random_string(8), random_string(32), random_string(8), random_string(32), random_string(32), ]) axon_text = axon.dumps(lst) ldr = axon.iloads(axon_text) gc.collect() gc.disable() t0 = time.time() v = axon.loads(axon_text) dt_axon = time.time() - t0 gc.enable() json_text = json.dumps(lst) gc.collect() gc.disable() t0 = time.time() v = json.loads(json_text) dt_json = time.time() - t0 gc.enable() print('axon:', dt_axon, 'json', dt_json)
def test_vs_json_3(self): d = {} for j in range(70): lst = [] for i in range(100): lst.append({ random_string(8): random.random(), random_string(8): random.randint(1,99999), random_string(8): random_string(32), random_string(8): random.random(), random_string(8): random.randint(1,99999), random_string(8): random_string(32), random_string(8): random.random(), random_string(8): random.randint(1,99999), random_string(8): random_string(32), }) d[random_string(8)] = lst axon_text = axon.dumps([d]) #print(axon_text) gc.collect() gc.disable() t0 = time.time() v = axon.loads(axon_text) dt_axon = time.time() - t0 gc.enable() json_text = json.dumps(d) gc.collect() gc.disable() t0 = time.time() v = json.loads(json_text) dt_json = time.time() - t0 gc.enable() print('axon:', dt_axon, 'json', dt_json)
def json2axon(from_, to_=None, pretty=1, braces=1): ''' Convert from `JSON` to `AXON`. :from: The path of input file with `JSON` or `JSON` string. :to: The path of output file with `JSON` (default is `None`). If `to` is valid path then result of convertion to `AXON` will write to the file. :result: If `to` is `None` then return string with `AXON`, else return `None`. ''' text = from_.lstrip() if text.startswith('[') or text.startswith('{'): val = json.loads(from_) else: val = json.load(from_) if to_ is None: return axon.dumps([val], pretty=pretty, braces=braces) else: axon.dump(to_, [val], pretty=pretty, braces=braces)
def test_time2(self): v = loads('^23:59:59')[0] self.assertEqual(type(v), time) s = dumps([v]) self.assertEqual(s, '^23:59:59')
def test_datetime3(self): v = loads('2010-01-01T23:59:59.000123')[0] self.assertEqual(type(v), datetime) self.assertEqual(v.tzinfo, None) s = dumps([v]) self.assertEqual(s, '^2010-01-01T23:59:59.000123')
def test_datetime2(self): v = loads('1-01-01T23:59:59')[0] self.assertEqual(type(v), datetime) self.assertEqual(v.tzinfo, None) s = dumps([v]) self.assertEqual(s, '^1-01-01T23:59:59')
def test_date1(self): v = loads('2010-12-01')[0] self.assertEqual(type(v), date) s = dumps([v]) self.assertEqual(s, '^2010-12-01')
def test_time3(self): v = loads('23:59:59.000123')[0] self.assertEqual(type(v), time) s = dumps([v]) self.assertEqual(s, '^23:59:59.000123')
def test_int1a(self): v = loads('-0')[0] self.assertEqual(v, 0) s = dumps([v]) self.assertEqual(s, '0')
def test_date4(self): v = loads('0-00-00')[0] self.assertEqual(type(v), date) s = dumps([v]) self.assertEqual(s, '^0-00-00')
def test_date3(self): v = loads('12-01-01')[0] self.assertEqual(type(v), date) s = dumps([v]) self.assertEqual(s, '^12-01-01')
def test_invalid_key_1(self): try: text = dumps([{('foo', 'bar'): True}]) except TypeError: pass
def test_time1(self): v = loads('00:00')[0] self.assertEqual(type(v), time) s = dumps([v]) self.assertEqual(s, '^00:00')
def test_datetime1(self): v = loads('2010-01-01T00:00')[0] self.assertEqual(type(v), datetime) self.assertEqual(v.tzinfo, None) s = dumps([v]) self.assertEqual(s, '^2010-01-01T00:00')
def test_int2(self): v = loads('-1')[0] self.assertEqual(v, -1) s = dumps([v]) self.assertEqual(s, '-1')