Thanks for the follow-up.
To the drawing board to re-architect the data model
Thanks for the follow-up.
To the drawing board to re-architect the data model
Why do you need 01
as a key of the object?
My JSON structure is hierarchical such as plant/line/cell. I used the plant name as the key with line keys within the value. In my case, the plant, line, or cell name can be alphanumeric, but sometimes it’s numeric, like 01.
Having the ID as the key allowed a very intuitive structure and very simple parsing of the hierarchy such as: json[plant][line][cell]
Given the constraints you outlined, I’ll have to rework that structure to create an id key and move the name to be a value of that key, and then convert each level of the hierarchy to be an array of dictionaries, rather than just a hierarchical dictionary.
Hopefully I explained it ok.
Consider just adding an alpha prefix to make your keys: Pxxxx
, Lyyyy
, Czzzz
.
Well, I see why you did it like that, but I’ve never been a fan of that approach because it leaves you open to key errors. When you access your “hierarchical” dictionary, you actually have no idea what keys it has until you try to get a key out of it. I much prefer to use “standardized” dictionaries, where they always have the keys I expect, but some key could potentially have no value.
my_dict = {"plant": None, "line", None, "cell": None}
Accessed as my_dict["plant"]
.
If I need to use the same structure in multiple places, I’ll actually make a more heavy-weight class to define my object, which makes things even less fragile, at the expense of extra overhead.
class MyDefinedObj:
def __init__(self, plant=None, line=None, cell=None):
self.plant = plant
self.line = line
self.cell = cell
Then initialized somewhere:
from MyProjectLibrary import MyDefinedObj
x = MyDefinedObj(plant="PlantA", line=1, cell=2)
# do some stuff
return x.line
Thanks for the suggestions, I really appreciate them.
I have some buttons on my view to select plants, lines, etc which act as filters. Having the gateway script that creates the memory tag do the heavy lifting to structure the JSON hierarchically seems that it would be more efficient, rather than having every client application need to parsing through all entries in the array when they apply a filter.
I’m an amateur of python, so maybe I’m wrong about this.
@cmallonee I understand what you are getting at regarding key validation. Fortunately for my use-case, this json structure is the single-source of truth, and the entire view is dynamically built based on it, so there isn’t a chance for a mis-matched key, because everything in the view is derived from the same information.