Slide 1

Slide 1 text

Click A Pleasure To Write A Pleasure To Use Sebastian Vetter @elbaschid Slides: http://bit.ly/click-slides

Slide 2

Slide 2 text

Seb • @elbaschid • Living in Vancouver ! • Python Engineer at Eventbase

Slide 3

Slide 3 text

Why This Talk?

Slide 4

Slide 4 text

Terminology https://www.flickr.com/photos/chris_schultz/11306328493

Slide 5

Slide 5 text

Parameter • Argument • Option

Slide 6

Slide 6 text

Argument • Mandatory parameter • Only values required What It Looks Like: $ pgcli postgresql://....

Slide 7

Slide 7 text

Option • Optional parameter • Name and value required What It Looks Like: $ heroku --help $ heroku logs --app my-heroku-app

Slide 8

Slide 8 text

(Sub-)Command • Nested commands allowed • Groups sub-commands • Has options & arguments What It Looks Like: $ pip install django

Slide 9

Slide 9 text

In Plain Python https://www.flickr.com/photos/tutam/5008073062/

Slide 10

Slide 10 text

Most Basic CLI import sys if len(sys.argv) <= 1: print('You need to give me an argument') sys.exit(1) args = sys.argv[1:] print('Your argument are: {}'.format(args))

Slide 11

Slide 11 text

The Limitations • Manual parsing • No input validation • No help text formatting

Slide 12

Slide 12 text

Choose A Library https://www.flickr.com/photos/14601516@N00/3778738056

Slide 13

Slide 13 text

Standard Library • optparse • argparse

Slide 14

Slide 14 text

There Are Others • docopt • clint • ...many more...

Slide 15

Slide 15 text

Introducing Click https://www.flickr.com/photos/27587002@N07/7155464950

Slide 16

Slide 16 text

click • Author: Armin Ronacher • Version 6.x • http://click.pocoo.org/6/

Slide 17

Slide 17 text

click • Intuitive • Nestable and composable • Better handling of input and output • Why Click?

Slide 18

Slide 18 text

Let's Build A CLI https://www.flickr.com/photos/31381897@N03/3967634792

Slide 19

Slide 19 text

pip install click

Slide 20

Slide 20 text

Setting Up A CLI https://www.flickr.com/photos/sveinhal/2416609728

Slide 21

Slide 21 text

On Github • Click Template: https://github.com/elbaschid/cc-python-cli • Example Code: bit.ly/pycon-click-example

Slide 22

Slide 22 text

Create A Project $ pip install cookiecutter $ cookiecutter https://github.com/elbaschid/cc-python-cli ... $ cd my-!-tool $ pip install -e . $ my_!_tool I am the my_!_tool CLI

Slide 23

Slide 23 text

Decorator-based approach # my_!_tool/cli.py import click @click.command() def main(): click.secho('I am the my_!_tool CLI', fg='blue')

Slide 24

Slide 24 text

Our Example https://www.flickr.com/photos/judy-van-der-velden/14687818499

Slide 25

Slide 25 text

Ad Notifications • Parsing a given search URL • Extracting ads • Ignore "seen" ads • Email notifications

Slide 26

Slide 26 text

Specify a URL

Slide 27

Slide 27 text

Run it! $ ad_notifier "http://www.pinkbike.com/buysell/..." Processing URL: http://www.pinkbike.com/buysell/list/... Found 20 ads!

Slide 28

Slide 28 text

Arguments https://www.flickr.com/photos/alper/2781645509

Slide 29

Slide 29 text

Add an argument # ad_notifier/cli.py @click.command() @click.argument('url') def main(url): click.echo('Processing URL:', url) ads = find_ads(url) click.echo('Found {} ads!'.format(len(ads)))

Slide 30

Slide 30 text

Send an Notification Email

Slide 31

Slide 31 text

Run it! $ ad_notifier "http://..." --email [email protected] Processing URL: http://... Found 20 ads! Sending email to [email protected]

Slide 32

Slide 32 text

Options https://www.flickr.com/photos/photographingtravis/15427838493

Slide 33

Slide 33 text

Add Email Option @click.command() @click.argument('url') @click.option('--email', envvar='EMAIL_ADDRESS') def main(url, email): ... if email and ads: send_email(email, ads)

