Skip to content

Instantly share code, notes, and snippets.

@sbliven
Last active April 22, 2021 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbliven/10c3ddabcb32301b9fb475748ed75e35 to your computer and use it in GitHub Desktop.
Save sbliven/10c3ddabcb32301b9fb475748ed75e35 to your computer and use it in GitHub Desktop.
Improved version of sbliven/d413c2f2f4af3bf56e13d1f8656751b1 using configparser
[nlsa] |~
name = Custom Settings |~
x = 4 |~
"""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()
#!/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 "$@"
@sbliven
Copy link
Author

sbliven commented Apr 22, 2021

This improves on the previous version by using ini files to store the settings rather than python modules.

  • Call sbatch submit_ini.sh mysettings.ini
  • Inside submit_ini.sh, "mysettings.ini" is stored as $@ (standard bash argument handling)
  • On the compute node, submit_ini.sh calls python read_settings_ini.py mysettings.ini
  • read_settings_ini.py gets "mysettings.ini" passed as sys.argv[1]. This is parsed by the cli library (here the built-in argparse, but click is nicer) and stored as args.settings
  • The config file is loaded

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"])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment