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!
Top comments (7)
I would also advise on using mkdir -p folder/ so that it doesn't raise an error if it already exists!
Don't forget to abuse
!!
to repeat the last command you entered in a bash, and use subshell to avoid cd .. : (cd stuff/foo/bar; python3 do-stuff.py). Using parenthesis create a subshell and everything inside is executed on the shell leaving your shell unaffected.Last thing but not least, which I really love : crontabs. I use them for anything, from checking if my discord bot is up and rebooting, to automatically docker prune images and containers, updating libs by periodically fetching its latest release and installing it.
crontabs sounds fun!
Just wondering about your
act
alias: did you know you could run github actions locally with a cli named act?Maybe in that case change alias to
activate
or whatever suits your need.Thanks for your post 😄
That is a nice question. I generally keep the aliases name different than any other command line tool I use, but if that happens, for e.g, act, for using the cli try
\act
which would run the command without the alias definition. So to use the command with alias asact
and without the alias definition as\act
.Wow, I didn't know that, thank you!
Great post! I have always thought of automating such repetitive stuff but never actually did it. Thanks!
Thank you! go do it! It will make life simple.