From 81f285985e4415a8248699eb16ea14ca3cb3c5a1 Mon Sep 17 00:00:00 2001 From: digehode Date: Fri, 17 Jul 2020 16:35:35 +0100 Subject: [PATCH] Created test cases and expanded on starter code --- Makefile | 7 ++++++ README.md | 26 ++++++++++++++++++++ src/transcoder.py | 51 ++++++++++++++++++++++++++++++++++++++-- tests/test_transcoder.py | 49 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+), 2 deletions(-) mode change 100644 => 100755 src/transcoder.py create mode 100644 tests/test_transcoder.py diff --git a/Makefile b/Makefile index 7dd09f1..7b04cec 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,9 @@ prereqs: venvcheck FORCE venv: FORCE python3 -m venv venv +docserve: + pydoc -p 5555 + venvcheck: ifeq ($(INVENV),) $(error You should only run this from within the venv. Use '. ./venv/bin/activate') @@ -15,4 +18,8 @@ else endif +test: FORCE venvcheck + py.test -v tests/ + + FORCE: diff --git a/README.md b/README.md index e3e2c22..cea4de0 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,16 @@ it (including yourself) and docstrings for people that have to make use of it without viewing the internals. That could be users, or people calling on your classes and functions in their own code. +### Prettier documentation + +To view the documentation in your browser, you have some options. The +basic methods are: generate HTML versions of your documentation and +store it to be viewed in your browser *or* use the built-in web server +that `pydoc` contains to generate your documentation on the fly and +point your browser at it. + +TODO: this bit + ## Virtualenv Python is a very popular programming language. This means there are @@ -187,3 +197,19 @@ This is OK to test that you at least have your environment set up, but it's not really enough to test the software is working perfectly. TODO: write about testing. + + +# Completing Transcoder + +TODO: + + - asASCII (basic) + - User input (intermediate) + - wrapping (advanced) + +# TODO - things left to do before students get access + + - Clear up TODO items + - Put proper pydoc generation in and fill in all docstrings + - Write bit about cloning repo + - Write tests for asASCII (but not give the function) diff --git a/src/transcoder.py b/src/transcoder.py old mode 100644 new mode 100755 index bf29af2..3311e2b --- a/src/transcoder.py +++ b/src/transcoder.py @@ -1,4 +1,38 @@ +#!python +import colored +def asHex(data): + #Start with an empty string + output="" + + #Now for each character... + for c in data: + #Add the hex representation of the character to the string + #The ord function returns the ASCII value of a character + #The hex function returns the hexadecimal representation of a number + output+=hex(ord(c))+" " + + return output.strip() #Take off any spare spaces at the start/end + +def asOctal(data): + output="" + + for c in data: + output+=" "+oct(ord(c)) + + return output.strip() + +def asBinary(data): + output="" + + for c in data: + output+=" "+bin(ord(c)) + + return output.strip() + + +def colourise(text, colour): + return colored.fg(colour)+text+colored.attr('reset') def main(): @@ -15,11 +49,24 @@ three in a row to signal the end. You can also go across multiple lines, like I have here. """ - print("Hello World") - + print(colourise("Transcoder V0.1 pre-alpha","#FF0000")) + inputData="Any text could go here..." + print(f"Input: {inputData}") + hexData=asHex(inputData) + octData=asOctal(inputData) + binData=asBinary(inputData) + print(f"Hex : {hexData}") + print(f"Octal : {octData}") + print(f"Binary: {binData}") if __name__=="__main__": main() + + + +# JS: You can ignore the stuff below. It's just for my spell-checker. +# LocalWords: Transcoder pre asHex JS inputData hexData asOctal +# LocalWords: octData binData asBinary diff --git a/tests/test_transcoder.py b/tests/test_transcoder.py new file mode 100644 index 0000000..8e4b324 --- /dev/null +++ b/tests/test_transcoder.py @@ -0,0 +1,49 @@ +import pytest +import sys +sys.path.append("./src/") +import transcoder + + + + + + +def test_asHex(): + """ Test the 'asHex' function """ + + #First a simple string + assert transcoder.asHex("ABC")=="0x41 0x42 0x43" + #An empty string + assert transcoder.asHex("")=="" + #The highest byte value + assert transcoder.asHex(chr(255))=="0xff" + #Lowest byte value + assert transcoder.asHex(chr(0))=="0x0" + + + + +def test_asOctal(): + """ Test the 'asOctal' function """ + + #First a simple string + assert transcoder.asOctal("ABC")=="0o101 0o102 0o103" + #An empty string + assert transcoder.asOctal("")=="" + #The highest byte value + assert transcoder.asOctal(chr(255))=="0o377" + #Lowest byte value + assert transcoder.asOctal(chr(0))=="0o0" + + +def test_asBinary(): + """ Test the 'asBinary' function """ + + #First a simple string + assert transcoder.asBinary("ABC")=="0b1000001 0b1000010 0b1000011" + #An empty string + assert transcoder.asBinary("")=="" + #The highest byte value + assert transcoder.asBinary(chr(255))=="0b11111111" + #Lowest byte value + assert transcoder.asBinary(chr(0))=="0b0"