SNABSuite  0.x
Spiking Neural Architecture Benchmark Suite
Functions | Variables
autoformat_json Namespace Reference

Functions

def to_json (o, level=0)
 
def prettyfy (filename)
 

Variables

 parser = argparse.ArgumentParser(description='Autoformat json files in a folder')
 
 metavar
 
 type
 
 str
 
 help
 
 args = parser.parse_args()
 
 list_files = glob.glob(args.files + "*.json")
 
int INDENT = 4
 
string SPACE = " "
 
string NEWLINE = "\n"
 

Function Documentation

def autoformat_json.prettyfy (   filename)

Definition at line 73 of file autoformat_json.py.

73 def prettyfy(filename):
74  with open(filename) as file:
75  data = json.load(file)
76  with open(filename, 'w') as file:
77  file.write(to_json(data))
78 
def to_json(o, level=0)
def prettyfy(filename)
def autoformat_json.to_json (   o,
  level = 0 
)

Definition at line 42 of file autoformat_json.py.

42 def to_json(o, level=0):
43  ret = ""
44  if isinstance(o, dict):
45  ret += "{" + NEWLINE
46  comma = ""
47  for k,v in o.items():
48  ret += comma
49  comma = ",\n"
50  ret += SPACE * INDENT * (level+1)
51  ret += '"' + str(k) + '":' + SPACE
52  ret += to_json(v, level + 1)
53 
54  ret += NEWLINE + SPACE * INDENT * level + "}"
55  elif isinstance(o, str):
56  ret += '"' + o + '"'
57  elif isinstance(o, list):
58  ret += "[" + ",".join([to_json(e, level+1) for e in o]) + "]"
59  elif isinstance(o, bool):
60  ret += "true" if o else "false"
61  elif isinstance(o, int):
62  ret += str(o)
63  elif isinstance(o, float):
64  ret += '%.7g' % o
65  elif isinstance(o, numpy.ndarray) and numpy.issubdtype(o.dtype, numpy.integer):
66  ret += "[" + ','.join(map(str, o.flatten().tolist())) + "]"
67  elif isinstance(o, numpy.ndarray) and numpy.issubdtype(o.dtype, numpy.inexact):
68  ret += "[" + ','.join(map(lambda x: '%.7g' % x, o.flatten().tolist())) + "]"
69  else:
70  raise TypeError("Unknown type '%s' for json serialization" % str(type(o)))
71  return ret
72 
def to_json(o, level=0)

Variable Documentation

autoformat_json.args = parser.parse_args()

Definition at line 33 of file autoformat_json.py.

autoformat_json.help

Definition at line 32 of file autoformat_json.py.

int autoformat_json.INDENT = 4

Definition at line 38 of file autoformat_json.py.

autoformat_json.list_files = glob.glob(args.files + "*.json")

Definition at line 35 of file autoformat_json.py.

autoformat_json.metavar

Definition at line 32 of file autoformat_json.py.

string autoformat_json.NEWLINE = "\n"

Definition at line 40 of file autoformat_json.py.

autoformat_json.parser = argparse.ArgumentParser(description='Autoformat json files in a folder')

Definition at line 29 of file autoformat_json.py.

string autoformat_json.SPACE = " "

Definition at line 39 of file autoformat_json.py.

autoformat_json.str

Definition at line 32 of file autoformat_json.py.

autoformat_json.type

Definition at line 32 of file autoformat_json.py.