Created
April 22, 2021 20:46
-
-
Save sbliven/d413c2f2f4af3bf56e13d1f8656751b1 to your computer and use it in GitHub Desktop.
Example of reading a "settings" python module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""A settings module based on global variables""" | |
name = "Custom Settings" | |
x = 4 | |
y = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""main module | |
usage: python read_settings_py.py mysettings | |
This will import settings from mysettings.py | |
""" | |
import argparse | |
import importlib | |
def main(args=None): | |
parser = argparse.ArgumentParser(description="") | |
parser.add_argument("settings", help="settings file") | |
args = parser.parse_args(args) | |
# Dynamic import based on the command line argument | |
settings = importlib.import_module(args.settings) | |
print("Name: %s" % settings.name) | |
print("Coords: %d,%d" % (settings.x, settings.y)) | |
if __name__ == "__main__": | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Slurm submission script | |
# usage: sbatch submit_py.sh mysettings | |
#SBATCH -p hourly | |
#SBATCH -n 1 | |
#SBATCH --account=merlin | |
module purge | |
module load anaconda | |
conda activate dev27 | |
# pass sbatch arguments to python | |
python read_settings_py.py "$@" |
Using python modules for storing settings is not recommended. The official way of storing settings is in ini
files. Here's a better implementation: https://gist.github.com/sbliven/10c3ddabcb32301b9fb475748ed75e35
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is an example of submitting a slurm job with an argument. It uses a bare module to store settings, similar to the current implementation.
sbatch submit_py.sh mysettings
mysettings
is stored as$@
(standard bash argument handling)python read_settings_py.py mysettings
importlib.import_module
. This line would be equivalent toimport mysettings
. It would search sys.path and find./mysettings.py