File string_tools.py
has 426 lines of code (exceeds 250 allowed). Consider refactoring. Open
# SPDX-FileCopyrightText: Copyright 2020-2023, Contributors to pocketutils
# SPDX-PackageHomePage: https://github.com/dmyersturnbull/pocketutils
# SPDX-License-Identifier: Apache-2.0
"""
StringUtils
has 24 functions (exceeds 20 allowed). Consider refactoring. Open
class StringUtils:
def pretty_dict(self: Self, dct: Mapping[Any, Any]) -> str:
"""
Returns a pretty-printed dict, complete with indentation. Will fail on non-JSON-serializable datatypes.
"""
Function pretty_float
has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring. Open
def pretty_float(self: Self, v: float | int, n_sigfigs: int | None = 5) -> str:
"""
Represents a float as a string, with symbols for NaN and infinity.
The returned string always has a minus or + prepended. Strip off the plus with `.lstrip('+')`.
If v is an integer (by isinstance), makes sure to display without a decimal point.
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function strip_paired
has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring. Open
def strip_paired(self: Self, text: str, pieces: Iterable[tuple[str, str] | str]) -> str:
"""
Strips pairs of (start, end) from the ends of strings.
Example:
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function pretty_function
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
def pretty_function(self: Self, function: Callable, *, with_addr: bool = False) -> str:
n_args = str(function.__code__.co_argcount) if hasattr(function, "__code__") else "?"
pat = re.compile(r"^<bound method [^ .]+\.([^ ]+) of (.+)>$")
boundmatch = pat.fullmatch(str(function))
addr = " @ " + hex(id(function)) if with_addr else ""
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function pretty_object
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
def pretty_object(self: Self, thing: Any, *, with_addr: bool = False) -> str:
"""
Get a better and shorter name for a function than str(function).
Ex: `pprint_function(lambda s: s) == '<λ>'`
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function strip_any_ends
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
def strip_any_ends(self: Self, s: str, prefixes: str | Sequence[str], suffixes: str | Sequence[str]) -> str:
"""
Flexible variant that strips any number of prefixes and any number of suffixes.
Also less type-safe than more specific variants.
Note that the order of the prefixes (or suffixes) DOES matter.
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function dict_to_str
has 6 arguments (exceeds 4 allowed). Consider refactoring. Open
def dict_to_str(
Avoid too many return
statements within this function. Open
return "⟨" + s + addr + "⟩"
Avoid too many return
statements within this function. Open
return s
Avoid too many return
statements within this function. Open
return str(thing)
Avoid too many return
statements within this function. Open
return s