-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhat_is_code.py
executable file
·176 lines (131 loc) · 5.01 KB
/
what_is_code.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
"""
WhatIsCode
This extremely simple script was an inspiration driven by a combination of
Paul Ford's article on Bloomberg Business Week, "What is Code?"[1] and
Haddaway's song, "What is Love"[2]. It is probably best enjoyed while
watching an 8-bit demake of the song[3], or 16-bit if you prefer[4]. :-)
[1] http://www.bloomberg.com/graphics/2015-paul-ford-what-is-code/
[2] https://en.wikipedia.org/wiki/What_Is_Love_%28Haddaway_song%29
[3] https://www.youtube.com/watch?v=CT8t_1JXWn8
[4] https://www.youtube.com/watch?v=I2ufcU7I9-I
Usage Instructions/Examples:
In a REPL or other program:
# Import the module.
from what_is_code import WhatIsCode
# Instantiate the object.
song = WhatIsCode()
# Output the song.
song.sing()
As a standalone script on the shell:
Make sure the script is executable:
chmod +x what_is_code.py
Run the script:
./what_is_code.py
Enjoy! :-)
Copyright 2015 Carlo Costino
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
class WhatIsCode(object):
def __init__(self):
"""
Initialize the main refrain and verses used throughout the song.
"""
self.main_refrain = [
'What is code?',
'Program don\'t crash now',
'Don\'t crash now, again',
]
self.first_verse = [
'I don\'t know why you don\'t work',
'I type things in right, but you just fail',
'So what is True?',
'And what is False?',
'Gimme a break',
]
self.second_verse = [
'Oh, I give up, nothing I do',
'Will work in this app, that I called foo',
'I wrote some tests',
'And they all pass',
'But my app fails',
]
self.third_verse = [
'Can you please just work, just work as I want',
'This is insane, wasted time',
'I thought this was easy, I though this was simple',
'Is it code?',
]
@property
def secondary_refrain(self):
"""
The secondary refrain is just the last two parts of the main refrain.
"""
return self.main_refrain[1:]
@property
def tertiary_refrain(self):
"""
A tertiary refrain appears once toward the end of the song made up of
a substring of the last line in the main refrain.
"""
return [self.main_refrain[2][:-7]] * 2
@property
def what_is_code(self):
"""
"What is code?" is repeated many times throughout the song, so let's
make it easy to repeat on its own.
"""
return self.main_refrain[0]
def sing_line(self, line=''):
"""
Outputs a single string with a newline character at the end for proper
formatting.
"""
return '%s\n' % line
def sing_lines(self, lines=[]):
"""
Outputs a list of strings with a newline character at the end of each
string, including the last one.
"""
return '%s\n' % '\n'.join(lines)
def sing(self):
"""
Sings the entire song by printing out every line found within it.
Yes, this is brute force and somewhat ugly, but also simple.
"""
print(self.sing_lines(self.main_refrain))
print(self.sing_lines(self.secondary_refrain))
print(self.sing_line(self.what_is_code))
print('Damn it\n')
print(self.sing_lines(self.first_verse))
print(self.sing_lines(self.main_refrain))
print(self.sing_lines(self.main_refrain))
print(self.sing_lines(self.second_verse))
print(self.sing_lines(self.main_refrain))
print(self.sing_lines(self.main_refrain))
print(self.sing_line(self.what_is_code))
print(self.sing_line(self.what_is_code))
print(self.sing_lines(self.main_refrain))
print(self.sing_lines(self.tertiary_refrain))
print(self.sing_lines(self.third_verse))
print(self.sing_lines(self.main_refrain))
print(self.sing_lines(self.main_refrain))
print('Damn\n')
print(self.sing_lines(self.main_refrain))
print(self.sing_lines(self.main_refrain))
print(self.sing_lines(self.secondary_refrain))
print(self.sing_lines(self.secondary_refrain))
print(self.sing_line(self.what_is_code))
# If this module is being run as a standalone script, output the song
# immediately.
if __name__ == '__main__':
song = WhatIsCode()
song.sing()