-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
István Bozsó
committed
Feb 9, 2020
1 parent
1e55e63
commit 5e878ee
Showing
2 changed files
with
42 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,11 @@ | ||
__all__ = ( | ||
"enforcer", "type_enforce", | ||
"Enforcer", | ||
) | ||
|
||
class Enforcer(object): | ||
__slots__ = ("exc",) | ||
|
||
def __init__(self, exc): | ||
self.exc = exc | ||
|
||
def __call__(self, cond, *args, **kwargs): | ||
def enforce(self, cond, *args, **kwargs): | ||
if not cond: | ||
raise self.exc(*args, **kwargs) | ||
raise self(*args, **kwargs) | ||
|
||
|
||
@ft.lru_cache() | ||
def enforcer(exc): | ||
return Enforcer(exc) | ||
|
||
type_enforce = enforcer(TypeError) | ||
class TypeEnforce(TypeError, Enforcer): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters