Skip to content

Commit 916b996

Browse files
committed
Few minor changes.
1 parent 327e51c commit 916b996

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed

state.py

+39-39
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,63 @@
1-
"""Implementation of the state pattern"""
1+
"""Implementation of the state pattern"""
22

3-
'''http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/'''
3+
# http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
44

55

66
class State(object):
7-
"""Base state. This is to share functionality"""
7+
"""Base state. This is to share functionality"""
88

9-
def scan(self):
10-
"""Scan the dial to the next station"""
11-
self.pos += 1
12-
if self.pos == len(self.stations):
13-
self.pos = 0
14-
print("Scanning... Station is", self.stations[self.pos], self.name)
9+
def scan(self):
10+
"""Scan the dial to the next station"""
11+
self.pos += 1
12+
if self.pos == len(self.stations):
13+
self.pos = 0
14+
print("Scanning... Station is", self.stations[self.pos], self.name)
1515

1616

1717
class AmState(State):
18-
def __init__(self, radio):
19-
self.radio = radio
20-
self.stations = ["1250", "1380", "1510"]
21-
self.pos = 0
22-
self.name = "AM"
18+
def __init__(self, radio):
19+
self.radio = radio
20+
self.stations = ["1250", "1380", "1510"]
21+
self.pos = 0
22+
self.name = "AM"
2323

24-
def toggle_amfm(self):
25-
print("Switching to FM")
26-
self.radio.state = self.radio.fmstate
24+
def toggle_amfm(self):
25+
print("Switching to FM")
26+
self.radio.state = self.radio.fmstate
2727

2828

2929
class FmState(State):
30-
def __init__(self, radio):
31-
self.radio = radio
32-
self.stations = ["81.3", "89.1", "103.9"]
33-
self.pos = 0
34-
self.name = "FM"
30+
def __init__(self, radio):
31+
self.radio = radio
32+
self.stations = ["81.3", "89.1", "103.9"]
33+
self.pos = 0
34+
self.name = "FM"
3535

36-
def toggle_amfm(self):
37-
print("Switching to AM")
38-
self.radio.state = self.radio.amstate
36+
def toggle_amfm(self):
37+
print("Switching to AM")
38+
self.radio.state = self.radio.amstate
3939

4040

4141
class Radio(object):
42-
"""A radio. It has a scan button, and an AM/FM toggle switch."""
43-
def __init__(self):
44-
"""We have an AM state and an FM state"""
45-
self.amstate = AmState(self)
46-
self.fmstate = FmState(self)
47-
self.state = self.amstate
42+
"""A radio. It has a scan button, and an AM/FM toggle switch."""
43+
def __init__(self):
44+
"""We have an AM state and an FM state"""
45+
self.amstate = AmState(self)
46+
self.fmstate = FmState(self)
47+
self.state = self.amstate
4848

49-
def toggle_amfm(self):
50-
self.state.toggle_amfm()
49+
def toggle_amfm(self):
50+
self.state.toggle_amfm()
5151

52-
def scan(self):
53-
self.state.scan()
52+
def scan(self):
53+
self.state.scan()
5454

5555

5656
# Test our radio out
5757
if __name__ == '__main__':
58-
radio = Radio()
59-
actions = [radio.scan] * 2 + [radio.toggle_amfm] + [radio.scan] * 2
60-
actions = actions * 2
58+
radio = Radio()
59+
actions = [radio.scan] * 2 + [radio.toggle_amfm] + [radio.scan] * 2
60+
actions *= 2
6161

62-
for action in actions:
62+
for action in actions:
6363
action()

0 commit comments

Comments
 (0)