command line interfaces
play

Command-line interfaces CREATIN G ROBUS T P YTH ON W ORK F LOW S - PowerPoint PPT Presentation

Command-line interfaces CREATIN G ROBUS T P YTH ON W ORK F LOW S Martin Skarzynski Co-Chair, Foundation for Advanced Education in the Sciences (FAES) Synergy with the shell Command-line interfaces (CLIs) Pass shell arguments to Python


  1. Command-line interfaces CREATIN G ROBUS T P YTH ON W ORK F LOW S Martin Skarzynski Co-Chair, Foundation for Advanced Education in the Sciences (FAES)

  2. Synergy with the shell Command-line interfaces (CLIs) Pass shell arguments to Python scripts python fit_model.py --alpha 0.2 Avoid using comments as an on/off switch # To retrain the model, # Uncomment the line below. # model.fit(x_train, y_train) Photo by Adrianna Calvo from Canva CREATING ROBUST PYTHON WORKFLOWS

  3. Build command-line interfaces argparse docopt Instantiate the ArgumentParser class Setup up CLI in �le docstring Call the add_argument() method Pass __doc__ to docopt() Call the parse_args() method Arguments are dictionary values Arguments are Namespace attributes """Pass a single argument to FILENAME. Usage: FILENAME.py ARGUMENT_NAME""" parser = argparse.ArgumentParser() argument_dict = docopt.docopt(__doc__) parser.add_argument('ARGUMENT_NAME') namespace = parser.parse_args() namespace.ARGUMENT_NAME CREATING ROBUST PYTHON WORKFLOWS

  4. Argparse and methods import datetime import argparse as ap from get_namespace import get_namespace def get_namespace() -> ap.Namespace: if __name__ == '__main__': parser = ap.ArgumentParser() namespace = get_namespace() parser.add_argument('format') print(datetime.datetime.now() return parser.parse_args() .strftime(namespace.format)) $ python now.py %H:%M 8:30 CREATING ROBUST PYTHON WORKFLOWS

  5. Docopt and docstrings import datetime as dt """Get the current date or time. from get_arg_dict import get_arg_dict Usage: now.py FORMAT""" if __name__ == '__main__': from typing import Dict arg_dict = get_arg_dict() import docopt print(dt.datetime.now() def get_arg_dict() -> Dict[str, str]: .strftime(arg_dict['FORMAT'])) return docopt.docopt(__doc__) $ python now.py %B/%d March 8 CREATING ROBUST PYTHON WORKFLOWS

  6. Automate execution of Shell Commands: echo hello Scripts: bash myscript.sh Python Scripts: python myscript.py Modules: python -m timeit -h CREATING ROBUST PYTHON WORKFLOWS

  7. Make shell command Make�le: Make�le structure: time: TARGET: python now.py %H:%M RECIPE $ make python now.py 19:30 CREATING ROBUST PYTHON WORKFLOWS

  8. Make dependencies Make�le: Make�le structure: time: TARGET: python now.py %H:%M RECIPE date: TARGET: python now.py %d/%m RECIPE all: date time TARGET: DEPENDENCIES CREATING ROBUST PYTHON WORKFLOWS

  9. Make all Make�le: $ make all time: python now.py %H:%M python now.py %H:%M 19:32 python now.py %d/%m date: 08/03 python now.py %m-%d all: date time CREATING ROBUST PYTHON WORKFLOWS

  10. Command-line interface documentation CREATING ROBUST PYTHON WORKFLOWS

  11. Let's practice building CLIs! CREATIN G ROBUS T P YTH ON W ORK F LOW S

  12. Git version control CREATIN G ROBUS T P YTH ON W ORK F LOW S Martin Skarzynski Co-Chair, Foundation for Advanced Education in the Sciences (FAES)

  13. Git Records and manages modi�cations made to projects Prevents lost work or unwanted changes CREATING ROBUST PYTHON WORKFLOWS

  14. Basic git work�ow Make changes in the working directory ( . ) Add changes to the index ( ./.git/index ) Commit changes to version control history ( ./.git ) A "commit" ~ a milestone CREATING ROBUST PYTHON WORKFLOWS

  15. Shell versus API Use git via Graphical User Interface (GUI) Command line (shell) Application Programming Interface (API) GitPython (Python interface to git) Create a version controlled directory Called a repository Add changes to the index Commit changes to version control history CREATING ROBUST PYTHON WORKFLOWS

  16. Git init Run git init shell command: Run Python code in a �le called init.py : import git print(git.Repo.init()) $ git init $ python init.py Initialized empty Git repository in <git.Repo "/Users/USER/REPO/.git"> /Users/USERNAME/REPONAME/.git/ CREATING ROBUST PYTHON WORKFLOWS

  17. Make init Run git init using Make: Run init.py using Make: .git/: .git/: git init python init.py $ make $ make git init python init.py make: '.git/' is up to date. make: '.git/' is up to date. CREATING ROBUST PYTHON WORKFLOWS

  18. Untracked �les import git repo.untracked_files repo = git.Repo() ['Makefile', 'init.py', 'commit.py'] CREATING ROBUST PYTHON WORKFLOWS

  19. Add untracked �les import git repo.untracked_files repo = git.Repo() add_list = repo.untracked_files [] if add_list: repo.index.add(add_list) CREATING ROBUST PYTHON WORKFLOWS

  20. Commit message import git repo = git.Repo() add_list = repo.untracked_files if add_list: repo.index.add(add_list) new = f"New files: {', '.join(add_list)}." CREATING ROBUST PYTHON WORKFLOWS

  21. Commit new �les import git repo = git.Repo() add_list = repo.untracked_files if add_list: repo.index.add(add_list) new = f"New files: {', '.join(add_list)}" print(repo.index.commit(new).message) $ python commit.py New files: Makefile, init.py, commit.py. CREATING ROBUST PYTHON WORKFLOWS

  22. Diff all �les import git repo = git.Repo() diff = repo.index.diff(None) CREATING ROBUST PYTHON WORKFLOWS

  23. Diff modi�ed �les import git repo = git.Repo() diff = repo.index.diff(None).iter_change_type('M') edit_list = [file.a_path for file in diff] CREATING ROBUST PYTHON WORKFLOWS

  24. Commit modi�ed �les import git repo = git.Repo() diff = repo.index.diff(None).iter_change_type('M') edit_list = [file.a_path for file in diff] if edit_list: repo.index.add(edit_list) modified = f"Modified files: {', '.join(edit_list)}." print(repo.index.commit(modified).message) CREATING ROBUST PYTHON WORKFLOWS

  25. Make commit $ make python commit.py Modified files: Makefile, commit.py. $ make python commit.py CREATING ROBUST PYTHON WORKFLOWS

  26. Let's practice automating version control work�ows! CREATIN G ROBUS T P YTH ON W ORK F LOW S

  27. Virtual environments CREATIN G ROBUS T P YTH ON W ORK F LOW S Martin Skarzynski Co-Chair, Foundation for Advanced Education in the Sciences (FAES)

  28. Virtual environments Are directories that Contain separate Python installations Facilitate Dependency management Reproducible analysis Python package development Photo by Katarzyna Modrzejewska from Canva CREATING ROBUST PYTHON WORKFLOWS

  29. Environment managers T ools that create and manage virtual environments Manager Superpower Part of the standard library venv Widely-used virtualenv High-level interface pipenv Not only for Python conda CREATING ROBUST PYTHON WORKFLOWS

  30. Dependency managers Dependency manager pip pipenv conda Dependency managers = tools that install and manage dependencies venv and virtualenv use pip pipenv and conda manage dependencies and environments CREATING ROBUST PYTHON WORKFLOWS

  31. Dependency manager �les Dependency manager Dependency management �le pip requirements.txt pipenv Pipfile conda environment.yml Dependency names and versions can be Speci�ed in dependency management �les All three dependency managers use requirements.txt Exact ( jupyter == 4.4.0 ) Minimum ( python >= 3.6.0 ) CREATING ROBUST PYTHON WORKFLOWS

  32. Install dependencies Create environment $ python -m venv .venv Option 1: Activate environment before pip install $ source .venv/bin/activate $ pip install --requirement requirements.txt Option 2: Use path to virtual environment Python to pip install $ .venv/bin/python -m pip install -r requirements.txt CREATING ROBUST PYTHON WORKFLOWS

  33. Make venv 1. Create a virtual environment in .venv 2. Install the dependencies in the virtual environment 3. Update the "Last Modi�ed" time for the target (.venv/bin/activate) .venv: python -m venv .venv .venv/bin/activate: .venv requirements.txt .venv/bin/python -m pip install -r requirements.txt touch .venv/bin/activate CREATING ROBUST PYTHON WORKFLOWS

  34. Make test Run all tests in a virtual environment Avoid failed tests due to missing or outdated project dependencies .venv: python -m venv .venv .venv/bin/activate: .venv requirements.txt .venv/bin/python -m pip install -r requirements.txt touch .venv/bin/activate test: .venv/bin/activate .venv/bin/python -m pytest src tests CREATING ROBUST PYTHON WORKFLOWS

  35. Venv module 1. Create a venv environment import venv With venv API venv.create('.venv') create() function CREATING ROBUST PYTHON WORKFLOWS

  36. Subprocess module 1. Create a venv environment import subprocess With venv API cp = subprocess.run( create() function ['.venv/bin/python', '-m', 2. List packages 'pip', 'list'], stdout=-1 With pip and subprocess ) subprocess.run() function print(cp.stdout.decode()) Returns CompletedProcess stdout or capture_output Package Version pip list ------------------------ ---------- PACKAGE_NAME 0.0.1 ... CREATING ROBUST PYTHON WORKFLOWS

  37. Package information cp = subprocess.run(['python', '-m', 'pip', 'show', 'pandas'], stdout=-1) print(cp.stdout.decode()) Name: pandas Version: 0.24.1 Summary: Powerful data structures for data analysis, time series, and statistics Home-page: http://pandas.pydata.org ... CREATING ROBUST PYTHON WORKFLOWS

Recommend


More recommend