Philosophers say that people need to be judged not by how high they can climb, but by how low they can fall.
There are many great developers in the world who can produce spectacular algorithms, elegant architectures, and great code. But these same programmers take and write very mediocre organized code for some small console script to calculate analytics or data patch in the database. No breakdown into classes and functions, clumsy passing arguments, primitive output of obscure information using
Today we’ll talk about how to make better programs that most people don’t care about - disposable console utilities and scripts.
The library from the world's second largest corporation of good - Facebook. The local engineers, too, were tired of the chaos in console applications, so they decided to provide them with an interactive interface. Of course, textual.
- Autocomplete for functions available in your console script.
- You can connect the autocomplete at the terminal level (works in bash and zsh).
- A pleasant and beautiful status bar that will show the user what is happening.
- Hints and validations on sets of function arguments and their types.
With the conclusion of information to the console, many generally have trouble. As stated above, it often comes down to the simple output of
print() . Reading this, of course, is not very convenient.
In order to draw a drawing of what is happening in the program, there is a termgraph
It allows you to make such pictures in the terminal
Or such more complicated
Using a simple bar, you can show the progress of the program in the form of a beautiful strip - and this will be cool and much more pleasant to print incomprehensible values.
Scripts to run on remote machines are a separate pain. Of course, we live in the DevOps age, when heaps of tasks are solved using Salt and Ansible. But it also happens that you need to regularly log in to a cluster of remote wheelbarrows and execute batches of commands there. For this, there is fabric
- Support for different host lists - you can execute commands on the database servers, in the application cluster - just write the remote cars in the script.
- Different authorization policies for wheelbarrows - you can use both keys and passwords.
- Breakdown of the script into subcommands, grouping of subcommands into larger teams - you can make an arbitrarily complex logical structure of the code that you want to run.
- And, of course, you can interfere with the terminal command code in the code too!
Fabric is built around Paramiko and in general you can execute commands using this low-level SSH communication library. But Fabric provides the necessary level of abstraction, which allows you to make it clear and easy to use.
This lib turns a simple script into a really powerful console application.
For example, you can add a command prompt line to a text interface.
from prompt_toolkit import prompt while 1: user_input = prompt('>') print(user_input)
And after that, you can add the history of the commands used, just like in your terminal.
from prompt_toolkit import prompt from prompt_toolkit.history import FileHistory while 1: user_input = prompt('>', history=FileHistory('history.txt'), ) print(user_input)
And you can make auto-prompts for lines from the input history.
from prompt_toolkit import prompt from prompt_toolkit.history import FileHistory from prompt_toolkit.auto_suggest import AutoSuggestFromHistory while 1: user_input = prompt('>', history=FileHistory('history.txt'), auto_suggest=AutoSuggestFromHistory(), ) print(user_input)
And teach auto-prompts to certain predefined commands.
from prompt_toolkit import prompt from prompt_toolkit.history import FileHistory from prompt_toolkit.auto_suggest import AutoSuggestFromHistory from prompt_toolkit.contrib.completers import WordCompleter SQLCompleter = WordCompleter(['select', 'from', 'insert', 'update', 'delete', 'drop'], ignore_case=True) while 1: user_input = prompt('SQL>', history=FileHistory('history.txt'), auto_suggest=AutoSuggestFromHistory(), completer=SQLCompleter, ) print(user_input)
Liba is extremely simple and makes it possible to create your own cool and fully customizable interface.
Why is this?
Attentiveness to seemingly one-time programs will allow you to write not code for ejection, but console tools that can easily be reused later. This saves time and, of course, makes working with your scripts much more enjoyable.