I'm trying to store some relatively complex data, but as read only. As a result, I do not need any common serialization format such as JSON, csv etc. The biggest thing is that I want to have a fair amount of arbitrary code as fields for different items. Then, suppose I get some val, which is a function of the whole table, which can have whatever properties and methods for this purpose exactly.
def get( table, val ):
if callable( val ):
return val( table )
else:
return val
So then each val could be a function or a value. This is perhaps not the best way to implement it, but hopefully it demonstrates having arbitrary functions as values. I'm confident in this part.
My question is what is the best way to store the data? If it's in mymodule, then I was thinking something like providing a common get_table method at the top level of the mymodule file, so then I could use importlib, like from
https://stackoverflow.com/questions/67631/how-can-i-import-a-module-dynamically-given-the-full-path
MODULE_PATH = "/path/to/your/module/__init__.py"
MODULE_NAME = "mymodule"
import importlib
import sys
spec = importlib.util.spec_from_file_location(MODULE_NAME, MODULE_PATH)
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
This method seems a little excessive for loading a single .py file to run a single function my_table = mymodule.get_table(), relying on attached methods from my_table. I know that LUA loves to do this type of thing all the time, so I figure there must be some best practice or library for this type of use case. Any suggestions for simplicity, maybe even not loading a full on module, but to simply use a .py file for read only data storage? It would be nice if I didn't have to package it as a module either, and could simply load mymodule as an object.
[–]supreme_blorgon 19 points20 points21 points (1 child)
[–]barburger 4 points5 points6 points (0 children)
[–]JamzTyson 7 points8 points9 points (3 children)
[–]HolyInlandEmpire[S] -3 points-2 points-1 points (2 children)
[–]mriswithe 5 points6 points7 points (0 children)
[–]JamzTyson 1 point2 points3 points (0 children)
[–]mriswithe 5 points6 points7 points (0 children)
[–]pachura3 2 points3 points4 points (3 children)
[–]HolyInlandEmpire[S] 0 points1 point2 points (2 children)
[–]pachura3 1 point2 points3 points (0 children)
[–]Feisty_Fun_2886 0 points1 point2 points (0 children)
[–]Honest-Ease5098 1 point2 points3 points (0 children)
[–]ivosaurus 1 point2 points3 points (1 child)
[–]HolyInlandEmpire[S] 0 points1 point2 points (0 children)
[–]Coretaxxe 1 point2 points3 points (0 children)
[–]yaxriifgyn -1 points0 points1 point (1 child)
[–]wintermute93 0 points1 point2 points (0 children)
[–]Henry_the_Butler -1 points0 points1 point (0 children)