A library of various functions and classes helping to program more efficiently and more intuitively
Project description
Pratik for Python
Overview
Pratik is a library of various functions and classes helping to program more efficiently and more intuitively.Summary
Table of Contents
How to use
Installation
By PyPI:
python -m pip install pratik
Libraries
Package functions
Menu
>> from pratik.functions import Menu
>> menu = Menu("to be", "not to be", title="Question", description="That is the question", back_button="to death", description_center=True, colored=True)
>> print(menu)
╔══════════╗
╔════╣ Question ╠════╗
║ ╚══════════╝ ║
║ That is the ║
║ question ║
╟────────────────────╢
║ ┌───┐┌───────────┐ ║
║ │ 1 ├┤ to be │ ║
║ └───┘└───────────┘ ║
║ ┌───┐┌───────────┐ ║
║ │ 2 ├┤ not to be │ ║
║ └───┘└───────────┘ ║
╟────────────────────╢
║ ╔═══╗╔═══════════╗ ║
║ ║ 0 ╠╣ to death ║ ║ # This button is in red color
║ ╚═══╝╚═══════════╝ ║
╚════════════════════╝
>> menu.select()
╔══════════╗
╔════╣ Question ╠════╗
║ ╚══════════╝ ║
║ That is the ║
║ question ║
╟────────────────────╢
║ ┌───┐┌───────────┐ ║
║ │ 1 ├┤ to be │ ║
║ └───┘└───────────┘ ║
║ ┌───┐┌───────────┐ ║
║ │ 2 ├┤ not to be │ ║
║ └───┘└───────────┘ ║
╟────────────────────╢
║ ╔═══╗╔═══════════╗ ║
║ ║ 0 ╠╣ to death ║ ║ # This button is in red color
║ ╚═══╝╚═══════════╝ ║
╚════════════════════╝
?> 2
>> menu.select(printed=False)
?> 1
>> print(menu)
╔══════════╗
╔════╣ Question ╠════╗
║ ╚══════════╝ ║
║ That is the ║
║ question ║
╟────────────────────╢
║ ╔═══╗╔═══════════╗ ║
║ ║ 1 ╠╣ to be ║ ║ # This button is in red color
║ ╚═══╝╚═══════════╝ ║
║ ┌───┐┌───────────┐ ║
║ │ 2 ├┤ not to be │ ║
║ └───┘└───────────┘ ║
╟────────────────────╢
║ ┌───┐┌───────────┐ ║
║ │ 0 ├┤ to death │ ║
║ └───┘└───────────┘ ║
╚════════════════════╝
>> menu.selected = 2
>> print(menu)
╔══════════╗
╔════╣ Question ╠════╗
║ ╚══════════╝ ║
║ That is the ║
║ question ║
╟────────────────────╢
║ ┌───┐┌───────────┐ ║
║ │ 1 ├┤ to be │ ║
║ └───┘└───────────┘ ║
║ ╔═══╗╔═══════════╗ ║
║ ║ 2 ╠╣ not to be ║ ║ # This button is in red color
║ ╚═══╝╚═══════════╝ ║
╟────────────────────╢
║ ┌───┐┌───────────┐ ║
║ │ 0 ├┤ to death │ ║
║ └───┘└───────────┘ ║
╚════════════════════╝
>> menu = Menu("to be", "not to be", title="Question", description="That is the question", back_button="to death")
>> print(menu)
╔══════════╗
╔════╣ Question ╠════╗
║ ╚══════════╝ ║
║ That is the ║
║ question ║
╟────────────────────╢
║ ┌───┐┌───────────┐ ║
║ │ 1 ├┤ to be │ ║
║ └───┘└───────────┘ ║
║ ┌───┐┌───────────┐ ║
║ │ 2 ├┤ not to be │ ║
║ └───┘└───────────┘ ║
╟────────────────────╢
║ ╔═══╗╔═══════════╗ ║
║ ║ 0 ╠╣ to death ║ ║ # This button is in red color
║ ╚═══╝╚═══════════╝ ║
╚════════════════════╝
>> menu = Menu("to be", "not to be", title="Question", back_button="to death")
>> print(menu)
╔══════════╗
╔════╣ Question ╠════╗
║ ╚══════════╝ ║
║ ┌───┐┌───────────┐ ║
║ │ 1 ├┤ to be │ ║
║ └───┘└───────────┘ ║
║ ┌───┐┌───────────┐ ║
║ │ 2 ├┤ not to be │ ║
║ └───┘└───────────┘ ║
╟────────────────────╢
║ ╔═══╗╔═══════════╗ ║
║ ║ 0 ╠╣ to death ║ ║ # This button is in red color
║ ╚═══╝╚═══════════╝ ║
╚════════════════════╝
>> menu = Menu("to be", "not to be", title="Question", back_button="to death", colored=False)
>> print(menu)
╔══════════╗
╔════╣ Question ╠════╗
║ ╚══════════╝ ║
║ ┌───┐┌───────────┐ ║
║ │ 1 ├┤ to be │ ║
║ └───┘└───────────┘ ║
║ ┌───┐┌───────────┐ ║
║ │ 2 ├┤ not to be │ ║
║ └───┘└───────────┘ ║
╟────────────────────╢
║ ┌───┐┌───────────┐ ║
║ │ 0 ├┤ to death │ ║ # This button is not in red color
║ └───┘└───────────┘ ║
╚════════════════════╝
>> menu = Menu("to be", "not to be", title="Question")
>> print(menu)
╔══════════╗
╔════╣ Question ╠════╗
║ ╚══════════╝ ║
║ ┌───┐┌───────────┐ ║
║ │ 1 ├┤ to be │ ║
║ └───┘└───────────┘ ║
║ ┌───┐┌───────────┐ ║
║ │ 2 ├┤ not to be │ ║
║ └───┘└───────────┘ ║
╚════════════════════╝
>> menu = Menu("to be", "not to be")
>> print(menu)
╔════════════════════╗
║ ┌───┐┌───────────┐ ║
║ │ 1 ├┤ to be │ ║
║ └───┘└───────────┘ ║
║ ┌───┐┌───────────┐ ║
║ │ 2 ├┤ not to be │ ║
║ └───┘└───────────┘ ║
╚════════════════════╝
>> menu = Menu()
>> print(menu)
The menu is empty.
>> menu = Menu("maybe")
>> print(menu)
╔════════════════════╗
║ ┌───┐┌───────────┐ ║
║ │ 1 ├┤ maybe │ ║
║ └───┘└───────────┘ ║
╚════════════════════╝
>> menu.select()
>> print(menu)
╔════════════════════╗
║ ╔═══╗╔═══════════╗ ║
║ ║ 1 ╠╣ maybe ║ ║
║ ╚═══╝╚═══════════╝ ║
╚════════════════════╝
>> menu = Menu(back_button="to death")
>> print(menu)
>> The menu is empty.
>> print(menu.select())
0
This class simply manages a menu of choice.
enter(__prompt='', __type=int)
>> from pratik.functions import enter
>> enter("Your number here: ") # default -> int
?> Your number here: 42
42
>> enter()
?> 5
5
>> enter("Result: ", list)
?> Result: FRANCE
['F', 'R', 'A', 'N', 'C', 'E']
>> ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'][enter("Where: ", slice)]
?> Where: 2:6:2
['C', 'E']
This function is inspired by the input() function by adding the type of a class in which to return the value.
humanize_number(__number, __fill_char='.')
>> from pratik.functions import humanize_number
>> print(humanize_number(1234567))
1.234.567
>> print(humanize_number(1234567, ' '))
1 234 567
This function helps to display numbers in a way that is easier for a human to read.
gcd(a, b)
>> from pratik.functions import gcd
>> print(gcd(1234567890, 9876543210))
90
>> print(humanize_number(48, 18))
6
This function allows you to retrieve the GCD of two numbers.
progress_bar(x, n, *, width=100)
>> from pratik.functions import progress_bar
>> print(progress_bar(67, 100))
067/100 | ███████████████████████████████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 67%
>> print(progress_bar(13, 50, width=50))
13/50 | █████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 26%
This function allows you to display a loading bar to have a visual of the execution of a task.
clear(*, return_line=False)
>> from pratik.functions import clear
>> clear()
<deletion character>
>> clear(return_line=True)
<deletion character>
>>
This function allows you to clear the current terminal, a parameter allows you to return to the line after the deletion character.
Package singleton
Singleton
from pratik.singleton import Singleton
class Foo(Singleton):
def singleton_init(self, var1, var2):
self.var1 = var1
self.var2 = var2
@property
def var(self):
return self.var1 + self.var2
>> f1 = Foo(2, 5)
>> print(f1.var1)
2
>> f2 = Foo()
>> print(f2.var1)
2
>> f3 = Foo(3, 8)
>> print(f1.var1)
3
>> print(f2.var1)
3
>> print(f3.var1)
3
Singleton is a class allowing the easy creation of a singleton.
To instantiate it like a regular class you can overwrite the singleton_init(self, *args, **kwargs)
method.
Package text
Color
from pratik.text import Color
print(f"{Color.GREEN}Is good!{Color.STOP}")
print(f"{Color.LIGHT_RED}Is Bad!{Color.STOP}")
print(f"{Color.get_rgb(42, 128, 200)}I don't know!{Color.STOP}")
print(f"{Color.get_hex('#ACAB42')}Meh !{Color.STOP}")
To color text
-
get_rgb(red, green, blue)
- Get the ANSI escape sequence for an RGB color.
-
get_hex(hexadecimal)
- Get the ANSI escape sequence for an RGB color.
Highlight
from pratik.text import Highlight
print(f"{Highlight.GREEN}Is good!{Highlight.STOP}")
print(f"{Highlight.LIGHT_RED}Is Bad!{Highlight.STOP}")
print(f"{Highlight.get_rgb(42, 128, 200)}I don't know!{Highlight.STOP}")
print(f"{Highlight.get_hex('#ACAB42')}Meh !{Highlight.STOP}")
To highlight text
-
get_rgb(red, green, blue)
- Get the ANSI escape sequence for an RGB color.
-
get_hex(hexadecimal)
- Get the ANSI escape sequence for an RGB color.
Style
To stylize text (Bold, Italic, ...).
generate(*code)
from pratik.text import generate
print(f"{generate(31, 45)}It's too much!{generate(0)}")
For concatenating too many codes.
information()
All ANSI code in table
STOP
Reset the ANSI sequence with \033[0m character.
Package time
TimeRemaining
import time
from pratik.time import TimeRemaining
if __name__ == '__main__':
how_many_objects = 100
tr = TimeRemaining(how_many_objects)
for i in range(how_many_objects):
time.sleep(0.1)
tr.add()
tr.progress_bar()
>> 017/100 | ████░░░░░░░░░░░░░░░░░░░░░ 17% 0:00:08
>> 053/100 | █████████████░░░░░░░░░░░░ 53% 0:00:04
>> 079/100 | ████████████████████░░░░░ 79% 0:00:02
>> 100/100 | █████████████████████████ 100% 0:00:00
This class is close to progress bar in terms of operation. It adds to the latter an estimate of the remaining time using a simple rule of three ((time spent * total number of elements / number of elements passed) - time spent).
Contributors
Licence
This library is licensed under the GNU GENERAL PUBLIC LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pratik-1.2.2.tar.gz
.
File metadata
- Download URL: pratik-1.2.2.tar.gz
- Upload date:
- Size: 27.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6f52e4c5565b6ecba474eb17c71de214df9248525f193bcf8779a00c46cfa2c |
|
MD5 | 57d74e211572aca5d3d647ffc6f591e8 |
|
BLAKE2b-256 | b307508d94410724abe4079c1c825f7f4c9d0c2e802008accd74cf9e979b7dd0 |
File details
Details for the file pratik-1.2.2-py3-none-any.whl
.
File metadata
- Download URL: pratik-1.2.2-py3-none-any.whl
- Upload date:
- Size: 24.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 983c79f7722e2243e71fe599e63d23cffdb1d8e913f32de7159c6636a1b56a1f |
|
MD5 | 2a7b1aa7af53a1dd0800bec73e15c96b |
|
BLAKE2b-256 | 59be8d8a4079b8918e9616eda8786a681604a3d49ac355d6e88a777ba29443e2 |