Tools to easily write exception handling.
Project description
erf
下の方に日本語の説明があります
Overview
- Tools to easily write exception handling.
Usage
import erf
# case 1
@erf({"KeyError": "no_data"})
def getter(dic, key):
return dic[key]
dic = {"fuga": 23}
print(getter(dic, "hoge")) # -> "no_data"
# case 2
@erf(KeyError = "no_data")
def getter2(dic, key):
return dic[key]
dic = {"fuga": 23}
print(getter2(dic, "hoge")) # -> "no_data"
Advanced Usage
import erf
import math
# case 3
@erf({"division by zero": math.inf}, error_str_func = "str")
def inf_div(a, b):
return a / b
print(inf_div(3, 0)) # -> inf
# case 4-1
print(erf({"division by zero": math.inf})(lambda a, b: a/b)(3, 0))
# case 4-2
func = lambda a, b: a/b
catcher_dic = {"division by zero": math.inf}
print(erf(catcher_dic)(func)(3, 0))
# case 4-3
func = lambda a, b: a/b
print(erf(div = math.inf)(func)(3, 0))
# case 4-4
func = lambda a, b: a/b
safe_div = erf(div = math.inf)(func)
print(safe_div(3, 0))
# case 5
# Example of recursion: recursion must be wrapped
def rec_func(arg):
if type(arg) == type(23):
return str(arg)
elif type(arg) == type([]):
return [rec_func(e) for e in arg]
else:
raise Exception("invalid type.")
@erf(recur = ...)
def wrapper(arg):
return rec_func(arg)
print(wrapper([1,[2,3]]))
obj = [1,2]
obj[1] = obj
print(wrapper(obj))
概略
- 例外処理を簡単に書けるツール
利用例
import erf
# 「KeyError」が含まれる例外時に「"no_data"」を返却するようにする
@erf({"KeyError": "no_data"})
def getter(dic, key):
return dic[key]
dic = {"fuga": 23}
print(getter(dic, "hoge")) # -> "no_data"
# 同様のコードの略記法
@erf(KeyError = "no_data")
def getter2(dic, key):
return dic[key]
dic = {"fuga": 23}
print(getter2(dic, "hoge")) # -> "no_data"
発展的な利用例
import erf
import math
# 例外オブジェクトを文字列化する方法を指定
@erf({"division by zero": math.inf}, error_str_func = "str")
def inf_div(a, b):
return a / b
print(inf_div(3, 0)) # -> inf
# 無名関数の例外処理
print(erf({"division by zero": math.inf})(lambda a, b: a/b)(3, 0))
# 無名関数の例外処理 その2
func = lambda a, b: a/b
catcher_dic = {"division by zero": math.inf}
print(erf(catcher_dic)(func)(3, 0))
# 無名関数の例外処理 その3
func = lambda a, b: a/b
print(erf(div = math.inf)(func)(3, 0))
# 無名関数の例外処理 その4
func = lambda a, b: a/b
safe_div = erf(div = math.inf)(func)
print(safe_div(3, 0))
# 再帰の例: 再帰の場合は1重ラップする必要がある
def rec_func(arg):
if type(arg) == type(23):
return str(arg)
elif type(arg) == type([]):
return [rec_func(e) for e in arg]
else:
raise Exception("invalid type.")
@erf(recur = ...)
def wrapper(arg):
return rec_func(arg)
print(wrapper([1,[2,3]]))
obj = [1,2]
obj[1] = obj
print(wrapper(obj))
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
erf-0.1.3.tar.gz
(3.6 kB
view details)
Built Distribution
erf-0.1.3-py3-none-any.whl
(4.1 kB
view details)
File details
Details for the file erf-0.1.3.tar.gz
.
File metadata
- Download URL: erf-0.1.3.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a51811d69df604ae4e2ba0570238f52dccd953af89ee86ef9cddb50e85db5131 |
|
MD5 | 6c241982962bee1175b1673dfe11a8e5 |
|
BLAKE2b-256 | cea9603aadcd3fd787f40aa13a0b7f46e0e8924d19a93b441a48095c84af72ba |
File details
Details for the file erf-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: erf-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03e2a7562496f05d731d1c25184708836eee95954f6f947d0e0ef9ea16c67884 |
|
MD5 | f95505ceeac0538c20b18f1906550c6a |
|
BLAKE2b-256 | 11d96bacc740d3ecbe2c66a9f353740af90f5f3d5c11ac287e6cfd4d5375af20 |