# MySmallUtils
Small Python utils to do life easier.
This includes tools to execute external commands, compress files,
manage configuration files, open different types of files (JSON, YAML and Pickle) compressed or not,
configure logging, obtain metrics, download files, etc.
This module is divided into the following categories:
* [Collections](#collections)
* [Head of a set or dict](#head-of-a-set-or-dict)
* [List union](#list-union)
* [Text](#text)
* [Remove urls](#remove-urls)
* [Clean text](#clean-text)
* [File access, load and save files](#file-access-load-and-save-files)
* [Open files](#open-files)
* [Read the first line of a file](#read-the-first-line-of-a-file)
* [Load and save json files](#load-and-save-json-files)
* [Load and save pickle files](#load-and-save-pickle-files)
* [Load and save Yaml files](#load-and-save-yaml-files)
* [Copy files](#copy-files)
* [Remove files](#remove-files)
* [Check if exists several files](#check-if-exists-several-files)
* [Count lines](#count-lines)
* [Touch](#touch)
* [Cat](#cat)
* [Read file](#read-file)
* [Compressing files](#compressing-files)
* [Gzip](#gzip)
* [Tar](#tar)
* [External commands](#external-commands)
* [Configuration files](#configuration-files)
* [Logging](#logging)
* [Method synchronization](#method-synchronization)
* Obtaining metrics
* [Services and Web](#services-and-web)
* [Download a file](#download-a-file)
* [Flask services](#flask-services)
## Collections<a id="collections"></a>
Some util functions for list, set or dict collections.
### Head of a set or dict<a id="head-of-a-set-or-dict"></a>
Get the first n elements of a dictionary or a set.
```python
from mysutils.collections import head
# A set of latin characters
set1 = {chr(97 + i) for i in range(26)}
# Select the first 5 elements of the set
head(set1, 5) # returns {'d', 'a', 'b', 'e', 'c'}
# By default select 10 elements
head(set1) # returns {'f', 'd', 'j', 'a', 'b', 'e', 'h', 'i', 'c', 'g'}
# A dictionary of latin characters
dict1 = {i: chr(97 + i) for i in range(26)}
# Select the first 5 items of the dictionary
head(dict1, 5) # Returns {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'}
# By default select 10 items
head(dict1) # Returns {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h', 8: 'i', 9: 'j'}
```
Also, you can use the specific functions for set and dictionaries: **sh()** for set head and **dh()** for dictionaries.
```python
from mysutils.collections import sh
# A set of latin characters
set1 = {chr(97 + i) for i in range(26)}
# Select the first 5 elements of the set
sh(set1, 5)
# By default select 10 elements
sh(set1)
```
```python
from mysutils.collections import dh
# A dictionary of latin characters
dict1 = {i: chr(97 + i) for i in range(26)}
# Select the first 5 items of the dictionary
dh(dict1, 5)
# By default select 10 items
dh(dict1)
```
### List union<a id="list-union"></a>
Create the union of two or more lists maintaining the order of elements.
```python
from mysutils.collections import list_union
l1 = [1, 2, 3]
l2 = [4, 5, 6, 1]
l3 = [2, 6, 24]
# This will return [1, 2, 3, 4, 5, 6, 24]
list_union(l1, l2, l3)
# This will return [1, 2, 3, 6, 24, 4, 5]
list_union(l1, l3, l2)
```
## Text<a id="text"></a>
Simple functions related to text.
### Remove urls<a id="remove-urls"></a>
Remove urls from a text.
```python
from mysutils.text import remove_urls
text = 'This is a test!\n Clean punctuation symbols and urls like this: '
'https://example.com/my_space/user?a=b&c=3#first '
'https://example.com/my_space/user#first'
remove_urls(text)
# Result:
# 'This is a test!\n Clean punctuation symbols and urls like this: '
```
### Clean text<a id="clean-text"></a>
Remove punctuation symbols, urls and convert to lower.
```python
from mysutils.text import clean_text
text = 'This is a test!\n Clean punctuation symbols and urls like this: ' \
'https://example.com/my_space/user?a=b&c=3#first ' \
'https://example.com/my_space/user#first'
# Remove punctuation, urls and convert to lower
clean_text(text)
# Remove punctuation and urls but do not convert to lower
clean_text(text, lower=False)
# Only remove punctuation
clean_text(text, lower=False, url=False)
```
## File access, load and save files<a id="file-access-load-and-save-files"></a>
With these functions you can open files, create json and pickle files, and execute external commands very easily.
Moreover, only changing the file extension you can store the information in a compressed file with gzip.
### Open files<a id="open-files"></a>
```python
from mysutils.file import open_file, force_open
# Open a text file to read
with open_file('file.txt') as file:
pass
# Open a compressed text file to write
with open_file('file.txt.gz', 'w') as file:
pass
# Open a file in a directory, if the directory does not exist,
# then create the parent directories.
with force_open('file.txt') as file:
pass
# The same as previously, but with a compressed file.
with force_open('file.txt.gz', 'w') as file:
pass
```
### Read the first line of a file<a id="read-the-first-line-of-a-file"></a>
This function only reads the first line of a text file (gzip compressed or not) and returns it without the \n if
it exists.
```python
from mysutils.file import first_line
# Read the first line of the file token.txt ignoring the character \n at the end of the line.
token = first_line('token.txt')
```
### Load and save json files<a id="load-and-save-json-files"></a>
```python
from mysutils.file import load_json, save_json
d = {
'version': 1.0,
'file_list': ['1.txt', '2.txt']
}
# Save the json in a text file
save_json(d, 'file.json')
# Load the json file from a text file
d = load_json('file.json')
# Save the json in a compressed file
save_json(d, 'file.json.gz')
# Load the json file from a compressed file
d = load_json('file.json.gz')
# Save the json into a text file in a given directory,
# if the directory does not exist, then create it
save_json(d, 'data/file.json', force=True)
# The same but wit a compressed file
save_json(d, 'data/file.json.gz', force=True)
# Load from a tar file
from mysutils.tar import load_tar_json
# Load a json (data.json) from a compressed tar file (file.tar.bz2)
d = load_tar_json('data/file.tar.bz2', 'data.json')
```
### Load and save pickle files<a id="load-and-save-pickle-files"></a>
```python
from mysutils.file import load_pickle, save_pickle
d = {
'version': 1.0,
'file_list': ['1.txt', '2.txt']
}
# Save a object in a pickle file
save_pickle(d, 'test1.pkl')
# Load the object from a pickle file
d = load_pickle('test1.pkl')
# Save the object into a compressed pickle file
save_pickle(d, 'test1.pkl.gz')
# Load the object from a compressed pickle file
d = load_pickle('test1.pkl.gz')
# Save the object into a pickle file in a given directory,
# if the directory does not exist, then create it
save_pickle(d, 'data/test1.pkl', force=True)
# The same but wit a compressed pickle file
save_pickle(d, 'data/test1.pkl.gz', force=True)
# Load from a tar file
from mysutils.tar import load_tar_pickle
# Load a compressed pickle (data.pkl.gz) from a compressed tar file (file.tar.bz2)
d = load_tar_pickle('data/file.tar.bz2', 'data.pkl.gz')
```
### Load and save Yaml files<a id="load-and-save-yaml-files"></a>
These functions require to install the PyYaml module with the following command:
```bash
pip install PyYAML~=5.4.1
```
Examples of usage:
```python
from mysutils.yaml import load_yaml, save_yaml
d = {
'version': 1.0,
'file_list': ['1.txt', '2.txt']
}
# Save a object in a yaml file
save_yaml(d, 'file.yml')
# Load the object from a yaml file
d = load_yaml('file.yml')
# Save the object into a compressed yaml file
save_yaml(d, 'file.yml.gz')
# Load the object from a compressed yaml file
d = load_yaml('file.yml.gz')
# Save the object into a yaml file in a given directory,
# if the directory does not exist, then create i
评论0