Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit b03e708

Browse files
committed
Add new from_range_list feature and improve docs
1 parent cf15b2b commit b03e708

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name='smart_range',
10-
version='1.0.0',
10+
version='1.1.0',
1111
packages=['smart_range'],
1212
url='https://github.com/PythonCoderAS/SmartRange',
1313
license='MIT',

smart_range/smart_range.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ class SmartRange:
3737
"""
3838

3939
ranges: List[range]
40-
r"""The list of :py:class:`range`\ s. Can be externally modified to add, change, or remove ranges."""
40+
r"""The list of :py:class:`range`\ s. Can be externally modified to add, change, or remove ranges.
41+
42+
.. warning::
43+
Adding a range out of order may result in unordered output from :py:meth:`~SmartRange.__iter__`.
44+
"""
4145

4246
def __init__(self, range_str: str, *, min_val: Optional[int] = None, max_val: Optional[int] = None):
4347
"""
@@ -53,7 +57,7 @@ def __init__(self, range_str: str, *, min_val: Optional[int] = None, max_val: Op
5357
5458
.. warning::
5559
If a ``max_val`` is supplied, no item in the range string can have an end value greater than the
56-
value.
60+
supplied maximum value.
5761
:type max_val: Optional[int]
5862
:raises NoMinimumValueError: If a type 2 or 4 range item is used at the beginning of the range string and no
5963
``min_val`` is supplied.
@@ -65,7 +69,33 @@ def __init__(self, range_str: str, *, min_val: Optional[int] = None, max_val: Op
6569
"""
6670
self.ranges = self._parse_range(range_str, min_val, max_val)
6771

72+
@classmethod
73+
def from_range_list(cls, range_list: List[range]) -> 'SmartRange':
74+
r"""Create a new :py:class:`SmartRange` object from a list of :py:class:`range`\ s.
75+
76+
.. note::
77+
:py:class:`range` objects exclude the ``stop`` parameter, so ``range(1, 2)`` is not the same as the
78+
range string ``1-2``. The range object for ``1-2`` is ``range(1, 3)``.
79+
80+
.. warning::
81+
When using this method, none of the checks for :py:meth:`~SmartRange.__init__` are performed. In order to
82+
process these checks, this workaround can be used:
83+
84+
.. code-block:: python
85+
86+
smart_range = SmartRange.from_range_list([range(1, 11), range(11, 16])
87+
validated_range = SmartRange(str(smart_range))
88+
89+
:param range_list: The list of :py:class:`range`\ s.
90+
:type range_list: List[range]
91+
"""
92+
obj = cls("")
93+
obj.ranges = range_list
94+
return obj
95+
6896
def _parse_range(self, range_str: str, min_val: Optional[int] = None, max_val: Optional[int] = None) -> List[range]:
97+
if not range_str:
98+
return []
6999
parts = range_str.split(",")
70100
ranges = []
71101
locked_max = max_val

0 commit comments

Comments
 (0)