Using functions for automation COMMAN D LIN E AUTOMATION IN P YTH - - PowerPoint PPT Presentation

using functions for automation
SMART_READER_LITE
LIVE PREVIEW

Using functions for automation COMMAN D LIN E AUTOMATION IN P YTH - - PowerPoint PPT Presentation

Using functions for automation COMMAN D LIN E AUTOMATION IN P YTH ON Noah Gift Lecturer, Northwestern & UC Davis & UC Berkeley | Founder, Pragmatic AI Labs Functions are units of work functions are units of work def work()


slide-1
SLIDE 1

Using functions for automation

COMMAN D LIN E AUTOMATION IN P YTH ON

Noah Gift

Lecturer, Northwestern & UC Davis & UC Berkeley | Founder, Pragmatic AI Labs

slide-2
SLIDE 2

COMMAND LINE AUTOMATION IN PYTHON

Functions are units of work

functions are units of work

def work() """Performs work""" return 1+1 In [1]: work() Out[1]: 2

slide-3
SLIDE 3

COMMAND LINE AUTOMATION IN PYTHON

Accepting input into a function

function that accepts input

def more_work(x,y): """Adds two variables""" return x+y In [2]: more_work(5,6) Out[2]: 1 more_work(9,6) Out[3]: 15

slide-4
SLIDE 4

COMMAND LINE AUTOMATION IN PYTHON

Decorators are also functions

from functools import wraps import time def instrument(f): @wraps(f) def wrap(*args, **kw): ts = time.time() result = f(*args, **kw) te = time.time() print( f"function: {f.__name__}, args: [{args}, {kw}] took: {te-ts} sec") return result return wrap

slide-5
SLIDE 5

COMMAND LINE AUTOMATION IN PYTHON

How does a decorator work?

from functools import wraps def do_nothing_decorator(f): @wraps(f) def wrapper(*args, **kwds): print('INSIDE DECORATOR: This is called before function') return f(*args, **kwds) return wrapper @do_nothing_decorator def hello_world(): """This is a hello world function""" ( )

slide-6
SLIDE 6

COMMAND LINE AUTOMATION IN PYTHON

Using a hello world decorator

# Call the decorated function hello_world() INSIDE DECORATOR: This is called before function Hello World Function # Name is preserved print(f"Function Name: {hello_world.__name__}") Function Name: hello_world

slide-7
SLIDE 7

COMMAND LINE AUTOMATION IN PYTHON

Timing decorator in action

@instrument def lazy_work(x,y, sleep=2): """Sleeps then works""" time.sleep(sleep) return x+y In [7]: lazy_work(4,9) function: lazy_work, args: [(4, 9), {'sleep': 3}] took: 3.000096082687378 sec Out[7]: 13

slide-8
SLIDE 8

COMMAND LINE AUTOMATION IN PYTHON

Putting it all together

Functions are units of work Functions are more powerful when they take input Decorators are functions too

slide-9
SLIDE 9

COMMAND LINE AUTOMATION IN PYTHON

Examples of common decorators

Many automation tasks involve functions and decorators

flask web framework click command line tool framework numba open source JIT compiler

custom proling, tracing and timing

slide-10
SLIDE 10

Let's practice!

COMMAN D LIN E AUTOMATION IN P YTH ON

slide-11
SLIDE 11

Understand script input

COMMAN D LIN E AUTOMATION IN P YTH ON

Noah Gift

Lecturer, Northwestern & UC Davis & UC Berkeley | Founder, Pragmatic AI Labs

slide-12
SLIDE 12

COMMAND LINE AUTOMATION IN PYTHON

Use sys.argv

sys.argv captures input to script import sys print(sys.argv) $ python ./script.py one two # Remember that Python lists index at 0 ['script.py', 'one', 'two']

slide-13
SLIDE 13

COMMAND LINE AUTOMATION IN PYTHON

Parsing arguments

Parse arguments by index

# grab first argument arg1 = out[1] print(arg1)

  • ne
slide-14
SLIDE 14

COMMAND LINE AUTOMATION IN PYTHON

Writing a script with sys.argv

anatomy of a python sys.argv script

import sys def hello(user_input): print(f"From a user: {user_input}") if __name__ == "__main__": arg1 = sys.argv[1] hello(arg1)

slide-15
SLIDE 15

COMMAND LINE AUTOMATION IN PYTHON

Parsing input from script

python hello_argv.py something From a user: something python hello_argv.py another From a user: another

slide-16
SLIDE 16

Let's practice!

COMMAN D LIN E AUTOMATION IN P YTH ON

slide-17
SLIDE 17

Introduction to Click

COMMAN D LIN E AUTOMATION IN P YTH ON

