GithubHelp home page GithubHelp logo

vhelpers's Introduction

Often used functions in vladimirs-git projects.

Python >=3.8

Install the package from pypi.org release

pip install vhelpers

or install the package from github.com release

pip install https://github.com/vladimirs-git/vhelpers/archive/refs/tags/0.1.18.tar.gz

or install the package from github.com repository

pip install git+https://github.com/vladimirs-git/vhelpers

For easy navigation, functions are grouped by some key concept, mostly based on their return data type. For more details, please refer to the ./examples directory where you will find numerous examples.

Calculate the elapsed days, hours, minutes and seconds between two datetime objects.

Parameter Type Description
start datetime The starting datetime object.
end datetime The ending datetime object. If None, the current datetime is used.
Return
DInt A dictionary containing the elapsed hours, minutes, and seconds. If end is None, update data in object.
from datetime import datetime
from vhelpers import vdate

start = datetime.strptime("2001-01-02 2:3:4", "%Y-%m-%d %H:%M:%S")
end = datetime.strptime("2002-02-03 3:4:5", "%Y-%m-%d %H:%M:%S")
print(vdate.delta(start, end))  # {'hours': 9529, 'minutes': 1, 'seconds': 1}

Calculate the elapsed time in the format %H:%M:%S.

Parameter Type Description
args   The arguments for calculating the time delta.
kwargs   The keyword arguments for calculating the time delta.
Return
str The elapsed time in the format %H:%M:%S.
from datetime import datetime
from vhelpers import vdate

start = datetime.strptime("2001-01-02 2:3:4", "%Y-%m-%d %H:%M:%S")
end = datetime.strptime("2002-02-03 3:4:5", "%Y-%m-%d %H:%M:%S")
print(vdate.delta_s(start, end))  # 9529:01:01

Helpers for dictionary processing.

Pop the specified item from the data by key. If key is absent in data, do nothing and return None.

Parameter Type Description
key str The key to be popped from the data.
data dict The data from which the key is to be popped.
Return
str The popped item if key is present in data, otherwise None.
from vhelpers import vdict

# Pop the specified item from the data by key.
data = {1: "a", 2: "b"}
assert vdict.pop(key=1, data=data) == "a"
assert data == {2: "b"}
# If key is absent in data, do nothing and return None.
assert vdict.pop(key=3, data=data) is None
assert data == {2: "b"}

Convert pyproject.toml to a dictionary.

Parameter Type Description
root Union[Path, str] The root directory or path to the pyproject.toml file.
Return
Dict[str, Any] A dictionary containing the data from pyproject.toml.
from vhelpers import vdict
from pathlib import Path

root = Path(__file__).parent.parent
data = vdict.pyproject_d(root)
assert data["tool"]["poetry"]["name"] == "vhelpers"

Helpers for int processing.

Convert string digit to integer.

Parameter Type Description
digit Union[int, str] Digit, string ot integer.
Return
int Integer or 0 if value is not digit.
from vhelpers import vint

assert vint.to_int(digit="1") == 1
assert vint.to_int(digit="a") == 0

Helpers for ip addresses processing.

Convert IPv4 address with mask to address with prefix length.

Parameter Type Description
address str IP addresses with mask.
Return
str IP addresses with prefix length.
from vhelpers import vip

assert vip.ip_prefixlen(address="10.0.0.1 255.255.255.0") == "10.0.0.1/24"

Convert IPv4 addresses with mask to addresses with prefix length.

Parameter Type Description
addresses List[str] A list of IP addresses with mask.
Return
List[str] A list of IP addresses with prefix length.
from vhelpers import vip

assert vip.ips_prefixlen(addresses=["10.0.0.1 255.255.255.0"]) == ["10.0.0.1/24"]

Helpers for list processing.

Find duplicates of the items.

Parameter Type Description
items list A list of items where need to find duplicates.
Return
list A list of items with duplicates.
from vhelpers import vlist

assert vlist.dupl([1, 2, 1]) == [1]
assert vlist.dupl([{1}, {2}, {1}]) == [{1}]

Convert a multidimensional list to a flattened list.

Parameter Type Description
items Sequence The list to be flattened.
ignore_types Tuple[Type] Types to be ignored during flattening, defaults to (str, bytes)
Return
Generator A generator that yields the flattened list.
from vhelpers import vlist

assert vlist.flatten([1, [2, [3]], 4, [5, [6]]]) == [1, 2, 3, 4, 5, 6]

Check if any item in items1 is present in items2.

Parameter Type Description
items1 list A list of items.
items2 list A list of items.
Return
bool True if any item in items1 is present in items2, False otherwise.

Remove duplicates from a list of items.

Parameter Type Description
items list A list of items.
Return
list A list of items without duplicates.
from vhelpers import vlist

# Remove duplicates from a list of items.
assert vlist.no_dupl(items=[1, 2, 1]) == [1, 2]

Replace one item with another.

Parameter Type Description
items list The list of items where need replace item.
old Any The item to be replaced.
new Any The item to replace with.
Return
None Update items.
from vhelpers import vlist

