Last active
April 22, 2021 21:39
-
-
Save sbliven/10c3ddabcb32301b9fb475748ed75e35 to your computer and use it in GitHub Desktop.
Improved version of sbliven/d413c2f2f4af3bf56e13d1f8656751b1 using configparser
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
[nlsa] |~ | |
name = Custom Settings |~ | |
x = 4 |~ |
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_ini.py mysettings.ini | |
This will import settings from mysettings.ini | |
""" | |
import argparse | |
import importlib | |
try: | |
import configparser | |
except ImportError: | |
# Python 2. Note that the python 3 syntax is much nicer | |
import ConfigParser as configparser | |
def load_config(filename): | |
config = configparser.ConfigParser() | |
# set defaults | |
config.add_section("nlsa") | |
config.set("nlsa", "x", 0) | |
config.set("nlsa", "y", 0) | |
# load actual file | |
config.read(filename) | |
return config | |
def main(args=None): | |
parser = argparse.ArgumentParser(description="") | |
parser.add_argument("settings", help="settings ini file") | |
args = parser.parse_args(args) | |
config = load_config(args.settings) | |
print("Name: %s" % config.get("nlsa", "name")) | |
print("Coords: %d,%d" % (config.getint("nlsa", "x"), config.getint("nlsa", "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_ini.sh mysettings.ini | |
#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_ini.py "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This improves on the previous version by using ini files to store the settings rather than python modules.
sbatch submit_ini.sh mysettings.ini
$@
(standard bash argument handling)python read_settings_ini.py mysettings.ini
This file is python 2.7 compatible. Unfortunately the 2.7 ConfigParser lacks many features available in python 3 (e.g. accessing values like
config["nlsa"]["x"]
)