Skip to content

Commit

Permalink
docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirs-git committed Jul 18, 2024
1 parent 77d7fe7 commit ad37939
Show file tree
Hide file tree
Showing 25 changed files with 946 additions and 1,101 deletions.
2 changes: 1 addition & 1 deletion examples/intf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Examples Intf"""
"""Examples Intf."""
from pprint import pprint

import netports
Expand Down
2 changes: 1 addition & 1 deletion examples/ip.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Examples IP protocols"""
"""Examples IP protocols."""

from pprint import pprint

Expand Down
10 changes: 5 additions & 5 deletions examples/numbers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Examples Numbers"""
"""Examples Numbers."""

import netports

Expand All @@ -22,7 +22,7 @@
# [1, 3, 4, 5]
print()

# Converts unsorted range to sorted *List[int]* without duplicates
# Converts unsorted range to sorted List[int] without duplicates
ports = netports.inumbers("3-5,1,3-5,1")
print(ports)
# [1, 3, 4, 5]
Expand All @@ -36,13 +36,13 @@
# [1, 3, 4, 5]
print()

# Converts unsorted range to *List[int]* with custom splitters
# Converts unsorted range to List[int] with custom splitters
ports = netports.inumbers("3 to 5 1 4 to 5 1", splitter=" ", range_splitter=" to ")
print(ports)
# [1, 3, 4, 5]
print()

# Converts unsorted range to sorted *str* without duplicates
# Converts unsorted range to sorted str without duplicates
ports = netports.snumbers("3-5,1,3-5,1")
print(ports)
# 1,3-5
Expand All @@ -56,7 +56,7 @@
# 1,3-5
print()

# Converts unsorted range to *str* with custom splitters
# Converts unsorted range to str with custom splitters
ports = netports.snumbers("3 to 5 1 4 to 5 1", splitter=" ", range_splitter=" to ")
print(ports)
# 1 3 to 5
Expand Down
2 changes: 1 addition & 1 deletion examples/range.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Examples Range"""
"""Examples Range."""

from netports import Range, NetportsValueError

Expand Down
2 changes: 1 addition & 1 deletion examples/tcp_udp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Examples TCP/UDP"""
"""Examples TCP/UDP."""

import netports
from netports import NetportsValueError
Expand Down
2 changes: 1 addition & 1 deletion examples/vlan.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Examples VLAN"""
"""Examples VLAN."""

import re

Expand Down
4 changes: 2 additions & 2 deletions netports/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""netports exceptions"""
"""netports exceptions."""


class NetportsValueError(ValueError):
"""Hostname naming convention error"""
"""Hostname naming convention error."""
140 changes: 76 additions & 64 deletions netports/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Helper functions"""
"""Helper functions."""
import re
import time
from typing import Any, Iterable
Expand All @@ -11,14 +11,16 @@


def findall1(pattern: str, string: str, flags=0) -> str:
"""Parses 1st item of re.findall(). If nothing is found, returns an empty string.
Group with parentheses in pattern is required
::
:return: Interested substring
:example:
pattern: "a(b)cde"
string: "abcde"
return: "b"
"""Parse 1st item of re.findall(). If nothing is found, returns an empty string.
Group with parentheses in pattern is required.
:return: Interested substring.
:example:
pattern: "a(b)cde"
string: "abcde"
return: "b"
"""
result = (re.findall(pattern=pattern, string=string, flags=flags) or [""])[0]
if isinstance(result, str):
Expand All @@ -29,14 +31,16 @@ def findall1(pattern: str, string: str, flags=0) -> str:


def findall2(pattern: str, string: str, flags=0) -> T2Str:
"""Parses 2 items of re.findall(). If nothing is found, returns 2 empty strings.
Group with parentheses in pattern is required
::
:return: Two interested substrings
:example:
pattern: "a(b)(c)de"
string: "abcde"
return: "b", "c"
"""Parse 2 items of re.findall(). If nothing is found, returns 2 empty strings.
Group with parentheses in pattern is required.
:return: Two interested substrings.
:example:
pattern: "a(b)(c)de"
string: "abcde"
return: "b", "c"
"""
result = (re.findall(pattern=pattern, string=string, flags=flags) or [("", "")])[0]
if isinstance(result, tuple) and len(result) >= 2:
Expand All @@ -45,14 +49,16 @@ def findall2(pattern: str, string: str, flags=0) -> T2Str:


def findall3(pattern: str, string: str, flags=0) -> T3Str:
"""Parses 3 items of re.findall(). If nothing is found, returns 3 empty strings.
Group with parentheses in pattern is required
::
:return: Three interested substrings
:example:
pattern: "a(b)(c)(d)e"
string: "abcde"
return: "b", "c", "d"
"""Parse 3 items of re.findall(). If nothing is found, returns 3 empty strings.
Group with parentheses in pattern is required.
:return: Three interested substrings.
:example:
pattern: "a(b)(c)(d)e"
string: "abcde"
return: "b", "c", "d"
"""
result = (re.findall(pattern=pattern, string=string, flags=flags) or [("", "", "")])[0]
if isinstance(result, tuple) and len(result) >= 3:
Expand All @@ -61,16 +67,17 @@ def findall3(pattern: str, string: str, flags=0) -> T3Str:


def findall4(pattern: str, string: str, flags=0) -> T4Str:
"""Parses 4 items of re.findall(). If nothing is found, returns 4 empty strings
::
:param pattern: Regex pattern, where 4 groups with parentheses in pattern are required
:param string: String where need to find pattern
:param flags: findall flags
:return: Three interested substrings
:example:
pattern = "a(b)(c)(d)(e)f"
string = "abcdef"
return: "b", "c", "d", "e"
"""Parse 4 items of re.findall(). If nothing is found, returns 4 empty strings.
:param pattern: Regex pattern, where 4 groups with parentheses in pattern are required.
:param string: String where need to find pattern.
:param flags: findall flags.
:return: Three interested substrings.
:example:
pattern = "a(b)(c)(d)(e)f"
string = "abcdef"
return: "b", "c", "d", "e"
"""
result = (re.findall(pattern=pattern, string=string, flags=flags) or [("", "", "", "")])[0]
if isinstance(result, tuple) and len(result) >= 4:
Expand All @@ -79,35 +86,36 @@ def findall4(pattern: str, string: str, flags=0) -> T4Str:


