Skip to content

Support for negative ranges #515

Closed
@harry-clarke

Description

@harry-clarke

Hey there,

Am I missing something here?
Negative ranges don't seem to be supported, it seems in part due to string formatting issues (-1-1 confuses the parser)

Is there support for negative ranges, and if not, would others find this a useful addition?

Activity

thiell

thiell commented on Dec 26, 2022

@thiell
Collaborator

@harry-clarke indeed it is not supported, we only test use cases with positive indexes. - is parsed as a range separator, not a minus sign. Could you tell us a bit more about your use case where that would be useful?

harry-clarke

harry-clarke commented on Dec 26, 2022

@harry-clarke
Author

Funnily, it's just because I've been making my way through the Advent of Code challenges, and I wanted a RangeSet for intersecting rows and columns with shapes.

In its simplest form, I want to do:

RangeSet(range(-5,7)) - RangeSet(range(1,3))
harry-clarke

harry-clarke commented on Dec 26, 2022

@harry-clarke
Author

At least for now, can we provide a ValueError when passing in ranges with negative values?

I don't mind creating a PR

Ideally, it would be good to remove the dependency on string parsing for interpreting ranges, which should reduce complexity a bit.

thiell

thiell commented on Dec 26, 2022

@thiell
Collaborator

I checked and it is an unexpected change (or "regression") between 1.8.x and 1.9.

In 1.9, we actually changed the way the inner set handles indices: we now rely on strings instead of integers. This allows RangeSet to support complex cases of zero padding, for things like 01-05,001-002 (see
https://clustershell.readthedocs.io/en/latest/tools/nodeset.html?#zero-padding)

With 1.8.4:

>>> RangeSet(range(-5,7))
-5-6
>>> RangeSet(range(1,3))
1-2
>>> RangeSet(range(-5,7)) - RangeSet(range(1,3))
-5-0,3-6

With 1.9:

>>> RangeSet(range(-5,7))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sthiell/github/thiell/clustershell/lib/ClusterShell/RangeSet.py", line 124, in __init__
    self._parse(pattern)
  File "/home/sthiell/github/thiell/clustershell/lib/ClusterShell/RangeSet.py", line 178, in _parse
    raise RangeSetParseError(subrange, msg)
ClusterShell.RangeSet.RangeSetParseError: cannot convert string to integer : "-5"

For now, you could use 1.8.4, but I will see if we can restore functionality in 1.9.x and officially support negative ranges. I will post updates in this ticket. Thanks for the report!

added this to the 1.9.1 milestone on Feb 8, 2023
self-assigned this
on Feb 8, 2023
added a commit that references this issue on Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

    Development

    Participants

    @thiell@harry-clarke

    Issue actions

      Support for negative ranges · Issue #515 · cea-hpc/clustershell