Skip to content

Instantly share code, notes, and snippets.

@mdmitry1
Last active February 21, 2024 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdmitry1/9ae3fe12b52d3db59e99896e8d2e14f9 to your computer and use it in GitHub Desktop.
Save mdmitry1/9ae3fe12b52d3db59e99896e8d2e14f9 to your computer and use it in GitHub Desktop.
Decorator with parameters and color output
#!/usr/bin/python3.11
from os import environ
from sys import argv
def sortDecorator(x):
def decorator(func):
def wrapper(*args, **kwargs): return func(*args, **kwargs)
return wrapper
return decorator
@sortDecorator(str)
def decorated_sort(*args,**kwargs):
if kwargs.get('ind') == None: return args[0].sort()
else: return args[0].sort(key=lambda elem : elem[1])
values = [[var,environ[var]] for var in [*environ]]
decorated_sort(values) if len(argv) < 2 else decorated_sort(values, ind='dummy')
for val in values: print(val[0],"=",val[1])
#!/usr/bin/python3.11
from rich import print as rprint
from rich.console import Console
from mpmath import mp, ctx_mp_python as ctx
blue_console = Console(style="blue on white")
green_console = Console(style="color(22) on white", color_system="256")
purple_console = Console(style="purple on white")
console_256=Console(color_system="256", style="dark_blue on CornSilk1")
def decodecorator(dataType, console):
def decorator(fun):
def wrapper(*args, **kwargs):
console.print('Calling ' + fun.__name__)
if all([type(arg) == dataType for arg in args]):
initial_value = kwargs.get('init')
if(kwargs.get('comment') != None ):
console.print(" with initial value =" + str("[red] ") + str(initial_value))
result = fun(*args, **kwargs)
console.print('Finished')
console.print()
return result
else:
return "[yellow on red]Invalid input\n"
return wrapper
return decorator
@decodecorator(str, blue_console)
def stringJoin(*args):
st = ''
for i in args: st += i
return st
@decodecorator(int, green_console)
def summation(*args,**kwargs):
summ = 0
initial_value = kwargs.get('init')
if(kwargs.get('init') != None ):
summ = initial_value
for arg in args: summ += arg
return summ
@decodecorator(float, purple_console)
def multiplication(*args):
prod = 1
for arg in args: prod *= arg
return prod
@decodecorator(ctx.mpf, console_256)
def integration(*args,**kwargs):
dps = kwargs.get('dps')
mp.dps = dps if dps != None else 100
return mp.quad(lambda x: mp.exp(-x**2), [-mp.inf, mp.inf]) ** 2
rprint(stringJoin('[magenta italic] "Knowledge ', 'and ', "human ", 'power ', "are ", 'synonomous"'))
print()
rprint(summation(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
print()
rprint(multiplication(2.0,3.0,4.0,5.0,6.0))
print()
rprint(summation(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,init=5,comment=" with initial value"))
print()
rprint("[black italic]Example with invalid arguments")
rprint(summation(1, 2, str))
print()
rprint("[magenta on yellow]Example with 256 colors")
console_256.print("[dark_green on white]" + str(integration(dps=50)))
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment