Debugging - Chapter 8 - Guardian Pattern, and Protecting From Code
### Debugging Assignment ###
# code to be fixed:
han = open('emaildata')
for line in han:
line = line.rstrip() # removed white spaces
words = line.split() # spliting the line
if words[0] != 'From':
continue
print(words[2])
# --------
# now after fixing:
han = open('emaildata')
for line in han:
line = line.rstrip() # removed white spaces
print('LINE: ', line) # print each line to see the whole process going on before T.B.
words = line.split() # spliting the line
print('WORDS: ', words) # print the splitted line to check at which point program breaks
# Guardian Pattern
if len(words) < 1: # fixing the issue by setting the minimum leve for range to be appropriate.
continue
if words[0] != 'From':
print('ignore')
continue
print(words[2])
# Final shape of a clean code
han = open('emaildata')
for line in han:
line = line.rstrip()
words = line.split()
if len(words) < 1:
continue
if words[0] != 'From':
continue
print(words[2])
# ------------------ alternative approach to creating a guardian
# Alternatively to writing a code for Guradian, there can be to "protect
han = open('emaildata')
for line in han:
line = line.rstrip() # removed white spaces
print('LINE: ', line)
if line == '':
print('skipskipskipskipskipskipskipskipskipskipskipskipskipskipskipskipskipskipskipskip')
continue
words = line.split() # spliting the line
print('WORDS: ', words)
if words[0] != 'From':
print('ignore')
continue
print(words[2])
# Final code
han = open('emaildata')
for line in han:
line = line.rstrip()
if line == '': # Protecting approach: code for skipping the empty line
continue
words = line.split()
if words[0] != 'From':
continue
print(words[2])
# -------------- making guardian a bit stronger
# Guardian approach: making the guardian a bit stronger
# this depends on you judgement and your assumption about the readability of data.
han = open('emaildata')
for line in han:
line = line.rstrip()
words = line.split()
# guardian a bit stronger
if len(words) < 3: # changing minimum number from 1 to 3.
continue
if words[0] != 'From':
continue
print(words[2])
# -------------- making guardian in compound statement
# Guardian approach: creating code for guradian in a compound statement.
# Remamber, the order of code is really important from left to right.
# if first condition blows-up, the programme will give T.B. so be careful.
han = open('emaildata')
for line in han:
line = line.rstrip()
words = line.split()
# guardian in a compound statement: Order is very important (from left to right).
if len(words) < 3 or words[0] != 'From': # means either one of the condition is true, it will skip the line.
continue
print(words[2])
##### SUMMARY #####
# As its observed, its not always about a wrong line,
# debugging can be about adding additional code to support the code already written.
# there are multiple ways to fix the issue.
# its always flexible, and depends upon your own capability to write a cleaner, better code.
# Adding a print statement to check upon whats going on is really helpful sometimes.
### Debugging Assignment ###
# code to be fixed:
han = open('emaildata')
for line in han:
line = line.rstrip() # removed white spaces
words = line.split() # spliting the line
if words[0] != 'From':
continue
print(words[2])
# --------
# now after fixing:
han = open('emaildata')
for line in han:
line = line.rstrip() # removed white spaces
print('LINE: ', line) # print each line to see the whole process going on before T.B.
words = line.split() # spliting the line
print('WORDS: ', words) # print the splitted line to check at which point program breaks
# Guardian Pattern
if len(words) < 1: # fixing the issue by setting the minimum leve for range to be appropriate.
continue
if words[0] != 'From':
print('ignore')
continue
print(words[2])
# Final shape of a clean code
han = open('emaildata')
for line in han:
line = line.rstrip()
words = line.split()
if len(words) < 1:
continue
if words[0] != 'From':
continue
print(words[2])
# ------------------ alternative approach to creating a guardian
# Alternatively to writing a code for Guradian, there can be to "protect
han = open('emaildata')
for line in han:
line = line.rstrip() # removed white spaces
print('LINE: ', line)
if line == '':
print('skipskipskipskipskipskipskipskipskipskipskipskipskipskipskipskipskipskipskipskip')
continue
words = line.split() # spliting the line
print('WORDS: ', words)
if words[0] != 'From':
print('ignore')
continue
print(words[2])
# Final code
han = open('emaildata')
for line in han:
line = line.rstrip()
if line == '': # Protecting approach: code for skipping the empty line
continue
words = line.split()
if words[0] != 'From':
continue
print(words[2])
# -------------- making guardian a bit stronger
# Guardian approach: making the guardian a bit stronger
# this depends on you judgement and your assumption about the readability of data.
han = open('emaildata')
for line in han:
line = line.rstrip()
words = line.split()
# guardian a bit stronger
if len(words) < 3: # changing minimum number from 1 to 3.
continue
if words[0] != 'From':
continue
print(words[2])
# -------------- making guardian in compound statement
# Guardian approach: creating code for guradian in a compound statement.
# Remamber, the order of code is really important from left to right.
# if first condition blows-up, the programme will give T.B. so be careful.
han = open('emaildata')
for line in han:
line = line.rstrip()
words = line.split()
# guardian in a compound statement: Order is very important (from left to right).
if len(words) < 3 or words[0] != 'From': # means either one of the condition is true, it will skip the line.
continue
print(words[2])
##### SUMMARY #####
# As its observed, its not always about a wrong line,
# debugging can be about adding additional code to support the code already written.
# there are multiple ways to fix the issue.
# its always flexible, and depends upon your own capability to write a cleaner, better code.
# Adding a print statement to check upon whats going on is really helpful sometimes.
Comments
Post a Comment