| 1 | | NODE, EDGE, ATTR = range(3) |
| 2 | 1 | |
| 2 | import sys, pytest, collections, collections.abc, urllib3.exceptions, _pytest.pytester, numpy; |
| 3 | collections.Mapping = collections.abc.Mapping; |
| 4 | collections.MutableMapping = collections.abc.MutableMapping; |
| 5 | collections.MutableSet = collections.abc.MutableSet; |
| 6 | collections.Sequence = collections.abc.Sequence; |
| 7 | collections.Callable = collections.abc.Callable; |
| 8 | collections.Iterable = collections.abc.Iterable; |
| 9 | collections.Iterator = collections.abc.Iterator; |
| 10 | urllib3.exceptions.SNIMissingWarning = urllib3.exceptions.DependencyWarning; |
| 11 | pytest.RemovedInPytest4Warning = DeprecationWarning; |
| 12 | _pytest.pytester.Testdir = _pytest.pytester.Pytester; |
| 13 | numpy.PINF = numpy.inf; |
| 14 | numpy.unicode_ = numpy.str_; |
| 15 | numpy.bytes_ = numpy.bytes_; |
| 16 | numpy.float_ = numpy.float64; |
| 17 | numpy.string_ = numpy.bytes_; |
| 18 | numpy.NaN = numpy.nan; |
| 19 | |
| 20 | |
| 21 | NODE, EDGE, ATTR = range(3) |
| 3 | 22 | |
| 4 | 23 | class Node: |
| 5 | | def __init__(self, name: str, attrs: dict): |
| 24 | def __init__(self, name, attrs=None): |
| 6 | 25 | self.name = name |
| 7 | | self.attrs = attrs |
| 8 | | |
| 26 | self.attrs = attrs or {} |
| 27 | |
| 9 | 28 | def __eq__(self, other): |
| 10 | 29 | return self.name == other.name and self.attrs == other.attrs |
| 11 | | |
| 30 | |
| 31 | def __repr__(self): |
| 32 | return f"Node({self.name!r}, {self.attrs!r})" |
| 12 | 33 | |
| 13 | 34 | class Edge: |
| 14 | | def __init__(self, src: str, dst: str, attrs: dict): |
| 35 | def __init__(self, src, dst, attrs=None): |
| 15 | 36 | self.src = src |
| 16 | 37 | self.dst = dst |
| 17 | | self.attrs = attrs |
| 18 | | |
| 38 | self.attrs = attrs or {} |
| 39 | |
| 19 | 40 | def __eq__(self, other): |
| 20 | | return (self.src == other.src and |
| 21 | | self.dst == other.dst and |
| 22 | | self.attrs == other.attrs) |
| 23 | | |
| 41 | return self.src == other.src and self.dst == other.dst and self.attrs == other.attrs |
| 42 | |
| 43 | def __repr__(self): |
| 44 | return f"Edge({self.src!r}, {self.dst!r}, {self.attrs!r})" |
| 24 | 45 | |
| 25 | 46 | class Graph: |
| 26 | | def __init__(self, data: list | None = None): |
| 27 | | pass |
| 47 | def __init__(self, data=None): |
| 48 | # Initialize empty collections |
| 49 | self.nodes = [] |
| 50 | self.edges = [] |
| 51 | self.attrs = {} |
| 52 | |
| 53 | # Check if data is None or not a list |
| 54 | if data is None or not isinstance(data, list): |
| 55 | raise TypeError("Graph data malformed") |
| 56 | |
| 57 | # Process each item in the data |
| 58 | for item in data: |
| 59 | # Check if item is a tuple |
| 60 | if not isinstance(item, tuple): |
| 61 | raise TypeError("Graph item malformed") |
| 62 | |
| 63 | # Check if tuple is not empty |
| 64 | if len(item) == 0: |
| 65 | raise TypeError("Graph item malformed") |
| 66 | |
| 67 | # Get the item type |
| 68 | item_type = item[0] |
| 69 | |
| 70 | # Process based on item type |
| 71 | if item_type == ATTR: |
| 72 | if len(item) != 3: |
| 73 | raise TypeError("Graph item malformed") |
| 74 | _, key, value = item |
| 75 | self.attrs[key] = value |
| 76 | elif item_type == NODE: |
| 77 | if len(item) != 3: |
| 78 | raise TypeError("Graph item malformed") |
| 79 | _, name, attrs = item |
| 80 | self.nodes.append(Node(name, attrs)) |
| 81 | elif item_type == EDGE: |
| 82 | if len(item) != 4: |
| 83 | raise TypeError("Graph item malformed") |
| 84 | _, src, dst, attrs = item |
| 85 | self.edges.append(Edge(src, dst, attrs)) |
| 86 | else: |
| 87 | raise ValueError("Unknown item") |