Noah Gift

Lecturer, Northwestern & UC Davis & UC Berkeley | Founder, Pragmatic AI Labs

slide-18
SLIDE 18

COMMAND LINE AUTOMATION IN PYTHON

What is click?

Python package for creating 'beautiful' command line interfaces Three main features: arbitrary nesting of commands automatic help page generation lazy loading of subcommands at runtime

slide-19
SLIDE 19

COMMAND LINE AUTOMATION IN PYTHON

Basic click structure

import click library

import click

use command decorator and option decorator

@click.command() @click.option() def func(): pass

call function when run as script

if __name__ == '__main__': ()

slide-20
SLIDE 20

COMMAND LINE AUTOMATION IN PYTHON

Simple click example

import click @click.command() @click.option('--phrase', prompt='Enter a phrase', help='') def tokenize(phrase): """tokenize phrase""" click.echo(f"tokenized phrase: {phrase.split()}") if __name__ == '__main__': tokenize()

slide-21
SLIDE 21

COMMAND LINE AUTOMATION IN PYTHON

Using a click application

running from the terminal

python hello_click.py

user prompted to enter phrase

Enter a phrase: this is a rabbit

  • utput of click application

tokenized phrase: ['this', 'is', 'a', 'rabbit']

slide-22
SLIDE 22

COMMAND LINE AUTOMATION IN PYTHON

Automatic help generation

? python hello_click.py --help Usage: hello_click.py [OPTIONS] tokenize phrase Options:

  • -phrase TEXT
  • -help Show this message and exit.
slide-23
SLIDE 23

Let's practice!

COMMAN D LIN E AUTOMATION IN P YTH ON

slide-24
SLIDE 24

Using click to write command line tools

COMMAN D LIN E AUTOMATION IN P YTH ON

Noah Gift

Lecturer, Northwestern & UC Davis & UC Berkeley | Founder, Pragmatic AI Labs

slide-25
SLIDE 25

COMMAND LINE AUTOMATION IN PYTHON

Mapping functions to subcommands

import click @click.group() def cli(): pass @cli.command() def one(): click.echo('One-1') @cli.command() def two(): click.echo('Two-2') if __name__ == '__main__': cli()

slide-26
SLIDE 26

COMMAND LINE AUTOMATION IN PYTHON

Using click subcommands

python click_functions.py Usage: click_functions.py [OPTIONS] COMMAND [ARGS]... Options:

  • -help Show this message and exit.

Commands:

  • ne

two python click_functions.py one

slide-27
SLIDE 27

COMMAND LINE AUTOMATION IN PYTHON

Click utilities

click utilities can:

generate colored output generate paginated output clear the screen wait for key press launch editors write les

# Write with click with click.open_file(filename, 'w') as f: f.write('jazz flute')

slide-28
SLIDE 28

COMMAND LINE AUTOMATION IN PYTHON

Click and stdout

import click click.echo('Hello DataCamp!') Hello DataCamp!

click.echo can: generate colored output generate blinking or bold text print both unicode and binary data

slide-29
SLIDE 29

COMMAND LINE AUTOMATION IN PYTHON

Testing click applications

import click from click.testing import CliRunner @click.command() @click.argument('phrase') def echo_phrase(phrase): click.echo('You said: %s' % phrase) runner = CliRunner() result = runner.invoke(echo_phrase, ['Have data will camp']) assert result.output == 'You said: Have data will camp\n'

slide-30
SLIDE 30

Let's practice!

COMMAN D LIN E AUTOMATION IN P YTH ON

slide-31
SLIDE 31

Course Summary: Command Line Automation

COMMAN D LIN E AUTOMATION IN P YTH ON

Noah Gift

Lecturer, Northwestern & UC Davis & UC Berkeley | Founder, Pragmatic AI Labs

slide-32
SLIDE 32

COMMAND LINE AUTOMATION IN PYTHON

Chapter 1 - IPython and Python interpreter

Python interpreter IPython Shell SList data types

slide-33
SLIDE 33

COMMAND LINE AUTOMATION IN PYTHON

Chapter 2 - Shell commands with subprocess

Execute shell commands Using subprocess Sending input Passing arguments safely

slide-34
SLIDE 34

COMMAND LINE AUTOMATION IN PYTHON

Chapter 3 - Walking the le system

Dealing with le systems Find les matching a pattern High-level le and directory operations Using pathlib

slide-35
SLIDE 35

COMMAND LINE AUTOMATION IN PYTHON

Chapter 4 - Command line functions

Using functions for automation Understand script input Introduction to click Using click

slide-36
SLIDE 36

Next steps

COMMAN D LIN E AUTOMATION IN P YTH ON