Function into Class
Convert 3 functions into a class with 3 methods.
Before
def calculate_area(length, width):
result = length * width
return result
def calculate_perimiter(length, width):
result = 2 * (length + width)
return result
def calculate_volume(length, width, height):
result = length * width * height
return result
After
class Calculator:
@staticmethod
def get_area(len, wid):
return len * wid
@staticmethod
def get_perimiter(len, wid):
return 2 * (len + wid)
@staticmethod
def get_volume(len, wid, hei):
return len * wid * hei
Preview
Command
%scalculate<enter>cget<esc>
O@staticmethod<esc>jj
vglyx<alt-d>xbRkxx
slength|width|height<enter>
bllled%>O<backspace>
class Calculator:
-
%
selects the entire file -
s
searches inside the current selection and creates sub-selections based on a pattern. Inputcalculate
then hit<enter>
to make a selection on all instances of the word -
c
then typeget
to change each "calculate" word into a "get" -
<esc>
to go back to normal mode -
Use
O
to create an empty line above each cursor, write:@staticmethod
-
Hit
<esc>
to go into normal mode. -
jj
moves each cursor down two lines -
vgl
selects the rest of each line past each cursor -
y
copies each selection -
x<alt-d>
selects each cursor's line and deletes the line without copying the selection -
xb
selects the last word of each cursor's line -
R
replaces each selection with the copied selections from earlier -
kxx
moves each cursor up one line and selects that line as well as the line below -
s
brings up a prompt to select sub-selections by a given regex. Typinglength|width|height
followed by<enter>
will select each instance of those 3 words: length, width, and height -
Our cursor is currently at the end of each word. Let's go to the beginning with
b
-
We want to keep the first 3 characters and discard the rest from each of the parameters. To do this, move to the 4th character with
lll
-
Use
e
to select until the end of each word and thend
to delete it -
Select whole file with
%
and indent with>
-
O
creates a newline above and enters Insert mode, then<backspace>
to delete an extra tab -
Write this:
class Calculator: