Reputation: 10191
I have a python script that, in it's "initialize" runmode, accesses multiple files on my system and assembles what it thinks is the correct data. This data needs to be reviewed by a user before setting the script to run in "final" mode, when the data is actually used.
Right now I'm writing out the data to be reviewed into a data.py file, in the form of python data structures, e.g. the contents of data.py could be:
data1 = "script_generated_filename_1"
data2 = [ "script_generated_date1",
"script_generated_date2" ]
After the user validates the data.py file, the "final" runmode then uses an "import data" call to gain access to the data, via data.data1, data.data2, etc...
I've been trying to clean up my python programming style, and come more in line with what is generally considered to be pythonic. After reading through the module docs, I have my doubts as to whether using the import function in this way is pythonic, or if there is a more mainstream way to accomplish this type of user-validation using python.
Upvotes: 3
Views: 180
Reputation: 8915
As far as I can tell, there's nothing un-pythonic about your code: You're properly importing namespaces and referring to them and you're treating the module as the unit of reuse.
There may be a more elegant solution, and if you can post more specific details about the problem domain, we can probably help you with that. But elegance must often be offset with economy.
My pragmatic view is that if you're not repeating yourself, and you're not breaking any major conventions, and your code works, then you're in the right: Even if the design is flawed, a good design can always be later imposed upon a flawed design that's DRY ;)
Upvotes: 1
Reputation: 375814
This is a fine thing to do with a module. The thing you want to avoid is executing code that does too much, or has side-effects, at import time, and this doesn't do that.
Upvotes: 6
Reputation: 9683
I think you should put your data in text files (JSON, CSV, whatever is appropriate) and then just have your script read it in. In general (not just in Python), it's best to keep code and data separate.
Upvotes: 3