Skip to content

Commit 4a0a3b6

Browse files
committed
Removing /tmp references so example code runs without modification on windows
1 parent 0fb46a6 commit 4a0a3b6

9 files changed

Lines changed: 29 additions & 16 deletions

File tree

example_code/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.txt
2+
*.bin
3+
*.json

example_code/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
test:
2+
for path in $$(ls ./*.py); \
3+
do \
4+
echo "\n\nRunning $$path"; \
5+
$$path 0</dev/null; \
6+
if [ $$? -ne 0 ]; then \
7+
echo "FAIL: $$path"; \
8+
exit 1; \
9+
fi; \
10+
done

example_code/item_03.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def to_bytes(bytes_or_str):
4747
# Example 5
4848
try:
4949
import os
50-
with open('/tmp/random.bin', 'w') as f:
50+
with open('random.bin', 'w') as f:
5151
f.write(os.urandom(10))
5252
except:
5353
logging.exception('Expected')
@@ -56,5 +56,5 @@ def to_bytes(bytes_or_str):
5656

5757

5858
# Example 6
59-
with open('/tmp/random.bin', 'wb') as f:
59+
with open('random.bin', 'wb') as f:
6060
f.write(os.urandom(10))

example_code/item_09.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@
2222

2323
# Example 1
2424
import random
25-
with open('/tmp/my_file.txt', 'w') as f:
25+
with open('my_file.txt', 'w') as f:
2626
for _ in range(10):
2727
f.write('a' * random.randint(0, 100))
2828
f.write('\n')
2929

30-
value = [len(x) for x in open('/tmp/my_file.txt')]
30+
value = [len(x) for x in open('my_file.txt')]
3131
print(value)
3232

3333

3434
# Example 2
35-
it = (len(x) for x in open('/tmp/my_file.txt'))
35+
it = (len(x) for x in open('my_file.txt'))
3636
print(it)
3737

3838

example_code/item_13.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121

2222

2323
# Example 1
24-
handle = open('/tmp/random_data.txt', 'w', encoding='utf-8')
24+
handle = open('random_data.txt', 'w', encoding='utf-8')
2525
handle.write('success\nand\nnew\nlines')
2626
handle.close()
27-
handle = open('/tmp/random_data.txt') # May raise IOError
27+
handle = open('random_data.txt') # May raise IOError
2828
try:
2929
data = handle.read() # May raise UnicodeDecodeError
3030
finally:
@@ -82,7 +82,7 @@ def divide_json(path):
8282
handle.close() # Always runs
8383

8484
# Everything works
85-
temp_path = '/tmp/random_data.json'
85+
temp_path = 'random_data.json'
8686
handle = open(temp_path, 'w')
8787
handle.write('{"numerator": 1, "denominator": 10}')
8888
handle.close()

example_code/item_16.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ def index_file(handle):
7171
and dedicated to the proposition that all men
7272
are created equal."""
7373

74-
with open('/tmp/address.txt', 'w') as f:
74+
with open('address.txt', 'w') as f:
7575
f.write(address_lines)
7676

7777
from itertools import islice
78-
with open('/tmp/address.txt', 'r') as f:
78+
with open('address.txt', 'r') as f:
7979
it = index_file(f)
8080
results = islice(it, 0, 3)
8181
print(list(results))

example_code/item_17.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def normalize(numbers):
3737

3838

3939
# Example 3
40-
path = '/tmp/my_numbers.txt'
40+
path = 'my_numbers.txt'
4141
with open(path, 'w') as f:
4242
for i in (15, 35, 80):
4343
f.write('%d\n' % i)
@@ -49,13 +49,13 @@ def read_visits(data_path):
4949

5050

5151
# Example 4
52-
it = read_visits('/tmp/my_numbers.txt')
52+
it = read_visits('my_numbers.txt')
5353
percentages = normalize(it)
5454
print(percentages)
5555

5656

5757
# Example 5
58-
it = read_visits('/tmp/my_numbers.txt')
58+
it = read_visits('my_numbers.txt')
5959
print(list(it))
6060
print(list(it)) # Already exhausted
6161

@@ -72,7 +72,7 @@ def normalize_copy(numbers):
7272

7373

7474
# Example 7
75-
it = read_visits('/tmp/my_numbers.txt')
75+
it = read_visits('my_numbers.txt')
7676
percentages = normalize_copy(it)
7777
print(percentages)
7878

example_code/item_43.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def debug_logging(level):
7070

7171

7272
# Example 7
73-
with open('/tmp/my_output.txt', 'w') as handle:
73+
with open('my_output.txt', 'w') as handle:
7474
handle.write('This is some data!')
7575

7676

example_code/item_44.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self):
3535

3636
# Example 3
3737
import pickle
38-
state_path = '/tmp/game_state.bin'
38+
state_path = 'game_state.bin'
3939
with open(state_path, 'wb') as f:
4040
pickle.dump(state, f)
4141

0 commit comments

Comments
 (0)