Superbird11 / ranges

Continuous Range, RangeSet, and RangeDict data structures for Python
MIT License
101 stars 10 forks source link

Is there a way to explicitely say inifinty ? #1

Closed ewjoachim closed 3 years ago

ewjoachim commented 4 years ago

I have an optional n variable (integer or None), and I'd like it to be the end of my Range (meaning until n or open-ended if None)

Is there a better choice for me than:

if n is None:
    range = Range(a)
else:
    range = Range(a, n)

# OR
range = Range(a, *([n] if n is not None else []))

I guess having a special sentinel value for "infinity" (without resorting to using the private _InfiniteValue) would be nice. Also, None would make a pretty rational choice.

ewjoachim commented 4 years ago

(For a better understanding of the use case, my real life exemple looks like:

        if n is None:
            kwargs = {"end": self.file_size} if self.file_size is not None else {}
        else:
            kwargs = {"end": start + n}
        read_range = ranges.Range(start=start, **kwargs)

If None was supported as "infinity", that would be:

read_range = ranges.Range(start=start, end=start + n if n is not None else self.file_size)

)

McCroden commented 4 years ago

I agree with @ewjoachim, this would be helpful. I initially thought None could be used to imply infinity but that a decision hadn't been finalized and use INF to explicitly define infinity. Analogous to how SQL databases use null for fields that have not been specified, but use "" when it is explicitly meant to be blank.

Superbird11 commented 3 years ago

Added a ranges.Inf constant that represents a Range-compatible infinity. It should be negatable with - like any other number, but should also work in a range with non-numeric types.