Remove prism

This commit is contained in:
Digital Studium
2023-04-23 17:56:27 +03:00
parent 0682ac8961
commit d90ff60a80
23 changed files with 181 additions and 35 deletions

View File

@@ -0,0 +1,142 @@
---
title: "Python: How to easily write a CLI tool for Linux using Fire"
category: python-lifehacks
filename: how-to-easily-write-linux-cli-tool
date: 2023-04-09
---
I want to share the easiest way I know to write a CLI tool for Linux administration
using python and Fire.
## Step 1: Install Fire
```bash
pip install fire
```
## Step 2. Create a simple CLI tool
Here is an example of a CLI tool that prints the Linux version to the terminal:
<!--more-->
```python
#!/usr/bin/env python3
import fire
import platform
class SysInfo:
"""A CLI tool for getting system information about Linux server"""
def kernel(self):
"""A method for getting kernel version"""
version = platform.release()
return f"Kernel version: {version}"
if __name__ == "__main__":
obj = SysInfo()
fire.Fire(obj)
```
Paste this code into a file called `my-cli-tool` and give it permission to execute:
```bash
chmod +x my-cli-tool
```
Then put this file in the path `/usr/local/bin`:
```bash
sudo cp ./my-cli-tool /usr/local/bin
```
To use this tool, just type the command:
```bash
my-cli-tool kernel
```
You will see output like this:
```plaintext
my-cli-tool kernel
Kernel version: 6.2.2-060202-generic
```
As you can see, it is enough to create a class, a method(s) in it, and pass the class object to the fire.Fire() function - and the cli tool is ready!
This will automatically generate a help page, which can be called using the `--help` flag:
```bash
my-cli-tool --help
```
You will get this output:
```plaintext
NAME
my-cli-tool - A CLI tool for getting system information about Linux server
SYNOPSIS
my-cli-tool COMMAND
DESCRIPTION
A CLI tool for getting system information about Linux server
COMMANDS
COMMAND is one of the following:
kernel
A method for getting kernel version
```
## Step 3. Making the tool more complex
For example, we also want our tool to be able to print the kernel version in short form, like this: `6.2.2`.
We rewrite the code as follows:
```python
#!/usr/bin/env python3
import fire
import platform
class SysInfo:
"""A CLI tool for getting system information about Linux server"""
def kernel(self, format: ("short", "full") = "full"):
"""A method for getting kernel version"""
version = platform.release()
if format == "short":
return version.split("-")[0]
return f"Kernel version: {version}"
if __name__ == "__main__":
obj = SysInfo()
fire.Fire(obj)
```
Now we can type the following command:
```bash
my-cli-tool kernel --format short
```
Output:
```plaintext
6.2.2
```
This will also automatically update the help page, adding the `--format` flag and its possible values:
```bash
my-cli-tool kernel --help
```
Output:
```plaintext
NAME
my-cli-tool kernel - A method for getting kernel version
SYNOPSIS
my-cli-tool kernel <flags>
DESCRIPTION
A method for getting kernel version
FLAGS
-f, --format=FORMAT
Type: ('short', 'full')
Default: 'full'
```
## Step 4. Create a binary file
First install `pyinstaller`:
```bash
pip install pytinstaller
```
Then we run the command:
```
pyinstaller my-cli-tool --onefile
```
A folder `dist` shoud appear, and a binary file `my-cli-tool` inside it with all dependencies, which can be used even on servers,
which do not have python or fire installed. Just put this file in the path `/usr/local/bin` and `my-cli-tool` can be used!

View File

@@ -0,0 +1,71 @@
---
title: "Python: How to load multiple web pages in parallel"
category: python-lifehacks
filename: how-to-load-multiple-web-pages-in-parallel-using-python
date: 2022-05-15
---
First you need to install an aiohttp package. To install aiohttp run the command:
```bash
pip install aiohttp[speedups]
```<!--more-->
The `[speedups]` suffix is needed to install aiohttp accelerating packages - aiodns and cchardet. Then create a main.py file with this code:
```python
import aiohttp
import asyncio
import socket
async def fetch_urls(urls):
resolver = aiohttp.AsyncResolver()
connector = aiohttp.TCPConnector(resolver=resolver, family=socket.AF_INET, use_dns_cache=False)
session = aiohttp.ClientSession(connector=connector)
async def fetch_url(url, session):
async with session.get(url) as resp:
print(resp.status)
print(await resp.text())
tasks = [fetch_url(url, session) for url in urls]
await asyncio.gather(*tasks)
await session.close()
loop = asyncio.get_event_loop()
urls = ['http://httpbin.org/get?key=value1', 'http://httpbin.org/get?key=value2', 'http://httpbin.org/get?key=value3']
loop.run_until_complete(fetch_urls(urls))
```
Now you can run main.py file with the command:
```bash
python3 main.py
```
You will see this output:
```plaintext
200
{
"args": {
"key": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
...
```
All three queries will be executed in parallel. You can add any urls to the `urls` list, for example:
```python
urls = ['https://yandex.com', 'https://google.com', 'https://yahoo.com']
```
In order to make HEAD, POST, PUT, DELETE requests, just replace `session.get(url)` in your code with the appropriate method:
```python
session.post('http://httpbin.org/post', data=b'data')
session.put('http://httpbin.org/put', data=b'data')
session.delete('http://httpbin.org/delete')
session.head('http://httpbin.org/get')
session.options('http://httpbin.org/get')
session.patch('http://httpbin.org/patch', data=b'data')
```