-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatom.py
More file actions
67 lines (55 loc) · 1.92 KB
/
atom.py
File metadata and controls
67 lines (55 loc) · 1.92 KB
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
#!/usr/bin/env python
from state import GroundState
class IonisationReached(Exception):
pass
class Atom(object):
def __init__(self):
# The ground state should always be a do-nothing, need-nothing state
self._states = [GroundState()]
self._energy = 0
self._target_energy = 0
@property
def ground_state(self):
return self.state(0)
def state(self, energy=-1):
if energy == -1:
energy = self._energy
if len(self._states) > energy:
return self._states[energy]
else:
return None
def add_state(self, state):
self._states.append(state)
def is_excited(self):
return self.state(self._energy).is_excited()
def excite(self, energy):
print '=== REQUEST:', self.state(energy)
if self._energy == energy:
return True
for E in range(self._energy+1, energy+1):
if self.state(E).excite():
print '+++ STATE:', self.state(E)
self._energy += 1
else:
print '--- STATE:', self.state(E)
return False
return True
def decay(self, energy):
raise NotImplementedError
def quantum_leap(self, target):
# Request a state transition
if target >= len(self._states):
raise IonisationReached()
self._target_energy = target
def observe(self):
# this might be called periodically from a main loop, checking whether
# we are in the state that we should be in, and
# whether we need to do a state transition
while not self.is_excited():
print '--- LOST:', self.state()
self._energy -= 1
if self._energy < self._target_energy:
self.excite(self._target_energy)
elif self._energy > self._target_energy:
self.decay(self._target_energy)
print self.state().name