python函数的小集合
pyutillib的Python项目详细描述
python实用程序库是几个类别的函数的一个小集合。
日期函数
要使用日期功能,您需要:
import pyutillib.date_utils
使用日期字符串
为了在字符串中的日期和日期时间之间有明确的关系 格式,只有有限的格式可用。违约通常是 很好。
用法示例:
>>> import pyutillib.date_utils as du >>> du.datestr2date('20001231') datetime.date(2000, 12, 31) >>> du.datestr2date('12/31/2000') datetime.date(2000, 12, 31) >>> du.datestr2date('31-12-2000') datetime.date(2000, 12, 31) >>> import datetime >>> d = datetime.date(2000, 12, 31) >>> du.date2datestr(d) '20001231' >>> du.date2datestr(d, 'd-m-yy') '31-12-00' >>> du.date2datestr(d, 'd-m-yyy') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "pyutillib/date_utils.py", line 128, in date2datestr raise ValueError('Invalid format string, year must have 2 or 4 digits') ValueError: Invalid format string, year must have 2 or 4 digits >>> du.date2datestr(d, 'mm/dd/yyyy') '12/31/2000' >>> du.date2datestr(d, 'yymmdd') '001231'
工作日
这些都是简单的事情,但很难记住,所以只是一些方便 功能:
>>> import pyutillib.date_utils as du >>> import datetime as dt >>> du.is_weekday(dt.date(2013,4,16)) True >>> du.is_weekday(dt.date(2013,4,14)) False >>> du.is_weekend(dt.date(2013,4,14)) True >>> du.is_weekend(dt.date(2013,4,16)) False >>> du.next_weekday(dt.date(2013,4,16)) datetime.date(2013, 4, 17) >>> du.next_weekday(dt.date(2013,4,12)) datetime.date(2013, 4, 15) >>> du.next_weekday(dt.date(2013,4,13)) datetime.date(2013, 4, 15) >>> du.previous_weekday(dt.date(2013,4,16)) datetime.date(2013, 4, 15) >>> du.previous_weekday(dt.date(2013,4,15)) datetime.date(2013, 4, 12) >>> du.previous_weekday(dt.date(2013,4,14)) datetime.date(2013, 4, 12)
工作年限
简单,但考虑闰年:
>>> import pyutillib.date_utils as du >>> import datetime as dt >>> du.last_year(dt.date(2000,2,29)) datetime.date(1999, 2, 28) >>> du.last_year(dt.date(2001,2,29)) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: day is out of range for month >>> du.last_year(dt.date(2001,2,28)) datetime.date(2000, 2, 28) >>> du.last_year(dt.date(2001,3,1)) datetime.date(2000, 3, 1)
日期列表类
这个类是标准python列表的子类,所以所有正常的列表功能 有空。不同之处在于有一些额外的方法 .index方法的工作方式不同。
实例化:日期列表按预期工作:
>>> import pyutillib.date_utils as du >>> import datetime as dt >>> dates = [dt.date(2012, 1, d) for d in range(1, 32)] >>> dl = du.DateList(dates) >>> dl[0] datetime.date(2012, 1, 1) >>> dl[-1] datetime.date(2012, 1, 31)
index返回日期的索引,或者(如果日期不在列表中)。 输入日期之前最近日期的索引:
>>> dl.index(dt.date(2012,1,2)) 1 >>> dl.index(dt.date(2012,1,30)) 29 >>> dates2 = [dt.date(2012, 1, d) for d in range(1, 32, 4)] >>> dl2 = du.DateList(dates2) >>> for d in dl2: print d 2012-01-01 2012-01-05 2012-01-09 2012-01-13 2012-01-17 2012-01-21 2012-01-25 2012-01-29 >>> dl2.index(dt.date(2012,1,8)) 1 >>> dl2.index(dt.date(2012,1,9)) 2 >>> dl2.index(dt.date(2012,1,10)) 2
在返回输入日期之前,或者(如果日期不在列表中)。 输入日期之前的最近日期:
>>> dl2.on_or_before(dt.date(2012,1,5)) datetime.date(2012, 1, 5) >>> dl2.on_or_before(dt.date(2012,1,4)) datetime.date(2012, 1, 1) >>> dl2.on_or_before(dt.date(2012,1,6)) datetime.date(2012, 1, 5)
delta返回列表中两个日期之间的天数:
>>> dl.delta(dt.date(2012,1,10), dt.date(2012,1,20)) 10 >>> dl2.delta(dt.date(2012,1,10), dt.date(2012,1,20)) 2
offset返回输入后n天(如果n天,则在n天之前<;0)的日期 日期,请注意这些不是日历日,而是列表中的日期:
>>> dl.offset(dt.date(2012,1,10),3) datetime.date(2012, 1, 13) >>> dl2.offset(dt.date(2012,1,10),3) datetime.date(2012, 1, 21)
subset返回两个指定日期之间的日期列表,仅返回 包括在原始列表中:
>>> for d in dl.subset(dt.date(2012,1,10), dt.date(2012,1,20)): print d 2012-01-10 2012-01-11 2012-01-12 2012-01-13 2012-01-14 2012-01-15 2012-01-16 2012-01-17 2012-01-18 2012-01-19 2012-01-20 >>> for d in dl2.subset(dt.date(2012,1,10), dt.date(2012,1,20)): print d 2012-01-13 2012-01-17
使用时间字符串
为了在字符串中的时间和 datetime.time格式,只有有限的格式可用。这个 违约通常是足够好的。
用法示例:
>>> from pyutillib import date_utils as du >>> du.timestr2time('123456') datetime.time(12, 34, 56) >>> du.timestr2time('12:34:56') datetime.time(12, 34, 56) >>> du.timestr2time('12:34') datetime.time(12, 34) >>> du.timestr2time('01:23') datetime.time(1, 23) >>> du.timestr2time('1:23') datetime.time(1, 23) >>> import datetime >>> t = datetime.time(23,59,59) >>> du.time2timestr(t) hhmmss 23:59:59 '235959' >>> du.time2timestr(t, 'hh:mm') hh:mm 23:59:59 '23:59' >>> du.time2timestr(t, 'hh:mm:ss') hh:mm:ss 23:59:59 '23:59:59' >>> t = datetime.time(5,59,59) >>> du.time2timestr(t) hhmmss 05:59:59 '055959' >>> du.time2timestr(t, 'hh:mm:ss') hh:mm:ss 05:59:59 '05:59:59' >>> du.time2timestr(t, 'h:mm:ss') h:mm:ss 05:59:59 '5:59:59' >>> du.time2timestr(t, 'h:mm') h:mm 05:59:59 '5:59' >>> du.time2timestr(t, 'hmmss') hmmss 05:59:59 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "pyutillib/date_utils.py", line 383, in time2timestr else: ValueError: Invalid character in format string. The following time formats are valid: hhmmss hh:mm:ss h:mm:ss hh:mm h:mm Where in the latter 2 formats hh has d digits which may include a leading zero and h may have 1 or 2 digits and no leading zero. h/hh is always in 24 hour clock.
数学函数
要使用数学函数,您需要:
import pyutillib.math_utils
安全区
有时你需要把2个数除以,而不用担心被0除。 div函数始终返回浮点值,即使有iteger结果。 同样,0/0的结果在数学上是不正确的,但实际上通常是可以的。
一些示例:
>>> from pyutillib import math_utils as mu >>> mu.div(1,0) inf >>> mu.div(1,1) 1.0 >>> mu.div(0,0) 0.0
评估条件
这个函数可以用来评估任意复杂的条件。这个 条件必须采用元组格式:(argument1, operator, argument2)
其中,如果运算符是逻辑运算符,则参数1和2都可以是布尔值 ('and'、'or')或参数1和2是python对象,如果operator是比较 运算符(“lt”、“le”、“eq”、“ne”、“ge”、“gt”)。在后一种情况下,两个论点 必须具有可比性,即具有相同类型。唯一的例外是float和 整数,它们可以互相比较。
每个求值的结果始终是布尔值,它们可以嵌套到 任意级别,用另一个元组替换布尔参数,例如:
>>> from pyutillib import math_utils as mu >>> mu.eval_conditions(((6, 'gt', 5.7), 'and', True)) True
如果在 听写:
>>> arg_dict = {'a': 11, 'b': 0.24} >>> condition = ('a', 'eq', 'b') >>> mu.eval_conditions(condition, arg_dict) False
字符串函数
要使用字符串函数,您需要:
import pyutillib.string_utils
生成随机字符串
此函数生成由字母组成的指定长度的随机字符串 还有数字。可以指定自定义字符集来限制(或扩展) 收藏:
>>> from pyutillib import string_utils as su >>> su.random_string() '7xgVQZxd' >>> su.random_string(charset='ABC+-') '-+-A+CBA' >>> su.random_string(20, 'ABC+-') 'A+AB--BCB++CA-A++++C'
安全评估字符串
ast提供了一个更好(更安全)的替代方案,而不是使用eval。这个函数 只是该函数的包装,以避免出现异常:
>>> su.safe_eval('(2,3,4)') (2, 3, 4) >>> print su.safe_eval('import os; os.name') None
使用字符串格式的元组和dict
如果输入字符串没有所需的 格式。
从字符串中提取元组:
>>> print su.str2tuple('(1,2,3)') (1, 2, 3) >>> print su.str2tuple('[1,2,3]') None >>> print su.str2tuple('hallo') None
从字符串中提取dict:
>>> print su.str2dict('{1:2, 3:4}') {1: 2, 3: 4} >>> print su.str2dict(' {1:2, 3:4}') None
从字符串中的dict获取密钥。钥匙将被退回 字母顺序:
>>> print su.str2dict_keys('{"a":1, 2:"3", -1: 0}') [-1, 2, 'a'] >>> print su.str2dict_values('{"a":1, 2:"3", -1: 0}') [0, '3', 1]
将十进制字符串转换为int
此函数接受表示十进制数的字符串,并返回 整数。decimals参数允许您“移动小数点”,例如:
>>> from pyutillib import string_utils as su >>> su.decstr2int('123.456', 3) 123456 >>> su.decstr2int('123.456', 2) 12345 >>> su.decstr2int('123.456', 4) 1234560 >>> su.decstr2int('123', 4) 1230000 >>> su.decstr2int('123.456', -1) 12
从上面的示例中注意,输入字符串不需要包含 小数点和小数参数可以是负数(或0)。