Debian Codegraph @

doc/sphinx/guide/rangesets.rst

raw
1 Range sets
2 ==========
3
4 .. highlight:: python
5
6 Cluster node names being typically indexed, common node sets rely heavily on
7 numerical range sets. The :mod:`.RangeSet` module provides two public classes
8 to deal directly with such range sets, :class:`.RangeSet` and
9 :class:`.RangeSetND`, presented in the following sections.
10
11 .. _class-RangeSet:
12
13 RangeSet class
14 --------------
15
16 The :class:`.RangeSet` class implements a mutable, ordered set of cluster node
17 indexes (over a single dimension) featuring a fast range-based API. This class
18 is used by the :class:`.NodeSet` class (see :ref:`class-NodeSet`). Since
19 version 1.6, :class:`.RangeSet` actually derives from the standard Python set
20 class (`Python sets`_), and thus provides methods like :meth:`.RangeSet.union`,
21 :meth:`.RangeSet.intersection`, :meth:`.RangeSet.difference`,
22 :meth:`.RangeSet.symmetric_difference` and their in-place versions
23 :meth:`.RangeSet.update`, :meth:`.RangeSet.intersection_update`,
24 :meth:`.RangeSet.difference_update()` and
25 :meth:`.RangeSet.symmetric_difference_update`.
26
27 In v1.9, the implementation of zero-based padding of indexes (e.g. ``001``) has
28 been improved. The inner set contains indexes as strings with the padding
29 included, which allows the use of mixed length zero-padded indexes (e.g. using
30 both ``01`` and ``001`` is valid and supported in the same object). Prior to v1.9,
31 zero-padding was a simple display feature of fixed length per
32 :class:`.RangeSet` object, and indexes were stored as integers in the inner
33 set.
34
35 To iterate over indexes as strings with zero-padding included, you can now
36 iterate over the :class:`.RangeSet` object (:meth:`.RangeSet.__iter__()`),
37 or still use the :meth:`.RangeSet.striter()` method which has not changed.
38 To iterate over the set's indexes as integers, you may use the new method
39 :meth:`.RangeSet.intiter()`, which is the equivalent of iterating over the
40 :class:`.RangeSet` object before v1.9.
41
42 Since v1.10, the :meth:`.RangeSet.index()` method returns the zero-based
43 position of an element, the reverse of :meth:`.RangeSet.__getitem__()`
44 (zero-padding is significant when the element is given as a string)::
45
46 >>> RangeSet("10-49").index("20")
47 10
48
49 .. _class-RangeSetND:
50
51 RangeSetND class
52 ----------------
53
54 The :class:`.RangeSetND` class builds an N-dimensional RangeSet mutable object
55 and provides the common set methods. This class is public and may be used
56 directly, however we think it is less convenient to manipulate than
57 :class:`.NodeSet` and does not necessarily provide the same one-dimension
58 optimization (see :ref:`class-NodeSet-nD`). Several constructors are
59 available, using RangeSet objects, strings or individual multidimensional
60 tuples, for instance::
61
62 >>> from ClusterShell.RangeSet import RangeSet, RangeSetND
63 >>> r1 = RangeSet("1-5/2")
64 >>> list(r1)
65 ['1', '3', '5']
66 >>> r2 = RangeSet("10-12")
67 >>> r3 = RangeSet("0-4/2")
68 >>> r4 = RangeSet("10-12")
69 >>> print(r1, r2, r3, r4)
70 1,3,5 10-12 0,2,4 10-12
71 >>> rnd = RangeSetND([[r1, r2], [r3, r4]])
72 >>> print(rnd)
73 0-5; 10-12
74
75 >>> print(list(rnd))
76 [('0', '10'), ('0', '11'), ('0', '12'), ('1', '10'), ('1', '11'), ('1', '12'), ('2', '10'), ('2', '11'), ('2', '12'), ('3', '10'), ('3', '11'), ('3', '12'), ('4', '10'), ('4', '11'), ('4', '12'), ('5', '10'), ('5', '11'), ('5', '12')]
77
78 >>> r1 = RangeSetND([(0, 4), (0, 5), (1, 4), (1, 5)])
79 >>> len(r1)
80 4
81 >>> str(r1)
82 '0-1; 4-5\n'
83 >>> r2 = RangeSetND([(1, 4), (1, 5), (1, 6), (2, 5)])
84 >>> str(r2)
85 '1; 4-6\n2; 5\n'
86 >>> r = r1 & r2
87 >>> str(r)
88 '1; 4-5\n'
89 >>> list(r)
90 [('1', '4'), ('1', '5')]
91
92
93 .. _Python sets: https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset