File tree 3 files changed +17
-10
lines changed
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'
400
400
string.
401
401
- `len (string)` returns string' s length.
402
402
- We can use `str ` , `int ` and `float ` to convert values to different
403
- types.
403
+ types.
404
404
405
405
# # Exercises
406
406
Original file line number Diff line number Diff line change @@ -203,13 +203,13 @@ if value is None: ... # best
203
203
## Summary
204
204
205
205
- ` 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.
208
208
- ` 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.
210
210
- 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.
213
213
214
214
***
215
215
Original file line number Diff line number Diff line change 31
31
import common
32
32
33
33
34
+ def fix (line ):
35
+ return line .rstrip ().expandtabs (4 )
36
+
37
+
34
38
def needs_stripping (file ):
35
39
with common .slashfix_open (file , 'r' ) as f :
36
40
for line in f :
37
41
line = line .rstrip ('\n ' )
38
- if line != line .rstrip ():
39
- # contains trailing whitespace other than '\n'
42
+ if line != fix (line ):
40
43
return True
41
44
return False
42
45
@@ -45,7 +48,11 @@ def strip(file):
45
48
lines = []
46
49
with common .slashfix_open (file , 'r' ) as f :
47
50
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 ))
49
56
with common .slashfix_open (file , 'w' ) as f :
50
57
for line in lines :
51
58
print (line , file = f )
@@ -57,7 +64,7 @@ def main():
57
64
print ("Stripping" , file )
58
65
strip (file )
59
66
else :
60
- print ("No trailing whitespace in" , file )
67
+ print ("No trailing whitespace or tabs in" , file )
61
68
62
69
63
70
if __name__ == '__main__' :
You can’t perform that action at this time.
0 commit comments