[Fabric] I was addicted to using boolean as an argument, so make a note of the countermeasures.

background

For example

from fabric.api import run

def test(flag=False):
    if flag is True:
        run("echo hoge")
    else:
        run("echo fuga")

Or write

$ fab -H host test:flag=True

When I try to do something like that fuga is output.

The argument passed in Fabric is str.

>>> "True" is True
False

It needs to be converted in some way.

counter-measure

Part 1 cast

Simply cast.

from fabric.api import run

def test(flag=True):
    if bool(flag) is True:
        run("echo hoge")
    else:
        run("echo fuga")
$ fab -H host test:flag=False
fuga

However,

$ fab -H host test:flag=false

I will die. " True " / " False " can be cast to boolean, but"true"/"false"cannot be cast

Part 2 strtobool

There is such a guy in the standard library.

from distutils.util import strtobool
from fabric.api import run

def __strtobool(arg):
    return bool(strtobool(arg))

def test(flag="True"):
    flag = __strtobool(flag)
    if flag is True:
        run("echo hoge")
    else:
        run("echo fuga")

Write like this. With this, you can use "y", "t", on "and other strings instead of"True".

However, if you write def test (flag = True): or the default argument as boolean type, an error will be thrown out.

Prepare an eclectic plan for that time. By the way, I also try to take an exception in case of a string that cannot be converted to strtobool ().

def __strtobool(arg):
    try:
        if type(arg) == type(True):
            return arg
        else:
            return bool(strtobool(arg))
    except ValueError:
        ...

It's getting more and more troublesome.

Recommended Posts

[Fabric] I was addicted to using boolean as an argument, so make a note of the countermeasures.
I was addicted to trying Cython with PyCharm, so make a note
I set up TensowFlow and was addicted to it, so make a note
The story I was addicted to when I specified nil as a function argument in Go
Note that I was addicted to accessing the DB with Python's mysql.connector using a web application.
I stumbled upon using MoviePy, so make a note
I wanted to use the find module of Ansible2, but it took some time, so make a note
Don't take an instance of a Python exception class directly as an argument to the exception class!
I tried to compare the accuracy of machine learning models using kaggle as a theme.
As for the process type written in honcho's Procfile, I was addicted to it for over an hour because I couldn't use-, so I'll write it as a show.
I tried to make a regular expression of "amount" using Python
[Python] I tried to implement stable sorting, so make a note
I tried to make a regular expression of "time" using Python
A note I was addicted to when making a beep on Linux
I tried to make a regular expression of "date" using Python
A note I was addicted to when creating a table with SQLAlchemy
When I tried to scrape using requests in python, I was addicted to SSLError, so a workaround memo
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
I failed to install django with pip, so a reminder of the solution
I made an appdo command to execute a command in the context of the app
Save an array of numpy to a wav file using the wave module
AtCoder AGC 041 C --I was addicted to the full search of Domino Quality
Suddenly I needed to work on a project using Python and Pyramid, so a note of how I'm studying
Work memorandum (pymongo) Part 3. I don't want to look it up again, so make a note of it (aggregate)
A note I was addicted to when running Python with Visual Studio Code
I made a script to record the active window using win32gui of Python
I tried to make a ○ ✕ game using TensorFlow
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
Note that I was addicted to npm script not passing in the verification environment
I want to collect a lot of images, so I tried using "google image download"
Note: The meaning of specifying only * (asterisk) as an argument in the Python function definition.
I want to take a screenshot of the site on Docker using any font
Make a note of the list of basic Pandas usage
A story that I was addicted to at np.where
What I was addicted to when using Python tornado
Python Note: The mystery of assigning a variable to a variable
Make a note of what you want to do in the future with Raspberry Pi
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
I made a function to crop the image of python openCV, so please use it.
I was in charge of maintaining the Fabric script, but I don't know.> <To those who
I tried to automate the construction of a hands-on environment using IBM Cloud's SoftLayer API
How to make an interactive LINE BOT 004 (answer the closing date of a listed company)
I made a function to check the model of DCGAN
I tried to make a stopwatch using tkinter in python
I tried to make a simple text editor using PyQt
[Fixed] I was addicted to alphanumeric judgment of Python strings
When I was shown a plot such as principal component analysis, "the distributions of these two data are not so different"?
I was a little addicted to installing Python3.3 + mod_wsgi3.4 on Sakura VPS (CentOS), so a retrospective memo
I tried to make a site that makes it easy to see the update information of Azure
[Python] I tried to make a simple program that works on the command line using argparse.
[Twitter] I want to make the downloaded past tweets (of my account) into a beautiful CSV
I want to clear up the question of the "__init__" method and the "self" argument of a Python class.
A story about a person who uses Python addicted to the judgment of an empty JavaScript dictionary
A story that I was addicted to calling Lambda from AWS Lambda.
Understand the function of convolution using image processing as an example
The record I was addicted to when putting MeCab on Heroku
I tried to get the index of the list using the enumerate function
Format when passing a long string as an argument of python
Precautions when using a list or dictionary as the default argument
[Introduction to StyleGAN] I played with "The Life of a Man" ♬