23
loading...
This website collects cookies to deliver better user experience
This is Day 10 of the #100DaysOfPython challenge.
PyInquirer
library to demonstrate how to add some command line prompts to make it easier to build an interactive program.hello-fire
directory with cli.py
as the main file with a few dependencies already installed.# Add the lib
$ pipenv install pyinquirer
cli.py
file that looks like the following:#!/usr/bin/env python
import fire
class IngestionStage(object):
def run(self):
return 'Ingesting! Nom nom nom...'
class DigestionStage(object):
def __init__(self):
self.satiated = False
def run(self, volume: int = 1) -> str:
self.satiated = True
return ' '.join(['Burp!'] * volume)
def status(self):
return 'Satiated.' if self.satiated else 'Not satiated.'
class Pipeline(object):
def __init__(self):
self.ingestion = IngestionStage()
self.digestion = DigestionStage()
def run(self):
print(self.ingestion.run())
print(self.digestion.run())
print(self.digestion.status())
return 'Pipeline complete'
if __name__ == '__main__':
fire.Fire(Pipeline)
python cli.py [command]
would run the program (ie python cli.py digestion run
would output Burp!
).DigestionStage
class run
method to default to 0 burps and request that we provide a volume.prompt
function and use that to get the input:from PyInquirer import prompt
# ... omitted for brevity
class DigestionStage(object):
def __init__(self):
self.satiated = False
# Update code with prompt question here
def run(self, volume: int = 0) -> str:
questions = [
{
'type': 'input',
'name': 'volume',
'message': 'How many burps?',
}
]
if volume == 0:
volume = int(prompt(questions)['volume'])
self.satiated = True
return ' '.join(['Burp!'] * volume)
def status(self):
return 'Satiated.' if self.satiated else 'Not satiated.'
python cli.py digestion run
:$ python cli.py digestion run
? How many burps? 5
Burp! Burp! Burp! Burp! Burp!
volume
altogether but we have left is so that Python Fire can cast the input into an int via a flag --volume
without a prompt.$ python cli.py digestion run --volume=10
# ... no prompt
Burp! Burp! Burp! Burp! Burp! Burp! Burp! Burp! Burp! Burp!
DigestionStage
to look like the following:class DigestionStage(object):
def __init__(self):
self.satiated = False
def run(self, volume: int = 0) -> str:
questions = [
{
'type': 'input',
'name': 'volume',
'message': 'How many burps?',
}
]
if volume == 0:
volume = int(prompt(questions)['volume'])
self.satiated = True
return ' '.join(['Burp!'] * volume)
# Update function with dialog questions here
def breakfast(self):
questions = [
{
'type': 'list',
'name': 'breakfast',
'message': 'What did you want for breakfast?',
'choices': ['eggs', 'bacon', 'toast']
}
]
# We are going to use associative arrays to switch on the value of the key
# to determine how many burps are required
switcher = {
'eggs': 1,
'bacon': 2,
'toast': 3,
}
volume = switcher.get(prompt(questions)['breakfast'], 0)
self.satiated = True
return ' '.join(['Burp!'] * volume)
def status(self):
return 'Satiated.' if self.satiated else 'Not satiated.'
digestion breakfast
command that we can run with python run digestion breakfast
:$ python cli.py digestion breakfast
? What did you want for breakfast? bacon
Burp! Burp!
$ python cli.py digestion breakfast
? What did you want for breakfast? eggs
Burp!
$ python cli.py digestion breakfast
? What did you want for breakfast? toast
Burp! Burp! Burp!
digestion breakfast
command that will run the breakfast
method and output the result of our input into the dialog.PyInquirer
to demo a simple command line input as well as a way to pick an option from a list.pawel_czerwinski