def test_to_toml(): import rtoml a = Diot(**test_dict) assert rtoml.loads(a.to_toml()) == { key: val for key, val in test_dict.items() if key != 'diot_nest' }
def test_subtable(): s = """\ [fruit] apple.color = "red" apple.taste.sweet = true [fruit.apple.texture] # you can add sub-tables smooth = true """ assert rtoml.loads(s) == ...
import json import rtoml configstr = {{in.config | repr}} outfile = {{out.outfile | quote}} infmt = {{envs.infmt | quote}} outfmt = {{envs.outfmt | quote}} data = rtoml.loads(configstr) if infmt == "toml" else json.loads(configstr) with open(outfile, "w") as fout: if outfmt == "toml": rtoml.dump(data, fout) else: json.dump(data, fout)
def rtoml_load(): return rtoml.loads(toml_str)
elif isinstance(value, list): a = [simplify(v) for v in value] try: a[0]['value'] except KeyError: return a except IndexError: pass return {'type': 'array', 'value': a} elif isinstance(value, str): return {'type': 'string', 'value': value} elif isinstance(value, bool): return {'type': 'bool', 'value': str(value).lower()} elif isinstance(value, int): return {'type': 'integer', 'value': str(value)} elif isinstance(value, float): return {'type': 'float', 'value': repr(value)} elif isinstance(value, datetime): return {'type': 'datetime', 'value': value.isoformat().replace('+00:00', 'Z')} elif isinstance(value, date): return {'type': 'date', 'value': value.isoformat()} elif isinstance(value, time): return {'type': 'time', 'value': value.strftime('%H:%M:%S.%f')} assert False, f'Unknown type: {type(value)}' if __name__ == '__main__': data = rtoml.loads(sys.stdin.read()) result = simplify(data) print(json.dumps(result))
def test_mixed_array(): assert rtoml.loads('x = [1.1, 2, 3.3]') == {'x': [1.1, 2, 3.3]} assert rtoml.loads('x = [1, ["Arrays are not integers."]]') == { 'x': [1, ['Arrays are not integers.']] } assert rtoml.loads('x = ["hi", 42]') == {'x': ['hi', 42]}