-
Notifications
You must be signed in to change notification settings - Fork 0
/
elements.py
131 lines (105 loc) · 3.71 KB
/
elements.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
Define network elements to communicate with PST.
"""
class DanglingLine(Exception):
"""Raised on a line with no start or end point."""
pass
class LineElement():
"""Contains references to a from BusElement and a to
BusElement."""
def __init__(self, frm=None, to=None):
self.set_from(frm)
self.set_to(to)
def set_from(self, frm):
"""Set the object that this line is connected to.
Set to None for no object. Nominal sending end."""
self._frm = frm
frm.next_el = self
def set_to(self, to):
"""Set the object that this line is connected to.
Set to None for no object. Nominal recieving end."""
self._to = to
to.next_el = self
def remove(self, obj):
"""Remove obj from to or from."""
if self._frm is obj:
self._frm = None
elif self._to is obj:
self._to = None
def convert(self):
""" Attempt to convert this line element into a PST type element."""
res = 0.01
reac = 0.05
chrg = 0.05
if self._from is None or self._to is None:
raise DanglingLine(
"Could not add element missing start or end.")
# get the from bus number and to bus number.
frm = self._frm.get_busno()
to = self._to.get_busno()
line = [frm, to, res, reac, chrg, 1.0, 0.0]
line_str = ','.join(line)
return line_str
class BusRegister():
"""Register buses as they are connected using lines.
Buses are only assigned a bus number once they are connected."""
_shared = {}
_buses = {}
_availableindex = None
_max_index = 0
def __init__(self):
"""Borg Type."""
self.__dict__ = self._shared
def register(self, bus):
"""Register the bus in a mapped object.
As objects are removed, those indexes are placed
into the available index.
"""
if self._availableindex is None:
self._buses[self._max_index] = bus
self._max_index += 1
# return the bus number for this bus.
return self._max_index - 1
# we have bus numbers that can be re-used.
busno = self._availableindex.pop()
self._buses[busno] = bus
# check if there are no more re-usable bus numbers.
if len(self._availableindex) == 0:
self._availableindex = None
return busno
#def deregister
class BusElement():
"""Load type can be True (or False for generator)."""
def __init__(self, p, q, load_type=False):
self._p = p
self._q = q
self._loadtype = load_type
# the next element (must be a LineEl).
self._next = None
def next_el():
doc = "property to access the next element."
def fget(self):
return self._next
def fset(self, value):
# often used once the next property has been deleted.
try:
self._next.remove(self)
except AttributeError:
# Error when _next is a None object.
pass
self._next = value
def fdel(self):
# remove this bus element but warn next element of deletion.
try:
self._next.remove(self)
except AttributeError:
# Error when _next is a None object.
pass
del(self._next)
return locals()
# create a property next_el to refer to the next element.
next_el = property(**next_el())
def __del__(self):
# define what occurs when this element is removed.
# remove i.e notify next element that it is no longer referenced.
del(self._next)