Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PyCon 23: Smettiamola di scrivere script

PyCon 23: Smettiamola di scrivere script

Python è beautiful perché rende facile ogni inizio. Ma se vuoi che funzioni a opera d’arte hai bisogno dei test. Test automatici, eseguiti da una pipeline, sempre. Ma non si può con i script! Perchè? Ve lo spiegherò. Impariamo a scrivere applicazioni CLI, con TDD. Ora è il momento, non mancare!

This talk was given in Italian at PyCon 23, Florence, Italy. All slides with technical content are provided in English.

See original slides at https://slides.com/bittner/pycon-23-smettiamola-di-scrivere-script

Peter Bittner

May 27, 2023
Tweet

More Decks by Peter Bittner

Other Decks in Programming

Transcript

  1. print("This is important code") 1 2 for index, arg in

    enumerate(sys.argv): 3 print(f"[{index}]: {arg}") 4 What's Wrong with Scripts? Easy to get started Limited possibilities for structure Hard to (unit) test No dependency management Deployment may require care Custom user experience
  2. print("Usage: foo <bar> --baz") def main(): 1 2 3 if

    __name__ == "__main__": 4 main() 5 Why CLI Applications? Standardized user experience More possibilities for structure Possibilities for all kinds of testing Dependency management Packaging & distribution
  3. import argparse from . import __version__ def parse_arguments(): parser =

    argparse.ArgumentParser(description=__doc__) parser.add_argument('--version', action='version', version=__version__) parser.add_argument('filename') args = parser.parse_args() return args def main(): args = parse_arguments() ... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Argparse
  4. import click @click.command() @click.version_option() @click.argument('filename') def main(filename): click.echo(filename) 1 2

    3 4 5 6 7 Click import click @click.command() @click.version_option() @click.argument('filename', type=click.Path(exists=True)) def main(filename): click.echo(filename) 1 2 3 4 5 6 7 import click @click.command() @click.version_option() @click.argument('infile', type=click.File()) def main(infile): click.echo(infile.read()) 1 2 3 4 5 6 7
  5. """Foobar Usage: foobar (-h | --help | --version) foobar [-s

    | --silent] <file> foobar [-v | --verbose] <file> Positional arguments: file target file path name Optional arguments: -h, --help show this help message and exit -s, --silent don't show progress output -v, --verbose explain progress verbosely --version show program's version number and exit """ from docopt import docopt from . import __version__ def parse_arguments(): args = docopt(__doc__, version=__version__) return dict( file=args['<file>'], silent=args['-s'] or args['--silent'], verbose=args['-v'] or args['--verbose'], ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Docopt
  6. $ copier gh:painless-software/python-cli-test-helpers pycon-italia-cli 🎤 engine (Use arrow keys) »

    Argparse Click Docopt ... Demo 🐍 ✨ ✨ https://python-cli-test-helpers.readthedocs.io
  7. Thank you! for your precious time Painless Software Less pain,

    more fun. Most images taken from Wikipedia (CC-SA) Alberto Sordi animated GIF stolen from giffetteria.it