Skip to content

Commit f266f43

Browse files
committed
made some internal methods private & made internal classmethods static if the class object isn't required
1 parent 54e9c04 commit f266f43

5 files changed

Lines changed: 50 additions & 51 deletions

File tree

src/xulbux/color.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ def is_valid(cls, color: AnyRgba | AnyHsla | AnyHexa, allow_alpha: bool = True)
934934
)
935935

936936
@classmethod
937-
def _parse_rgba(cls, color: AnyRgba) -> rgba:
937+
def __parse_rgba(cls, color: AnyRgba) -> rgba:
938938
"""Internal method to parse a color to an RGBA object."""
939939
if isinstance(color, rgba):
940940
return color
@@ -951,7 +951,7 @@ def _parse_rgba(cls, color: AnyRgba) -> rgba:
951951
raise ValueError(f"Could not parse RGBA color: {color!r}")
952952

953953
@classmethod
954-
def _parse_hsla(cls, color: AnyHsla) -> hsla:
954+
def __parse_hsla(cls, color: AnyHsla) -> hsla:
955955
"""Internal method to parse a color to an HSLA object."""
956956
if isinstance(color, hsla):
957957
return color
@@ -1007,11 +1007,11 @@ def to_rgba(cls, color: Rgba | Hsla | Hexa) -> rgba:
10071007
if isinstance(color, (hsla, hexa)):
10081008
return color.to_rgba()
10091009
elif cls.is_valid_hsla(color):
1010-
return cls._parse_hsla(color).to_rgba()
1010+
return cls.__parse_hsla(color).to_rgba()
10111011
elif cls.is_valid_hexa(color):
10121012
return hexa(cast(str | int, color)).to_rgba()
10131013
elif cls.is_valid_rgba(color):
1014-
return cls._parse_rgba(color)
1014+
return cls.__parse_rgba(color)
10151015
raise ValueError(f"Could not convert color {color!r} to RGBA.")
10161016

10171017
@classmethod
@@ -1022,11 +1022,11 @@ def to_hsla(cls, color: Rgba | Hsla | Hexa) -> hsla:
10221022
if isinstance(color, (rgba, hexa)):
10231023
return color.to_hsla()
10241024
elif cls.is_valid_rgba(color):
1025-
return cls._parse_rgba(color).to_hsla()
1025+
return cls.__parse_rgba(color).to_hsla()
10261026
elif cls.is_valid_hexa(color):
10271027
return hexa(cast(str | int, color)).to_hsla()
10281028
elif cls.is_valid_hsla(color):
1029-
return cls._parse_hsla(color)
1029+
return cls.__parse_hsla(color)
10301030
raise ValueError(f"Could not convert color {color!r} to HSLA.")
10311031

10321032
@classmethod
@@ -1037,9 +1037,9 @@ def to_hexa(cls, color: Rgba | Hsla | Hexa) -> hexa:
10371037
if isinstance(color, (rgba, hsla)):
10381038
return color.to_hexa()
10391039
elif cls.is_valid_rgba(color):
1040-
return cls._parse_rgba(color).to_hexa()
1040+
return cls.__parse_rgba(color).to_hexa()
10411041
elif cls.is_valid_hsla(color):
1042-
return cls._parse_hsla(color).to_hexa()
1042+
return cls.__parse_hsla(color).to_hexa()
10431043
elif cls.is_valid_hexa(color):
10441044
return color if isinstance(color, hexa) else hexa(cast(str | int, color))
10451045
raise ValueError(f"Could not convert color {color!r} to HEXA")
@@ -1183,11 +1183,9 @@ def hex_int_to_rgba(cls, hex_int: int, preserve_original: bool = False) -> rgba:
11831183
else:
11841184
raise ValueError(f"Could not convert HEX integer 0x{hex_int:X} to RGBA color.")
11851185

