Skip to main content

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))

Metadata

Release files for erf 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for erf 0.1.3
File Size Uploaded Provenance Actions
erf-0.1.3.tar.gz 3.6 kB Without provenance Details for erf-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for erf 0.1.3
File Interpreter ABI Platform Actions
erf-0.1.3-py3-none-any.whl Python 3 none any Details for erf-0.1.3-py3-none-any.whl

Total release size: 7.7 kB

Release files / erf-0.1.3.tar.gz

Download URL erf-0.1.3.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
a51811d69df604ae4e2ba0570238f52dccd953af89ee86ef9cddb50e85db5131
BLAKE2b-256 checksum
How to use checksums
cea9603aadcd3fd787f40aa13a0b7f46e0e8924d19a93b441a48095c84af72ba
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / erf-0.1.3-py3-none-any.whl

Download URL erf-0.1.3-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
03e2a7562496f05d731d1c25184708836eee95954f6f947d0e0ef9ea16c67884
BLAKE2b-256 checksum
How to use checksums
11d96bacc740d3ecbe2c66a9f353740af90f5f3d5c11ac287e6cfd4d5375af20
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for erf 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page