Jun-27-2018, 04:03 AM
Hello! My first Python project is called python-utils. You can find the full project on GitHub.
Features
I am looking for constructive criticism, optimization suggestions, additions, or just ideas. All replies are appreciated. :)
AsyncUtils
Features
- Decorators - Dynamically alter the functionality of your functions.
- String Functions - Functions for creating or manipulating strings.
- List Functions - Functions for extracting, displaying, joining, or manipulating lists.
- Async Functions - Useful set of Async functions for multi-threading.
- Translator - Quickly translate text with multiple language options.
I am looking for constructive criticism, optimization suggestions, additions, or just ideas. All replies are appreciated. :)
AsyncUtils
# Executes the specified function asynchronously. # Example: ExecuteFunctionAsync(print, "Hello World", delay=5) class ExecuteFunctionAsync(threading.Thread): def __init__(self, func, *args, delay: int = 0): super(ExecuteFunctionAsync, self).__init__() self.func = func self.delay = delay self.args = args def run(self): if self.delay > 0: time.sleep(self.delay) self.func(*self.args)Decorators
# Time decorator. # Add above any function to print execution time. def time(func): import time @wraps(func) def wrapper(*args, **kwargs): before = time.time() result = func(*args, **kwargs) after = time.time() - before print("{} ran in: {} sec".format(func.__name__, after)) return result return wrapper # Asynchronous decorator. # Add above any function to execute async. def async(func): import threading @wraps(func) def wrapper(*args, **kwargs): thread = threading.Thread(target=func, args=args, kwargs=kwargs) thread.start() return func return wrapperLangUtils
from enum import Enum from bs4 import BeautifulSoup import requests # A translation function. # Example: translate("Hello, how are you!", Language.SPANISH) returns "¿Hola como estas?" def translate(text: str, language: Enum): url = 'https://www.google.com/search?q=' + text + '+in+' + language.name.lower() + '&oq=' + text\ + '+in+' + language.name.lower() + '&aqs=chrome.0.0l6.3047j1j7&sourceid=chrome&ie=UTF-8' src = requests.get(url) plain = src.text readable = BeautifulSoup(plain, "html.parser") results = [] for link in readable.findAll("span", {"class": "nobr"}): results.append(link.string) return results[1] class Language(Enum): SPANISH = 1 FRENCH = 2 GERMAN = 3 DANISH = 4 ARABIC = 5 CHINESE = 6 JAPANESE = 7 KOREAN = 8ListUtils
# Connects two lists. # Example: connect(['1', '2'], ['3', '4']) returns ['1', '2', '3', '4'] def connect(first: list, second: list): return first + second # Removes all empty values from a list. # Example: trim(['1', '2', '3', '']) returns ['1', '2', '3'] def trim(array: list): for i in range(array.__len__()): try: value = array[i] if value is '' or value is None: array.__delitem__(i) except IndexError: pass return array # Separates each value in a list with a symbol. # Example: join(['1', '2', '3'], ':') returns "1:2:3" def join(array: list, symbol: str): text = "" length = array.__len__() for i in range(length): if i == (length - 1): text += array[i] else: text += array[i] + symbol return text # Capitalize all string values in a list # Example: capitalize(['a', 'b', 'c']) returns ['A', 'B', 'C'] def capitalize(array: list, first: bool = False): if first: return list(map(str.capitalize, array)) else: return list(map(str.upper, array)) # Lowercase all string values in a list # Example: lowercase(['A', 'B', 'C']) returns ['a', 'b', 'c'] def lowercase(array: list, first: bool = False): if first: vs = [] for i in range(array.__len__()): vs.append(array[i][0].lower() + array[i][1:]) return vs else: return list(map(str.lower, array)) # Returns a new list containing values with the specified data type. # Example: extract(["Hello, World!", 1, 2, 3], int) returns [1, 2, 3] def extract(array: list, data: type): elements = [] for value in array: if type(value) == data: elements.append(value) return elements