-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tools): useful functions that I usually use fro python coding
- Loading branch information
Showing
1 changed file
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
""" | ||
# Tools Module | ||
There are some useful functions that I usually use for python coding. | ||
""" | ||
|
||
import sys | ||
import time | ||
import requests | ||
from time import sleep | ||
from mdsanima_dev.colors import get_complex_color | ||
|
||
|
||
def read_file(file_path: str) -> str: | ||
""" | ||
Function reads the file and splits it into separate lines. | ||
Args: | ||
file_path (str): File path to read and split. | ||
Returns: | ||
[list, str, int]: Function returns a list of lines in the string by | ||
line and the total number of lines. | ||
Usage: | ||
.. code:: | ||
lines, lines_len = read_file(file_path) | ||
""" | ||
with open(file_path, 'r', encoding='utf-8') as r: | ||
lines = r.read().splitlines() | ||
lines_len = len(lines) | ||
|
||
return lines, lines_len | ||
|
||
|
||
def append_file(file_path: str, data: str) -> str: | ||
""" | ||
Function save data into file. | ||
Args: | ||
file_path (str): File path to save data with append mode. | ||
data (str): Data to write into file. | ||
Usage: | ||
.. code:: | ||
append_file(file_path, data) | ||
""" | ||
with open(file_path, 'a', encoding='utf-8') as w: | ||
w.write(str(data) + '\n') | ||
|
||
|
||
def get_response_json(url: str): | ||
""" | ||
This function get response json dictionary from url. | ||
Args: | ||
url (str): Url address to get response json. | ||
Returns: | ||
(json): Dictionary. | ||
""" | ||
response = requests.get(url) | ||
response.raise_for_status() | ||
return response.json() | ||
|
||
|
||
def machine(text: str, speed: int = 0.1) -> str: | ||
""" | ||
This function printing text as a typing on screen at intervals time after | ||
each letter. | ||
Args: | ||
text (string): Text to be entered as an animation. | ||
speed (int, optional): Time after each print letter. Defaults to 0.1. | ||
Returns: | ||
str: Printing text as an animation. | ||
""" | ||
machine = (text+"\n") | ||
for chars in machine: | ||
sys.stdout.write(chars) | ||
sys.stdout.flush() | ||
time.sleep(speed) | ||
|
||
|
||
class progress: | ||
def __init__(self, | ||
txt_first: str = 'get data', txt_end: str = 'done', | ||
txt_first_color: int = 112, txt_end_color: int = 192, | ||
bar_sten_color: int = 203, bar_color: int = 113, | ||
percent_color: int = 243) -> str: | ||
self.mds = get_complex_color | ||
self.txt_first = txt_first | ||
self.txt_end = txt_end | ||
self.txt_first_clr = txt_first_color | ||
self.txt_end_clr = txt_end_color | ||
self.bar_sten_clr = bar_sten_color | ||
self.bar_clr = bar_color | ||
self.percent_clr = percent_color | ||
self.symbol = '\u25a0' | ||
bar_st = '\u258c' | ||
bar_en = '\u2590' | ||
|
||
def progress_conf(self, percent: int, width: int) -> str: | ||
start = width * percent // 100 | ||
end = width - start | ||
|
||
s_start = str(self.symbol * start) | ||
s_end = str(' ' * end) | ||
perc = str(percent) + '%' | ||
|
||
self.mds('\r' + self.txt_first.upper(), self.txt_first_clr, ' ') | ||
self.mds('[', self.bar_sten_clr, '') | ||
self.mds(s_start, self.bar_clr, '') | ||
self.mds(s_end, self.bar_clr, '') | ||
self.mds(']', self.bar_sten_clr, '') | ||
self.mds(' ' + perc, self.percent_clr, '') | ||
|
||
def progress_bar(self, width: int, speed: int) -> str: | ||
for i in range(101): | ||
self.progress_conf(i, width) | ||
sleep(speed) | ||
self.mds(' ' + self.txt_end.upper(), self.txt_end_clr) | ||
|
||
|
||
#progress().progress_bar(100, 0.01) | ||
#progress().progress_bar(100, 0.01) |