The long title contain also a mini-exaple because I couldn't explain well what I'm trying to do. Nonethless, the similar questions windows led me to various implementation. But since I read multiple times that it's a bad design, I would like to ask if what I'm trying to do is a bad design rather asking how to do it. For this reason I will try to explain my use case with a minial functional code.
Suppose I have a two classes, each of them with their own parameters:
class MyClass1:
def __init__(self,param1=1,param2=2):
self.param1=param1
self.param2=param2
class MyClass2:
def __init__(self,param3=3,param4=4):
self.param3=param3
self.param4=param4
I want to print param1...param4 as a string (i.e. "param1"..."param4") and not its value (i.e.=1...4).
Why? Two reasons in my case:
I have a GUI where the user is asked to select one of of the class type (Myclass1, Myclass2) and then it's asked to insert the values for the parameters of that class. The GUI then must show the parameter names ("param1", "param2" if MyClass1 was chosen) as a label with the Entry Widget to get the value. Now, suppose the number of MyClass and parameter is very high, like 10 classes and 20 parameters per class. In order to minimize the written code and to make it flexible (add or remove parameters from classes without modifying the GUI code) I would like to cycle all the parameter of Myclass and for each of them create the relative widget, thus I need the paramx names under the form od string. The real application I'm working on is even more complex, like parameter are inside other objects of classes, but I used the simpliest example. One solution would be to define every parameter as an object where param1.name="param1" and param1.value=1. Thus in the GUI I would print param1.name. But this lead to a specifi problem of my implementation, that's reason 2:
MyClass1..MyClassN will be at some point printed in a JSON. The JSON will be a huge file, and also since it's a complex tree (the example is simple) I want to make it as simple as possibile. To explain why I don't like to solution above, suppose this situation: class MyClass1: def init(self,param1,param2,combinations=[]): self.param1=param1 self.param2=param2 self.combinations=combinations
Supposse param1 and param2 are now list of variable size, and combination is a list where each element is composed by all the combination of param1 and param2, and generate an output from some sort of calculation. Each element of the list combinations is an object SingleCombination,for example (metacode):
param1=[1,2] param2=[5,6] SingleCombination.param1=1 SingleCombination.param2=5 SingleCombination.output=1*5 MyInst1.combinations.append(SingleCombination).
In my case I will further incapsulated param1,param2 in a object called parameters, so every condition will hace a nice tree with only two object, parameters and output, and expanding parameters node will show all the parameters with their value.
If I use JSON pickle to generate a JSON from the situation above, it is nicely displayed since the name of the node will be the name of the varaible ("param1", "param2" as strings in the JSON). But if I do the trick at the end of situation (1), creating an object of paramN as paramN.name and paramN.value, the JSON tree will become ugly but especially huge, because if I have a big number of condition, every paramN contains 2 sub-element. I wrote the situation and displayed with a JSON Viewer, see the attached immage
I could pre processing the data structure before creating the JSON, the problem is that I use the JSON to recreate the data structure in another session of the program, so I need all the pieces of the data structure to be in the JSON.
So, from my requirements, it seems that the workround to avoid print the variable names creates some side effect on the JSON visualization that I don't know how to solve without changing the logic of my program...