简单的、可读的、经过充分测试的、保留签名的python装饰器,以确保您的方法遵守其约定。

pytsa的Python项目详细描述


https://img.shields.io/pypi/pyversions/pytsa.svghttp://img.shields.io/badge/license-MIT-green.svghttp://img.shields.io/travis/thimovss/pytsa.svghttps://coveralls.io/repos/github/thimovss/pytsa/badge.svg

python严格参数

简单的、可读的、经过充分测试的、保留签名的python装饰器,以确保您的方法遵守其约定。 通过使方法的契约更加严格,去掉测试所有那些边缘情况以获取传递的参数!

安装

如何使用

pytsa为大多数python类型提供装饰符。
这些修饰符允许您验证传递给 方法的类型和/或遵循指定的规则。
decorator名称指定预期的类型,例如sau int 如果需要整数。
decorator的第一个参数是 decorator适用于。
第一个参数之后的参数指定此参数具有的规则 如果有的话,请跟随。
@sa_TYPE(PARAM_NAME, RULE1=RULE1_VAL, RULE2=RULE2_VAL)
例如,
@sa_string('a', starts_with='a', ends_with='c', is_lower=True, contains='1')
def foo(a):
    ...
@sa_path('b', is_dir=True, is_abs=True)
def bar(b):
    ...
@sa_list('c', len=8, type=float)
def foo(a, b, c):
    ...
@sa_int('d', gt=-4, lte=4, non_zero=True, mod=2)
def bar(d):
    ...

演示

想要一个其他规则的演示吗?检查测试目录,它有一个 为每一条规则树立榜样!

from pytsa import sa_int

@sa_int('val', gt=0, lte=10)
def assign_score(val)
    ""assign an integer score higher than 0, up to 10""
    print('set score to {val}')
    score = val

assign_score(5)
> set score to 5

assign_score(0)
> Error: int argument val with value 0 was not greater than 0

assign_score('abc')
> Error: int argument val with value 'abc' was of type string, not of type 'int'

assign_score(None)
> Error: int argument val was None

assign_score(35)
> Error: int argument val with value 35 was not less than, or equal to 10

规则

要更详细地描述规则的行为,请 一定要看看它的测试用例!不知道@sau list的len是如何处理的 没有类型?见‘test/test_sa_list.py’ test_rule_len()

所有规则
RuleDescription
allow_none(bool)By default, no rule allows a value of None. This can be circumvented with the rule ``allow_none``(bool), which is available for every rule. important!: If the value provided is None, all other checks will not be executed!

字符串@sa_string

RuleDescription
not_empty(bool)ensure the argument is not an empty string.
not_blank(bool)ensure the argument is not an empty string, or contains only whitespace characters. According to ^{tt4}$
ends_with(string)ensure the argument ends with the rule value. According to ^{tt5}$
starts_with(string)ensure the argument starts with the rule value. According to ^{tt6}$
contains(string)ensure the argument contains the rule value. According to ^{tt7}$
is_lower(bool)ensure all non-whitespace characters in the argument are lowercase.
is_upper(bool)ensure all non-whitespace characters in the argument are uppercase.
regex(string)ensure the argument matches the regex. According to ^{tt8}$

数字(浮点数,整数)@sa_number@sa_float@sa_int

规则@sa_数字同时接受整数值和浮数值。
规则@sa_number、@sa_int和@sa_float共享相同的规则集。
规则@sau int和@sau float只接受它们的参数 各自的类型,但接受浮点数和整数作为值 他们的规则。
RuleDescription
non_zero(bool)ensure the argument does not equal 0 or 0.0.
gt(number)ensure the argument is greater than the rule value.
gte(number)ensure the argument is greater than, or equal to the rule value.
lt(number)ensure the argument is lesser than the rule value.
lte(number)ensure the argument is lesser than, or equal to the rule value.
mod(number)ensure the argument is a multiple of the rule value.

布尔型@sa_bool

bool没有可用的规则。

列表@sa_list

!警告:某些测试(如type)将强制执行整个列表 每次调用该方法时,都可能导致性能问题。

RuleDescription
type(type)ensure all the values in the list are of the given type. (Tip: make sure not to call as ``type=type(int)``, as this will check if everything is of type type, instead of type int)
len(int)ensure the argument has the given length. None is counted in the length.
not_empty(bool)ensure the argument is not an empty list

路径@sa_path

RuleDescription
exists(bool)ensure that the argument is an existing path. According to ^{tt15}$
is_dir(bool)ensure the argument is an existing path to a directory. According to ^{tt16}$
is_file(bool)ensure the argument is an existing path to a file. According to ^{tt17}$
is_abs(bool)ensure the argument is an absolute path. According to ^{tt18}$
can_owner_read(bool)ensure the owner has read permission.
can_group_read(bool)ensure the group has read permission.
can_others_read(bool)ensure the others has read permission.
can_owner_write(bool)ensure the owner has write permission.
can_group_write(bool)ensure the group has write permission.
can_others_write(bool)ensure the others has write permission.
can_owner_execute(bool)ensure the owner has execute permission.
can_group_execute(bool)ensure the group has execute permission.
can_others_execute(bool)ensure the others has execute permission.

产量

您可能希望禁用对 生产部署是性能原因。
可以通过设置环境变量禁用pytsa “Pytsa_disabled”设置为“true”

许可证

根据MIT License

授权

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
JavaGWT:何时使用Lazydemelement?   Java中跟踪消失线程的多线程处理   java Springboot未能配置数据源:“url”,但我没有使用数据库   java为按钮生成随机位置   math Java:包含二项式系数计算的表达式   java通过AsyncTask传递参数   从路径错误创建java文件   高流量网站的性能播放框架、Java、Apache、PostgreSQL、JPA和Hibernate   java将4D矢量转换为长矢量   arraylist Java循环在没有任何错误的情况下终止   java正在制作一个计算器应用程序,希望在第二个片段中更新历史,但无法完成   java将信息从IntentService发送到Activity   java如何在游戏中插入大量实体!工作   javascript如何在ScriptEngineforJava中从数学中获得准确的结果?