Last active
March 26, 2022 17:01
-
-
Save kenjoe41/778771060b63d55aea45351e678c196f to your computer and use it in GitHub Desktop.
Python code and mostly pseudo code to encode and decode text. For beginners to know what to go through in implementing a thorough programs to do it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # Shebang line. Include it in all the main.py scripts. Readup why, and google more: https://stackoverflow.com/questions/6908143/should-i-put-shebang-in-python-scripts-and-what-form-should-it-take | |
| # Insert your imports here. | |
| def encode_to_base64(inText): | |
| ''' | |
| This Function takes in plain text as an input, encodes it to base64, check if everything | |
| went well, then returns encoded text or nil/None/'' otherwise. | |
| ''' | |
| # Write code to encode the inText to base64 | |
| encoded_text = #encode logic, etc | |
| # check if text was encoded well, incase of error, return nil/None/'' | |
| # else no error so return encoded_text | |
| return encoded_text | |
| def decode_from_base64(encText): | |
| ''' | |
| This function takes in base64 encoded text, decodes it to plain text, checks if everything | |
| went well, then return the decoded text or nil/None/'' otherwise. | |
| ''' | |
| # Write code here to decode encText to plain text | |
| decoded_text = # decode logic, etc | |
| # Check for errors and return nil/None/'' | |
| # else no issues, return the decoded text | |
| return decoded_text | |
| def read_from_file(inFile): | |
| ''' | |
| This function takes a file arg, reads it and returns the text it contains. | |
| ''' | |
| # check that the file exists and readable by us. | |
| # YOu can use the try..except clauses too or something that shows us an error gracefully. | |
| # Case of error, exit program and tell us what issue we have. | |
| # Implement file read logic here | |
| with open(inFile, 'r') as f: | |
| # etc | |
| return text_from_file | |
| def write_to_file(out_text, args): | |
| # No return from this function, just printout the location of the file you wrote out. | |
| def output(out_text, args): | |
| if not args.output: | |
| print(out_text) | |
| else if args.output: | |
| write_to_file(out_text, args) | |
| if __name__ == '__main__': | |
| # Argparse logic here | |
| # add bool argument for --encode, and another for --decode | |
| # add string arg to take encode/decode text, --text | |
| # add --file input, to be used instead of --text, to read text from to decode/encode | |
| # add --output arg, this is optional, if set, it should be a file name. Implement this later. | |
| # argparse.parse() | |
| # check if --file arg is set | |
| text = read_from_file(args.file) | |
| # else if the text arg has been set instead | |
| text = args.text | |
| # else: exit program and print help screen with error that either --text of --file options need to be set. | |
| # Last check to be sure text has been set | |
| if text: | |
| # check if encode arg was specified | |
| enc_text = encode_to_base64(text) | |
| if enc_text: | |
| output(enc_text, args) | |
| # check if decode arg was specified in an elif | |
| # do as above | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment