| 10 | |
| 11 | |
| 12 | class Edge(object): |
| 13 | def __init__(self, fr, to, w=None): |
| 14 | """ |
| 15 | A generic directed edge object. |
| 16 | |
| 17 | Parameters |
| 18 | ---------- |
| 19 | fr: int |
| 20 | The id of the vertex the edge goes from |
| 21 | to: int |
| 22 | The id of the vertex the edge goes to |
| 23 | w: float, :class:`Object` instance, or None |
| 24 | The edge weight, if applicable. If weight is an arbitrary Object it |
| 25 | must have a method called 'sample' which takes no arguments and |
| 26 | returns a random sample from the weight distribution. If `w` is |
| 27 | None, no weight is assumed. Default is None. |
| 28 | """ |
| 29 | self.fr = fr |
| 30 | self.to = to |
| 31 | self._w = w |
| 32 | |
| 33 | def __repr__(self): |
| 34 | return "{} -> {}, weight: {}".format(self.fr, self.to, self._w) |
| 35 | |
| 36 | @property |
| 37 | def weight(self): |
| 38 | return self._w.sample() if hasattr(self._w, "sample") else self._w |
| 39 | |
| 40 | def reverse(self): |
| 41 | """Reverse the edge direction""" |
| 42 | return Edge(self.t, self.f, self.w) |
| 43 | |
| 44 | |
| 45 | ####################################################################### |
no outgoing calls