Some decorators that simplify Python code
Project description
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))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ezdecs-1.0.0.tar.gz.
File metadata
- Download URL: ezdecs-1.0.0.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09377b10ec86c59e402e03a9105f6c339e76a5a1bc05265e943ffb00f6fa596c
|
|
| MD5 |
b6f99a8e29a76071ba79e9e6b61cb08c
|
|
| BLAKE2b-256 |
7691dbd64e66471469b6fb677c13eba72b0d01561f0892703495762f4a1611b1
|
File details
Details for the file ezdecs-1.0.0-py3-none-any.whl.
File metadata
- Download URL: ezdecs-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa2d03de8a5a53868ebed6e79c85af85ded60643c61a227f3b65e763713c6d2f
|
|
| MD5 |
13d30171354d86b05eecfaeb871e1f64
|
|
| BLAKE2b-256 |
09f1fbc6bb6d053c2e746583c0c309d3522c7c9e94290f8f7e110c7bc4ebc5e4
|