mercredi 1 mai 2019

Storing data that includes functions

What I have is a list types of data, each identified by a name. For example 'length' and 'weight'. These are used to read and write bytes from files. Each metric stores a different amount of bytes, so I have a class Metric that stores for example a description and a byte length for the metric. Then, I can just keep a JSON file or an XML file of metric definitions that can be added to whenever.

I now want to differentiate how these bytes are read and written by adding a to_bytes and from_bytes method to the class. I could store the function definition as a string in the JSON file and just eval() it, or I could write separate child classes for each metric.

What is the best way to store function definitions alongside other data? I'm open to any suggestions, but the best solution is one that keeps it easy to manually add new metrics to the collection.

Aucun commentaire:

Enregistrer un commentaire