python的区间算法
python-intervals的Python项目详细描述
python的区间算法
此库为Python2.7+和Python3.4+提供间隔算法。
- 功能
- 安装
- 文档和用法
- 间隔创建
- 算术运算
- 区间的界限
- 间隔转换
- 迭代和索引
- 比较运算符
- 将间隔导入和导出到字符串
- 将间隔导入和导出到python内置数据类型
- 贡献
- 许可证
- 更改日志
功能
- 支持任何(可比)对象的间隔。
- 封闭或开放、有限或(半)无限间隔。
- 支持原子间隔和间隔集。
- 自动简化间隔。
- 支持迭代、比较、转换、交集、并、补、差、容。
- 将间隔导入并导出到字符串和python内置数据类型。
安装
您可以使用pip
来安装它,就像往常一样:pip install python intervals
这将安装最新的可用版本。 可以从github上的master分支获取预发行版
为了方便起见,库包含在单个python文件中,因此可以很容易地集成到其他 不需要显式依赖项的项目(提示:不要这样做!)
文档和用法
间隔创建
假设这个库是使用iimport intervals导入的,那么可以使用
以下助手:
>>>I.open(1,2)(1,2)>>>I.closed(1,2)[1,2]>>>I.openclosed(1,2)(1,2]>>>I.closedopen(1,2)[1,2)>>>I.singleton(1)[1]>>>I.empty()()
使用此库创建的间隔是interval
实例。
interval
对象是表示单个间隔(例如[1,2]
)的原子间隔的析取,对应于atomicinterval
实例。
除非显式创建或检索原子间隔,否则只会公开interval
实例。
边界区间可以是任意值,只要它们是可比的:
>>>I.closed(1.2,2.4)[1.2,2.4]>>>I.closed('a','z')['a','z']>>>importdatetime>>>I.closed(datetime.date(2011,3,15),datetime.date(2013,10,10))[datetime.date(2011,3,15),datetime.date(2013,10,10)]
使用i.inf
和-i.inf
作为上界或下界,支持无限和半无限间隔。
这两个对象支持与任何其他对象进行比较。
当无穷大用作下限或上限时,相应的边界将自动转换为开放边界。
>>>I.inf>'a',I.inf>0,I.inf>True(True,True,True)>>>I.openclosed(-I.inf,0)(-inf,0]>>>I.closed(-I.inf,I.inf)# Automatically converted to an open interval(-inf,+inf)
空间隔始终解析为(i.inf,-i.inf)
,而不考虑提供的界限:
>>>I.empty()==I.open(I.inf,-I.inf)True>>>I.closed(4,3)==I.open(I.inf,-I.inf)True>>>I.openclosed('a','a')==I.open(I.inf,-I.inf)True
为了方便起见,间隔会自动简化:
>>>I.closed(0,2)|I.closed(2,4)[0,4]>>>I.closed(1,2)|I.closed(3,4)|I.closed(2,3)[1,4]>>>I.empty()|I.closed(0,1)[0,1]>>>I.closed(1,2)|I.closed(2,3)|I.closed(4,5)[1,3]|[4,5]
请注意,不支持离散间隔,例如,将 以下方法仅适用于 可以分别访问 类似地,可以使用 可以根据间隔的界限轻松检查某些间隔属性: 可以传递函数而不是值。如果函数是pa它是用当前对应的
值,除非相应的界限是无穷大,如果设置为 当 要在间隔上应用任意转换, 当与 可以迭代间隔来访问底层的atomicinterval对象,按其上下界排序。 可以使用经典的 此外,使用例如 类似地,如果 间隔也可以与单个值进行比较。如果 注意,这些语义不同于经典的比较运算符。
因此,有些区间在经典意义上是不可比的,如下所示: 可以使用 此函数接受 类似地,可以使用 与 当绑定包含逗号或其表示不能用字符串中的 间隔也可以通过 用于转换边界的函数可以用 间隔可以通过 非常欢迎您的贡献!
可以使用GitHub问题和/或请求请求报告错误或建议新功能。 分布在lgplv3-gnu Lesser General Public License,版本3 该库遵循语义版本控制方案。 1.8.0(2018-12-15) 1.7.0(2018-12-06) 1.6.0(2018-08-29) 1.5.4(2018-07-29) 1.5.3(2018-06-21) 1.5.2(2018-06-15) 1.5.1(2018-04-25) 1.5.0(2018-04-17) 1.4.0(2018-04-17) 1.3.2(2018-04-13) trong>1.3.1[0,1]
与[2,3]
组合将不支持结果。
在[0,3]
中,即使1
和2
之间没有整数
算术运算
interval
和atomicinterval
都支持以下间隔操作:x.is_empty()
测试间隔是否为空。>>>I.closed(0,1).is_empty()False>>>I.closed(0,0).is_empty()False>>>I.openclosed(0,0).is_empty()True>>>I.empty().is_empty()True
x.交集(其他)
或x&;其他
返回两个区间的交集。>>>I.closed(0,2)&I.closed(1,3)[1,2]>>>I.closed(0,4)&I.open(2,3)(2,3)>>>I.closed(0,2)&I.closed(2,3)[2]>>>I.closed(0,2)&I.closed(3,4)()
x.union(其他)
或x其他
返回两个间隔的并集。>>>I.closed(0,1)|I.closed(1,2)[0,2]>>>I.closed(0,1)|I.closed(2,3)[0,1]|[2,3]
x.补码(其他)
或~x
返回间隔的补码。>>>~I.closed(0,1)(-inf,0)|(1,+inf)>>>~(I.open(-I.inf,0)|I.open(1,I.inf))[0,1]>>>~I.open(-I.inf,I.inf)()
x.差异(其他)
或x-其他
返回x
和其他
之间的差异>>>I.open(1,2)(1,2)>>>I.closed(1,2)[1,2]>>>I.openclosed(1,2)(1,2]>>>I.closedopen(1,2)[1,2)>>>I.singleton(1)[1]>>>I.empty()()
0
x.contains(other)
或other in x
如果间隔中包含给定项,则返回true。
支持interval
,atomicinterval
和任意可比值。>>>I.open(1,2)(1,2)>>>I.closed(1,2)[1,2]>>>I.openclosed(1,2)(1,2]>>>I.closedopen(1,2)[1,2)>>>I.singleton(1)[1]>>>I.empty()()
1
x.重叠(其他)
测试两个间隔之间是否有重叠。
此方法接受默认为false
的permissive
参数。如果为真,则认为[1,2]和
[2,3]在2(但不是[1,2]和(2,3])上有重叠。
>>>I.open(1,2)(1,2)>>>I.closed(1,2)[1,2]>>>I.openclosed(1,2)(1,2]>>>I.closedopen(1,2)[1,2)>>>I.singleton(1)[1]>>>I.empty()()
2
间隔
实例:
x.enclosure()
返回包含当前间隔的最小间隔。>>>I.open(1,2)(1,2)>>>I.closed(1,2)[1,2]>>>I.openclosed(1,2)(1,2]>>>I.closedopen(1,2)[1,2)>>>I.singleton(1)[1]>>>I.empty()()
3
x.to_atomic()
相当于x.enclosure()
但返回一个atomicInterval而不是
Interval
对象。x.is_atomic()
如果间隔由单个(可能为空)原子间隔组成,则计算结果为true
。>>>I.open(1,2)(1,2)>>>I.closed(1,2)[1,2]>>>I.openclosed(1,2)(1,2]>>>I.closedopen(1,2)[1,2)>>>I.singleton(1)[1]>>>I.empty()()
4
区间的界限
atomicinterval
的左右边界和上下界
其左
,右
,下
和上
属性。
左
和右
边界要么是i.closed
(true
)要么是i.open
(false
)。>>>I.open(1,2)(1,2)>>>I.closed(1,2)[1,2]>>>I.openclosed(1,2)(1,2]>>>I.closedopen(1,2)[1,2)>>>I.singleton(1)[1]>>>I.empty()()
5
左
,右
访问间隔
实例的界限,
下
和上
属性。在这种情况下,左
和下
指其外壳的下限,
而右
和上
则指其外壳的上限:>>>I.open(1,2)(1,2)>>>I.closed(1,2)[1,2]>>>I.openclosed(1,2)(1,2]>>>I.closedopen(1,2)[1,2)>>>I.singleton(1)[1]>>>I.empty()()
6
>>>I.open(1,2)(1,2)>>>I.closed(1,2)[1,2]>>>I.openclosed(1,2)(1,2]>>>I.closedopen(1,2)[1,2)>>>I.singleton(1)[1]>>>I.empty()()
7
interval
和atomicinterval
实例都是不可变的,但是提供了一个replace
方法
可用于基于当前实例创建新实例。此方法接受四个可选
参数左
,下
,上
,和右
:>>>I.open(1,2)(1,2)>>>I.closed(1,2)[1,2]>>>I.openclosed(1,2)(1,2]>>>I.closedopen(1,2)[1,2)>>>I.singleton(1)[1]>>>I.empty()()
8
false
>>>I.open(1,2)(1,2)>>>I.closed(1,2)[1,2]>>>I.openclosed(1,2)(1,2]>>>I.closedopen(1,2)[1,2)>>>I.singleton(1)[1]>>>I.empty()()
9
replace
应用于非原子的interval
时,它被扩展和/或限制为
它的外壳满足新的界限。>>>I.closed(1.2,2.4)[1.2,2.4]>>>I.closed('a','z')['a','z']>>>importdatetime>>>I.closed(datetime.date(2011,3,15),datetime.date(2013,10,10))[datetime.date(2011,3,15),datetime.date(2013,10,10)]
0
区间变换
interval
实例将公开一个apply
方法。
此方法接受将应用于每个底层原子间隔的函数,以执行所需的转换。
该函数应返回atomicinterval
、interval
或4倍的(左、下、上、右)
>>>I.closed(1.2,2.4)[1.2,2.4]>>>I.closed('a','z')['a','z']>>>importdatetime>>>I.closed(datetime.date(2011,3,15),datetime.date(2013,10,10))[datetime.date(2011,3,15),datetime.date(2013,10,10)]
1
replace结合使用时,
apply
方法非常强大。
因为后者允许函数作为参数传递,并且可以忽略无穷大,所以可以
方便地用于在无穷远处变换间隔。>>>I.closed(1.2,2.4)[1.2,2.4]>>>I.closed('a','z')['a','z']>>>importdatetime>>>I.closed(datetime.date(2011,3,15),datetime.date(2013,10,10))[datetime.date(2011,3,15),datetime.date(2013,10,10)]
2
迭代索引
>>>I.closed(1.2,2.4)[1.2,2.4]>>>I.closed('a','z')['a','z']>>>importdatetime>>>I.closed(datetime.date(2011,3,15),datetime.date(2013,10,10))[datetime.date(2011,3,15),datetime.date(2013,10,10)]
3
interval的
atomicinterval
对象也可以使用其索引来访问:>>>I.closed(1.2,2.4)[1.2,2.4]>>>I.closed('a','z')['a','z']>>>importdatetime>>>I.closed(datetime.date(2011,3,15),datetime.date(2013,10,10))[datetime.date(2011,3,15),datetime.date(2013,10,10)]
4
比较运算符
=
运算符检查间隔之间的相等性:>>>I.closed(1.2,2.4)[1.2,2.4]>>>I.closed('a','z')['a','z']>>>importdatetime>>>I.closed(datetime.date(2011,3,15),datetime.date(2013,10,10))[datetime.date(2011,3,15),datetime.date(2013,10,10)]
5
>;
、>;=
、<;
或<;=
可以比较间隔和
原子间隔。
这些比较运算符的行为与通常的不同。
例如,如果
a
完全位于b
的下界左侧,则a<;b
保持,如果a
完全位于a
的下界左侧,则a<;b
保持
在b
>>>I.closed(1.2,2.4)[1.2,2.4]>>>I.closed('a','z')['a','z']>>>importdatetime>>>I.closed(datetime.date(2011,3,15),datetime.date(2013,10,10))[datetime.date(2011,3,15),datetime.date(2013,10,10)]
6
a
完全位于b
的上限左侧且a>;=b
如果a
完全位于b
的下限右侧,则保持
>>>I.closed(1.2,2.4)[1.2,2.4]>>>I.closed('a','z')['a','z']>>>importdatetime>>>I.closed(datetime.date(2011,3,15),datetime.date(2013,10,10))[datetime.date(2011,3,15),datetime.date(2013,10,10)]
7
i
是一个间隔并且x
是一个值,则
x<;i
如果x
位于i
的下限左侧,则保持;如果x
位于
i
的上限的左边。这种行为类似于第一个
将x
转换为单次间隔。>>>I.closed(1.2,2.4)[1.2,2.4]>>>I.closed('a','z')['a','z']>>>importdatetime>>>I.closed(datetime.date(2011,3,15),datetime.date(2013,10,10))[datetime.date(2011,3,15),datetime.date(2013,10,10)]
8
>>>I.closed(1.2,2.4)[1.2,2.4]>>>I.closed('a','z')['a','z']>>>importdatetime>>>I.closed(datetime.date(2011,3,15),datetime.date(2013,10,10))[datetime.date(2011,3,15),datetime.date(2013,10,10)]
9
导入和导出间隔到字符串
repr
(如上所示)或使用to\u string
函数将间隔导出为字符串。>>>I.inf>'a',I.inf>0,I.inf>True(True,True,True)>>>I.openclosed(-I.inf,0)(-inf,0]>>>I.closed(-I.inf,I.inf)# Automatically converted to an open interval(-inf,+inf)
0
interval
和atomicinterval
实例。
构建字符串表示的方式可以使用
到字符串
:>>>I.inf>'a',I.inf>0,I.inf>True(True,True,True)>>>I.openclosed(-I.inf,0)(-inf,0]>>>I.closed(-I.inf,I.inf)# Automatically converted to an open interval(-inf,+inf)
1
from_string
函数从字符串创建间隔。
必须提供转换函数(conv
参数)才能将绑定(作为字符串)转换为值。>>>I.inf>'a',I.inf>0,I.inf>True(True,True,True)>>>I.openclosed(-I.inf,0)(-inf,0]>>>I.closed(-I.inf,I.inf)# Automatically converted to an open interval(-inf,+inf)
2
to_string
类似,可以对函数from_string
进行参数化,以处理更详细的输入。
请注意,由于from_string
需要正则表达式模式,因此我们需要转义一些字符。>>>I.inf>'a',I.inf>0,I.inf>True(True,True,True)>>>I.openclosed(-I.inf,0)(-inf,0]>>>I.closed(-I.inf,I.inf)# Automatically converted to an open interval(-inf,+inf)
3
自动解析时,
绑定
参数可用于指定应用于匹配其表示形式的正则表达式。>>>I.inf>'a',I.inf>0,I.inf>True(True,True,True)>>>I.openclosed(-I.inf,0)(-inf,0]>>>I.closed(-I.inf,I.inf)# Automatically converted to an open interval(-inf,+inf)
4
导入和导出间隔到python内置数据类型
to_data
导出到一个4元组的列表,例如,支持json序列化。>>>I.inf>'a',I.inf>0,I.inf>True(True,True,True)>>>I.openclosed(-I.inf,0)(-inf,0]>>>I.closed(-I.inf,I.inf)# Automatically converted to an open interval(-inf,+inf)
5
conv
参数指定。
必须用来表示正无穷大和负无穷大的值可以用
pinf
和ninf
。它们默认分别为float('inf')
和float('-inf')
。>>>I.inf>'a',I.inf>0,I.inf>True(True,True,True)>>>I.openclosed(-I.inf,0)(-inf,0]>>>I.closed(-I.inf,I.inf)# Automatically converted to an open interval(-inf,+inf)
6
from_data
从这样一个4元组列表中导入。
同一组参数可用于指定如何转换边界和无穷大。>>>I.inf>'a',I.inf>0,I.inf>True(True,True,True)>>>I.openclosed(-I.inf,0)(-inf,0]>>>I.closed(-I.inf,I.inf)# Automatically converted to an open interval(-inf,+inf)
7
贡献
许可证
更改日志
左
、下
、上
和右
属性,它们表示其外壳。replace
方法来基于当前间隔创建新的间隔。此方法同时接受值和函数。apply
方法来在底层原子区间上应用函数。i.empty()
返回相同的实例以节省内存。len(i.empty())=1
和i.empty()[0]==i.empty()。为保持一致性,请将其设置为u atomic()
。from廑data
和to廑data
(\6)。中添加对自定义无限表示的支持到字符串
和
来自字符串
(\3)。
。重叠
(\2)。repr
。interval
和atomicinterval
时的无效比较。(i.inf,-i.inf)
,修复\1interval.\uu init\uu
除了atomicinterval
实例之外,还接受interval
实例。i.to_string
将间隔导出为字符串,有许多选项可自定义表示。i.from_string
从字符串创建间隔,有许多选项可自定义解析。
- 定义
插槽以降低内存使用率并加快属性访问。
- 定义
interval.\uu rand\uu
(和其他魔术方法)以支持interval
从atomicinterval
而不是 在atomicinterval中有一段专用代码 - 修复所有问题
- 更多测试涵盖所有比较。
1.3.0(2018-04-04)
- 有意义的
<;=
和>;=
间隔比较。
1.2.0(2018-04-04)
interval
支持索引以检索底层的atomicinterval
对象。
1.1.0(2018-04-04)
atomicinterval
和interval
都是完全可比的。- 添加
singleton(x)
以创建singleton间隔[x]。 - 添加
empty()
以创建空间隔。 - 添加
interval.enclosure()
以返回包含当前间隔的最小间隔。 - 区间简化是o(n)而不是o(n*m)。
atomicinterval
间隔中的对象按上下界排序。
1.0.4(2018-04-03)
atomicinterval
的所有操作(重叠除外)都接受interval
- 如果不支持类型(与未实现的类型一致),则提升
typeerror
而不是valueerror
。
1.0.3(2018-04-03)
- PYPI的初始工作版本。
1.0.0(2018-04-03)
- 初始版本。