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

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如何从ImageIO中排除特定的TIFF读取器?   JavaJMockit和passbyreference。我们中一定有一个人错了(可能是我!)   java Android camera2放弃了牛轧糖的表面,但在棉花糖上工作   java按字符串中出现的顺序对字符数组进行排序   如何获取Groovy生成的java源代码   java无法使用AutoIT和Selenium Webdriver在所需位置/文件夹保存图像   java为什么我的冒泡排序代码会打印出这些奇怪的东西?   java JAXB:typesafeEnumMemberName=“generateName”是否可自定义?   Java编程输入:今天是星期天输出:星期天是今天   java不理解首个OOAD书的吉他示例   java如何从JformattedTextfield检索货币格式值   java可以从相同的源代码生成功能不同的可执行文件吗?