Skip to content

Commit

Permalink
style: apply automated linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
megalinter-bot committed May 29, 2024
1 parent 3118b6e commit 2702c5b
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 230 deletions.
12 changes: 3 additions & 9 deletions src/safeds_runner/cli/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,14 @@ def cli() -> None: # pragma: no cover


def _get_args() -> argparse.Namespace: # pragma: no cover
parser = argparse.ArgumentParser(
description="Execute Safe-DS programs that were compiled to Python."
)
parser = argparse.ArgumentParser(description="Execute Safe-DS programs that were compiled to Python.")
parser.add_argument(
"-V",
"--version",
action="version",
version=f"%(prog)s {version('safe-ds-runner')}",
)
parser.add_argument(
"-v", "--verbose", help="increase logging verbosity", action="store_true"
)
parser.add_argument("-v", "--verbose", help="increase logging verbosity", action="store_true")

# Commands
subparsers = parser.add_subparsers(dest="command")
Expand All @@ -49,7 +45,5 @@ def _get_args() -> argparse.Namespace: # pragma: no cover
def _add_start_subparser(
subparsers: argparse._SubParsersAction,
) -> None: # pragma: no cover
parser = subparsers.add_parser(
Commands.START, help="start the Safe-DS Runner server"
)
parser = subparsers.add_parser(Commands.START, help="start the Safe-DS Runner server")
parser.add_argument("-p", "--port", type=int, default=5000, help="the port to use")
17 changes: 4 additions & 13 deletions src/safeds_runner/memoization/_memoization_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ def get_cache_size(self) -> int:
"""
return functools.reduce(
operator.add,
[
functools.reduce(operator.add, stats.memory_sizes, 0)
for stats in self._map_stats.values()
],
[functools.reduce(operator.add, stats.memory_sizes, 0) for stats in self._map_stats.values()],
0,
)

Expand Down Expand Up @@ -172,9 +169,7 @@ def memoized_function_call(

# Hit
if memoized_value is not None:
self._update_stats_on_hit(
fully_qualified_function_name, access_timestamp, lookup_time
)
self._update_stats_on_hit(fully_qualified_function_name, access_timestamp, lookup_time)
return memoized_value

# Miss
Expand Down Expand Up @@ -235,9 +230,7 @@ def _lookup_value(self, key: MemoizationKey) -> Any | None:
looked_up_value = self._map_values.get(key)
return _unwrap_value_from_shared_memory(looked_up_value)

def _update_stats_on_hit(
self, function_name: str, access_timestamp: int, lookup_time: int
) -> None:
def _update_stats_on_hit(self, function_name: str, access_timestamp: int, lookup_time: int) -> None:
"""
Update the memoization stats on a cache hit.
Expand Down Expand Up @@ -285,7 +278,5 @@ def _update_stats_on_miss(
if stats is None:
stats = MemoizationStats()

stats.update_on_miss(
access_timestamp, lookup_time, computation_time, memory_size
)
stats.update_on_miss(access_timestamp, lookup_time, computation_time, memory_size)
self._map_stats[function_name] = stats
23 changes: 5 additions & 18 deletions src/safeds_runner/memoization/_memoization_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

# Sort functions by miss-rate in reverse (max. misses first)
def _stat_order_miss_rate(function_stats: tuple[str, MemoizationStats]) -> float:
return -(
len(function_stats[1].computation_times)
/ max(1, len(function_stats[1].lookup_times))
)
return -(len(function_stats[1].computation_times) / max(1, len(function_stats[1].lookup_times)))


STAT_ORDER_MISS_RATE: StatOrderExtractor = _stat_order_miss_rate
Expand All @@ -32,12 +29,8 @@ def _stat_order_lru(function_stats: tuple[str, MemoizationStats]) -> float:

# Sort functions by time saved (difference average computation time and average lookup time, least time saved first)
def _stat_order_time_saved(function_stats: tuple[str, MemoizationStats]) -> float:
return (
sum(function_stats[1].computation_times)
/ max(1, len(function_stats[1].computation_times))
) - (
sum(function_stats[1].lookup_times)
/ max(1, len(function_stats[1].lookup_times))
return (sum(function_stats[1].computation_times) / max(1, len(function_stats[1].computation_times))) - (
sum(function_stats[1].lookup_times) / max(1, len(function_stats[1].lookup_times))
)


Expand All @@ -46,15 +39,9 @@ def _stat_order_time_saved(function_stats: tuple[str, MemoizationStats]) -> floa

# Sort functions by priority (ratio average computation time to average size, lowest priority first)
def _stat_order_priority(function_stats: tuple[str, MemoizationStats]) -> float:
return (
sum(function_stats[1].computation_times)
/ max(1, len(function_stats[1].computation_times))
) / max(
return (sum(function_stats[1].computation_times) / max(1, len(function_stats[1].computation_times))) / max(
1.0,
(
sum(function_stats[1].memory_sizes)
/ max(1, len(function_stats[1].memory_sizes))
),
(sum(function_stats[1].memory_sizes) / max(1, len(function_stats[1].memory_sizes))),
)


Expand Down
27 changes: 6 additions & 21 deletions src/safeds_runner/memoization/_memoization_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,7 @@ def _is_deterministically_hashable(value: Any) -> bool:
result:
True, if the object can be deterministically hashed.
"""
return (
_is_not_primitive(value)
and hasattr(value, "__class__")
and value.__class__.__hash__ != object.__hash__
)
return _is_not_primitive(value) and hasattr(value, "__class__") and value.__class__.__hash__ != object.__hash__


def _has_explicit_identity(value: Any) -> bool:
Expand Down Expand Up @@ -362,15 +358,11 @@ def _make_hashable(value: Any) -> Any:
if _is_deterministically_hashable(value) and _has_explicit_identity_memory(value):
return ExplicitIdentityWrapperLazy.existing(value)
elif (
not _is_deterministically_hashable(value)
and _is_not_primitive(value)
and _has_explicit_identity_memory(value)
not _is_deterministically_hashable(value) and _is_not_primitive(value) and _has_explicit_identity_memory(value)
):
return ExplicitIdentityWrapper.existing(value)
elif isinstance(value, dict):
return tuple(
(_make_hashable(key), _make_hashable(value)) for key, value in value.items()
)
return tuple((_make_hashable(key), _make_hashable(value)) for key, value in value.items())
elif isinstance(value, list):
return tuple(_make_hashable(element) for element in value)
elif callable(value):
Expand Down Expand Up @@ -398,9 +390,7 @@ def _get_size_of_value(value: Any) -> int:
size_immediate = sys.getsizeof(value)
if isinstance(value, dict):
return (
sum(map(_get_size_of_value, value.keys()))
+ sum(map(_get_size_of_value, value.values()))
+ size_immediate
sum(map(_get_size_of_value, value.keys())) + sum(map(_get_size_of_value, value.values())) + size_immediate
)
elif isinstance(value, frozenset | list | set | tuple):
return sum(map(_get_size_of_value, value)) + size_immediate
Expand Down Expand Up @@ -462,10 +452,7 @@ def _wrap_value_to_shared_memory(
if isinstance(result, list):
return [_wrap_value_to_shared_memory(entry) for entry in result]
if isinstance(result, dict):
return {
_wrap_value_to_shared_memory(key): _wrap_value_to_shared_memory(value)
for key, value in result.items()
}
return {_wrap_value_to_shared_memory(key): _wrap_value_to_shared_memory(value) for key, value in result.items()}
if isinstance(result, set):
return {_wrap_value_to_shared_memory(entry) for entry in result}
if isinstance(result, frozenset):
Expand Down Expand Up @@ -507,9 +494,7 @@ def _unwrap_value_from_shared_memory(
return [_unwrap_value_from_shared_memory(entry) for entry in result]
if isinstance(result, dict):
return {
_unwrap_value_from_shared_memory(key): _unwrap_value_from_shared_memory(
value
)
_unwrap_value_from_shared_memory(key): _unwrap_value_from_shared_memory(value)
for key, value in result.items()
}
if isinstance(result, set):
Expand Down
4 changes: 1 addition & 3 deletions src/safeds_runner/server/_json_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def default(self, o: Any) -> Any:
# Convert NaN / Infinity to None, as the JSON encoder generates invalid JSON otherwise
return {
key: [
value
if not isinstance(value, float) or math.isfinite(value)
else None
value if not isinstance(value, float) or math.isfinite(value) else None
for value in dict_with_nan_infinity[key]
]
for key in dict_with_nan_infinity
Expand Down
21 changes: 5 additions & 16 deletions src/safeds_runner/server/_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,7 @@ def create_placeholder_description(name: str, type_: str) -> dict[str, str]:
return {"name": name, "type": type_}


def create_placeholder_value(
placeholder_query: QueryMessageData, type_: str, value: Any
) -> dict[str, Any]:
def create_placeholder_value(placeholder_query: QueryMessageData, type_: str, value: Any) -> dict[str, Any]:
"""
Create the message data of a placeholder value message containing name, type and the actual value.
Expand All @@ -236,23 +234,16 @@ def create_placeholder_value(
message: dict[str, Any] = {"name": placeholder_query.name, "type": type_}
# Start Index >= 0
start_index = max(
placeholder_query.window.begin
if placeholder_query.window.begin is not None
else 0,
placeholder_query.window.begin if placeholder_query.window.begin is not None else 0,
0,
)
# Length >= 0
length = (
max(placeholder_query.window.size, 0)
if placeholder_query.window.size is not None
else None
)
length = max(placeholder_query.window.size, 0) if placeholder_query.window.size is not None else None
if isinstance(value, safeds.data.labeled.containers.TabularDataset):
value = value.to_table()

if isinstance(value, safeds.data.tabular.containers.Table) and (
placeholder_query.window.begin is not None
or placeholder_query.window.size is not None
placeholder_query.window.begin is not None or placeholder_query.window.size is not None
):
max_index = value.number_of_rows
value = value.slice_rows(start=start_index, length=length)
Expand All @@ -266,9 +257,7 @@ def create_placeholder_value(
return message


def create_runtime_error_description(
message: str, backtrace: list[dict[str, Any]]
) -> dict[str, Any]:
def create_runtime_error_description(message: str, backtrace: list[dict[str, Any]]) -> dict[str, Any]:
"""
Create the message data of a runtime error message containing error information and a backtrace.
Expand Down
9 changes: 2 additions & 7 deletions src/safeds_runner/server/_module_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,12 @@ def find_spec(
self.imports_to_remove.add(fullname)
return importlib.util.spec_from_loader(
fullname,
loader=InMemoryLoader(
self.code[""][fullname].encode("utf-8"), fullname.replace(".", "/")
),
loader=InMemoryLoader(self.code[""][fullname].encode("utf-8"), fullname.replace(".", "/")),
origin="",
)
parent_package_path = ".".join(module_path[:-1])
submodule_name = module_path[-1]
if (
parent_package_path in self.code
and submodule_name in self.code[parent_package_path]
):
if parent_package_path in self.code and submodule_name in self.code[parent_package_path]:
self.imports_to_remove.add(fullname)
return importlib.util.spec_from_loader(
fullname,
Expand Down
26 changes: 6 additions & 20 deletions src/safeds_runner/server/_pipeline_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ def execute_pipeline(
Unique ID to identify this execution.
"""
if execution_id not in self._placeholder_map:
self._placeholder_map[execution_id] = (
self._process_manager.create_shared_dict()
)
self._placeholder_map[execution_id] = self._process_manager.create_shared_dict()
process = PipelineProcess(
pipeline,
execution_id,
Expand All @@ -89,9 +87,7 @@ def execute_pipeline(
)
process.execute(self._process_manager)

def get_placeholder(
self, execution_id: str, placeholder_name: str
) -> tuple[str | None, Any]:
def get_placeholder(self, execution_id: str, placeholder_name: str) -> tuple[str | None, Any]:
"""
Get a placeholder type and value for an execution id and placeholder name.
Expand Down Expand Up @@ -176,9 +172,7 @@ def save_placeholder(self, placeholder_name: str, value: Any) -> None:
if isinstance(value, Image):
value = Image(value._image_tensor.cpu())
placeholder_type = _get_placeholder_type(value)
if _is_deterministically_hashable(value) and _has_explicit_identity_memory(
value
):
if _is_deterministically_hashable(value) and _has_explicit_identity_memory(value):
value = ExplicitIdentityWrapperLazy.existing(value)
elif (
not _is_deterministically_hashable(value)
Expand Down Expand Up @@ -241,9 +235,7 @@ def _execute(self) -> None:
run_name="__main__",
alter_sys=True,
)
self._send_message(
message_type_runtime_progress, create_runtime_progress_done()
)
self._send_message(message_type_runtime_progress, create_runtime_progress_done())
except BaseException as error: # noqa: BLE001
self._send_exception(error)
finally:
Expand All @@ -252,9 +244,7 @@ def _execute(self) -> None:

def _catch_subprocess_error(self, error: BaseException) -> None:
# This is a callback to log an unexpected failure, executing this is never expected
logging.exception(
"Pipeline process unexpectedly failed", exc_info=error
) # pragma: no cover
logging.exception("Pipeline process unexpectedly failed", exc_info=error) # pragma: no cover

def execute(self, process_manager: ProcessManager) -> None:
"""
Expand Down Expand Up @@ -366,11 +356,7 @@ def memoized_dynamic_call(
return None # pragma: no cover

fully_qualified_function_name = (
receiver.__class__.__module__
+ "."
+ receiver.__class__.__qualname__
+ "."
+ function_name
receiver.__class__.__module__ + "." + receiver.__class__.__qualname__ + "." + function_name
)

member = getattr(receiver, function_name)
Expand Down
16 changes: 4 additions & 12 deletions src/safeds_runner/server/_process_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ class ProcessManager:
def __init__(self) -> None:
self._lock = Lock()
self._state: _State = "initial"
self._on_message_callbacks: set[
Callable[[Message], Coroutine[Any, Any, None]]
] = set()
self._on_message_callbacks: set[Callable[[Message], Coroutine[Any, Any, None]]] = set()

@cached_property
def _manager(self) -> SyncManager:
Expand Down Expand Up @@ -70,9 +68,7 @@ def _consume_queue_messages(self, event_loop: asyncio.AbstractEventLoop) -> None
for callback in self._on_message_callbacks:
asyncio.run_coroutine_threadsafe(callback(message), event_loop)
except BaseException as error: # noqa: BLE001 # pragma: no cover
logging.warning(
"Message queue terminated: %s", error.__repr__()
) # pragma: no cover
logging.warning("Message queue terminated: %s", error.__repr__()) # pragma: no cover

def startup(self) -> None:
"""
Expand Down Expand Up @@ -116,9 +112,7 @@ def create_shared_dict(self) -> DictProxy:
self.startup()
return self._manager.dict()

def on_message(
self, callback: Callable[[Message], Coroutine[Any, Any, None]]
) -> Unregister:
def on_message(self, callback: Callable[[Message], Coroutine[Any, Any, None]]) -> Unregister:
"""
Get notified when a message is received from another process.
Expand All @@ -143,9 +137,7 @@ def get_queue(self) -> Queue[Message]:
_P = ParamSpec("_P")
_T = TypeVar("_T")

def submit(
self, func: Callable[_P, _T], /, *args: _P.args, **kwargs: _P.kwargs
) -> Future[_T]:
def submit(self, func: Callable[_P, _T], /, *args: _P.args, **kwargs: _P.kwargs) -> Future[_T]:
"""Submit a function to be executed by a worker process."""
self.startup()
return self._process_pool.submit(func, *args, **kwargs)
Expand Down
Loading

0 comments on commit 2702c5b

Please sign in to comment.