I used various file formats, but the readability is low except for json, yaml, and toml. Json was dissatisfied with not being able to write comments, and was dissatisfied with not being able to include lambda expressions, so I made it.
pyson.py
import sys
import argparse
class File(dict):
@classmethod
def Load(cls,path):
data = None
with open(path,'r',encoding="utf-8") as fln:
var = []
for f in fln.read():
if(f.strip() == "" or f.strip()[0:1] == '#' or f.strip().split('(')[0] == '__import__'):
continue
else:
var.append(f)
data = cls.__Ld(var)
ret = eval(data)
return ret
@classmethod
def __Ld(cls,var):
ret = ""
for val in var:
ret += val
return ret
load = File.Load
Classes.pyson
{
"Vector3":lambda:[0 for var in range(3)],
"Vector4":lambda:[0 for var in range(4)],
"Matrix3x3":lambda:[[0 for x in range(3)]for y in range(3)],
"Matrix4x4":lambda:[[0 for x in range(4)]for y in range(4)],
#Identity matrix
"Identity":lambda mat:[[1 if x==y else 0 for x in range(len(mat[y]))]for y in range(len(mat))],
"is_liveing":True
}
Setting.pyson
(
"1",True,3.5
)
app.py
import pyson
ldfil = pyson.load("Classes.pyson")
stngfil = pyson.load("Setting.pyson")
mat = ldfil["Identity"](ldfil["Matrix4x4"]())
print("Identity matrix(4x4):",mat)
print("Can be set?:",stngfil[1])
Identity matrix(4x4):[[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
Can be set?:True
I'm glad that I was able to make something that was quite satisfying. I also want to improve it.
Recommended Posts