From 61b3976905deb1cbd16516b24d7b5a1f6880c3c6 Mon Sep 17 00:00:00 2001 From: KTrain5369 Date: Sat, 7 Dec 2024 13:01:24 +1000 Subject: [PATCH] cleanup, flake8 config, modulise read_file in cli --- .flake8 | 14 ++++++++++ library/TextConverter.py | 56 ++++++++++++++++++++++++++-------------- library/file_utils.py | 13 ++++++++++ versions/cli_args.py | 13 +--------- 4 files changed, 65 insertions(+), 31 deletions(-) create mode 100644 .flake8 create mode 100644 library/file_utils.py diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..ccf30f0 --- /dev/null +++ b/.flake8 @@ -0,0 +1,14 @@ +[flake8] +max-line-length = 120 +extend-ignore = E402 +per-file-ignores = + library/TextConverter.py: W605 +exclude = + .git, + __pycache__, + venv, + build, + dist, + *.spec, + .github, + .vscode \ No newline at end of file diff --git a/library/TextConverter.py b/library/TextConverter.py index 64cba2f..08f45d8 100644 --- a/library/TextConverter.py +++ b/library/TextConverter.py @@ -14,7 +14,7 @@ import barcode from barcode.writer import ImageWriter import math -import numpy as np # Add this import at the top of the file +import numpy as np class TextConverter: @@ -123,7 +123,8 @@ def caesar_cipher(self, text, shift): if char.isalpha(): shift_amount = 65 if char.isupper() else 97 encrypted.append( - chr((ord(char) - shift_amount + shift) % 26 + shift_amount)) + chr((ord(char + ) - shift_amount + shift) % 26 + shift_amount)) else: encrypted.append(char) return ''.join(encrypted) @@ -135,8 +136,11 @@ def border_text(self, text): return text2art(text, font='block') def zalgo_text(self, text): - zalgo_chars = ['̍', '̎', '̄', '̅', '̿', '̑', '̆', '̐', '͒', '͗', '͑', '̇', '̈', '̊', '͂', '̓', '̈', '͊', '͋', '͌', '̃', '̂', '̌', '͐', - '̀', '́', '̋', '̏', '̒', '̓', '̔', '̽', '̉', 'ͣ', 'ͤ', 'ͥ', 'ͦ', 'ͧ', 'ͨ', 'ͩ', 'ͪ', 'ͫ', 'ͬ', 'ͭ', 'ͮ', 'ͯ', '̾', '͛', '͆', '̚'] + zalgo_chars = ['̍', '̎', '̄', '̅', '̿', '̑', '̆', '̐', '͒', '͗', '͑', + '̇', '̈', '̊', '͂', '̓', '̈', '͊', '͋', '͌', '̃', '̂', + '̌', '͐', '̀', '́', '̋', '̏', '̒', '̓', '̔', '̽', '̉', + 'ͣ', 'ͤ', 'ͥ', 'ͦ', 'ͧ', 'ͨ', 'ͩ', 'ͪ', 'ͫ', 'ͬ', 'ͭ', + 'ͮ', 'ͯ', '̾', '͛', '͆', '̚'] return ''.join(random.choice(zalgo_chars) + char for char in text) def morse_code(self, text): @@ -154,7 +158,8 @@ def morse_code(self, text): '-': '-....-', '(': '-.--.', ')': '-.--.-', ' ': '/' } - return ' '.join(MORSE_CODE_DICT.get(char.upper(), char) for char in text) + return ' '.join(MORSE_CODE_DICT.get(char.upper( + ), char) for char in text) def binary_text(self, text): return ' '.join(format(ord(char), '08b') for char in text) @@ -187,7 +192,8 @@ def generate_code(self, text, code_type, filename=None): if code_type == 'qr': qr = qrcode.QRCode( - version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4) + version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, + box_size=10, border=4) qr.add_data(text) qr.make(fit=True) img = qr.make_image(fill='black', back_color='white') @@ -204,28 +210,38 @@ def text_to_emoticons(self, text): 'hello': '👋', 'world': '🌍', 'love': '❤️', 'happy': '😊', 'sad': '😢', 'laugh': '😂', 'smile': '😃', 'angry': '😠', 'cool': '😎', 'sun': '☀️', 'moon': '🌙', 'star': '⭐', 'food': '🍔', 'drink': '🍹', 'music': '🎵', - 'book': '📚', 'computer': '💻', 'phone': '📱', 'car': '🚗', 'house': '🏠', + 'book': '📚', 'computer': '💻', 'phone': '📱', 'car': '🚗', + 'house': '🏠', 'tree': '🌳', 'flower': '🌸', 'dog': '🐶', 'cat': '🐱', 'bird': '🐦', - 'fish': '🐠', 'heart': '❤️', 'fire': '🔥', 'water': '💧', 'earth': '🌎', + 'fish': '🐠', 'heart': '❤️', 'fire': '🔥', 'water': '💧', + 'earth': '🌎', 'air': '💨', 'time': '⏰', 'money': '💰', 'work': '💼', 'sleep': '😴', - 'party': '🎉', 'gift': '🎁', 'camera': '📷', 'movie': '🎬', 'music': '🎵', + 'party': '🎉', 'gift': '🎁', 'camera': '📷', 'movie': '🎬', + 'music': '🎵', 'sport': '⚽', 'win': '🏆', 'yes': '👍', 'no': '👎', 'ok': '👌', - 'hello': '👋', 'bye': '👋', 'please': '🙏', 'thanks': '🙏', 'sorry': '😔', + 'hello': '👋', 'bye': '👋', 'please': '🙏', 'thanks': '🙏', + 'sorry': '😔', 'wow': '😮', 'omg': '😱', 'lol': '😂', 'idea': '💡', 'question': '❓', - 'answer': '✅', 'warning': '⚠️', 'stop': '🛑', 'go': '🚦', 'fast': '⚡', + 'answer': '✅', 'warning': '⚠️', 'stop': '🛑', 'go': '🚦', + 'fast': '⚡', 'slow': '🐌', 'up': '⬆️', 'down': '⬇️', 'left': '⬅️', 'right': '➡️', 'back': '🔙', 'soon': '🔜', 'new': '🆕', 'free': '🆓', 'hot': '🔥', 'cold': '❄️', 'big': '🐘', 'small': '🐜', 'loud': '📢', 'quiet': '🤫', 'good': '👍', 'bad': '👎', 'sick': '🤒', 'healthy': '💪', 'smart': '🧠', 'crazy': '🤪', 'king': '👑', 'queen': '👸', 'baby': '👶', 'ghost': '👻', - 'alien': '👽', 'robot': '🤖', 'rainbow': '🌈', 'unicorn': '🦄', 'pizza': '🍕', + 'alien': '👽', 'robot': '🤖', 'rainbow': '🌈', 'unicorn': '🦄', + 'pizza': '🍕', 'beer': '🍺', 'wine': '🍷', 'coffee': '☕', 'tea': '🍵', 'cake': '🎂', - 'balloon': '🎈', 'rocket': '🚀', 'airplane': '✈️', 'train': '🚂', 'boat': '⛵', - 'beach': '🏖️', 'mountain': '⛰️', 'camping': '⛺', 'fire': '🔥', 'snow': '❄️', - 'rain': '🌧️', 'wind': '💨', 'cloud': '☁️', 'thunder': '⚡', 'rainbow': '🌈' + 'balloon': '🎈', 'rocket': '🚀', 'airplane': '✈️', 'train': '🚂', + 'boat': '⛵', + 'beach': '🏖️', 'mountain': '⛰️', 'camping': '⛺', 'fire': '🔥', + 'snow': '❄️', + 'rain': '🌧️', 'wind': '💨', 'cloud': '☁️', 'thunder': '⚡', + 'rainbow': '🌈' } words = text.split() - return ' '.join(emoticon_dict.get(word.lower(), word) for word in words) + return ' '.join(emoticon_dict.get(word.lower( + ), word) for word in words) def nerd_mode(self, text): word_count = len(text.split()) @@ -268,7 +284,7 @@ def morse_code_audio(self, text): 'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--', 'Z': '--..', - + '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', @@ -281,7 +297,8 @@ def morse_code_audio(self, text): def generate_sine_wave(freq, duration, volume=1.0, sample_rate=44100): num_samples = int(sample_rate * duration) - samples = [int(volume * 32767 * math.sin(2 * math.pi * freq * t / sample_rate)) + samples = [int(volume * 32767 * math.sin( + 2 * math.pi * freq * t / sample_rate)) for t in range(num_samples)] return samples @@ -306,7 +323,8 @@ def generate_sine_wave(freq, duration, volume=1.0, sample_rate=44100): morse_audio.extend(medium_gap) # Create a temporary WAV file - with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as temp_wav: + with tempfile.NamedTemporaryFile(suffix='.wav', + delete=False) as temp_wav: temp_wav_path = temp_wav.name with wave.open(temp_wav_path, 'w') as wav_file: wav_file.setnchannels(1) diff --git a/library/file_utils.py b/library/file_utils.py new file mode 100644 index 0000000..4d192af --- /dev/null +++ b/library/file_utils.py @@ -0,0 +1,13 @@ +import sys + + +def read_file(file_path): + try: + with open(file_path, 'r', encoding='utf-8') as file: + return file.read().strip() + except FileNotFoundError: + print(f"Error: File '{file_path}' not found.") + sys.exit(1) + except IOError: + print(f"Error: Unable to read file '{file_path}'.") + sys.exit(1) diff --git a/versions/cli_args.py b/versions/cli_args.py index dd6533a..e9e401c 100644 --- a/versions/cli_args.py +++ b/versions/cli_args.py @@ -4,18 +4,7 @@ import pyperclip sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from library.TextConverter import TextConverter - - -def read_file(file_path): - try: - with open(file_path, 'r', encoding='utf-8') as file: - return file.read().strip() - except FileNotFoundError: - print(f"Error: File '{file_path}' not found.") - sys.exit(1) - except IOError: - print(f"Error: Unable to read file '{file_path}'.") - sys.exit(1) +from library.file_utils import read_file def main():