File tree Expand file tree Collapse file tree 3 files changed +17
-10
lines changed
Expand file tree Collapse file tree 3 files changed +17
-10
lines changed Original file line number Diff line number Diff line change @@ -400,7 +400,7 @@ ValueError: could not convert string to float: 'hello'
400400 string.
401401- `len (string)` returns string' s length.
402402- We can use `str ` , `int ` and `float ` to convert values to different
403- types.
403+ types.
404404
405405# # Exercises
406406
Original file line number Diff line number Diff line change @@ -203,13 +203,13 @@ if value is None: ... # best
203203## Summary
204204
205205- ` if thing: ` does the same thing as ` if bool(thing): ` . This also
206- works with while loops and most other things that are usually used
207- with Booleans.
206+ works with while loops and most other things that are usually used
207+ with Booleans.
208208- ` bool() ` of most things is True, but ` bool() ` values of None,
209- zero and most empty things are False.
209+ zero and most empty things are False.
210210- Use ` is ` and ` is not ` when comparing to None, ` == ` and ` != ` when
211- checking if a number is zero and rely on the Boolean value
212- when checking if something is empty.
211+ checking if a number is zero and rely on the Boolean value
212+ when checking if something is empty.
213213
214214***
215215
Original file line number Diff line number Diff line change 3131import common
3232
3333
34+ def fix (line ):
35+ return line .rstrip ().expandtabs (4 )
36+
37+
3438def needs_stripping (file ):
3539 with common .slashfix_open (file , 'r' ) as f :
3640 for line in f :
3741 line = line .rstrip ('\n ' )
38- if line != line .rstrip ():
39- # contains trailing whitespace other than '\n'
42+ if line != fix (line ):
4043 return True
4144 return False
4245
@@ -45,7 +48,11 @@ def strip(file):
4548 lines = []
4649 with common .slashfix_open (file , 'r' ) as f :
4750 for line in f :
48- lines .append (line .rstrip ())
51+ line = line .rstrip ('\n ' )
52+ # it's important to do as much as possible here because the
53+ # file may be lost if writing fails, that's why fix() is
54+ # here
55+ lines .append (fix (line ))
4956 with common .slashfix_open (file , 'w' ) as f :
5057 for line in lines :
5158 print (line , file = f )
@@ -57,7 +64,7 @@ def main():
5764 print ("Stripping" , file )
5865 strip (file )
5966 else :
60- print ("No trailing whitespace in" , file )
67+ print ("No trailing whitespace or tabs in" , file )
6168
6269
6370if __name__ == '__main__' :
You can’t perform that action at this time.
0 commit comments