def repr_params(*args, **kwargs) -> str:
"""Makes params for __repr__() method"""
"""Make params for __repr__() method."""
args_ = ", ".join([f"{v!r}" for v in args if v])
kwargs_ = ", ".join([f"{k}={v!r}" for k, v in kwargs.items() if v])
params = [s for s in (args_, kwargs_) if s]
return ", ".join(params)


def join(items: LAny) -> str:
"""Joins items by "," """
"""Join items by ","."""
return SPLITTER.join([str(i) for i in items])


# =============================== bool ===============================


def is_all(**kwargs) -> bool:
"""Get param `all` from `kwargs`"""
"""Get param `all` from `kwargs`."""
return bool(kwargs.get("all"))


def is_brief(**kwargs) -> bool:
"""Get param `verbose` from `kwargs` and reverse the result"""
"""Get param `verbose` from `kwargs` and reverse the result."""
return not is_verbose(**kwargs)


def is_brief_in_items(items: Any) -> bool:
"""Checks is "-1" or -1 in `items`, used for verbose=False
::
:param items: *str, int, List[str], List[int]*
:return: True - if "-1" or -1 present in `items`
"""Check is "-1" or -1 in `items`, used for verbose=False.
:param items: str, int, List[str], List[int].
:return: True - if "-1" or -1 present in `items`.
"""
if isinstance(items, (str, int)):
items = [items]
Expand All @@ -127,15 +135,15 @@ def is_brief_in_items(items: Any) -> bool:


def is_verbose(**kwargs) -> bool:
"""Get param `verbose` from `kwargs`"""
"""Get param `verbose` from `kwargs`."""
verbose = kwargs.get("verbose")
if verbose is None:
return False
return bool(verbose)


def is_strict(**kwargs) -> bool:
"""Get param `strict` from `kwargs`"""
"""Get param `strict` from `kwargs`."""
strict = kwargs.get("strict")
if strict is None:
return True
Expand All @@ -144,12 +152,14 @@ def is_strict(**kwargs) -> bool:

# =============================== int ================================


def to_int(number: StrInt) -> int:
"""Converts *str* to *int*
::
:param number: *int* or digit as *str*
:return: *int*
:raises TypeError: If number is not digit
"""Convert str to int.
:param number: int or digit as str/
:return: int.
:raises TypeError: If number is not digit.
"""
if isinstance(number, int):
return number
Expand All @@ -159,11 +169,11 @@ def to_int(number: StrInt) -> int:


def to_lint(numbers: IStrInt) -> LInt:
"""Converts *List[str]* to *List[int]*
::
:param numbers: *List[str]*
:return: *List[int]*
:raises TypeError: If number is not digit
"""Convert List[str] to List[int].
:param numbers: List[str].
:return: List[int].
:raises TypeError: If number is not digit.
"""
if not isinstance(numbers, (list, set, tuple)):
raise TypeError(f"{numbers=} {list} expected")
Expand All @@ -172,8 +182,9 @@ def to_lint(numbers: IStrInt) -> LInt:

# =============================== list ===============================


def lstr(items: Any) -> LStr:
"""Converts any types of items to *List[str]*"""
"""Convert any types of items to List[str]."""
if not isinstance(items, (bytes, dict, float, int, list, set, str, tuple)):
raise TypeError(f"{items=} {list} expected")
if isinstance(items, (bytes, float, int, str)):
Expand All @@ -190,7 +201,7 @@ def lstr(items: Any) -> LStr:


def no_dupl(items: Iterable) -> list:
"""Remove duplicate items from list"""
"""Remove duplicate items from list."""
items_ = []
for item in items:
if item not in items_:
Expand All @@ -199,18 +210,18 @@ def no_dupl(items: Iterable) -> list:


def remove_brief_items(items: Any) -> list:
"""Remove "-1", -1 from items
::
:param items: *str, int, List[str], List[int]*
:return: Items without "-1", -1
"""Remove "-1", -1 from items.
:param items: str, int, List[str], List[int].
:return: Items without "-1", -1.
"""
if isinstance(items, (str, int)):
items = [items]
return [i for i in items if i not in (BRIEF_ALL_S, BRIEF_ALL_I)]


def split(items: Any) -> LStr:
"""Splits items *str*, *List[str]* by "," " " """
"""Split items str, List[str] by "," " "."""
results: LStr = []
for items_s in lstr(items):
for item_s in items_s.split():
Expand All @@ -220,11 +231,12 @@ def split(items: Any) -> LStr:

# ============================= wrapper ==============================


def time_spent(func):
"""Wrapper measure function execution time"""
"""Wrapper measure function execution time."""

def wrap(*args, **kwargs):
"""Wrap"""
"""Wrap."""
started = time.time()
return_ = func(*args, **kwargs)
elapsed = time.time() - started
Expand Down
Loading

0 comments on commit ad37939

Please sign in to comment.