MCPcopy Create free account
hub / github.com/rthalley/dnspython / Set

Class Set

dns/set.py:21–308  ·  view source on GitHub ↗

A simple set class. This class was originally used to deal with python not having a set class, and originally the class used lists in its implementation. The ordered and indexable nature of RRsets and Rdatasets is unfortunately widely used in dnspython applications, so for backward

Source from the content-addressed store, hash-verified

19
20
21class Set:
22 """A simple set class.
23
24 This class was originally used to deal with python not having a set class, and
25 originally the class used lists in its implementation. The ordered and indexable
26 nature of RRsets and Rdatasets is unfortunately widely used in dnspython
27 applications, so for backwards compatibility sets continue to be a custom class, now
28 based on an ordered dictionary.
29 """
30
31 __slots__ = ["items"]
32
33 def __init__(self, items=None):
34 """Initialize the set.
35
36 *items*, an iterable or ``None``, the initial set of items.
37 """
38
39 self.items = dict()
40 if items is not None:
41 for item in items:
42 # This is safe for how we use set, but if other code
43 # subclasses it could be a legitimate issue.
44 self.add(item) # lgtm[py/init-calls-subclass]
45
46 def __repr__(self):
47 return f"dns.set.Set({repr(list(self.items.keys()))})" # pragma: no cover
48
49 def add(self, item):
50 """Add an item to the set."""
51
52 if item not in self.items:
53 self.items[item] = None
54
55 def remove(self, item):
56 """Remove an item from the set."""
57
58 try:
59 del self.items[item]
60 except KeyError:
61 raise ValueError
62
63 def discard(self, item):
64 """Remove an item from the set if present."""
65
66 self.items.pop(item, None)
67
68 def pop(self):
69 """Remove an arbitrary item from the set."""
70 (k, _) = self.items.popitem()
71 return k
72
73 def _clone(self) -> "Set":
74 """Make a (shallow) copy of the set.
75
76 There is a 'clone protocol' that subclasses of this class
77 should use. To make a copy, first call your super's _clone()
78 method, and use the object returned as the new instance. Then

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected