1
Hello, I’m studying Python for some time and doing some programs to join with Arduino.
My question is on good practices in file uploading.
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. With this make changes to the file with the instances and another program reads and executes the created codes. Just like an HTML editor.
I’ve seen serialization forms: pickle, Shelve and Json. I didn’t want encrypted files.
But what else did what I would like is the example below with exec and Garbage Collector.
Example:
arqinstancias.py file
pt1 = ponto('cor pt1')
pt2 = ponto('cor pt2')
Program that reads the file Arqinstancias.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 / output:
cor pt1
cor pt2
And if I call pt1.cor I also have output. IE, this instance has been incorporated into the program.
It works, but I wonder if this is a good practice or if there is some other way to do this "import instances" without encrypting the text.
I’m not sure I understand your need, but just from hitting my eye, it seems to me that your approach to running an external script is potentially unsafe. Someone with bad intentions can create anything you will run without knowing... If you want to receive data points, isn’t it easier (and safe) to simply read a yaml, xml or json file, for example? All these are open text data, and are not encrypted.
– Luiz Vieira
Hello Thank you. I understand the risk of this approach. My question is if I care about xml, json if I will have the instances available in memory.
– Luiz Sousa
My goal is a program that reads a file, interprets the data and printa on the screen. Ex: pt1(x,y). pt2(w,z) and when I open it in the program it with Tkinter shows the points in the positions (x,y) and (w,z)
– Luiz Sousa
See more json and xml examples. mto thanks!
– Luiz Sousa
Well, json and xml are not encrypted but have an organization of their own. E creating/editing code in the format is complicated. I search a way to embed the instances created as if I had manually typed. I don’t know if it’s feasible.
– Luiz Sousa
Okay. Well, I offered an answer that might help you. Take a look.
– Luiz Vieira