Commit 88c20221 authored by Toby Hodges's avatar Toby Hodges
Browse files

Merge branch 'argparse-draft' into 'master'

draft of argparse section

See merge request grp-bio-it-workshops/intermediate-python!34
parents be8fe6f2 d03884e1
This diff is collapsed.
......@@ -6,6 +6,7 @@
[aoc-2018]: https://adventofcode.com/2018
[aoc-2019]: https://adventofcode.com/2019
[aoc-2019-stats]: https://adventofcode.com/2019/stats
[argparse]: https://docs.python.org/3/library/argparse.html
[bio-it-itpp]: https://git.embl.de/grp-bio-it/ITPP
[biopython]: https://biopython.org/
[black-still-beta]: https://github.com/psf/black#note-this-is-a-beta-product
......@@ -15,6 +16,7 @@
[cc-by-legal]: https://creativecommons.org/licenses/by/4.0/legalcode
[cdh]: https://cdh.carpentries.org/
[ci]: http://communityin.org/
[click]: https://click.palletsprojects.com/en/7.x/
[coc-reporting]: https://docs.carpentries.org/topic_folders/policies/incident-reporting.html
[coc]: https://docs.carpentries.org/topic_folders/policies/code-of-conduct.html
[concept-maps]: https://carpentries.github.io/instructor-training/05-memory/
......@@ -25,6 +27,8 @@
[cran-stringr]: https://cran.r-project.org/package=stringr
[cschin-assembler-notebook]: https://nbviewer.jupyter.org/github/cschin/Write_A_Genome_Assembler_With_IPython/blob/master/Write_An_Assembler.ipynb
[dc-lessons]: http://www.datacarpentry.org/lessons/
[docopt]: http://docopt.org/
[doctest]: https://docs.python.org/3/library/doctest.html
[email]: mailto:team@carpentries.org
[eu-odp]: https://data.europa.eu/euodp/en/
[flake8]: https://flake8.pycqa.org/
......@@ -32,6 +36,7 @@
[ford-any-colour-black]: https://en.wikiquote.org/wiki/Talk:Henry_Ford#Any_color,_as_long_as_it_is_black
[github-blog-contributing]: https://github.blog/2012-09-17-contributing-guidelines/
[github-importer]: https://import.github.com/
[gooey]: https://pypi.org/project/Gooey/
[grus-dont-like-notebooks]: https://www.youtube.com/watch?v=7jiPeIFXb6U
[importer]: https://github.com/new/import
[jekyll-collection]: https://jekyllrb.com/docs/collections/
......@@ -51,6 +56,7 @@
[lesson-reference]: {{ relative_root_path }}{% link reference.md %}
[lesson-setup]: {{ relative_root_path }}{% link setup.md %}
[linter-comparison]: https://books.agiliq.com/projects/essential-python-tools/en/latest/linters.html
[logging]: https://docs.python.org/3/library/logging.html
[markdown-cheatsheet]: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
[matplotlib-hinton]: https://matplotlib.org/examples/specialty_plots/hinton_demo.html
[mit-license]: https://opensource.org/licenses/mit-license.html
......@@ -70,6 +76,7 @@
[pep-8-recommendations]: https://www.python.org/dev/peps/pep-0008/#programming-recommendations
[pep-8-whitespace]: https://www.python.org/dev/peps/pep-0008/#whitespace-in-expressions-and-statements
[py-rse-coc]: https://merely-useful.github.io/py-rse/teams.html#teams-coc
[py-rse-config]: https://merely-useful.github.io/py-rse/configuration.html
[py-rse-style]: https://merely-useful.github.io/py-rse/py-rse-style.html
[pycodestyle]: https://pypi.org/project/pycodestyle/
[pyflakes]: https://pypi.org/project/pyflakes/
......@@ -77,6 +84,7 @@
[python-gapminder]: https://swcarpentry.github.io/python-novice-gapminder/
[pyyaml]: https://pypi.python.org/pypi/PyYAML
[r-markdown]: https://rmarkdown.rstudio.com/
[realpython-parser-comparison]: https://realpython.com/comparing-python-command-line-parsing-libraries-argparse-docopt-click/
[repository-issues]: https://git.embl.de/grp-bio-it-workshops/intermediate-python/-/issues
[rosalind]: http://rosalind.info/
[rosalind-list-view]: http://rosalind.info/problems/list-view/
......@@ -91,6 +99,7 @@
[swc-lessons]: https://software-carpentry.org/lessons/
[swc-python-gapminder]: http://swcarpentry.github.io/python-novice-gapminder/
[swc-releases]: https://github.com/swcarpentry/swc-releases
[sys-module]: https://docs.python.org/3/library/sys.html
[training]: https://carpentries.github.io/instructor-training/
[vscode-docstring]: https://marketplace.visualstudio.com/items?itemName=njpwerner.autodocstring
[workshop-repo]: {{ site.workshop_repo }}
......
import argparse
parser=argparse.ArgumentParser()
parser.parse_args()
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("input_file",
help="path to an input file for processing")
parser.add_argument("numbers", type=int, nargs="+",
help="integers to be summed")
parser.add_argument("--language", "-l",
choices=["en", "de", "es", "pt"], default="en",
help="language of logging messages")
parser.add_argument("--output", "-o",
help="path to an output file (default: print to STDOUT)")
args = parser.parse_args()
logging_messages = {
"en": "input file:\t{}",
"de": "Eingabedatei:\t{}",
"es": "nombre de archivo de entrada:\t{}",
"pt": "nome do arquivo de entrada:\t{}",
}
message = logging_messages[args.language].format(args.input_file)
print(message)
result = sum(args.numbers)
if args.output:
with open(args.output, "w") as outfh:
outfh.write(f"{result}\n")
else:
print(result)
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("input_file",
help="path to an input file for processing")
parser.add_argument("numbers", type=int, nargs="+",
help="integers to be summed")
parser.add_argument("--language", "-l",
choices=["en", "de", "es", "pt"], default="en",
help="language of logging messages")
parser.add_argument("--output", "-o",
help="path to an output file (default: print to STDOUT)")
group = parser.add_mutually_exclusive_group()
group.add_argument("--verbose", "-v", action="store_true")
group.add_argument("--silent", "-q", action="store_true")
args = parser.parse_args()
if args.verbose:
print("Starting...")
if not args.silent:
logging_messages = {
"en": "input file:\t{}",
"de": "Eingabedatei:\t{}",
"es": "nombre de archivo de entrada:\t{}",
"pt": "nome do arquivo de entrada:\t{}",
}
message = logging_messages[args.language].format(args.input_file)
print(message)
if args.verbose:
print("Calculating result...")
result = sum(args.numbers)
if args.output:
if args.verbose:
print("Writing result to file...")
with open(args.output, "w") as outfh:
outfh.write(f"{result}\n")
else:
print(result)
if args.verbose:
print("Finishing...")
import argparse
parser=argparse.ArgumentParser(
description="A multilingual program to calculate the sum of "
"an arbitrary number of integers "
"and do something with an input file..."
)
parser.add_argument("input_file",
help="path to an input file for processing")
parser.add_argument("numbers", type=int, nargs="+",
help="integers to be summed")
parser.add_argument("--language", "-l",
choices=["en", "de", "es", "pt"], default="en",
help="language of logging messages")
parser.add_argument("--output", "-o",
help="path to an output file (default: print to STDOUT)")
group = parser.add_mutually_exclusive_group()
group.add_argument("--verbose", "-v", action="store_true")
group.add_argument("--silent", "-q", action="store_true")
args = parser.parse_args()
if args.verbose:
print("Starting...")
if not args.silent:
logging_messages = {
"en": "input file:\t{}",
"de": "Eingabedatei:\t{}",
"es": "nombre de archivo de entrada:\t{}",
"pt": "nome do arquivo de entrada:\t{}",
}
message = logging_messages[args.language].format(args.input_file)
print(message)
if args.verbose:
print("Calculating result...")
result = sum(args.numbers)
if args.output:
if args.verbose:
print("Writing result to file...")
with open(args.output, "w") as outfh:
outfh.write(f"{result}\n")
else:
print(result)
if args.verbose:
print("Finishing...")
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("input_file",
help="path to an input file for processing")
parser.parse_args()
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("input_file",
help="path to an input file for processing")
args = parser.parse_args()
print(f"input_file provided was {args.input_file}")
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("input_file",
help="path to an input file for processing")
parser.add_argument("number1", help="the first number")
parser.add_argument("number2", help="the second number")
args = parser.parse_args()
print(f"input_file provided was {args.input_file}")
print(args.number1)
print(args.number2)
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("input_file",
help="path to an input file for processing")
parser.add_argument("number1", type=int, help="the first number")
parser.add_argument("number2", type=int, help="the second number")
args = parser.parse_args()
print(f"input_file provided was {args.input_file}")
print(args.number1 + args.number2)
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("input_file",
help="path to an input file for processing")
parser.add_argument("number1", type=int, help="the first number")
parser.add_argument("number2", type=int, help="the second number")
parser.add_argument("--output", "-o",
help="path to an output file (default: print to STDOUT)")
args = parser.parse_args()
print(f"input_file provided was {args.input_file}")
result = args.number1 + args.number2
if args.output:
with open(args.output, "w") as outfh:
outfh.write(f"{result}\n")
else:
print(result)
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("input_file",
help="path to an input file for processing")
parser.add_argument("number1", type=int, help="the first number")
parser.add_argument("number2", type=int, help="the second number")
parser.add_argument("--output", "-o",
help="path to an output file (default: print to STDOUT)")
parser.add_argument("--loud", "-l", action="store_true",
help="print info about the input filepath in ALL CAPS")
args = parser.parse_args()
message = f"input_file provided was {args.input_file}"
if args.loud:
message=message.upper()
print(message)
result = args.number1 + args.number2
if args.output:
with open(args.output, "w") as outfh:
outfh.write(f"{result}\n")
else:
print(result)
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("input_file",
help="path to an input file for processing")
parser.add_argument("number1", type=int, help="the first number")
parser.add_argument("number2", type=int, help="the second number")
parser.add_argument("language", choices=["en", "de", "es", "pt"],
help="language of logging messages")
parser.add_argument("--output", "-o",
help="path to an output file (default: print to STDOUT)")
args = parser.parse_args()
logging_messages = {
"en": "input file:\t{}",
"de": "Eingabedatei:\t{}",
"es": "nombre de archivo de entrada:\t{}",
"pt": "nome do arquivo de entrada:\t{}",
}
message = logging_messages[args.language].format(args.input_file)
print(message)
result = args.number1 + args.number2
if args.output:
with open(args.output, "w") as outfh:
outfh.write(f"{result}\n")
else:
print(result)
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("input_file",
help="path to an input file for processing")
parser.add_argument("number1", type=int, help="the first number")
parser.add_argument("number2", type=int, help="the second number")
parser.add_argument("--language", "-l",
choices=["en", "de", "es", "pt"], default="en",
help="language of logging messages")
parser.add_argument("--output", "-o",
help="path to an output file (default: print to STDOUT)")
args = parser.parse_args()
logging_messages = {
"en": "input file:\t{}",
"de": "Eingabedatei:\t{}",
"es": "nombre de archivo de entrada:\t{}",
"pt": "nome do arquivo de entrada:\t{}",
}
message = logging_messages[args.language].format(args.input_file)
print(message)
result = args.number1 + args.number2
if args.output:
with open(args.output, "w") as outfh:
outfh.write(f"{result}\n")
else:
print(result)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment