Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 21, 2020 04:21 pm GMT

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

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  
Enter fullscreen mode Exit fullscreen mode

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

$ pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

#!/bin/bashREQ=requirements.txt[[ ! -z "$VIRTUAL_ENV" ]] && deactivate && returnif [[ -d "venv" ]]; then    echo "Using 'venv' as virtual environement.."    source venv/bin/activateelse    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    fifi
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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";}
Enter fullscreen mode Exit fullscreen mode

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

$ md some_directory
Enter fullscreen mode Exit fullscreen mode

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 cant 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!


Original Link: https://dev.to/geekypandey/make-the-computer-work-for-you-automate-the-boring-stuff-3pg6

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To