0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonで設定ファイルを作ってみた

Last updated at Posted at 2020-11-23

Pythonベースで設定ファイル作ってみた

色々なファイル形式を使ったが、jsonとyaml、toml以外は可読性が低いことjsonはコメントが書けないことが不満だったのと、ラムダ式が入れれないのが不満なので作ってみた。

※以下のPysonPYthon Setting Object Notationから。

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":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("単位行列(4x4):",mat)
print("設定出来ている?:",stngfil[1])
単位行列(4x4):[[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
設定出来ている?:True

感想

結構満足できるものが作れられたので良かった。改良もやってみたい。

0
0
8

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?