ezdecs: Some decorators that simplify Python code
Wanna time your code execution easily? Don't wanna spend too much time on input validation? Maybe ezdecs can help!
timer(): Timing execution
By adding @timer before a function, it times the function when the function is called.
from ezdecs import *
@timer
def f():
print('Hello, world!')
return 3
print(f())
'''
Example output:
Hello, world!
(3, 6.939999999966417e-05)
'''
noexcept() and cexcept(): Silence exceptions
Using the decorator @noexcept, any exception raised is silenced.
from ezdecs import *
@noexcept
def divide(a,b):
return a/b
print(divide(9,3)) # 3.0
print(divide(1,0)) # None
If you still wanna know what exception is raised, use @cexcept.
from ezdecs import *
@cexcept
def divide(a,b):
return a/b
print(divide(9,3)) # (3.0, None)
print(divide(1,0)) # (None, ZeroDivisionError('division by zero'))
tnexcept(): Retry when an exception occurs
By using @tnexcept, the function is repeated until no exception occurs. This is especially useful for input validation
and retrying.
For example, instead of writing:
def read_number():
while 1:
try:
result=int(input('Please enter a number: '))
except:
pass
else:
return result
print(read_number())
All you have to write is:
from ezdecs import *
@tnexcept
def read_number():
return int(input('Please enter a number: '))
print(read_number())
'''
Example interaction:
Please enter a number: a
Please enter a number: 3.14
Please enter a number: 3
3
'''
The keyword argument handler allows printing custom error messages when encountering invalid input by raising AssertionError.
from ezdecs import *
@tnexcept(handler=print)
def enter_password():
psw=input('Please enter password: ')
assert psw=="123","Incorrect password."
enter_password()
print('Correct!')
'''
Example interaction:
Please enter password: a
Incorrect password.
Please enter password: 1234
Incorrect password.
Please enter password: 123
Correct!
'''
For documentation on other decorators see docstring.
Aliases
In order to shorten code, every decorator has a two-character alias:
TM=timer
NE=noexcept
CE=cexcept
BL=block
NL=nolog
TE=tnexcept
RP=repeat
FE=foreach
Problems with recursion
Sometimes using these decorators on recursive functions may cause problems.
The following is an attempt to time a recursive Fibonacci function:
from ezdecs import *
@TM
def f(n):
if n<2:
return n
return f(n-1)+f(n-2)
print(f(5))
Instead of the time and result, a complex nested tuple is returned. That's because after being wrapped by the decorator,
f now returns a tuple instead of an integer.
To make it work correctly, the __wrapped__ attribute can be used to retrieve the unwrapped function:
from ezdecs import *
@TM
def f(n):
if n<2:
return n
fw=f.__wrapped__
return fw(n-1)+fw(n-2)
print(f(5))
Metadata
Release files for ezdecs 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ezdecs-1.0.0.tar.gz | 3.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ezdecs-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.3 kB
Release files / ezdecs-1.0.0.tar.gz
| Download URL | ezdecs-1.0.0.tar.gz |
|---|---|
| Size | 3.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
09377b10ec86c59e402e03a9105f6c339e76a5a1bc05265e943ffb00f6fa596c
|
|
BLAKE2b-256 checksum How to use checksums |
7691dbd64e66471469b6fb677c13eba72b0d01561f0892703495762f4a1611b1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.7.0
|
Release files / ezdecs-1.0.0-py3-none-any.whl
| Download URL | ezdecs-1.0.0-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fa2d03de8a5a53868ebed6e79c85af85ded60643c61a227f3b65e763713c6d2f
|
|
BLAKE2b-256 checksum How to use checksums |
09f1fbc6bb6d053c2e746583c0c309d3522c7c9e94290f8f7e110c7bc4ebc5e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.7.0
|