Skip to content

Commit

Permalink
Return differently based on command
Browse files Browse the repository at this point in the history
  • Loading branch information
Moosems committed Sep 6, 2024
1 parent d50251d commit f14aed2
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 80 deletions.
69 changes: 11 additions & 58 deletions collegamento/client_server/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,14 @@ def __init__(
id_max: int = 15_000,
server_type: type = Server,
) -> None:
"""To initiate the Client class, you are not required to give any input.
"""See tests, examples, and the file variant code to see how to give input.
Should you choose to give input, the options are as follows:
TODO: update the docstring
"""
The most common input is commands and id_max. server_type is only really useful for wrappers."""

self.all_ids: list[int] = []
self.id_max = id_max

# If the user searches by a command they give a str and get a single Response as
# one can only search by command if it allows multiple Requests they give an int
# which corresponds to a Requests from a command that allows multiple Requests
# and it gives all Responses of that command
# int corresponds to str and str to int = int -> str & str -> int
self.current_ids: dict[str | int, int | str] = {}

self.newest_responses: dict[str, list[Response]] = {}
Expand Down Expand Up @@ -171,7 +166,7 @@ def check_responses(self) -> None:
while not self.response_queue.empty():
self.parse_response(self.response_queue.get())

def get_response(self, command: str) -> list[Response]:
def get_response(self, command: str) -> Response | list[Response] | None:
"""Checks responses and returns the current response of type command if it has been returned - external API"""
if command not in self.commands:
raise CollegamentoError(
Expand All @@ -181,7 +176,13 @@ def get_response(self, command: str) -> list[Response]:
self.check_responses()
response: list[Response] = self.newest_responses[command]
self.newest_responses[command] = []
# TODO: if we know that the command doesn't allow multiple requests don't give a list
if not len(response):
return None

# If we know that the command doesn't allow multiple requests don't give a list
if not self.commands[command][1]:
return response[0]

return response

def add_command(
Expand Down Expand Up @@ -215,51 +216,3 @@ def kill_IPC(self):
def __del__(self):
# Multiprocessing bugs arise if the Process is created, not saved, and not terminated
self.main_process.terminate()


if __name__ == "__main__":
from test_func import foo

# Mini tests
Client({"foo": foo})
x = Client({"foo": (foo, True), "foo2": foo})

x.request({"command": "foo"})
x.request({"command": "foo"})
x.request({"command": "foo2"})
x.request(
{"command": "foo2"}
) # If you see six "Foo called"'s, thats bad news bears
x.add_command("foo3", foo)
x.request({"command": "foo3"})
x.add_command("foo4", foo, True)
x.request({"command": "foo4"})
x.request({"command": "foo4"})

sleep(1)

x.check_responses() # Not necessary, we're just checking that doing
# this first doesn't break get_response

foo_r: list[Response] = x.get_response("foo")
foo_two_r: list[Response] = x.get_response("foo2")
foo_three_r: list[Response] = x.get_response("foo3")
foo_four_r: list[Response] = x.get_response("foo4")

assert len(foo_r) == 2
assert len(foo_two_r) == 1
assert len(foo_three_r) == 1
assert len(foo_four_r) == 2

x.check_responses()
x.create_server()

Client()
Client({"foo": foo}).request({"command": "foo"})
Client().kill_IPC()
Client().create_server()

sleep(1)

# from doctest import testmod
# testmod()
5 changes: 2 additions & 3 deletions docs/source/examples/file_example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ File Example
def main():
commands: dict[str, USER_FUNCTION] = {"test": split_str}
context = FileClient(commands)
context = FileClient({"test": split_str})
context.update_file("test", "test contents")
sleep(1)
context.request({"command": "test", "file": "test"})
sleep(1)
output: Response | None = context.get_response("test")
output: Response = context.get_response("test") # type: ignore
print(output)
context.kill_IPC()
Expand Down
21 changes: 13 additions & 8 deletions docs/source/examples/simple_example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,37 @@ Simple Example

.. code-block:: python
from time import sleep
from collegamento import (
USER_FUNCTION,
Request,
Response,
SimpleClient,
SimpleServer,
Client,
Server,
)
def foo(server: "SimpleServer", bar: Request) -> bool:
def foo(server: "Server", bar: Request) -> bool:
if bar["command"] == "test":
return True
return False
def main():
commands: dict[str, USER_FUNCTION] = {"test": foo}
context = SimpleClient(commands)
# As of collegamento v0.3.0 you can allow multiple requests for the same command
# like so: {"test": (foo, True)} (using (foo, False)) is the default (only newest request)
context = Client({"test": foo})
context.request({"command": "test"})
output: Response | None = context.get_response("test")
if output is not None and output["result"]: # type: ignore
sleep(1)
output: Response = context.get_response("test") # type: ignore
if output and output[0]["result"]: # type: ignore
print("Yippee! It worked!")
else:
print("Aww, maybe your compute is just a little slow?")
print("Aww, maybe your computer is just a little slow?")
context.kill_IPC()
Expand Down
2 changes: 1 addition & 1 deletion examples/file_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def main():

sleep(1)

output: list[Response] = context.get_response("test")
output: Response = context.get_response("test") # type: ignore
print(output)

context.kill_IPC()
Expand Down
4 changes: 3 additions & 1 deletion examples/simple_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ def foo(server: "Server", bar: Request) -> bool:


def main():
# As of collegamento v0.3.0 you can allow multiple requests for the same command
# like so: {"test": (foo, True)} (using (foo, False)) is the default (only newest request)
context = Client({"test": foo})

context.request({"command": "test"})

sleep(1)

output: list[Response] = context.get_response("test")
output: Response = context.get_response("test") # type: ignore
if output and output[0]["result"]: # type: ignore
print("Yippee! It worked!")
else:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_file_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ def test_file_variants():

sleep(1)

output: list[Response] = context.get_response("test")
output: Response = context.get_response("test") # type: ignore
assert output is not None # noqa: E711
assert output[0]["result"] is True # noqa: E712 # type: ignore
assert output["result"] is True # noqa: E712 # type: ignore

context.add_command("test1", split_str, True)
context.request({"command": "test1", "file": "test"})
context.request({"command": "test1", "file": "test2"})

sleep(1)

output = context.get_response("test1")
output: list[Response] = context.get_response("test1") # type: ignore
assert output is not None # noqa: E711
assert output[0]["result"] == ["test", "contents"] # noqa: E712 # type: ignore
assert output[1]["result"] == ["test", "contents2"] # noqa: E712 # type: ignore
Expand Down
12 changes: 6 additions & 6 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ def test_normal_client():
x.check_responses() # Not necessary, we're just checking that doing
# this first doesn't break get_response

foo_r: list[Response] = x.get_response("foo")
foo_two_r: list[Response] = x.get_response("foo2")
foo_three_r: list[Response] = x.get_response("foo3")
foo_four_r: list[Response] = x.get_response("foo4")
foo_r: list[Response] = x.get_response("foo") # type: ignore
foo_two_r: Response = x.get_response("foo2") # type: ignore
foo_three_r: Response = x.get_response("foo3") # type: ignore
foo_four_r: list[Response] = x.get_response("foo4") # type: ignore

assert len(foo_r) == 2
assert len(foo_two_r) == 1
assert len(foo_three_r) == 1
assert foo_two_r
assert foo_three_r
assert len(foo_four_r) == 2

x.check_responses()
Expand Down

0 comments on commit f14aed2

Please sign in to comment.