Skip to content

Instantly share code, notes, and snippets.

@pmslavin
Created July 6, 2022 09:41
Show Gist options
  • Save pmslavin/bb3c5a7a67889797bc2ce6bf00679016 to your computer and use it in GitHub Desktop.
Save pmslavin/bb3c5a7a67889797bc2ce6bf00679016 to your computer and use it in GitHub Desktop.
Script to update Pywr documents using `pandas_kwargs` between new and old formats
import argparse
import json
import sys
def configure_args(args):
parser = argparse.ArgumentParser(
usage="%(prog)s [--remove-pandas-kwargs | --add-pandas-kwargs] <filename>",
epilog="",
description="Updates Pywr model `pandas_kwargs` syntax. A new file containing updated syntax is created."
)
meg = parser.add_mutually_exclusive_group()
meg.add_argument("-r", "--remove-pandas-kwargs",
metavar="<filename>",
help="Remove `pandas_kwargs` key from parameters and add contents directly to component",
type=str,
default=None
)
meg.add_argument("-a", "--add-pandas-kwargs",
metavar="<filename>",
type=str,
help="Add `pandas_kwargs` key to parameters and nest arguments within",
default=None
)
return parser.parse_args(args)
def handle_args(args):
if args.remove_pandas_kwargs:
srcfile = args.remove_pandas_kwargs
action = "rem"
elif args.add_pandas_kwargs:
srcfile = args.add_pandas_kwargs
action = "add"
patched = process(srcfile, action)
write_output(srcfile, patched, action)
def process(srcfile, action):
pk = "pandas_kwargs"
pd = "parse_dates"
with open(srcfile, 'r') as fp:
src = json.load(fp)
if "parameters" not in src:
return
for param in src["parameters"].values():
if action == "rem" and pk in param:
for k, v in param[pk].items():
param[k] = v
del param[pk]
elif action == "add" and pd in param:
param[pk] = {pd: param[pd]}
del param[pd]
return src
def write_output(srcfile, output, action):
newtok = srcfile.split('.')
act_txt = "no_pandas_kwargs" if action == "rem" else "pandas_kwargs"
newtok.insert(-1, act_txt)
newfile = ".".join(newtok)
with open(newfile, 'w') as fp:
json.dump(output, fp, indent=2)
if __name__ == "__main__":
args = configure_args(sys.argv[1:])
handle_args(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment