尝试使用两个范围对象创建多范围。

huangapple go评论75阅读模式
英文:

Trying to form a Multirange with two Range objects

问题

根据文档,

https://access.crunchydata.com/documentation/psycopg3/3.1.4/

psycopg3 Range 是:

范围对象是不可变的、可哈希的,并支持 in 运算符(检查元素是否在范围内)。

但是在 Python 中,我想将两个 Range 连接成一个 psycopg3 Multirange。我知道 Range(与小写 'r' 范围不同)在 Python 中的功能有限。

我该如何做到这一点?

请注意,这是在 foo 是一个 psycopg3 MultiRange 对象的情况下有效。

foo.append(newrange)

我可以在 MultiRange 对象的末尾添加一个 Range 对象。

这是一种尝试将一个 Range 对象连接到另一个以创建 MultiRange 的方法。

from itertools import chain
t = Range(300, 400)
newrange = Range(2900, 4300)
print("Newrange data and type", newrange, type(newrange))
mt = chain(t, newrange)
print(mt, type(mt))
print(sorted(mt))  # 如果尝试使用 'list' 而不是 'sorted' 会出现相同的错误

输出:

Newrange data and type [2900, 4300) <class 'psycopg.types.range.Range'>

<itertools.chain object at 0x7fd30892fd60> <class 'itertools.chain'>
Traceback (most recent call last):
File "range_agg_test.py", line 95, in <module>
print(sorted(mt))
TypeError: 'Range' object is not iterable

有没有办法创建一个 MultiRange,给定两个 Range?

英文:

According to the docs,

https://access.crunchydata.com/documentation/psycopg3/3.1.4/

The psycopg3 Range is:

Range objects are immutable, hashable, and support the in operator (checking if an element is within the range).

However in Python I would like to concatenate two Range into a psycopg3 Multirange.
I know the Range (as opposed to small case 'r' range) has limited functionality in Python.

How can I do that?

Note this is valid where foo is a psycopg3 MultiRange object.

foo.append(newrange) 

I am allowed to tack on a Range object at the end of a MultiRange object.

Here is an attempt to tack one Range object after another to create a MultiRange.

```from itertools import chain 
t = Range(300,400) 
newrange = Range(2900,4300)
print(&quot;Newrange data and type&quot;, newrange, type(newrange))
mt = chain(t, newrange) 
print(mt, type(mt))
print(sorted(mt))   # same error if I try &#39;list&#39; instead of &#39;sorted&#39; 
```

Output:

```Newrange data and type [2900, 4300) &lt;class &#39;psycopg.types.range.Range&#39;&gt;
  
   &lt;itertools.chain object at 0x7fd30892fd60&gt; &lt;class &#39;itertools.chain&#39;&gt;
   Traceback (most recent call last):
   File &quot;range_agg_test.py&quot;, line 95, in &lt;module&gt;
   print(sorted(mt))
   TypeError: &#39;Range&#39; object is not iterable```

Is there any way to accomplish the creation of a MultiRange given 2 Ranges?

答案1

得分: 0

我需要初始化一个新的 MultiRange 对象如下:

newbie = Multirange()
newbie.append(newrange)
newbie.append(t)

如果业务领域要求 MultiRange 严格升序,通常情况下,开发人员需要确保 newrange 和 t 的顺序正确,因为 append 语句不会进行检查。

英文:

After some fumbling I realized I needed to initialize a new MultiRange object as follows

newbie = Multirange() 
newbie.append(newrange)
newbie.append(t) 

If the business domain requires the MultiRange be strictly ascending, as is usual, it is up to the developer to make sure newrange and t are in the right order, because the append statements do not check.

huangapple
  • 本文由 发表于 2023年5月26日 00:28:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定