Skip to content

Instantly share code, notes, and snippets.

@wassname
Last active November 10, 2023 06:07
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 wassname/f1c23636cc04b39176bd82f45c6e398a to your computer and use it in GitHub Desktop.
Save wassname/f1c23636cc04b39176bd82f45c6e398a to your computer and use it in GitHub Desktop.
argparse in jupyter?
"""
sometimes you want to run or adapt a cli script from jupyter, here a decent way to do it
"""
argvs = """
--rank 16
--context=128
--vae_context=64
"""
argvs = argvs.replace('\n', ' ').strip()
argv = [s.strip() for s in argvs.split(" ") if s and not s.startswith("#")]
print(argv)
args = parser.parse_args(argv)
args
args = parser.parse_args(argv)
args
# or using a argparse Namespace
from argparse import Namespace
Namespace(a=1)
# another way with no args is
args = parser.parse_known_args()[0]
args.__dict__['a']=1 # an any positional args
# a better way might be to use https://pypi.org/project/simple-parsing/ where you can just insansiate the options classe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment