Skip to content

Commit

Permalink
Updating example code to address various errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bslatkin committed May 31, 2020
1 parent 7e75b47 commit 1ce625e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion example_code/item_04.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def close_open_files():
'Today\'s soup is %(soup)s, '
'buy one get two %(oyster)s oysters, '
'and our special entrée is %(special)s.')
old_formatted = template % {
old_formatted = old_template % {
'soup': 'lentil',
'oyster': 'kumamoto',
'special': 'schnitzel',
Expand Down
4 changes: 2 additions & 2 deletions example_code/item_34.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def my_generator():
received = yield 1
print(f'received = {received}')

it = iter(my_generator())
it = my_generator()
output = next(it) # Get first generator output
print(f'output = {output}')

Expand All @@ -90,7 +90,7 @@ def my_generator():


# Example 4
it = iter(my_generator())
it = my_generator()
output = it.send(None) # Get first generator output
print(f'output = {output}')

Expand Down
14 changes: 7 additions & 7 deletions example_code/item_48.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __new__(meta, name, bases, class_dict):
print(f'* Running {meta}.__new__ for {name}')
print('Bases:', bases)
print = pprint
print(class_dict)
print(class_dict)
print = orig_print
return type.__new__(meta, name, bases, class_dict)

Expand Down Expand Up @@ -124,7 +124,7 @@ class BetterPolygon:
def __init_subclass__(cls):
super().__init_subclass__()
if cls.sides < 3:
raise ValueError('Polygons need 3+ sides')
raise ValueError('Polygons need 3+ sides')

@classmethod
def interior_angles(cls):
Expand Down Expand Up @@ -238,24 +238,24 @@ class Filled:
def __init_subclass__(cls):
super().__init_subclass__()
if cls.color not in ('red', 'green', 'blue'):
raise ValueError('Fills need a valid color')
raise ValueError('Fills need a valid color')


# Example 13
class RedTriangle(Filled, Polygon):
class RedTriangle(Filled, BetterPolygon):
color = 'red'
sides = 3

ruddy = RedTriangle()
assert isinstance(ruddy, Filled)
assert isinstance(ruddy, Polygon)
assert isinstance(ruddy, BetterPolygon)


# Example 14
try:
print('Before class')

class BlueLine(Filled, Polygon):
class BlueLine(Filled, BetterPolygon):
color = 'blue'
sides = 2

Expand All @@ -270,7 +270,7 @@ class BlueLine(Filled, Polygon):
try:
print('Before class')

class BeigeSquare(Filled, Polygon):
class BeigeSquare(Filled, BetterPolygon):
color = 'beige'
sides = 4

Expand Down
4 changes: 4 additions & 0 deletions example_code/item_58.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ def game_logic(state, neighbors):


# Example 5
# Clear the sentinel object from the out queue
for _ in out_queue:
pass

# Restore the working version of this function
def game_logic(state, neighbors):
if state == ALIVE:
Expand Down
8 changes: 4 additions & 4 deletions example_code/item_78.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def do_rounds(database, species, *, utcnow=datetime.utcnow):

for name, last_mealtime in animals:
if (now - last_mealtime) > feeding_timedelta:
feed_func(database, name, now)
feed_animal(database, name, now)
fed += 1

return fed
Expand Down Expand Up @@ -297,9 +297,9 @@ def do_rounds(database, species, *, utcnow=datetime.utcnow):
result = do_rounds(database, 'Meerkat', utcnow=now_func)
assert result == 2

food_func.assert_called_once_with(database, 'Meerkat')
animals_func.assert_called_once_with(database, 'Meerkat')
feed_func.assert_has_calls(
get_food_period.assert_called_once_with(database, 'Meerkat')
get_animals.assert_called_once_with(database, 'Meerkat')
feed_animal.assert_has_calls(
[
call(database, 'Spot', now_func.return_value),
call(database, 'Fluffy', now_func.return_value),
Expand Down

0 comments on commit 1ce625e

Please sign in to comment.