-
Notifications
You must be signed in to change notification settings - Fork 0
/
pythontemplate.py
90 lines (73 loc) · 3.31 KB
/
pythontemplate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# vim:fileencoding=utf-8:expandtab:tabstop=8:shiftwidth=2:softtabstop=2:textwidth=120:noautoindent:nocindent:nosmartindent
##
## ~ $ PYTHONOPTIMIZE=0 python3 -i <your-script.py> ## when you are developing
## ~ $ python3 -I -E -O <your-script.py> ## when you want to deploy...this removes the if __debug__ and assert checks!
## ~ $ python3 -I -E -OO <your-script.py> ## even more optimization... removes docstrings!
import re, os, sys, sqlite3, datetime, logging, typing, functools, pathlib, inspect, dataclasses as dc,pytest
import numpy as np, pandas as pd, pyarrow as pa, duckdb as ddb
import matplotlib.pyplot as plt,seaborn as sns
import hypothesis as hy, hypothesis.strategies as st
ddb.execute("SET GLOBAL pandas_analyze_sample = 100_000")
if __debug__: import pdb
T = typing.TypeVar("T")
P = typing.ParamSpec("P")
logging.basicConfig(filename=f"log-{str(datetime.datetime.now().date())}.log", level=logging.DEBUG, encoding="utf-8", format="{asctime} - {levelname} - {message!r}", style="{", datefmt="%Y.%m.%dT%H:%M:%S%z")
def grid(axis="both"):
"Show the grid...kinda like graph paper"
plt.minorticks_on()
plt.grid(which="major", linestyle="-", alpha=0.75, axis=axis)
plt.grid(which="minor", linestyle=":", alpha=0.5, axis=axis)
def log(func: typing.Callable[P,T]) -> typing.Callable[P,T]:
@functools.wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
signature = ", ".join([f"{a!r}" for a in args] + [f"{k}={v!r}" for k,v in kwargs.items()])
logger = logging.getLogger()
logger.debug(f"function {func.__name__}{inspect.signature(func)} called ==> {func.__name__}({signature})")
try:
result = func(*args, **kwargs)
return result
except Exception as e:
logger.exception(f"Exception raised in {func.__name__}. exception: {e!s}")
raise e
return wrapper
@log
def f1(**kwargs: typing.Dict[typing.Any,typing.Any]) -> typing.Any:
print("key\tvalue")
print("---\t-----")
for k,v in kwargs.items():
print(f"{k=}",f"{v=}",sep="\t")
Num = typing.TypeVar('Num', int, float, complex)
@log
def sum(a:Num, b:Num) -> Num: return a+b
@log
def prod(a:Num, b:Num) -> Num: return a*b
@log
@hy.settings(max_examples=500, verbosity=hy.Verbosity.verbose)
@hy.given(a=st.one_of(st.integers(), st.floats()), b=st.one_of(st.integers(), st.floats()))
def test_sum[T: int|float](a:T, b:T) -> None:
""" This ought to fail for quite a few cases!!!
Included here just to demonstrate how to use hypothesis for PBT.
"""
assert a + b == sum(a,b)
assert sum(a,b) >= a
assert sum(a,b) >= b
@log
@pytest.mark.parametrize(("test_input","expected"),[("3+5",8),("2+4",6),pytest.param("6*9",42,marks=pytest.mark.xfail)])
def test_eval(test_input, expected): ## the args have to match args listed in the pytest.mark.parametrize tuple!
assert eval(test_input) == expected
@log
def main():
logging.info("In main")
print("hello world!")
f1(fname="vijay",lname="lulla",addr="mythical city",salary=5.2)
f1(x=[1,2,3,4,5],y=[1,2,3,4,5])
million, scaler = 1_000_000, 0.89
if __debug__: pdb.set_trace()
print(f"${million:_d} US = \u20ac{million*scaler:4_.2f}")
print(f"{sum(2,18.5)=}\n{prod(2,18.5)=}")
today = datetime.datetime.now()
print(f"{today:%B %d, %Y}\n"
f"{today=:%B %d, %Y}\n"
f"{ today = :%B %d, %Y}" ) ## NOTE: whitespace preserved!
if __name__ == "__main__":
main()