I made a configuration file with Python

I made a configuration file based on Python

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

Impressions

I'm glad that I was able to make something that was quite satisfying. I also want to improve it.

Recommended Posts

I made a configuration file with Python
I made a fortune with Python.
I made a character counter with Python
I made a Hex map with Python
I made a roguelike game with Python
I made a simple blackjack with Python
I made a neuron simulator with Python
I made a python dictionary file for Neocomplete
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
I made blackjack with python!
I made a Twitter fujoshi blocker with Python ①
[Python] I made a Youtube Downloader with Tkinter.
I made a python text
I made blackjack with Python.
I want to write to a file with Python
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made a Twitter BOT with GAE (python) (with a reference)
I made a Christmas tree lighting game with Python
I made a net news notification app with Python
I made a Python3 environment on Ubuntu with direnv.
I made a LINE BOT with Python and Heroku
I made a Line-bot using Python!
I made a simple typing game with tkinter in Python
I made a package to filter time series with python
I made a simple book application with python + Flask ~ Introduction ~
Creating a simple PowerPoint file with Python
I made a payroll program in Python!
I drew a heatmap with seaborn [Python]
I tried a functional language with Python
What I did with a Python array
I made a life game with Numpy
After studying Python3, I made a Slackbot
I made a WEB application with Django
I made a library to easily read config files with Python
I made a package that can compare morphological analyzers with Python
Python> I made a test code for my own external file
I made a lot of files for RDP connection with Python
[Python] I made an image viewer with a simple sorting function.
I made a shuffle that can be reset (reverted) with Python
I made a poker game server chat-holdem using websocket with python
I made a library that adds docstring to a Python stub file.
I made a segment tree with python, so I will introduce it
[python] I made a class that can write a file tree quickly
〇✕ I made a game
I made a stamp substitute bot with line
How to read a CSV file with Python 2/3
I want to make a game with Python
Procedure for creating a LineBot made with Python
I made a simple Bitcoin wallet with pycoin
I made a LINE Bot with Serverless Framework!
I tried reading a CSV file using Python
Create a Photoshop format file (.psd) with python
Read line by line from a file with Python
I made a random number graph with Numpy
Open a file dialog with a python GUI (tkinter.filedialog)
I made a Caesar cryptographic program in Python.
I made a Python Qiita API wrapper "qiipy"
I made a QR code image with CuteR
I made a tool to automatically browse multiple sites with Selenium (Python)