Set type for datetime intervals

Random832 random832 at fastmail.com
Fri Apr 1 11:45:37 EDT 2016


More thoughts... sorry.

On Fri, Apr 1, 2016, at 01:24, Nagy László Zsolt wrote:
>  
>   Hello,
> 
> I need to compare sets of datetime intervals, and make set operations on
> them: intersect, union, difference etc. One element of a set would be an
> interval like this:
> 
> element ::= (start_point_in_time, end_point_in_time)
> intervalset ::= { element1, element2, .... }
> 
> Operations on elements:


Eh... I think these should be realized as operations on an intervalset
with a single element, and that elements should simply have properties
like the start, end, and if it's open or closed on each side. In
particular, any one of these could return something that is not an
element.

> element1.intersect(element2)
May return the empty set, if they do not intersect.

> element1.union(element2)
May return {element1, element 2}

> element1.minus(element2)
May return { (start1, start2), (end2, end1) }... and the open/closeness
of the element2 endpoints is reversed.


> Operations on sets:
> 
> intervalset1.intersect(intervalset2)
> intervalset1.union(intervalset2)
> intervalset1.minus(intervalset2)

These should be operators. s1 & s2, s1 | s2, s1 - s2. There's also a s1
^ s2 operation for python sets, the symmetric difference.


Wouldn't it be useful to have some operations on datetimes?

intervalset1.ltall(dt)
.... all(dt < x.start or dt == x.start and x.startopen for x in dt)
intervalset1.gtall(dt)
.... all(dt > x.end or dt == x.end and x.endopen for x in dt)
intervalset1.leall(dt)
.... all(dt <= x.start for x in intervalset1) # I _think_ this is all
you need for this
intervalset1.geall(dt)
.... all(dt >= x.end for x in intervalset1)
dt in intervalset1
... any(dt in elem for x in intervalset1)
dt in elem
... dt > elem.start and dt < elem.end or dt == elem.start and not
elem.startopen or dt == elem.end and not elem.endopen
min and max
... seems trivial, but what to return if the first/last element is open?

I'm truly shocked you didn't even mention the "in" operation.

Also, as long as we're setting up an infinite (not really but anyway)
set, why not also have the ability to have open-ended intervals that
extend to infinity. Then you could invert the set too, for free given
the minus operation: ~x == {(-inf, inf)} - x.


It occurs to me that while I mentioned numbers, in principle you could
use _any_ total-ordered type*. strings, tuples...

*nitpicker's corner: here, "type" is used to mean any set/class/category
of objects, possibly of multiple python types and possibly not
comprising the entirety of a single type, which are total-ordered with
respect to each other, including the codomain of any key projection
function suitable for sorting.



More information about the Python-list mailing list