### Range Constructor Examples Source: https://python-ranges.readthedocs.io/en/latest/about.html Demonstrates various ways to instantiate a Range object, including using start and end arguments, string representations, copying existing ranges, and specifying bound inclusivity. ```python Range(2, 5) # two arguments, start and end respectively ``` ```python Range(‘[2..5)’) # one argument, of type String - resolves to a numeric range. Use ‘..’ or ‘,’ as separator ``` ```python Range(Range(2, 5)) # one argument, of type Range - copies the given Range ``` ```python Range(start=2, end=5) # keyword arguments specify start and end. If not given, they default to -Inf/Inf ``` ```python Range() # no arguments - infinite bounds by default ``` ```python Range(2, 5, include_start=False, include_end=True) ``` ```python Range(‘(2, 5]’) ``` -------------------------------- ### Create Range from Keyword Arguments Source: https://python-ranges.readthedocs.io/en/latest/about.html Constructs a Range object using keyword arguments 'start' and/or 'end'. Default values are negative infinity for start and positive infinity for end if not provided. Custom inclusivity can also be specified. ```python g = Range(start=3, end=5) # [3, 5) h = Range(start=3) # [3, inf) i = Range(end=5) # [-inf, 5) j = Range(start=3, end=5, include_start=False, include_end=True) # (3, 5] ``` -------------------------------- ### Range with String Objects Source: https://python-ranges.readthedocs.io/en/latest/about.html Constructs a Range using strings for start and end, demonstrating lexicographical comparisons for checking inclusion. ```python n = Range("killer", "rabbit") >>> print("grenade" in n) # False >>> print("pin" in n) # True >>> print("three" in n) # False ``` -------------------------------- ### Create Range from Positional Arguments Source: https://python-ranges.readthedocs.io/en/latest/about.html Constructs a Range object using two positional arguments for start and end. The condition start <= end must be true. Includes examples with custom inclusivity for start and end. ```python e = Range(3, 5) >>> print(e) # [3, 5) f = Range(3, 5, include_start=False, include_end=True) >>> print(f) # (3, 5] ``` -------------------------------- ### Create Range from String Source: https://python-ranges.readthedocs.io/en/latest/about.html Constructs a Range object from a string in the format '[start, end)'. Both '()' and '[]' are valid brackets. The start and end must be parsable as floats, and start <= end must be true. ```python c = Range("(-3, 5.5)") # Infers include_start and include_end to both be false d = Range("[-3, 5.5)") # Infers include_start to be true, include_end to be false ``` -------------------------------- ### Range with Date Objects Source: https://python-ranges.readthedocs.io/en/latest/about.html Constructs a Range using datetime.date objects for start and end, and checks for date inclusion within the range. ```python import datetime m = Range(datetime.date(1478, 11, 1), datetime.date(1834, 7, 15)) >>> print(datetime.date(1492, 8, 3) in m) # True >>> print(datetime.date(1979, 8, 17) in m) # False ``` -------------------------------- ### Numeric Infinity Range Source: https://python-ranges.readthedocs.io/en/latest/about.html Demonstrates how infinity conforms to IEEE 754 for numeric types. Shows a range with inclusive start and exclusive end, including negative infinity. ```python r = Range(include_start=True, include_end=False) >>> print(r) # [-inf, inf) >>> print(float('-inf') in r) # True >>> print(float('inf') in r) # False ``` -------------------------------- ### Constructing RangeSet Instances Source: https://python-ranges.readthedocs.io/en/latest/set.html Demonstrates various ways to initialize a RangeSet, including empty sets, sets from iterables, and sets with mixed Range and string representations. ```python >>> a = RangeSet() # empty RangeSet >>> b = RangeSet([Range(0, 1), Range(2, 3), Range(4, 5)]) # single iterable as a positional argument >>> c = RangeSet(Range(0, 1), Range(2, 3), Range(4, 5)) # multiple Ranges as positional arguments >>> d = RangeSet("[0, 1)", ["[1.5, 2)", "[2.5, 3)"], "[4, 5]") # multiple positional arguments, one is an iterable ``` -------------------------------- ### RangeDict with an infinite range Source: https://python-ranges.readthedocs.io/en/latest/dict.html Demonstrates the behavior when a RangeDict is initialized with an infinite range. Subsequent additions of finite ranges can alter the infinite range's coverage. ```python e = RangeDict({Range(include_end=True): "inquisition"}) print(e) # {{[-inf, inf)}: inquisition} print(e.get(None)) # inquisition print(e.get(3)) # inquisition print(e.get("holy")) # inquisition print(e.get("spanish")) # inquisition e[Range("a", "m")] = "grail" print(e) # {{[-inf, a), [m, inf)}: inquisition, {[a, m)}: grail} print(e.get("spanish")) # inquisition print(e.get("holy")) # grail print(e.get(3)) # KeyError print(e.get(None)) # KeyError ``` -------------------------------- ### Create Range with Date and String Bounds Source: https://python-ranges.readthedocs.io/en/latest/about.html Demonstrates creating ranges with non-numeric types like dates and strings. Default infinite bounds can be used with these types. ```python k = Range(start=datetime.date(1969, 10, 5)) # [1969-10-05, inf) includes any date after 5/10/1969 l = Range(end="ni", include_end=True) # [-inf, ni) all strings lexicographically less than 'ni' ``` -------------------------------- ### Create a RangeDict from a dictionary of Ranges Source: https://python-ranges.readthedocs.io/en/latest/dict.html Initialize a RangeDict using a dictionary where keys are Range objects and values are the corresponding data. ```python c = RangeDict({ Range('a', 'h'): "First third of the lowercase alphabet", Range('h', 'p'): "Second third of the lowercase alphabet", Range('p', '{'): "Final third of the lowercase alphabet", }) print(c['brian']) # First third of the lowercase alphabet print(c['king arthur']) # Second third of the lowercase alphabet print(c['python']) # Final third of the lowercase alphabet ``` -------------------------------- ### Create an empty RangeDict Source: https://python-ranges.readthedocs.io/en/latest/dict.html An empty RangeDict can be initialized without any arguments. ```python a = RangeDict() ``` -------------------------------- ### Create a RangeDict from another RangeDict Source: https://python-ranges.readthedocs.io/en/latest/dict.html A new RangeDict can be created as a copy of an existing one. ```python b = RangeDict(a) ``` -------------------------------- ### Create a Default Range Source: https://python-ranges.readthedocs.io/en/latest/about.html Constructs a Range object representing a range from negative infinity to positive infinity. This is a copy of an existing Range object. ```python a = Range() # range from -infinity to +infinity (see point #4 for how "infinity" works here) b = Range(a) # copy of a ``` -------------------------------- ### Default Infinite Range Source: https://python-ranges.readthedocs.io/en/latest/about.html Creates a Range with default infinite bounds and inclusive end. This range contains all possible values for its type. ```python q = Range(include_end=True) >>> print(q) # [-inf, inf] >>> print(0 in q) # True >>> print(1 in q) # True >>> print(-99e99 in q) # True >>> print("one" in q) # True >>> print(datetime.date(1975, 3, 14) in q) # True >>> print(None in q) # True ``` -------------------------------- ### RangeSet Construction with Nested Iterables Source: https://python-ranges.readthedocs.io/en/latest/set.html Illustrates that nested iterables are not supported when constructing a RangeSet and will result in a ValueError. ```python >>> e = RangeSet([[Range(0, 1), Range(2, 3)], [Range(4, 5), Range(6, 7)]]) ``` -------------------------------- ### RangeSet Combining Overlapping Ranges Source: https://python-ranges.readthedocs.io/en/latest/set.html Shows how a RangeSet automatically combines overlapping or adjacent ranges into a single, larger range during initialization. ```python >>> f = RangeSet("[0, 3]", "[2, 4)", "[5, 6]") >>> str(f) == "{[0, 4), [5, 6]}" ``` -------------------------------- ### Using Identity Comparison in RangeDict Source: https://python-ranges.readthedocs.io/en/latest/dict.html Construct a RangeDict with identity=True to use `is` instead of `==` for comparing range keys with the same associated value, preventing unintended coalescing. ```python >>> # `{3} is {3}` is False, so the two ranges don't coalesce >>> g = RangeDict({Range(1, 2): {3}, Range(4, 5): {3}}, identity=True) >>> print(g) # {{[1, 2)}: {3}, {[4, 5)}: {3}} ``` -------------------------------- ### RangeDict with shared mutable values Source: https://python-ranges.readthedocs.io/en/latest/dict.html Illustrates how RangeDict handles shared mutable values. Modifications to a mutable value through one range affect all ranges pointing to the same object. ```python # [{3}] == [{3}] is True, so the two ranges are made to point to the same object f = RangeDict({Range(1, 2): {3}, Range(4, 5): {3}}) print(f) # {{[1, 2), [4, 5)}: {3}} # f[1] returns the {3}. When |= is used, this object changes to {3, 4} f[Range(1, 2)] |= {4} # since the entire rangeset is pointing at the same object, the entire range changes print(f) # {{[1, 2), [4, 5)}: {3, 4}} ``` -------------------------------- ### Create a RangeDict from an iterable of 2-tuples Source: https://python-ranges.readthedocs.io/en/latest/dict.html Similar to a regular dictionary, a RangeDict can be constructed from an iterable of (Range, value) tuples. ```python d = RangeDict([ (Range('A', 'H'), "First third of the uppercase alphabet"), (Range('H', 'P'), "Second third of the uppercase alphabet"), (Range('P', '['), "Final third of the uppercase alphabet"), ]) ``` -------------------------------- ### Avoiding Mutation Issues in RangeDict Source: https://python-ranges.readthedocs.io/en/latest/dict.html To avoid mutation problems, do not mutate mutable values that multiple range keys may refer to. Instead, use non-mutative operations like the union operator `|`. ```python >>> h = RangeDict({Range(1, 2): {3}, Range(4, 5): {3}}) >>> print(h) # {{[1, 2), [4, 5)}: {3}} >>> h[Range(1, 2)] = h[Range(1, 2)] | {4} >>> print(h) # {{[4, 5)}: {3}, {[1, 2)}: {3, 4}} ``` -------------------------------- ### Range with NaN Check Source: https://python-ranges.readthedocs.io/en/latest/about.html Shows that NaN (Not a Number) is not considered to be within the default infinite range for numeric types. ```python >>> print(float('nan') in q) # False ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.