1186-
@classmethod
1187-
def _linearize_srgb(cls, c: float) -> float:
1188-
"""Helper method to linearize sRGB component following the WCAG standard.\n
1189-
----------------------------------------------------------------------------
1190-
- `c` -⠀the sRGB component value in range [0.0, 1.0] inclusive"""
1186+
@staticmethod
1187+
def __linearize_srgb(c: float) -> float:
1188+
"""Helper method to linearize sRGB component following the WCAG standard."""
11911189
if not (0.0 <= c <= 1.0):
11921190
raise ValueError(f"The 'c' parameter must be in range [0.0, 1.0] inclusive, got {c!r}")
11931191

@@ -1229,14 +1227,14 @@ def luminance(
12291227
elif method == "bt601":
12301228
luminance = 0.299 * _r + 0.587 * _g + 0.114 * _b
12311229
elif method == "wcag3":
1232-
_r = cls._linearize_srgb(_r)
1233-
_g = cls._linearize_srgb(_g)
1234-
_b = cls._linearize_srgb(_b)
1230+
_r = cls.__linearize_srgb(_r)
1231+
_g = cls.__linearize_srgb(_g)
1232+
_b = cls.__linearize_srgb(_b)
12351233
luminance = 0.2126729 * _r + 0.7151522 * _g + 0.0721750 * _b
12361234
else:
1237-
_r = cls._linearize_srgb(_r)
1238-
_g = cls._linearize_srgb(_g)
1239-
_b = cls._linearize_srgb(_b)
1235+
_r = cls.__linearize_srgb(_r)
1236+
_g = cls.__linearize_srgb(_g)
1237+
_b = cls.__linearize_srgb(_b)
12401238
luminance = 0.2126 * _r + 0.7152 * _g + 0.0722 * _b
12411239

12421240
if output_type == int:

src/xulbux/console.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -513,32 +513,36 @@ def log(
513513
end=end,
514514
)
515515

516+
@staticmethod
517+
def __find_string_part(pos: int, cumulative_pos: list[int]) -> int:
518+
"""Finds the index of the string part that contains the given position."""
519+
left, right = 0, len(cumulative_pos) - 1
520+
while left < right:
521+
mid = (left + right) // 2
522+
if cumulative_pos[mid] <= pos < cumulative_pos[mid + 1]:
523+
return mid
524+
elif pos < cumulative_pos[mid]:
525+
right = mid
526+
else:
527+
left = mid + 1
528+
return left
529+
516530
@classmethod
517531
def __add_back_removed_parts(cls, split_string: list[str], removals: tuple[tuple[int, str], ...]) -> list[str]:
518532
"""Adds back the removed parts into the split string parts at their original positions."""
519-
lengths, cumulative_pos = [len(s) for s in split_string], [0]
520-
for length in lengths:
533+
cumulative_pos = [0]
534+
for length in (len(s) for s in split_string):
521535
cumulative_pos.append(cumulative_pos[-1] + length)
536+
522537
result, offset_adjusts = split_string.copy(), [0] * len(split_string)
523538
last_idx, total_length = len(split_string) - 1, cumulative_pos[-1]
524539

525-
def find_string_part(pos: int) -> int:
526-
left, right = 0, len(cumulative_pos) - 1
527-
while left < right:
528-
mid = (left + right) // 2
529-
if cumulative_pos[mid] <= pos < cumulative_pos[mid + 1]:
530-
return mid
531-
elif pos < cumulative_pos[mid]:
532-
right = mid
533-
else:
534-
left = mid + 1
535-
return left
536-
537540
for pos, removal in removals:
538541
if pos >= total_length:
539542
result[last_idx] = result[last_idx] + removal
540543
continue
541-
i = find_string_part(pos)
544+
545+
i = cls.__find_string_part(pos, cumulative_pos)
542546
adjusted_pos = (pos - cumulative_pos[i]) + offset_adjusts[i]
543547
parts = [result[i][:adjusted_pos], removal, result[i][adjusted_pos:]]
544548
result[i] = "".join(parts)
@@ -863,9 +867,8 @@ def log_box_bordered(
863867
end=end,
864868
)
865869

866-
@classmethod
870+
@staticmethod
867871
def __prepare_log_box(
868-
cls,
869872
values: list[object] | tuple[object, ...],
870873
default_color: Optional[Rgba | Hexa] = None,
871874
has_rules: bool = False,

src/xulbux/data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,8 @@ def print(
660660
end=end,
661661
)
662662

663-
@classmethod
664-
def __sep_path_id(cls, path_id: str) -> list[int]:
663+
@staticmethod
664+
def __sep_path_id(path_id: str) -> list[int]:
665665
if len(split_id := path_id.split(">")) == 2:
666666
id_part_len, path_id_parts = split_id
667667

src/xulbux/env_path.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def remove_path(cls, path: Optional[str] = None, cwd: bool = False, base_dir: bo
5151
if cls.has_path(path := cls.__get(path, cwd, base_dir)):
5252
cls.__persistent(path, remove=True)
5353

54-
@classmethod
55-
def __get(cls, path: Optional[str] = None, cwd: bool = False, base_dir: bool = False) -> str:
54+
@staticmethod
55+
def __get(path: Optional[str] = None, cwd: bool = False, base_dir: bool = False) -> str:
5656
"""Get and/or normalize the given path, CWD or base directory.\n
5757
------------------------------------------------------------------------------------
5858
Raise an error if no path is provided and neither `cwd` or `base_dir` is `True`."""

src/xulbux/format_codes.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,12 @@ def _config_console(cls) -> None:
523523
pass
524524
_CONSOLE_ANSI_CONFIGURED = True
525525

526-
@classmethod
527-
def __formats_to_keys(cls, formats: str) -> list[str]:
526+
@staticmethod
527+
def __formats_to_keys(formats: str) -> list[str]:
528528
return [k.strip() for k in formats.split("|") if k.strip()]
529529

530-
@classmethod
531-
def __validate_default_color(cls, default_color: Optional[Rgba | Hexa]) -> tuple[bool, Optional[rgba]]:
530+
@staticmethod
531+
def __validate_default_color(default_color: Optional[Rgba | Hexa]) -> tuple[bool, Optional[rgba]]:
532532
"""Validate and convert `default_color` to rgba format."""
533533
if default_color is None:
534534
return False, None
@@ -538,9 +538,8 @@ def __validate_default_color(cls, default_color: Optional[Rgba | Hexa]) -> tuple
538538
return True, Color.to_rgba(default_color)
539539
raise TypeError("The 'default_color' parameter must be either a valid RGBA or HEXA color, or None.")
540540

541-
@classmethod
541+
@staticmethod
542542
def __get_default_ansi(
543-
cls,
544543
default_color: rgba,
545544
format_key: Optional[str] = None,
546545
brightness_steps: Optional[int] = None,
@@ -606,16 +605,15 @@ def __get_replacement(cls, format_key: str, default_color: Optional[rgba], brigh
606605
pass
607606
return _format_key
608607

609-
@classmethod
610-
def __normalize_key(cls, format_key: str) -> str:
608+
@staticmethod
609+
def __normalize_key(format_key: str) -> str:
611610
"""Normalizes the given format key."""
612611
k_parts = format_key.replace(" ", "").lower().split(":")
613612
prefix_str = "".join(
614613
f"{prefix_key.lower()}:" for prefix_key, prefix_values in _PREFIX.items()
615614
if any(k_part in prefix_values for k_part in k_parts)
616615
)
617616
return prefix_str + ":".join(
618-
part for part in k_parts if part not in {val
619-
for values in _PREFIX.values()
620-
for val in values}
617+
part for part in k_parts \
618+
if part not in {val for values in _PREFIX.values() for val in values}
621619
)

0 commit comments

Comments
 (0)