Helpers¶
Problematic¶
How can we avoid writing the same thing every time?
Documentation¶
The tool to check the availability or syntax of domains, IPv4, IPv6 or URL.
██████╗ ██╗ ██╗███████╗██╗ ██╗███╗ ██╗ ██████╗███████╗██████╗ ██╗ ███████╗
██╔══██╗╚██╗ ██╔╝██╔════╝██║ ██║████╗ ██║██╔════╝██╔════╝██╔══██╗██║ ██╔════╝
██████╔╝ ╚████╔╝ █████╗ ██║ ██║██╔██╗ ██║██║ █████╗ ██████╔╝██║ █████╗
██╔═══╝ ╚██╔╝ ██╔══╝ ██║ ██║██║╚██╗██║██║ ██╔══╝ ██╔══██╗██║ ██╔══╝
██║ ██║ ██║ ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝
Provides the helpers.
- Author:
- Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom
- Special thanks:
- https://pyfunceble.github.io/special-thanks.html
- Contributors:
- https://pyfunceble.github.io/contributors.html
- Project link:
- https://github.com/funilrys/PyFunceble
- Project documentation:
- https://pyfunceble.readthedocs.io///en/master/
- Project homepage:
- https://pyfunceble.github.io/
License:
MIT License
Copyright (c) 2017, 2018, 2019, 2020 Nissar Chababy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Command()
¶
-
class
PyFunceble.helpers.
Command
(command, encoding='utf-8')[source]¶ Shell command execution.
Parameters: -
_decode_output
(to_decode)[source]¶ Decode the output of a shell command in order to be readable.
Parameters: to_decode (bytes) – Output of a command to decode. Returns: The decoded output. Return type: str
-
run
(rstrip=True)[source]¶ Run the given command and yield each line(s) one by one.
Note
The difference between this method and
execute()
is thatexecute()
wait for the process to end in order to return its output while this method return each line one by one - as they are outputed.Parameters: rstrip (bool) – Deactivates the rstrip of the output.
-
Dict()
¶
-
class
PyFunceble.helpers.
Dict
(main=None)[source]¶ Simplify some
dict
manipulation.Parameters: main (dict) – The main dict
to work with.Raises: TypeError – When main
is not a dict nor a list (tolarated).-
classmethod
from_json
(json_str, return_dict_on_error=True)[source]¶ Converts a given JSON string to dict/list.
Parameters: return_dict_on_error (bool) – Return a dict instead of a NoneType. Return type: dict|list
-
classmethod
from_json_file
(file_path, encoding='utf-8', return_dict_on_error=True)[source]¶ Reads the given file path and convert it’s content to dict/list (tolarated).
Parameters: Return type: dict|list
-
classmethod
from_yaml
(yaml_str)[source]¶ Converts the given YAML string to dict/list.
Parameters: yaml_str (str) – The YAML string to convert. Return type: dict|list
-
classmethod
from_yaml_file
(file_path, encoding='utf-8')[source]¶ Converts a given YAML formatted file, into dict/list.
Parameters: Return type: dict|list
-
has_same_keys_as
(to_check, loop=False)[source]¶ A dirty solution which checks keys are presents in both given
dict
.Parameters: Return type:
-
remove_key
(key_to_remove)[source]¶ Remove a given key from a given dictionary.
Parameters: key_to_remove (list|str) – The key(s) to delete. Returns: The dict without the given key(s). Return type: dict|None
-
rename_key
(key_to_rename, strict=True)[source]¶ Rename the given keys from the given dictionary.
Parameters: Returns: The well formatted dict.
Return type: dict|None
-
to_json
(ensure_ascii=False, indent=4, sort_keys=True)[source]¶ Converts a given dict to JSON and return the json string.
Parameters: Return type:
-
to_json_file
(file_path, ensure_ascii=False, indent=4, sort_keys=True, encoding='utf-8')[source]¶ Converts the given
dict
to JSON and save the result into a given file path.Parameters:
-
to_yaml
(encoding='utf-8', default_flow_style=False, indent=4, allow_unicode=True, sort_keys=True)[source]¶ Converts the given dict/list to the YAML format and return the result.
Parameters: Return type: dict|list
-
classmethod
Directory()
¶
-
class
PyFunceble.helpers.
Directory
(dir_path=None)[source]¶ Simplify the directories manipulation.
:param str dir_path the path to work with.
-
create
(dir_path=None)[source]¶ Creates the given directory path.
Returns: The output of self.exists
after the directory creation.Return type: bool
-
delete
(dir_path=None)[source]¶ Deletes the given directory path.
Returns: not self.exists
after the directory deletion.Rtypt: bool
-
Download()
¶
-
class
PyFunceble.helpers.
Download
(url, verify_certificate=True)[source]¶ Simplification of the downloads.
Parameters: -
text
(destination=None)[source]¶ Download the body of the given url.
Note
if
destination
is set toNone
, we only return the output.Otherwise, we save the output into the given destination, but we also return the output.
Parameters: destination (str) – The download destination.
Return type: str:
Raises: - Exception – When the status code is not 200.
- NoInternetConnection – When no connection could be made.
-
File()
¶
-
class
PyFunceble.helpers.
File
(file_path=None)[source]¶ Simplify the file manipulations.
Parameters: file_path (str) – The file path to work with. -
copy
(destination)[source]¶ Copy the globaly given file path to the given destination.
Parameters: destination (str) – The destination of the copy.
-
delete
(file_path=None)[source]¶ Deletes the given file path if it exists.
Parameters: file_path (str) – The file path to check.
Note
If
None
is given, we report to the globally given file path.Returns: The non existance state of the file. Return type: bool
-
exists
(file_path=None)[source]¶ Checks if the given file path exists.
Parameters: file_path (str) – The file path to check.
Note
If
None
is given, we report to the globally given file path.Return type: bool
-
get_size
(file_path=None)[source]¶ Provides the size (in bytes) of the given file path.
Parameters: file_path (str) – The file path to check.
Note
If
None
is given, we report to the globally given file path.Return type: int
-
move
(destination)[source]¶ Move the globally given file path to the given destination.
Parameters: destination (str) – The destination of the file.
-
read
(file_path=None, encoding='utf-8')[source]¶ Read the given file path and return it’s content.
Parameters: Return type:
-
Hash()
¶
-
class
PyFunceble.helpers.
Hash
(algo='sha512_224')[source]¶ Simplify the hashing of data or file content.
Parameters: algo (str) – The algorithm to use for hashing. Raises: ValueError – When the given algo is not known.
List()
¶
-
class
PyFunceble.helpers.
List
(main, remove_empty=False)[source]¶ Simplify the list manipulation.
Parameters: -
custom_format
(key_method, reverse=False)[source]¶ Return a well formatted list. With the key_method as a function/method to format the elements before sorting.
Parameters: - key_method (function|method) – A function or method to use to format the readed element before sorting.
- reverse (bool) – Tell us if we have to reverse the list.
Returns: A sorted list.
Return type:
-
Merge()
¶
-
class
PyFunceble.helpers.
Merge
(main)[source]¶ Simplify the merging of dict and list.
Parameters: main (str, tuple, bool, int, dict, list, float) – The main data to work with. -
_Merge__dict
(origin, strict=True)¶ Process the dict merging.
Parameters: strict (bool) – Activates the strict mode. Return type: dict
-
Regex()
¶
-
class
PyFunceble.helpers.
Regex
(regex, escape=False)[source]¶ Simplify the regex matching and usage.
Parameters: -
get_matching_list
(data)[source]¶ Returns the strings which does the match the regex in the given data.
-
get_not_matching_list
(data)[source]¶ Returns the strings which does not the match the regex in the given data.
-
match
(data, rematch=False, group=0, return_match=True)[source]¶ Checks if the given data match the given regex string.
Parameters:
-