From 60a2a880ceb230a443ef0a14d211b6830c268fc4 Mon Sep 17 00:00:00 2001 From: jo2-holdsworth <jack2.holdsworth@live.uwe.ac.uk> Date: Tue, 4 Apr 2023 19:20:17 +0100 Subject: [PATCH] removed part 2 a few more bugs after unittest --- README.md | 17 +++++------------ binaryHeap.py | 27 --------------------------- binaryHeap2.py | 24 ------------------------ convert.py | 6 ------ morse.py | 9 ++++----- runtime.py | 6 ++++++ server.py | 50 -------------------------------------------------- 7 files changed, 15 insertions(+), 124 deletions(-) delete mode 100644 binaryHeap.py delete mode 100644 binaryHeap2.py delete mode 100644 convert.py create mode 100644 runtime.py delete mode 100644 server.py diff --git a/README.md b/README.md index 6d36bae..2554708 100644 --- a/README.md +++ b/README.md @@ -17,15 +17,16 @@ - [Getting Started](#getting-started) - [Dependencies](#dependencies) - [Installing](#installing) - - [Executing program](#executing-program) - [Authors](#authors) - [Version History](#version-history) - [License](#license) ## Description -Morse code :: Worksheet(2 part 1) -A program to encode and decode morse. +Morse code :: Worksheet(2 part 1)\ +This is a rep for only worksheet 2 part 1. Worksheet 2 part 2 can be found here [IOT - Worksheet 2 part 2 ](https://gitlab.uwe.ac.uk/jo2-holdsworth/iot-worksheet-2-part-2) + + ### Task 1: Getting to know morse code * *The CSCT cloud server has a port open on 10105 which contains a morse encoder and decoder. We are required to connect to the service and test its functionality*. * To connect to the port on the csct server we need to be able to access the port. Todo this we can port ford the port via VS Code. Using the command `Ctrl + Shift + P` we can bring up the command wind and open ports:  if we then add the port 10105  should be able to access that port on our local machine using a browser to access `localhost:10105` as we can see it has worked and we can now connect to the 10105 port on the csct cloud server:  @@ -86,20 +87,12 @@ morse.decode('..- ...') ### Dependencies -* None +* unittest ### Installing * Nothing to install -### Executing program - -``` -python3 main.py -# or -python3 morseunit.py -``` - ## Authors Contributors names and contact info diff --git a/binaryHeap.py b/binaryHeap.py deleted file mode 100644 index e54fc59..0000000 --- a/binaryHeap.py +++ /dev/null @@ -1,27 +0,0 @@ -tree = "-ETIANMSURWDKGOHVF*L*PJBXCYZQ**54*3*¿?2&*+****16=/***(*7***8*90*************_****\"**.********'**-********;!*)***¡*,****:****************$***********************************************************************************************************************" - - -def decode_bt(string): - - output = "" - parent = 1 - for i in range(len(string)): - if i < len(string) - 1 : - if string[i] == " ": - output += tree[parent - 1] - parent = 1 - if string[i] == ".": - parent = 2 * parent - elif string[i] == "-": - parent = 2 * parent + 1 - else: - if string[i] == ".": - parent = 2 * parent - if string[i] == "-": - parent = 2 * parent + 1 - output += tree[parent - 1] - parent = 1 - return output - - -print(decode_bt(".-. .---- -.. . ... .---- -...- .... .. -...- -.--.")) \ No newline at end of file diff --git a/binaryHeap2.py b/binaryHeap2.py deleted file mode 100644 index 56710c5..0000000 --- a/binaryHeap2.py +++ /dev/null @@ -1,24 +0,0 @@ -# encode_ham(sender: str, receiver: str, msg:str) -> str -import morse -import server -import asyncio - -def encode_ham(sender, receiver, msg): - output = receiver + "de" + sender + "=" + msg + "=(" - # print(output) - return morse.encode(output) - -def decode_ham(str2): - print("hi") - ret = morse.decode(str2) - sender = ret.split("DE") - receiver = sender[1].split("=") - msg = receiver[1].split("=(") - return (sender[0],receiver[0],msg[0]) - -# print(encode_ham("jack","echo","its me")) -print(encode_ham("s","echo","msg")) -asyncio.run(server.main(encode_ham("jack","time","hello world"))) -print(decode_ham(asyncio.run(server.main(encode_ham("jack","echo","hello how are you"))))) - -# print(decode_ham(".-. .---- -.. . ... .---- -...- .... .. -...- -.--.")) \ No newline at end of file diff --git a/convert.py b/convert.py deleted file mode 100644 index 6542bc0..0000000 --- a/convert.py +++ /dev/null @@ -1,6 +0,0 @@ -tree = "-ETIANMSURWDKGOHVF*L*PJBXCYZQ**54*3*¿?2&*+****16=/***(*7***8*90*************_****\"**.********'**-********;!*)***¡*,****:****************$***********************************************************************************************************************" -output = "" -for item in tree: - output += "\"" + item + "\"" + "," - -print(output) \ No newline at end of file diff --git a/morse.py b/morse.py index 5ea523c..bcc2529 100644 --- a/morse.py +++ b/morse.py @@ -52,12 +52,12 @@ class node: output.append(self.decode(y)) else: output.append(" ") # add a space back in output if empty - return "".join(output) + return "".join(output).replace(" ", " ") def decode(self,values): # a method to decode morse suing recursion if values: # if not end of string - + # if . go to left child or is - go to right child and call recursion if values[0] == ".": return self.leftChildNode.decode(values[1:]) @@ -75,7 +75,7 @@ class node: output = [] # output string for letter in word: # for letters in words if letter == " ": # spaces for words - output.append(" ") + output.append(" ") continue ret = self.encoder(letter) # calls the encoder function if ret != "": # in not empty string for error check @@ -142,5 +142,4 @@ def encode(value): def decode(value): # the decoder tree = initTree() - return tree.words(value) - + return tree.words(value) \ No newline at end of file diff --git a/runtime.py b/runtime.py new file mode 100644 index 0000000..6253b70 --- /dev/null +++ b/runtime.py @@ -0,0 +1,6 @@ +import time +import morse +start = time.time() +morse.decode('.. --- - .. ... - .... . -... . ... -') +end = time.time() +print(f"morse(oop): {end-start}") \ No newline at end of file diff --git a/server.py b/server.py deleted file mode 100644 index cc50898..0000000 --- a/server.py +++ /dev/null @@ -1,50 +0,0 @@ -import asyncio -import websockets -import json -import time - - -async def main(str): - uri = "ws://localhost:10102" - async with websockets.connect(uri) as websocket: - # After joining server will send client unique id. - message = json.loads(await websocket.recv()) - print(message) - # Get the client_id from the join message - if message['type'] == 'join_evt': - client_id = message['client_id'] - # Send a ping to the server - await send_message(websocket, str, client_id) - # Wait for the 'ping' response from the server - response = await recv_message(websocket) - print("The Server Sent Back:") - print(response) - - else: - # If first message is not the join message exit - print("Did not receive a correct join message") - return 0 - return response - -async def send_message(websocket, message, client_id): - outward_message = { - 'type' : 'morse_evt', - 'client_id': client_id, - 'payload': message - } - await websocket.send(json.dumps(outward_message)) - -async def recv_message(websocket): - message = json.loads(await websocket.recv()) - return message['payload'] - - - - - -if __name__ == "__main__": - print("Echo client") - - while 1: - time.sleep(1) - asyncio.run(main()) -- GitLab