assert vlist.replace(items=[1, 2, 3], old=2, new=4) == [1, 4, 3]

Split string by punctuation chars.

Parameter Type Description
text str Text to split by punctuation.
chars str Extra punctuation chars.
ignore str Ignore punctuation chars.
Return
List[str] Values without punctuation.
from vhelpers import vlist

assert vlist.split(text="1; 2_3-4X5,6", chars="_X", ignore=",") == ["1", "2", "3", "4", "5,6"]

Convert the input items from any into a list. If items is a list, set or tuple, simply change its type to list. Otherwise, create a list with the value as its first item. If items is None return an empty list.

Parameter Type Description
items list The items to be converted into a list.
Return
list The converted list.
from vhelpers import vlist

# Convert the input items into a list.
#  If items is a list, set or tuple, simply change its type to list
assert vlist.to_list(items=(1, 2)) == [1, 2]
# Otherwise, create a list with the value as its first item.
assert vlist.to_list(items=1) == [1]
# If items is None return an empty list.
assert vlist.to_list(items=None) == []

Convert a flat list into a multidimensional list with a fixed number of inner lists.

Parameter Type Description
items list The flat list to convert.
count int The number of inner lists.
Return
List[List[Any] A multidimensional list.
from vhelpers import vlist

# Convert a flat list into a multidimensional list with a fixed number of inner lists.
assert vlist.to_lists(items=[1, 2, 3, 4, 5], count=2) == [[1, 2, 3], [4, 5]]
assert vlist.to_lists(items=(1, 2, 3, 4, 5), count=3) == [[1, 2], [3, 4], [5]]

Convert the input items from any into a list of string. If items is a list, set or tuple, simply change its type to list. If items is None or empty string return an empty list.

Parameter Type Description
items Any The items to be converted into a list of string.
Return
list The converted list.
from vhelpers import vlist

# Convert the input items from any into a list of string.
assert vlist.to_lstr(items=[1, "2"]) == ["1", "2"]
assert vlist.to_lstr(1) == ["1"]
assert vlist.to_lstr("") == []

Convert a flat list into a multidimensional list. Convert a list with the specified number of items in each inner list.

Parameter Type Description
items list The flat list to convert.
count int The number of items to include in each inner list.
Return
LLAny A multidimensional list with the specified number of items in each inner list.
from vhelpers import vlist

assert vlist.to_multi(items=[1, 2, 3, 4, 5], count=2) == [[1, 2], [3, 4], [5]]

Helpers for parameters processing. Parameters are typically included in the query string of a URL, which is the part of a URL that comes after the question mark "?" character.

Convert a dictionary to a list of parameters.

Parameter Type Description
params_d dict A dictionary with keys and values.
Return
list[tuple[str, Any]] A list of parameters. If params_d is empty, returns an empty list.
from vhelpers import vparam

# Convert a dictionary to a list of parameters.
assert vparam.from_dict(params_d={"a": [1, 1]}) == [("a", 1), ("a", 1)]

Convert a list of parameters to a dictionary.

Parameter Type Description
params list[tuple[str, Any]] A list of parameters.
Return
dict A dictionary where key is param name.

Helpers for path processing.

Get paths to files with interested extension in root directory.

Parameter Type Description
root str Root directory to search for files with required extension.
ext str Extension, end of file name.
Return
List[str] A list of paths with required extension.

Convert a dictionary to a list of parameters.

Helpers for regex processing.

Find all substrings between the start and end regexes.

Parameter Type Description
text str Text where need to find start and end.
start str Regex of start.
end str Regex of end.
w_start bool True - Returns text with matched start text, False - (default) Returns text without matched start text.
w_end bool True - Returns text with matched end text, False - (default) Returns text without matched end text.
strict bool True - Raises ValueError if absent start or end, False - Returns empty string if absent start or end.
Return
str Text between start and end.
from vhelpers import vre

TEXT = "a1\nb2\nc3\nd4"
assert vre.between(text=TEXT, start="b2", end="c3", w_start=True, w_end=True) == "b2\nc3"

Parse 1 item using findall. 1 group with parentheses in pattern is required. If nothing is found, return 1 empty string.

Parameter Type Description
pattern str The regular expression pattern to search for.
string str The string to search within.
flags int Optional flags to modify the behavior of the search.
Return
str The interested substring, or an empty string if nothing is found.
from vhelpers import vre

assert vre.find1(pattern="a(b)cde", string="abcde") == "b"
assert vre.find1(pattern="a(b)cde", string="acde") == ""

Parse 2 items using findall. 2 groups with parentheses in pattern is required. If nothing is found, return 2 empty strings.

Parameter Type Description
pattern str The regular expression pattern.
string str The string to search within.
flags int Optional flags to modify the behavior of the search.
Return
Tuple[str, str] A tuple with two interested substrings, or empty strings if nothing is found.
from vhelpers import vre

assert vre.find2(pattern="a(b)(c)de", string="abcde") == ("b", "c")
assert vre.find2(pattern="a(b)(c)de", string="acde") == ("", "")

Parse 3 items using findall. 3 groups with parentheses in pattern is required. If nothing is found, returns 3 empty strings.

Parameter Type Description
pattern str The regular expression pattern.
string str The string to search within.
flags int Optional flags to modify the behavior of the search.
Return
Tuple[str, str, str] A tuple with three interested substrings, or empty strings if nothing is found.
from vhelpers import vre

assert vre.find3(pattern="a(b)(c)(d)e", string="abcde") == ("b", "c", "d")
assert vre.find3(pattern="a(b)(c)(d)e", string="acde") == ("", "", "")

Parse 4 items using findall. 4 groups with parentheses in pattern is required. If nothing is found, return 4 empty strings.

Parameter Type Description
pattern str The regular expression pattern.
string str The string to search within.
flags int Optional flags to modify the behavior of the search.
Return
Tuple[str, str, str, str] A tuple with three interested substrings, or empty strings if nothing is found.
from vhelpers import vre

assert vre.find4(pattern="a(b)(c)(d)(e)", string="abcde") == ("b", "c", "d", "e")
assert vre.find4(pattern="a(b)(c)(d)(e)", string="acde") == ("", "", "", "")

Parse 1 digit using findall. 1 group with parentheses in pattern is required. If nothing is found, return 0.

Parameter Type Description
pattern str The regular expression pattern to search for.
string str The string to search within.
flags int Optional flags to modify the behavior of the search.
Return
int The interested integer, or 0 if nothing is found.
from vhelpers import vre

assert vre.find1i(pattern="a([0-9]+)b", string="a123b") == 123
assert vre.find1i(pattern="a([0-9]+)b", string="ab") == 0

Parse 2 digits using findall. 2 groups with parentheses in pattern is required. If nothing is found, return tuple of 0.

Parameter Type Description
pattern str The regular expression pattern to search for.
string str The string to search within.
flags int Optional flags to modify the behavior of the search.
Return
T2Int The interested integers, or tuple of 0 if nothing is found.
from vhelpers import vre

assert vre.find2i(pattern="a([0-9])b([0-9])c", string="a1b2c") == (1, 2)
assert vre.find2i(pattern="a([0-9])b([0-9])c", string="a1bc") == (0, 0)

Parse 1st item that match one of regex in patterns. 1 group with parentheses in pattern is required. If nothing is found, return 1 empty string.

Parameter Type Description
patterns SeqStr The list of regular expression patterns to search for.
string str The string to search within.
flags int Optional flags to modify the behavior of the search.
Return
str The interested substring, or an empty string if nothing is found.
from vhelpers import vre

assert vre.find1s(patterns=["a(a)cde", "a(b)cde"], string="abcde") == "b"

Parse 1st IP address from string. If nothing is found, returns an empty string.

Parameter Type Description
string str String where need to find IP address.
Return
str IP address.
from vhelpers import vre

assert vre.ip("text 10.0.0.1/24 10.0.0.2/24 text") == "10.0.0.1"

Parse 1st prefix from string. If nothing is found, returns an empty string.

Parameter Type Description
string str String where need to find prefix.
Return
str Prefix.
from vhelpers import vre

assert vre.prefix("text 10.0.0.1/24 10.0.0.2/24 text") == "10.0.0.1/24"

Create info without qutes for the __repr__() method.

Parameter Type Description
args   The positional arguments.
kwargs   The keyword arguments.
Return
str A string representation of the parameters.
from vhelpers import vstr

assert vstr.repr_params("a", "b", c="c", d="d") == "a, b, c=c, d=d"

Create parameters for the __repr__() method.

Parameter Type Description
args   The positional arguments.
kwargs   The keyword arguments.
Return
str A string representation of the parameters.
from vhelpers import vstr

assert vstr.repr_params("a", "b", c="c", d="d") == "'a', 'b', c='c', d='d'"

Reverse the characters in a string.

Parameter Type Description
line str The input string.
Return
str The reversed string.
from vhelpers import vstr

assert vstr.reverse("abc") == "cba"

Helpers for YAML processing.

Create commands in YAML format. Where the hostname is the key and the list of commands is the value.

Parameter Type Description
items List[Tuple[str, str, Union[str, List] List of tuples that contain: hostname, parent command, children commands.
Return
str YAML formatted commands.
from vhelpers import vyml

# Create commands in YAML format.
items = [("router1", "interface Ethernet1/1", ["description text", "shutdown"])]
result = """
---
router1: |
 interface Ethernet1/1
  description text
  shutdown
""".strip()
assert vyml.host_cmds(items) == result

Join parent command and children commands using indentation.

Parameter Type Description
cmd str Parent command.
cmds Union[str, List] Children commands.
Return
str YAML formatted commands with indentation.
from vhelpers import vyml

result = """ interface Ethernet1/1\n  description text\n  shutdown"""
assert vyml.cmd_cmds(cmd="interface Ethernet1/1", cmds=["description text", "shutdown"]) == result

vhelpers's People

Contributors

vladimirs-git avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.