Make the computer work for you! Automate the boring stuff!

Make the computer work for you! Automate the boring stuff!

Originally posted on dev.

Imagine yourself working on a Python project. So,the best practice is to create a virtual environment for the project. Okay, that is easy, you quickly go,

$ python3 -m venv venv
$ source venv/bin/activate  

Now the next step is to install the dependencies for the project,

$ pip install -r requirements.txt

Flash! done!

Now imagine yourself doing these things repeatedly over time! Hundreds of time! With time, doing this over and over again gets boring.

Boring means, that you are doing same thing over and over again which signifies that the thing you are doing is constant and doesn’t change, so you could easily automate that stuff!!

alias create='python3 -m venv venv && . venv/bin/activate'
alias act='source venv/bin/activate'

The above lines create aliases.Put this in your .bashrc file in your home directory and boom you are done! Next time you want to create a virtual environment, it is just six key strokes away, how cool is that!
The above typing reduces down to,

$ create
$ act

You could go further and maybe write a bash script for it, which could look like this:

#!/bin/bash

REQ=requirements.txt

[[ ! -z "$VIRTUAL_ENV" ]] && deactivate && return

if [[ -d "venv" ]]; then
    echo "Using 'venv' as virtual environement.."
    source venv/bin/activate
else
    python -m venv venv
    source venv/bin/activate
    echo "Created new virtual environement venv.."
    if [[ -f "$REQ" ]]; then
        read -p "Do you want to install requirements.xt?(y/n)" CHOICE
        if [[ "$CHOICE" == "y" ]]; then
            echo "Installing dependencies..."
            pip install -r "$REQ"
        fi
    fi
fi

You don’t have to understand the above code completely.

This is not particular to Python project. Whatever language or machine you use, there are some parts that you do repeatedly, identify them, and then automate them!
This helps to remove the barrier for you to actually get started on the real stuff.

As Scott Hanselman said, you only have a limited number of keystrokes to give, use it wisely.

It doesn’t have to be some big stuff you are automating, for example, this rarely happens that we create a directory and don’t open it, we always almost do,

$ mkdir some_directory
$ cd some_directory

Now doing that is really boring!
Simple solution is to write a function and put it in a .bashrc file as:

md() {
    mkdir "$1" && cd "$1";
}

Done! It doesn’t have to be production grade, it just have to work. Now you could do,

$ md some_directory

and it would create and change to the directory for you!

Key takeaways

So the key points to take from this article are,

  • Identify the boring stuff in your day to day work, the stuff that is constant and try to write a script or other things that make it possible to automate it.
  • You only have limited number of keystrokes in your hand so use it wisely.

Where to go from here?

  • Check this article on digital ocean to learn more about aliases and functions in bash.
  • Check this article on Bash aliases you can’t live without to get more idea on what different things you can automate.

Also

Go here to read Scott Hanselman’s blog. He writes on many different topics.You could also watch his amazing talk titled Scaling yourself.

Happy learning!

Source: dev