mirror of
https://github.com/bytedream/dreamutils.git
synced 2025-05-09 20:15:07 +02:00
Initial commit
This commit is contained in:
commit
51d22ceb77
61
LICENCE
Normal file
61
LICENCE
Normal file
@ -0,0 +1,61 @@
|
||||
Copyright © 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
|
||||
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
||||
|
||||
This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
|
||||
0. Additional Definitions.
|
||||
|
||||
As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
|
||||
|
||||
“The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
|
||||
|
||||
An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
|
||||
|
||||
A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
|
||||
|
||||
The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
|
||||
|
||||
The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
|
||||
1. Exception to Section 3 of the GNU GPL.
|
||||
|
||||
You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
|
||||
2. Conveying Modified Versions.
|
||||
|
||||
If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
|
||||
|
||||
a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
|
||||
b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
|
||||
|
||||
3. Object Code Incorporating Material from Library Header Files.
|
||||
|
||||
The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
|
||||
|
||||
a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
|
||||
b) Accompany the object code with a copy of the GNU GPL and this license document.
|
||||
|
||||
4. Combined Works.
|
||||
|
||||
You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
|
||||
|
||||
a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
|
||||
b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
|
||||
c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
|
||||
d) Do one of the following:
|
||||
0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
|
||||
1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
|
||||
e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
|
||||
|
||||
5. Combined Libraries.
|
||||
|
||||
You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
|
||||
b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
|
||||
|
||||
6. Revised Versions of the GNU Lesser General Public License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
|
||||
|
||||
If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
|
93
README.md
Normal file
93
README.md
Normal file
@ -0,0 +1,93 @@
|
||||
# dreamutils
|
||||
|
||||
<p align="center"><h5>A collection of useful and often repeated python methods</h5></p>
|
||||
|
||||
---
|
||||
|
||||
The package contains code / method that were written over and over again,
|
||||
without creating a centralized package for them. It project was created to solve this problem.
|
||||
Surely, some of the methods are exotic(most of the `dreamutils.python` file i think),
|
||||
but if you need those methods you know where to find them.
|
||||
|
||||
|
||||
<p align="center">
|
||||
<a href="#Installation">Installation</a>
|
||||
•
|
||||
<a href="#Examples">Examples</a>
|
||||
•
|
||||
<a href="#Licence">Licence</a>
|
||||
</p>
|
||||
|
||||
|
||||
## Installation
|
||||
- Use `Python >= 3.6`
|
||||
|
||||
- Install it
|
||||
- via `pip`
|
||||
```bash
|
||||
pip install dreamutils
|
||||
```
|
||||
- or with `git`
|
||||
```bash
|
||||
git clone https://github.com/ByteDream/dreamutils.git
|
||||
cd dreamutils
|
||||
python setup.py install
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
Here are examples of some useful packages
|
||||
|
||||
#### XML
|
||||
|
||||
A easy to use and powerful xml manipulation class
|
||||
```python
|
||||
import dreamutils.types.xml as xml
|
||||
|
||||
my_xml = xml.new_xml()
|
||||
sub_elem_id = my_xml.add(0, 'sub_elem', 'example_text')
|
||||
# every new created element has an id
|
||||
|
||||
my_xml.get_element(sub_elem_id).attrib = {'attrib': 'example'}
|
||||
# with the id you can obtain the element later...
|
||||
my_xml.add(sub_elem_id, 'sub_sub_elem')
|
||||
# ... and use it to add new sub element
|
||||
|
||||
# Note: the root element has always the id `0`
|
||||
|
||||
print(my_xml.get_string())
|
||||
```
|
||||
|
||||
#### Sorting
|
||||
|
||||
A collection of sorting algorithms (the most common I think)
|
||||
|
||||
```python
|
||||
from dreamutils.sort import QuickSort
|
||||
|
||||
sorted = QuickSort.integer([2, 9, 4, 623, 5])
|
||||
print(sorted)
|
||||
```
|
||||
|
||||
#### Net
|
||||
|
||||
A file with nice internet methods
|
||||
|
||||
```python
|
||||
from dreamutils.net import get_ip_infos
|
||||
|
||||
infos = get_ip_infos('8.8.8.8')
|
||||
# if no argument is passed, information about your own ip will be returned
|
||||
print(infos)
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
**The tools are (currently) only tested on linux, but they should also work on Windows and MacOS.**
|
||||
|
||||
So if there are any problems feel free to open a new [issue](https://github.com/ByteDream/dreamutils/issues/new).
|
||||
|
||||
|
||||
## Licence
|
||||
|
||||
This project is licensed under the GNU Lesser General Public License v3.0 (LGPL-3.0) - see the [LICENSE](LICENCE) file for more details.
|
0
dreamutils/__init__.py
Normal file
0
dreamutils/__init__.py
Normal file
8
dreamutils/_internal.py
Normal file
8
dreamutils/_internal.py
Normal file
@ -0,0 +1,8 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
|
||||
class _NotSupported:
|
||||
|
||||
@staticmethod
|
||||
def os() -> str:
|
||||
return 'Your OS is not supported'
|
6
dreamutils/encoding.py
Normal file
6
dreamutils/encoding.py
Normal file
@ -0,0 +1,6 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
ASCII = 'ascii'
|
||||
|
||||
UTF_8 = 'utf-8'
|
||||
UTF_16 = 'utf-16'
|
98
dreamutils/file.py
Normal file
98
dreamutils/file.py
Normal file
@ -0,0 +1,98 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import os as _os
|
||||
from typing import Generator as _Generator, List as _List, Union as _Union
|
||||
|
||||
"""This file contains utils for file manipulation"""
|
||||
|
||||
|
||||
def recursive_directory_data(directory: str, full_path=True) -> _Generator[str]:
|
||||
"""
|
||||
_Lists every subfile and subdirectory of the given directory
|
||||
|
||||
Args:
|
||||
directory (str): Path of directory from which you want to get the subfiles /- directories
|
||||
full_path (bool, optional): If True the full path of the files gets returned. If False the relative path
|
||||
|
||||
Yields:
|
||||
str: The next recursive file or directory in the given directory
|
||||
|
||||
Examples:
|
||||
>>> print(recursive_directory_data('/home/ByteDream/NOTHENTAI'))
|
||||
['/home/ByteDream/NOTHENTAI/download_1.mp4', '/home/ByteDream/NOTHENTAI/best/', '/home/ByteDream/NOTHENTAI/best/best_1.mp4']
|
||||
|
||||
"""
|
||||
if directory.endswith(_os.sep):
|
||||
directory = directory[:-1]
|
||||
|
||||
for path, subdirs, files in _os.walk(directory):
|
||||
if full_path:
|
||||
yield _os.path.join(path, _os.sep.join(subdirs))
|
||||
else:
|
||||
if path.endswith(_os.sep):
|
||||
path = path[:path.rfind(_os.sep)]
|
||||
yield path[path.rfind(_os.sep) + 1:]
|
||||
for name in files:
|
||||
if full_path:
|
||||
yield _os.path.join(path, name)
|
||||
else:
|
||||
yield _os.path.join(path.replace(directory, ''), name)
|
||||
|
||||
|
||||
def replace_line(file: str, to_replace: _Union[int, str, _List[int], _List[str]], new_content: str, ignore_case=False) -> None:
|
||||
"""
|
||||
Replaces lines given by their number or content with new content
|
||||
|
||||
Args:
|
||||
file: File in which the lines are to be replaced
|
||||
to_replace: Content like line numbers or line content which should be replaced
|
||||
new_content: New content to replace the old one
|
||||
ignore_case: If True and `to_replace` is a string or a _List of strings, the comparison is not case sensitive
|
||||
|
||||
Examples:
|
||||
test.txt before:
|
||||
```
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
```
|
||||
|
||||
>>> replace_line('test.txt', 'line 3', 'replaced line 3')
|
||||
|
||||
test.txt after:
|
||||
```
|
||||
line 1
|
||||
line 2
|
||||
replaced line 3
|
||||
line 4
|
||||
```
|
||||
|
||||
"""
|
||||
lines = open(file, 'r').readlines()
|
||||
|
||||
if isinstance(to_replace, str):
|
||||
to_replace = [to_replace]
|
||||
|
||||
if ignore_case:
|
||||
for index, item in enumerate(to_replace):
|
||||
to_replace[index] = item.lower()
|
||||
|
||||
if isinstance(to_replace, int):
|
||||
lines[to_replace] = new_content
|
||||
elif to_replace:
|
||||
if isinstance(to_replace[0], int):
|
||||
for index, _ in enumerate(lines.copy()):
|
||||
if index in to_replace:
|
||||
lines[index] = new_content
|
||||
to_replace.remove(index)
|
||||
else:
|
||||
for index, item in enumerate(lines.copy()):
|
||||
if ignore_case:
|
||||
item = item.lower()
|
||||
if item in to_replace:
|
||||
lines[index] = new_content
|
||||
|
||||
with open(file, 'w') as file:
|
||||
file.writelines(lines)
|
||||
file.cl_ose()
|
64
dreamutils/net.py
Normal file
64
dreamutils/net.py
Normal file
@ -0,0 +1,64 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import socket as _socket
|
||||
|
||||
from json import load as _load
|
||||
from urllib.request import urlopen as _urlopen
|
||||
|
||||
"""This file contains utils for networking stuff"""
|
||||
|
||||
|
||||
def online(timeout=5) -> bool:
|
||||
"""
|
||||
Tests if your machine is connected to the internet
|
||||
|
||||
Args:
|
||||
timeout (optional): Timeout until False is returned if no connection can be established
|
||||
|
||||
Returns:
|
||||
If the pc is online (theoretically it could return False when google.com is down, but if this happens your connect status is the smallest problem)
|
||||
|
||||
Examples:
|
||||
>>> print(online(2))
|
||||
True
|
||||
|
||||
"""
|
||||
try:
|
||||
host = _socket.gethostbyname('google.com')
|
||||
|
||||
connection = _socket.create_connection((host, 80), timeout)
|
||||
connection.close()
|
||||
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
|
||||
def get_ip_infos(ip_address: str = None) -> dict:
|
||||
"""
|
||||
A dict with infos about the ip address of your computer (unless you use a vpn) or a specified ip
|
||||
|
||||
Args:
|
||||
ip_address (optional): IP from which you want to receive the information
|
||||
|
||||
Returns:
|
||||
A dict filled with the ip address information
|
||||
|
||||
Examples:
|
||||
>>> print(get_ip_infos()())
|
||||
{'ip': '69.69.69.69',
|
||||
'hostname': 'examplehostname',
|
||||
'city': 'Suginami City',
|
||||
'region': 'Tokyo',
|
||||
'country': 'JP,
|
||||
'loc': '35.6986, 139.6367',
|
||||
'org': 'exampleprovider',
|
||||
'postal': '166-0015',
|
||||
'timezone': 'Asia/Tokyo',
|
||||
'readme': 'https://ipinfo.io/missingauth'}
|
||||
|
||||
"""
|
||||
if ip_address:
|
||||
return _load(_urlopen('http://ipinfo.io/' + ip_address + '/json'))
|
||||
else:
|
||||
return _load(_urlopen('http://ipinfo.io/json'))
|
59
dreamutils/os.py
Normal file
59
dreamutils/os.py
Normal file
@ -0,0 +1,59 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import ctypes as _ctypes
|
||||
import os as _os
|
||||
from enum import Enum as _Enum
|
||||
from sys import platform as _platform
|
||||
|
||||
"""This file contains utils for os manipulation"""
|
||||
|
||||
|
||||
class Platform(_Enum):
|
||||
LINUX = 'linux'
|
||||
MAC = 'mac'
|
||||
WINDOWS = 'windows'
|
||||
OTHER = 'other'
|
||||
|
||||
|
||||
def platform() -> Platform:
|
||||
"""
|
||||
Returns the current platform / os
|
||||
|
||||
Returns:
|
||||
Platform: The working platform / os
|
||||
|
||||
Examples:
|
||||
>>> print(platform())
|
||||
Platform.LINUX
|
||||
|
||||
"""
|
||||
if _platform in ['linux', 'linux2']:
|
||||
return Platform.LINUX
|
||||
elif _platform == 'darwin':
|
||||
return Platform.MAC
|
||||
elif _platform in ['win32', 'cygwin', 'msys']:
|
||||
return Platform.WINDOWS
|
||||
else:
|
||||
return Platform.OTHER
|
||||
|
||||
|
||||
def is_root() -> bool:
|
||||
"""
|
||||
Checks if the python script is running with root / admin rights
|
||||
|
||||
Notes:
|
||||
If the user who started the script is the root user on the system (e.g. pi on raspberry pi os)
|
||||
the script can execute root command even if it was not started with root rights
|
||||
|
||||
Returns:
|
||||
If the script was started with root rights
|
||||
|
||||
Examples:
|
||||
>>> print(is_root())
|
||||
False
|
||||
|
||||
"""
|
||||
if platform().LINUX or platform().MAC:
|
||||
return _os.geteuid() == 0
|
||||
elif platform().WINDOWS:
|
||||
return _ctypes.windll.shell32.IsUserAdmin() != 0
|
141
dreamutils/python.py
Normal file
141
dreamutils/python.py
Normal file
@ -0,0 +1,141 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import ast as _ast
|
||||
from types import FunctionType as _FunctionType, ModuleType as _ModuleType, MethodType as _MethodType
|
||||
from typing import Dict as _Dict, List as _List, Union as _Union
|
||||
|
||||
"""This file contains utils to analyze and manipulate raw python files"""
|
||||
|
||||
|
||||
def class_of_method(method: _MethodType) -> _Union[None, type]:
|
||||
"""
|
||||
Returns the class in which the given `method` was defined
|
||||
|
||||
Args:
|
||||
method (method): Method from which you want to find out from which class it was declared
|
||||
|
||||
Returns:
|
||||
The class where the method was defined or None
|
||||
|
||||
"""
|
||||
method_name = method.__name__
|
||||
if method.__self__:
|
||||
classes = [method.__self__.__class__]
|
||||
else:
|
||||
classes = [method.im_class]
|
||||
while classes:
|
||||
c = classes.pop()
|
||||
if method_name in c.__dict__:
|
||||
return c
|
||||
else:
|
||||
classes = list(c.__bases__) + classes
|
||||
return None
|
||||
|
||||
|
||||
def defined_functions_and_classes(file_or_module: _Union[str, _ModuleType], include_external_modules=False) -> _Dict[str, _Union[_FunctionType, type, _ModuleType]]:
|
||||
"""
|
||||
Returns all functions and classes from a given python file or python module
|
||||
|
||||
Args:
|
||||
file_or_module (str or ModuleType): The file or module from which you want to get the functions and classes.
|
||||
include_external_modules (bool): If True the modules, functions and classes which were imported are getting returned too.
|
||||
|
||||
Returns:
|
||||
dict: A dict of str - function, class or module pairs with all functions, classes and modules -> (if `include_external_modules` is True).
|
||||
|
||||
Notes:
|
||||
If `file_or_module` is a file the returned dict values will all be None
|
||||
|
||||
"""
|
||||
functions_and_classes = {}
|
||||
if isinstance(file_or_module, str):
|
||||
with open(file_or_module, "r") as file:
|
||||
parsed = _ast.parse(file.read(), filename=file_or_module)
|
||||
for x in parsed.body:
|
||||
if isinstance(x, (_ast.FunctionDef, _ast.ClassDef, _ModuleType)):
|
||||
functions_and_classes[x.name] = None
|
||||
file.close()
|
||||
elif isinstance(file_or_module, _ModuleType):
|
||||
for x in dir(file_or_module):
|
||||
functions_and_classes[x] = getattr(file_or_module, x)
|
||||
|
||||
if not include_external_modules:
|
||||
for name, module_type in functions_and_classes.copy().items():
|
||||
try:
|
||||
if isinstance(module_type, _ModuleType):
|
||||
del functions_and_classes[name]
|
||||
except:
|
||||
pass
|
||||
|
||||
return functions_and_classes
|
||||
|
||||
|
||||
def extract_decorated_func(function: _FunctionType) -> _FunctionType:
|
||||
"""
|
||||
Extracts the 'core' function of a function which has decorators.
|
||||
When a function with decorators is called, the decorators are called first and then the function.
|
||||
This is good to see when `print(function_with_decorator)` is called, then the decorator object gets printed out instead of the function object.
|
||||
|
||||
Note:
|
||||
This method only works properly if every decorator calls the function normally (function_name()).
|
||||
|
||||
Args:
|
||||
function (FunctionType): Function which should be extracted.
|
||||
|
||||
Returns:
|
||||
FunctionType: The extracted function.
|
||||
|
||||
"""
|
||||
function_types = []
|
||||
|
||||
try:
|
||||
for x in function.__closure__:
|
||||
function_types.append(x.cell_contents)
|
||||
except TypeError:
|
||||
return function
|
||||
|
||||
while len(function_types) > 0:
|
||||
for func in function_types.copy():
|
||||
if isinstance(func, _FunctionType):
|
||||
try:
|
||||
for x in func.__closure__:
|
||||
function_types.append(x.cell_contents)
|
||||
except TypeError:
|
||||
return func
|
||||
|
||||
|
||||
def get_decorators(function: _FunctionType) -> _List[_FunctionType]:
|
||||
"""
|
||||
Returns all decorators of a function
|
||||
|
||||
Note:
|
||||
This method only works properly if every decorator calls the function normally (function_name()).
|
||||
|
||||
Args:
|
||||
function (FunctionType): Function from which the decorators should be extracted from.
|
||||
|
||||
Returns:
|
||||
list: A list of the function decorators
|
||||
|
||||
"""
|
||||
decorators = []
|
||||
function_types = []
|
||||
try:
|
||||
for x in function.__closure__:
|
||||
function_types.append(x.cell_contents)
|
||||
decorators.append(function)
|
||||
except TypeError:
|
||||
return []
|
||||
|
||||
while len(function_types) > 0:
|
||||
for func in function_types:
|
||||
if isinstance(func, _FunctionType):
|
||||
decorators.append(func)
|
||||
try:
|
||||
for z in func.__closure__:
|
||||
function_types.append(z.cell_contents)
|
||||
except TypeError:
|
||||
pass
|
||||
function_types.remove(func)
|
||||
|
||||
return decorators[:-1]
|
162
dreamutils/sort.py
Normal file
162
dreamutils/sort.py
Normal file
@ -0,0 +1,162 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from typing import List as _List
|
||||
|
||||
"""
|
||||
This file provides different sorting algorithms
|
||||
|
||||
Todo:
|
||||
Add `ignore_case` argument to `Sort.string(...)` or `string_ignore_case(...)` method
|
||||
|
||||
Thanks:
|
||||
https://www.tutorialspoint.com/python_data_structure/python_sorting_algorithms.htm
|
||||
https://www.educative.io/edpresso/how-to-implement-quicksort-in-python
|
||||
"""
|
||||
|
||||
|
||||
def _copy_or_not(to_sort: list, copy=True) -> list:
|
||||
if copy:
|
||||
return to_sort.copy()
|
||||
else:
|
||||
return to_sort
|
||||
|
||||
|
||||
class Sort:
|
||||
|
||||
@classmethod
|
||||
def integer(cls, to_sort: _List[int], new_list=True) -> _List[int]:
|
||||
"""
|
||||
Sorts a list of integers
|
||||
|
||||
Args:
|
||||
to_sort: The list of integers to sort
|
||||
new_list: If True the given list is copied and returned. If False the given list will be updated
|
||||
|
||||
Returns:
|
||||
The sorted list of integerss
|
||||
|
||||
"""
|
||||
return cls.object(to_sort, new_list)
|
||||
|
||||
@staticmethod
|
||||
def object(to_sort: list, new_list=True) -> list:
|
||||
"""
|
||||
Sorts a list, regardless of its type
|
||||
|
||||
Args:
|
||||
to_sort: The list sort
|
||||
new_list: If True the given list is copied and returned. If False the given list will be updated
|
||||
|
||||
Returns:
|
||||
The sorted list
|
||||
|
||||
"""
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def string(cls, to_sort: _List[str], new_list=True) -> _List[str]:
|
||||
"""
|
||||
Sorts a list of strings
|
||||
|
||||
Args:
|
||||
to_sort: The list of strings to sort
|
||||
new_list: If True the given list is copied and returned. If False the given list will be updated
|
||||
|
||||
Returns:
|
||||
The sorted list of strings
|
||||
|
||||
"""
|
||||
return cls.object(to_sort, new_list)
|
||||
|
||||
|
||||
class BubbleSort(Sort):
|
||||
"""Bubble sort is a comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order"""
|
||||
|
||||
@staticmethod
|
||||
def object(to_sort: list, new_list=True) -> list:
|
||||
sorted_list = _copy_or_not(to_sort, new_list)
|
||||
for num in range(len(sorted_list) - 1, 0, -1):
|
||||
for i in range(num):
|
||||
if sorted_list[i] > sorted_list[i + 1]:
|
||||
temp = sorted_list[i]
|
||||
sorted_list[i] = sorted_list[i + 1]
|
||||
sorted_list[i + 1] = temp
|
||||
|
||||
return sorted_list
|
||||
|
||||
|
||||
class InsertionSort(Sort):
|
||||
"""
|
||||
Insertion sort involves finding the right place for a given element in a sorted list.
|
||||
So in beginning we compare the first two elements and sort them by comparing them.
|
||||
Then we pick the third element and find its proper position among the previous two sorted elements.
|
||||
This way we gradually go on adding more elements to the already sorted list by putting them in their proper position
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def object(to_sort: list, new_list=True) -> list:
|
||||
sorted_list = _copy_or_not(to_sort, new_list)
|
||||
for i in range(1, len(sorted_list)):
|
||||
j = i - 1
|
||||
key = sorted_list[i]
|
||||
|
||||
while j >= 0 and key < sorted_list[j]:
|
||||
sorted_list[j + 1] = sorted_list[j]
|
||||
j -= 1
|
||||
sorted_list[j + 1] = key
|
||||
|
||||
return sorted_list
|
||||
|
||||
|
||||
class QuickSort(Sort):
|
||||
"""QuickSort is an in-place sorting algorithm with worst-case time complexity of n^2"""
|
||||
|
||||
@staticmethod
|
||||
def object(to_sort: list, new_list=True) -> list:
|
||||
sorted_list = _copy_or_not(to_sort, new_list)
|
||||
elements = len(sorted_list)
|
||||
|
||||
if elements < 2:
|
||||
return sorted_list
|
||||
|
||||
pos = 0
|
||||
|
||||
for i in range(1, elements):
|
||||
if sorted_list[i] <= sorted_list[0]:
|
||||
pos += 1
|
||||
temp = sorted_list[i]
|
||||
sorted_list[i] = sorted_list[pos]
|
||||
sorted_list[pos] = temp
|
||||
|
||||
temp = sorted_list[0]
|
||||
sorted_list[0] = sorted_list[pos]
|
||||
sorted_list[pos] = temp
|
||||
|
||||
left = QuickSort.object(sorted_list[0:pos], False)
|
||||
right = QuickSort.object(sorted_list[pos + 1:elements], False)
|
||||
|
||||
sorted_list = left + [sorted_list[pos]] + right
|
||||
|
||||
return sorted_list
|
||||
|
||||
|
||||
class SelectionSort(Sort):
|
||||
"""
|
||||
In selection sort we start by finding the minimum value in a given list and move it to a sorted list.
|
||||
Then we repeat the process for each of the remaining elements in the unsorted list.
|
||||
The next element entering the sorted list is compared with the existing elements and placed at its correct position.
|
||||
So at the end all the elements from the unsorted list are sorted
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def object(to_sort: list, new_list=True) -> list:
|
||||
sorted_list = _copy_or_not(to_sort, new_list)
|
||||
for index in range(len(sorted_list)):
|
||||
min_index = index
|
||||
for j in range(index + 1, len(sorted_list)):
|
||||
if sorted_list[min_index] > sorted_list[j]:
|
||||
min_index = j
|
||||
|
||||
sorted_list[index], sorted_list[min_index] = sorted_list[min_index], sorted_list[index]
|
||||
|
||||
return sorted_list
|
0
dreamutils/types/__init__.py
Normal file
0
dreamutils/types/__init__.py
Normal file
44
dreamutils/types/dict.py
Normal file
44
dreamutils/types/dict.py
Normal file
@ -0,0 +1,44 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
|
||||
def index(dictionary: dict, index: int):
|
||||
"""
|
||||
Index a dictionary
|
||||
|
||||
Args:
|
||||
dictionary: Dictionary to index
|
||||
index: Position of value to index
|
||||
|
||||
Returns:
|
||||
The indexed value
|
||||
|
||||
Raises:
|
||||
IndexError: If the dictionary index is out of range
|
||||
|
||||
"""
|
||||
|
||||
try:
|
||||
return list(dictionary)[index]
|
||||
except IndexError:
|
||||
raise IndexError('dict index out of range') # just replacing 'list' with 'dict' in the error message
|
||||
|
||||
|
||||
def index_by_value(dictionary: dict, value):
|
||||
"""
|
||||
Index a dictionary by a value of it
|
||||
|
||||
Args:
|
||||
dictionary: Dictionary to index
|
||||
value: Value to index
|
||||
|
||||
Returns:
|
||||
The indexed key
|
||||
|
||||
Raises:
|
||||
IndexError: If the given value is not in the dictionary
|
||||
|
||||
"""
|
||||
try:
|
||||
return list(dictionary.keys())[list(dictionary.values()).index(value)]
|
||||
except ValueError:
|
||||
raise IndexError('dict value index out of range')
|
66
dreamutils/types/string.py
Normal file
66
dreamutils/types/string.py
Normal file
@ -0,0 +1,66 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import re as _re
|
||||
from typing import Union as _Union
|
||||
|
||||
"""This file contains utils for string manipulation"""
|
||||
|
||||
|
||||
def remove_brackets(string: str) -> str:
|
||||
"""
|
||||
Removes brackets and the content between it from a string
|
||||
|
||||
Args:
|
||||
string: string from which the brackets should be removed
|
||||
|
||||
Returns:
|
||||
`string` without brackets and the content between it
|
||||
|
||||
Examples:
|
||||
>>> print(remove_brackets('This is [not] an example string (yuy)'))
|
||||
This is an example string
|
||||
|
||||
"""
|
||||
finished_string = ''
|
||||
square_brackets = 0
|
||||
parentheses = 0
|
||||
for brackets in string:
|
||||
if brackets == '[':
|
||||
square_brackets += 1
|
||||
elif brackets == '(':
|
||||
parentheses += 1
|
||||
elif brackets == ']' and square_brackets > 0:
|
||||
square_brackets -= 1
|
||||
elif brackets == ')' and parentheses > 0:
|
||||
parentheses -= 1
|
||||
elif square_brackets == 0 and parentheses == 0:
|
||||
finished_string += brackets
|
||||
|
||||
return finished_string
|
||||
|
||||
|
||||
def remove_space(string: str, space: _Union[str, int] = ' ', replace_with_single_space=True) -> str:
|
||||
"""
|
||||
Removes all white space from `string` which is equal or higher than from the argument `space` given space
|
||||
|
||||
Args:
|
||||
string: string from which the space should be removed
|
||||
space: Minimum size of the space to start the removal of white space in `string`
|
||||
replace_with_single_space: If True the space to remove is replaced with a single space instead of nothing
|
||||
|
||||
Returns:
|
||||
`string` without the given space and higher
|
||||
|
||||
Examples:
|
||||
>>> print(remove_space("This string has way to much space"))
|
||||
This string has way to much space
|
||||
|
||||
"""
|
||||
if isinstance(space, int):
|
||||
space = ' ' * space
|
||||
|
||||
space_to_replace = ''
|
||||
if replace_with_single_space:
|
||||
space_to_replace = ' '
|
||||
|
||||
return _re.sub(space + '+', space_to_replace, string)
|
254
dreamutils/types/xml.py
Normal file
254
dreamutils/types/xml.py
Normal file
@ -0,0 +1,254 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import xml.etree.ElementTree as _ET
|
||||
from os.path import isfile as _isfile
|
||||
from random import randint as _randint
|
||||
from typing import Dict as _Dict, List as _List, Union as _Union
|
||||
from xml.dom import minidom as _minidom
|
||||
|
||||
from .dict import index_by_value as _index_by_value
|
||||
from ..encoding import UTF_8 as _UTF_8
|
||||
|
||||
|
||||
def prettify(xml: _Union[_ET.Element, str], space: _Union[str, int] = ' ') -> str:
|
||||
"""
|
||||
Prettifies a xml string or element
|
||||
|
||||
Args:
|
||||
xml: XML string or `ElementTree.Element` to prettify
|
||||
space: The space before every new sub element
|
||||
|
||||
Returns:
|
||||
The prettified string
|
||||
|
||||
Examples:
|
||||
>>> print(prettify('<root><sub_elem></sub_elem></root>'))
|
||||
<root>
|
||||
<sub_elem/>
|
||||
</root>
|
||||
|
||||
"""
|
||||
if isinstance(space, int):
|
||||
space = ' ' * space
|
||||
|
||||
if not isinstance(xml, str):
|
||||
reparsed = _minidom.parseString(_ET.tostring(xml, _UTF_8))
|
||||
else:
|
||||
reparsed = _minidom.parseString(bytes(xml, _UTF_8, errors='ignore'))
|
||||
|
||||
pre_output = reparsed.toprettyxml(indent=space)
|
||||
return "\n".join(pre_output.split('\n')[1:])
|
||||
|
||||
|
||||
class XMLManipulator:
|
||||
"""Class to build a new xml element"""
|
||||
|
||||
def __init__(self, fname_or_element: _Union[str, _ET.Element]):
|
||||
"""
|
||||
Args:
|
||||
fname_or_element: File name of a xml file or a python xml `Element`
|
||||
"""
|
||||
if isinstance(fname_or_element, str):
|
||||
if _isfile(fname_or_element):
|
||||
self.root = _ET.parse(fname_or_element).getroot()
|
||||
else:
|
||||
raise FileNotFoundError('The given file could not be found')
|
||||
else:
|
||||
self.root = fname_or_element
|
||||
|
||||
self.root_id = 0
|
||||
self.elements = {0: self.root}
|
||||
|
||||
for elem in self.root.iter():
|
||||
self.elements[_randint(111111111, 999999999)] = elem
|
||||
|
||||
def add(self, parent_id: int, tag: str, text: str = '', **attrib) -> int:
|
||||
"""
|
||||
Adds an element to a parent
|
||||
|
||||
Args:
|
||||
parent_id: ID of the parent element
|
||||
tag: Tag / name of the new element
|
||||
text: Text of the new element
|
||||
**attrib: Attributes of the new element
|
||||
|
||||
Returns:
|
||||
The id of the new created element
|
||||
|
||||
"""
|
||||
if parent_id == 0:
|
||||
element = _ET.Element(tag, **attrib)
|
||||
element.text = text
|
||||
self.root.append(element)
|
||||
elif parent_id in self.elements.keys():
|
||||
element = _ET.SubElement(self.elements[parent_id], tag, **attrib)
|
||||
element.text = text
|
||||
else:
|
||||
raise IndexError('The parent element does not exist')
|
||||
|
||||
while True: # do while would be nice
|
||||
elem_id = _randint(111111111, 999999999)
|
||||
if elem_id not in self.elements:
|
||||
break
|
||||
self.elements[elem_id] = element
|
||||
|
||||
return elem_id
|
||||
|
||||
def remove(self, id: int) -> None:
|
||||
"""
|
||||
Removes a xml element
|
||||
|
||||
Args:
|
||||
id: ID of the element
|
||||
|
||||
"""
|
||||
self.root.remove(self.elements[id])
|
||||
del self.elements[id]
|
||||
|
||||
def update(self, id: int, new_tag: str = None, new_text: str = None, **new_attrib) -> None:
|
||||
"""
|
||||
Updates a xml element
|
||||
|
||||
Args:
|
||||
id: ID of the element
|
||||
new_tag: New tag of the element
|
||||
new_text: New text of the element
|
||||
new_attrib: New attributes of the element
|
||||
|
||||
"""
|
||||
elem = self.elements[id]
|
||||
if new_tag:
|
||||
elem.tag = new_tag
|
||||
if new_text:
|
||||
elem.text = new_text
|
||||
if new_attrib:
|
||||
elem.attrib = new_attrib
|
||||
|
||||
def get_element(self, id: int) -> _ET.Element:
|
||||
"""
|
||||
Returns the ids element
|
||||
|
||||
Args:
|
||||
id: ID of the element
|
||||
|
||||
Returns:
|
||||
The element
|
||||
|
||||
"""
|
||||
return self.elements[id]
|
||||
|
||||
def get_id(self, tag: str, attrib: _Dict = None, parent_tag: str = None, parent_attrib: _Dict = {}) -> int:
|
||||
"""
|
||||
Searches the element id by given attributes
|
||||
|
||||
Args:
|
||||
tag: Tag of the element
|
||||
attrib: Attributes of the element
|
||||
parent_tag: Tag of the elements parent. May be useful if more than one element with the same name exists
|
||||
parent_attrib: Attributes of the elements parent. May be useful if more than one element with the same name exists
|
||||
|
||||
Returns:
|
||||
The id of the element
|
||||
|
||||
"""
|
||||
if not parent_tag and not parent_attrib:
|
||||
for num, elem in self.elements.items():
|
||||
if tag == elem.tag:
|
||||
if attrib:
|
||||
if attrib == elem.attrib:
|
||||
return num
|
||||
else:
|
||||
return num
|
||||
else:
|
||||
for elem in self.get_infos(tag, attrib, parent_tag, False):
|
||||
if elem['parent'].attrib == parent_attrib:
|
||||
try:
|
||||
return _index_by_value(self.elements, elem['element'])
|
||||
except IndexError:
|
||||
break
|
||||
|
||||
raise ValueError('The element \'' + tag + '\' could not be found')
|
||||
|
||||
def get_infos(self, tag: str, attrib: _Dict = None, parent_tag: str = None, stop_at_first=True) -> _Union[_Dict[str, _Union[_ET.Element, _List[_ET.Element], _List, None]],
|
||||
_List[_Dict[str, _Union[_ET.Element, _List[_ET.Element], _List, None]]]]:
|
||||
"""
|
||||
Returns infos about a specific xml element
|
||||
|
||||
Args:
|
||||
tag: Tag of the element from which you want to obtain the infos
|
||||
attrib: Attributes of the element. May be useful if more than one element with the same name exists
|
||||
parent_tag: Tag of the elements parent. May be useful if more than one element with the same name exists
|
||||
stop_at_first: If True only the first found element gets returned
|
||||
|
||||
Returns:
|
||||
The xml element infos
|
||||
|
||||
"""
|
||||
elements = []
|
||||
|
||||
if self.root.tag == tag and not parent_tag:
|
||||
if attrib and self.root.attrib == attrib:
|
||||
elements.append({'element': self.root, 'parent': None})
|
||||
else:
|
||||
elements.append({'element': self.root, 'parent': None})
|
||||
|
||||
for element in self.root.iter():
|
||||
if elements and stop_at_first:
|
||||
return elements[0]
|
||||
|
||||
if element.tag == tag:
|
||||
if parent_tag and parent_tag is not self.root.tag:
|
||||
continue
|
||||
|
||||
if attrib and element.attrib == attrib:
|
||||
elements.append({'element': element, 'parent': self.root})
|
||||
else:
|
||||
elements.append({'element': element, 'parent': self.root})
|
||||
|
||||
for sub_element in element:
|
||||
if elements and stop_at_first:
|
||||
return elements[0]
|
||||
|
||||
if sub_element.tag == tag:
|
||||
if parent_tag and parent_tag is not element.tag:
|
||||
continue
|
||||
|
||||
if attrib and sub_element.attrib == attrib:
|
||||
elements.append({'element': sub_element, 'parent': element})
|
||||
else:
|
||||
elements.append({'element': sub_element, 'parent': element})
|
||||
|
||||
return elements
|
||||
|
||||
def get_string(self, pretty_print=True) -> str:
|
||||
"""
|
||||
Returns the xml as a string
|
||||
|
||||
Notes:
|
||||
If you want to get the XML element, use `XMLBuilder.root`
|
||||
|
||||
Args:
|
||||
pretty_print: If True the string to be return will be prettified
|
||||
|
||||
Returns:
|
||||
The built xml as string
|
||||
|
||||
"""
|
||||
if pretty_print:
|
||||
return prettify(self.root)
|
||||
else:
|
||||
return _ET.tostring(self.root, _UTF_8)
|
||||
|
||||
|
||||
def new_xml(root_element='root') -> XMLManipulator:
|
||||
"""
|
||||
Creates a new xml element
|
||||
|
||||
Args:
|
||||
root_element: Name of the xml root element
|
||||
|
||||
Returns:
|
||||
The xml manipulator to edit the new xml
|
||||
|
||||
"""
|
||||
return XMLManipulator(_ET.Element(root_element))
|
34
setup.py
Normal file
34
setup.py
Normal file
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from setuptools import find_packages, setup
|
||||
|
||||
# with open('requirements.txt', 'r') as requirements:
|
||||
# required = requirements.read().splitlines()
|
||||
|
||||
with open('README.md', 'r') as readme:
|
||||
long_description = readme.read()
|
||||
|
||||
setup(
|
||||
name='dreamutils',
|
||||
version='0.1.0',
|
||||
packages=find_packages(),
|
||||
url='',
|
||||
license='LGPL-3.0',
|
||||
author='ByteDream',
|
||||
author_email='',
|
||||
description='A collection of useful and often repeated python methods',
|
||||
long_description=long_description,
|
||||
long_description_content_type="text/markdown",
|
||||
classifiers=[
|
||||
'License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)',
|
||||
'Operating System :: OS Independent',
|
||||
'Development Status :: 4 - Beta',
|
||||
'Topic :: Software Development :: Libraries :: Python Modules',
|
||||
'Programming Language :: Python :: 3.6',
|
||||
'Programming Language :: Python :: 3.7',
|
||||
'Programming Language :: Python :: 3.8',
|
||||
'Programming Language :: Python :: 3.9',
|
||||
],
|
||||
python_requires='>=3.6.*',
|
||||
# install_requires=required
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user