开发人员友好的范围检查和用户友好的错误消息

Rangeforce的Python项目详细描述


rangeforce:开发人员友好的范围检查,并显示用户友好的错误消息

必须编写相同的python代码来验证 用户输入?厌倦了不可理解的错误信息,如 “非法值”

rangeforce是一个非常简单的模块,提供多个函数检查 值的范围,包括整数或浮点数,或结构的长度 比如字符串或列表。它在一行代码中这样做,同时提供 可以直接显示给用户的可理解的错误消息。

defclassic_approach():value=int(input('How many hours do you sleep per day? '))ifvalue<0:raiseValueError('Hours of sleep must be positive')elifvalue>24:raiseValueError('Hours of sleep must be max 24')else:returnvalue# Same code, but simplified using Rangeforceimportrangeforceasrfdefwith_rangeforce():value=int(input('How many hours do you sleep per day? '))returnrf.limited(value,0,24,'Hours of sleep')# Magically in 1 line

功能

  • 验证值是否在[min,max]间隔内
    • 包括最小为-无穷大和最大为+无穷大的情况
    • 类型的可选强制(例如,值必须是浮点)
  • 用于验证值是否为无符号/有符号整数的函数 它适合8/16/32/64位,因为C数据类型
  • 用于验证正整数、负整数、非正整数、非负整数的函数
  • 在[min,max]间隔内验证对象的长度 或精确长度
  • 将值剪裁(限制)在一个范围内的实用函数
  • 为错误消息验证的变量的可自定义名称

安装

pip install Rangeforce

或者只在项目中包含rangeforce.py文件(复制粘贴)。

示例用法

importrangeforceasrfvalue=rf.limited(8000,20,5000,dtype=int)# If successful, value will held 8000, otherwise (as it would happen in this# example) raises a rangeforce.RangeError with a useful message:# "Value must be in range [20, 5000]. 8000 found instead."# Can be also shown directly to the user:try:value=rf.limited(8000,20,5000,dtype=int)exceptrf.RangeErroraserror:print(str(error))# A missing bound (min or max) means unboundedvalue=rf.limited(2000.0,None,5000.0,dtype=float)# Value must be <= 5000 but can be as small as it gets, including negative# Especially useful in setters to validate the input in one lineclassFullHdPicturesPixel(object):def__init__(self,x,y):self._x=Noneself._y=Noneself.x=xself.y=y@propertydefx(self):returnself._x@x.setterdefx(self,new_x):self._x=rf.limited(new_x,0,1920,'The X pixel coordinate',int)@propertydefy(self):returnself._y@y.setterdefy(self,new_y):self._y=rf.limited(new_y,0,1080,'The Y pixel coordinate',int)pixel=FullHdPicturesPixel(10,2000)# This raises a RangeError with the message:# "The Y pixel coordinate must be in range [0, 1080]. 2000 found instead."# Operating with C-like data structures and binary data?# These functions might come in handy!value=rf.int8(20)value=rf.int16(20)value=rf.int32(20)value=rf.int64(20)value=rf.uint8(20)value=rf.uint16(20)value=rf.uint32(20)value=rf.uint64(20)# Customize the name of the variable:distance=rf.uint8(-3,'Distance')# This raises a RangeError with the message:# "Distance must be in range [0, 255]. -3 found instead."# To check the length range of anything (e.g. a list or bytes):value=rf.limited_len([1,2,3],2,7)# 'value' will hold [1, 2, 3]# If you need an exact length, do it like thispair=rf.exact_len([10,20,30],2,name='pair of values')# This raises a RangeError with the message:# "Length of pair of values must be exactly 2. 3 found instead.

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

推荐PyPI第三方库


热门话题
如何使用外部java程序在minecraft中移动minecraft角色的相机   java输出文本文件中的变量   java LazyLoadingException在我尝试从多通关系获取对象时出现   java json rest API的错误:ClassCastException:org。json。无法将JSONObject强制转换为组织。json。杰索纳雷   java BigInteger。C中的intValue()等价物#   java大写所有字符,但不包括带引号字符串中的字符   java获取特殊字符   javascript为什么Selenium中的所有getX()调用都需要这么长时间?   rabbitmq rabbitmq java客户端并行消费   如何使用selenium Java在popover窗口中提取文本   对象在java中构造一类对象   java Room数据库未实现   json JSONObject可以使用java保存大的格式化双值吗?   有时限的旅行推销员   java HttpsURLConnection openConnection查询   java无法使用Spring@Entity注释创建MySQL表   lambda Java 8仅映射到值类型集合   java提供OSGi服务而不实现接口   java单个对象重写对象数组,不确定原因