Slide 34

Slide 34 text

Handle New Ads Only

Slide 35

Slide 35 text

Run it! $ ad_notifier "http://..." ... Found 0 ads! $ ad_notifier "http://..." --reset-cache ... Found 20 ads!

Slide 36

Slide 36 text

Flags https://www.flickr.com/photos/pennstatelive/16216912657

Slide 37

Slide 37 text

Reset the cache @click.command() ... @click.option('--reset-cache', default=False, is_flag=True) def main(url, email, reset_cache): click.echo('Processing URL:', url) new_ads = find_new_ads(url, reset_cache) click.echo('Found {} ads!'.format(len(new_ads))) ...

Slide 38

Slide 38 text

Make it a Periodic Task

Slide 39

Slide 39 text

Run it! $ # With scheduler $ ad_notifier run_periodically \ --run-every 5 --email [email protected] $ # Once $ ad_notifier run_once \ --email [email protected]

Slide 40

Slide 40 text

(Sub-)Commands (https://www.flickr.com/photos/soldiersmediacenter/5754070333)

Slide 41

Slide 41 text

Add a run & schedule command @click.group() def main(): pass @main.command() ... def run_once(url, email, reset_cache): pass @main.command() ... @click.option('--run-every', default=5, type=int) def run_periodically(..., run_every): pass

Slide 42

Slide 42 text

Not Very DRY @main.command() @click.argument('url') @click.option('--email') @click.option('--reset-cache', default=False, is_flag=True) def run_once(url, email, reset_cache): ... @main.command() @click.argument('url') @click.option('--email') @click.option('--reset-cache', default=False, is_flag=True) @click.option('--run-every', default=5, type=int) def run_periodically(url, email, reset_cache, run_every): ...

Slide 43

Slide 43 text

Context https://www.flickr.com/photos/chanceprojects/16286089602

Slide 44

Slide 44 text

Using the Context obj @click.group() @click.argument('url') @click.option('--email') @click.option('--reset-cache', ...) @click.pass_context def main(context, url, email, reset_cache): context.obj = { 'url': url, 'email': email, 'reset_cache': reset_cache}

Slide 45

Slide 45 text

Updating Sub-Commands @main.command() @click.pass_context def run_once(context): process_url(**context.obj)

Slide 46

Slide 46 text

Add schedule subcommand @main.command() @click.option('--run-every', default=5, type=int) @click.pass_context def run_periodically(context, run_every): schedule.every(run_every).minutes.do( process_url, **context.obj) while True: schedule.run_pending() time.sleep(1)

Slide 47

Slide 47 text

The Final Command $ # With scheduler $ ad_notifier run_periodically \ --run-every 5 --email [email protected] $ # Once $ ad_notifier run_once \ --email [email protected]

Slide 48

Slide 48 text

! We Did It !

Slide 49

Slide 49 text

Improve Documentation

Slide 50

Slide 50 text

No Effort $ ad_notifier Usage: ad_notifier [OPTIONS] URL COMMAND [ARGS]... Options: --email TEXT --reset-cache --help Show this message and exit. Commands: run_once run_periodically

Slide 51

Slide 51 text

Adding help text @click.option('--reset-cache', default=False, is_flag=True, help='reset the internal ads cache') @click.pass_context def main(context, url, email, reset_cache): """ Check pinkbike ads for URL and (optionally) send email notification. """

Slide 52

Slide 52 text

A Little Effort $ ad_notifier Usage: ad_notifier [OPTIONS] URL COMMAND [ARGS]... Check pinkbike ads for URL and (optionally) send email notification. Options: --email TEXT email to send notifications to --reset-cache reset the internal ads cache --help Show this message and exit. Commands: run_once Run check for new ads once. run_periodically Run scheduler to check for new ads...

Slide 53

Slide 53 text

Also Check Out • Environment Variables • Parameter Types • Testing • Bash Autocomplete

Slide 54

Slide 54 text

Questions? https://www.flickr.com/photos/thomashawk/9058066607

Slide 55

Slide 55 text

Click A Pleasure To Write A Pleasure To Use Sebastian Vetter @elbaschid Slides: http://bit.ly/click-slides