| 16 | |
| 17 | |
| 18 | class Node: |
| 19 | def __init__(self, id, **attrs): |
| 20 | # Each node must has a unique id |
| 21 | self._id = id |
| 22 | # Attributes for Node |
| 23 | self._attrs = {} |
| 24 | self._attrs.update(attrs) |
| 25 | |
| 26 | @property |
| 27 | def id(self): |
| 28 | return self._id |
| 29 | |
| 30 | @property |
| 31 | def attrs(self): |
| 32 | return self._attrs |
| 33 | |
| 34 | def __getitem__(self, attr_name): |
| 35 | return self._attrs[attr_name] |
| 36 | |
| 37 | def __setitem__(self, attr_name, attr_value): |
| 38 | self._attrs[attr_name] = attr_value |
| 39 | |
| 40 | def __contains__(self, attr_name): |
| 41 | try: |
| 42 | return attr_name in self._attrs |
| 43 | except TypeError: |
| 44 | return False |
| 45 | |
| 46 | def __str__(self): |
| 47 | str = f"(id: {self.id}, attrs: {self.attrs})" |
| 48 | return str |
| 49 | |
| 50 | |
| 51 | class Edge: |