Hello, I have been studying Python for some time and doing some programs to join with Arduino.
My question is good practice in loading files.
I want to make a program that interprets files. My idea was inspired by the logic of HTML, CSS and SonicPI. Where files are created in any editor. This makes changes to the file with the instances and another program reads and executes the created codes. Just like an HTML editor.
I've seen the serialization forms: pickle, Shelve, and Json. I did not want encrypted files.
But what else did you like is the example below with the exec and Garbage Collector.
Example:
file Arguments.py
pt1 = ponto('cor pt1')
pt2 = ponto('cor pt2')
Program that reads file Files.py
import gc
class ponto(object):
def __init__(self,cor):
self.cor = cor
exec(open("ArqInstancias.py").read())
# execução e leitura
instancias = [i for i in gc.get_objects() if i.__class__ is ponto]
# recebe as instancias da classe ponto
for i in range(len(instancias)):
print(instancias[i].cor)
# imprime o atributo cor de cada instancia.
output / result:
cor pt1
cor pt2
And if I call pt1.cor I also have output. That is, this instance has been incorporated into the program.
It works, but I would like to know if this is a good practice or if there is any other way to do this "instance import" without encrypting the text.