Skip to content

Commit b24f43e

Browse files
committed
Added Item_19
1 parent f6e08b7 commit b24f43e

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

item_19.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env python3
2+
3+
'''Item 19 from Effective Python'''
4+
5+
6+
# Example 1
7+
''' calling a function in Python allows for passing arguments by position '''
8+
print('Example 1:\n==========')
9+
10+
def remainder(number, divisor):
11+
return number % divisor
12+
13+
assert remainder(20, 7) == 6
14+
15+
16+
# Example 2
17+
''' The keyword arguments can be passed in any order as long as all of the
18+
required positional arguments are specified. You can mix and match keyword and
19+
positional arguments. These calls are equivalent '''
20+
print('\nExample 2:\n==========')
21+
22+
remainder(20, 7)
23+
remainder(20, divisor=7)
24+
remainder(number=20, divisor=7)
25+
remainder(divisor=7, number=20)
26+
27+
28+
# Example 3
29+
''' compute the rate of fluid flowing into a vat. If the vat is also on a
30+
scale, then you could use the difference between two weight measurements at two
31+
different times to deter- mine the flow rate '''
32+
print('\nExample 3:\n==========')
33+
34+
def flow_rate(weight_diff, time_diff):
35+
return weight_diff / time_diff
36+
37+
weight_diff = 0.5
38+
time_diff = 3
39+
flow = flow_rate(weight_diff, time_diff)
40+
print('%.3f kg per second' % flow)
41+
42+
43+
# Example 4
44+
''' it'd be helpful to use the last sensor measurements to approximate larger
45+
time scales, like hours or days. You can provide this behavior in the same
46+
function by adding an argument for the time period scaling factor '''
47+
print('\nExample 4:\n==========')
48+
49+
def flow_rate(weight_diff, time_diff, period):
50+
return (weight_diff / time_diff) * period
51+
52+
flow_per_second = flow_rate(weight_diff, time_diff, 1)
53+
print('Flow per second: %s kg' % flow_per_second)
54+
55+
56+
# Example 5
57+
''' To make this less noisy, I can give the period argument a default value '''
58+
print('\nExample 5:\n==========')
59+
60+
def flow_rate(weight_diff, time_diff, period=1):
61+
return (weight_diff / time_diff) * period
62+
63+
flow_per_second = flow_rate(weight_diff, time_diff)
64+
flow_per_hour = flow_rate(weight_diff, time_diff, period=3600)
65+
print('Flow per second: %s kg' % flow_per_second)
66+
print('Flow per hour: %s kg' % flow_per_hour)
67+
68+
69+
# Example 6
70+
''' extend the flow_rate function above to calculate flow rates in weight units
71+
besides kilograms '''
72+
print('\nExample 6:\n==========')
73+
74+
def flow_rate(weight_diff, time_diff, period=1, units_per_kg=1):
75+
return ((weight_diff * units_per_kg) / time_diff) * period
76+
77+
pounds_per_hour = flow_rate(weight_diff, time_diff, period=3600,
78+
units_per_kg=2.2)
79+
80+
81+
# Example 7
82+
''' The only problem with this approach is that optional keyword ­arguments
83+
like period and units_per_kg may still be specified as positional arguments '''
84+
print('\nExample 7:\n==========')
85+
86+
pounds_per_hour = flow_rate(weight_diff, time_diff, 3600, 2.2)

0 commit comments

Comments
 (0)