‹ Kinyanjui Wangonya

Tags / python

Page 2


Getting user input is an important part of any kind of application. Since we’ve already learned about options, adding a user prompt to our hello world app should be a breeze. All we need to do is add prompt=True to the option decorator, so that it prompts the user for input if no option is passed in.

Arguments work very similarly to options. If you’re familiar with functional programming, then you’re familiar with arguments. The concept is the same in Click.

In the previous post, we used setuptools to package our app and enable us to run it using a single executable command hello. In this post, we look at how to make CLI apps more functional and interactive using options.

In our previous post, we made a Hello World! app to get us started with Click. You might have noticed though that we needed to do python hello-world.py to run it. Since we’re building CLI apps, that’s just not cool and it’s not how CLI apps work anyway. We need to be able to run a single command, something like:

(venv) $ hello
Hello World!

That’s where setuptools comes in. Setuptools helps us bundle our script so we can install it and run it like an app ($ hello), instead of like a script ($ python hello-world.py).

Setuptools is a collection of enhancements to the Python distutils that allow developers to more easily build and distribute Python packages, especially ones that have dependencies on other packages. - Docs

In this post, we’ll cover the very basics of working with Python and Click to create a CLI app. We’ll install Python 3, set up our development environment, install Click, and create a hello-world app.

Python 3.x

Getting started with Python is easy. Most *nix systems come with Python pre-installed. We’ll be using Python 3. To check which version of Python you have, open up your terminal and run python -V. You should get an output similar to this:

Python 2.7.10

This means the default Python version installed on my computer is 2.7.10. Most Macs ship with Python 2 by default. If you got a 3.x.x, then you’re good to go. After installing Python 3, running python3 -V outputs:

Python 3.7.3

If you don’t have Python installed, you can head over to python.org, download the appropriate installer for your system, and run it. This step is necessary if you’re on Windows.

The Fibonacci sequence is a sequence of numbers such that any number, except for the first and second, is the sum of the previous two.

In this tutorial, we’ll create a simple todo api with Python 3 and Django 2.

Modules

Modules are single Python files that can be imported. Any python file can be a module. For example, if I have two Python files: module.py and hello.py in the same directory:

# module.py

def hello(name):
    print("Hello {}".format(name))

I can import that module in my hello.py:

#hello.py

import module

module.hello("World!") # Hello World!

You might need to capture print statements in your tests while debugging. This might be just to help you debug, or for some other reason. I mostly do this kind of thing when testing for database connections where, in addition to other tests, I want to make sure I see that “Database connection successful” message.

First time I had someone review my pull requests, she was pretty strict on tests. I couldn’t merge if the tests were failing, of course. But I also couldn’t merge if coverage had decreased. TDD was still new to me so maintaining coverage was a challenge since I was only testing the bare minimum I could. I had to find out how to make my tests more robust and ensure as much of my code was tested as possible. One area that I wasn’t really sure how to test was the custom exceptions I had written.

« Older posts Newer posts »