DEV Community 👩‍💻👨‍💻

geraldew
geraldew

Posted on

Python Settings with Enumerations Saving and Loading as JSON

This is just some sharing of a method I recently derived during some work on hand. I make no claim that it's a "best" anything in any way at all.

I've written a general description into the README of the repository but as I don't like seeing articles here on Dev that just say "see my thing at link", I'll give some commentary here too.

The repository is located at:

The idea is to have a dictionary structure in Python - probably with multiple levels of sub-dictionaries inside - and to be able to save that to a JSON file and then also load it back from a JSON file. In particular, to service the use-case of a program having internally coded "settings" but which it can write to JSON - to then allow customisation from outside the program.

  • Nothing new there you may say! True, that's generally how JSON gets used.

But the catch is that I wanted the Python structure to have Enumerations as either keys or values - and for those to be duly reloaded from the text-only JSON.

Frankly this isn't exactly anything innovative, instead it's just a matter of writing enough code to go through the motions and to enact the required logic.

It was an interesting enough idea to seem worth exploring as an exercise. I was certainly able to produce something good enough for the situation that I had to hand - i.e. it seems to work.

The current method involves defining two structures in the Python code:

  • one to be the originating "settings" (that gets exported to JSON)
  • and one to hold a "pro forma" as a guide to the parts of the structure hold an enumeration data type.

Of course, if you know about JSON you may well know that people have been talking the idea of a JSON schema system for quite some time. So let me be clear, this is NOT a JSON schema system. It's more of a Python hack for getting the JSON handler to just one more thing.

  • And for those who know about XML, yes of course that comes with a full schema ability that can define types and perform conformity validations. But I was after something that was a tweak on JSON for the same reasons we ever use JSON - its relative simplicity compared to XML.

At this point, what I've created can work with dictionaries and values, where either/both of those are string or Enumeration types. I do think I'd like to make it also handle lists and integers inside the structures. In terms of the program code I'd also like to have it properly "detect" the enumeration types rather than have to already know explicitly what they are. I expect that will possible, but I'll have to scratch a bit deeper into the surface of how Python knows its own object types than I immediately know how to code for.

This was a sideways exercise and I have other priorities to attend to. However I'd like to make this robust enough to start using in some other projects.

Anyway, if this sounds interesting then perhaps take a look. Ideally one of you will point to where someone else has already done something like this - and much better.

Top comments (0)

👋 New to DEV?

Head over to our Welcome Thread and tell us what you're working on!