Skip to content

Commit

Permalink
minor bug fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasWeise committed Jan 4, 2024
1 parent 6dfece2 commit 9d18273
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
59 changes: 59 additions & 0 deletions moptipy/evaluation/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,65 @@ def _get_goal_reach_index(f: np.ndarray, goal_f: int | float): # noqa
6
>>> _get_goal_reach_index(ft, 0.9)
-1
>>> ft = np.array([10, 9, 8, 5, 5, 3, 2, 1])
>>> _get_goal_reach_index(ft, 8)
2
>>> _get_goal_reach_index(ft, 7)
3
>>> _get_goal_reach_index(ft, 6)
3
>>> _get_goal_reach_index(ft, 5)
3
>>> _get_goal_reach_index(ft, 4)
5
>>> ft = np.array([10, 9, 9, 8, 5, 5, 3, 2, 1])
>>> _get_goal_reach_index(ft, 9)
1
>>> ft = np.array([10, 9, 9, 9, 8, 5, 5, 3, 2, 1])
>>> _get_goal_reach_index(ft, 9)
1
>>> ft = np.array([10, 9, 9, 9, 9, 8, 5, 5, 3, 2, 1])
>>> _get_goal_reach_index(ft, 9)
1
>>> ft = np.array([10])
>>> _get_goal_reach_index(ft, 10)
0
>>> ft = np.array([10, 10])
>>> _get_goal_reach_index(ft, 10)
0
>>> ft = np.array([10, 10, 10])
>>> _get_goal_reach_index(ft, 10)
0
>>> ft = np.array([10, 10, 10, 10])
>>> _get_goal_reach_index(ft, 10)
0
>>> ft = np.array([10, 9])
>>> _get_goal_reach_index(ft, 9)
1
>>> ft = np.array([10, 9, 9])
>>> _get_goal_reach_index(ft, 9)
1
>>> ft = np.array([10, 9, 9, 9])
>>> _get_goal_reach_index(ft, 9)
1
>>> ft = np.array([10, 9, 9, 9, 9])
>>> _get_goal_reach_index(ft, 9)
1
>>> ft = np.array([10, 9, 9, 9, 9, 8])
>>> _get_goal_reach_index(ft, 9)
1
>>> ft = np.array([10, 9, 9, 9, 8, 8])
>>> _get_goal_reach_index(ft, 9)
1
>>> ft = np.array([10, 9, 9, 8, 8, 8])
>>> _get_goal_reach_index(ft, 9)
1
>>> ft = np.array([10, 9, 8, 8, 8, 8])
>>> _get_goal_reach_index(ft, 9)
1
>>> ft = np.array([10, 8, 8, 8, 8, 8])
>>> _get_goal_reach_index(ft, 9)
1
"""
res: Final = np.searchsorted(f[::-1], goal_f, side="right")
if res <= 0:
Expand Down
17 changes: 8 additions & 9 deletions moptipy/utils/formatted_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ class FormattedStr(str):
def __new__(cls, value, bold: bool = False, italic: bool = False, # noqa
code: bool = False, mode: int = TEXT): # noqa
"""
Construct the object.
Construct the formatted string.
:param value: the string value
:param bold: should the format be bold face?
:param italic: should the format be italic face?
:param code: should the format be code face?
:param mode: the mode of the formatted string
:returns: an instance of `FormattedStr`
"""
if not isinstance(value, str):
raise type_error(value, "value", str)
Expand All @@ -78,14 +79,12 @@ def __new__(cls, value, bold: bool = False, italic: bool = False, # noqa
if not isinstance(code, bool):
raise type_error(code, "code", bool)
check_int_range(mode, "mode", TEXT, SPECIAL)
if bold or italic or code or (mode != TEXT):
ret = super().__new__(cls, value)
ret.bold = bold
ret.italic = italic
ret.code = code
ret.mode = mode
return ret
return value
ret = super().__new__(cls, value)
ret.bold = bold
ret.italic = italic
ret.code = code
ret.mode = mode
return ret

@staticmethod
def add_format(s: str, bold: bool = False, italic: bool = False,
Expand Down
2 changes: 1 addition & 1 deletion moptipy/utils/number_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def render(self, source: int | float | None | Iterable[int | float | None],
raise type_error(source, "source", Iterable)
if (none_str is not None) and (
not isinstance(none_str, FormattedStr)):
raise type_error(none_str, "none_str", (str, None))
raise type_error(none_str, "none_str", (FormattedStr, None))

# get the format parameters
int_renderer: Final[Callable[[int], str]] = \
Expand Down
2 changes: 1 addition & 1 deletion moptipy/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from typing import Final

#: the version string of `moptipy`
__version__: Final[str] = "0.9.102"
__version__: Final[str] = "0.9.103"

0 comments on commit 9d18273

Please sign in to comment.