Skip to content

Commit ee641c1

Browse files
committed
ch04
1 parent 8b802c5 commit ee641c1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+601
-0
lines changed

ch04/arguments.combined.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# arguments.combined.py
2+
def func(a, b, c, d, e, f):
3+
print(a, b, c, d, e, f)
4+
5+
func(1, *(2, 3), f=6, *(4, 5))
6+
func(*(1, 2), e=5, *(3, 4), f=6)
7+
func(1, **{'b': 2, 'c': 3}, d=4, **{'e': 5, 'f': 6})
8+
func(c=3, *(1, 2), **{'d': 4}, e=5, **{'f': 6})

ch04/arguments.keyword.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# arguments.keyword.py
2+
def func(a, b, c):
3+
print(a, b, c)
4+
5+
func(a=1, c=2, b=3) # prints: 1 3 2

ch04/arguments.multiple.value.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# arguments.multiple.value.py
2+
def func(a, b, c):
3+
print(a, b, c)
4+
5+
func(2, 3, a=1)
6+
7+
"""
8+
$ python arguments.multiple.value.py
9+
Traceback (most recent call last):
10+
File "arguments.multiple.value.py", line 5, in <module>
11+
func(2, 3, a=1)
12+
TypeError: func() got multiple values for argument 'a'
13+
"""
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# arguments.positional.keyword.py
2+
def func(a, b, c):
3+
print(a, b, c)
4+
5+
func(42, b=1, c=2)
6+
7+
func(b=1, c=2, 42) # positional argument after keyword arguments
8+
9+
"""
10+
$ python arguments.positional.keyword.py
11+
File "arguments.positional.keyword.py", line 7
12+
func(b=1, c=2, 42) # positional argument after keyword arguments
13+
^
14+
SyntaxError: positional argument follows keyword argument
15+
"""

ch04/arguments.positional.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# arguments.positional.py
2+
def func(a, b, c):
3+
print(a, b, c)
4+
5+
func(1, 2, 3) # prints: 1 2 3

ch04/arguments.unpack.dict.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# arguments.unpack.dict.py
2+
def func(a, b, c):
3+
print(a, b, c)
4+
5+
values = {'b': 1, 'c': 2, 'a': 42}
6+
func(**values) # equivalent to func(b=1, c=2, a=42)

ch04/arguments.unpack.iterable.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# arguments.unpack.iterable.py
2+
def func(a, b, c):
3+
print(a, b, c)
4+
5+
values = (1, 3, -7)
6+
func(*values) # equivalent to: func(1, 3, -7)

ch04/data.science.example.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# data.science.example.py
2+
def do_report(data_source):
3+
# fetch and prepare data
4+
data = fetch_data(data_source)
5+
parsed_data = parse_data(data)
6+
filtered_data = filter_data(parsed_data)
7+
polished_data = polish_data(filtered_data)
8+
9+
# run algorithms on data
10+
final_data = analyse(polished_data)
11+
12+
# create and return report
13+
report = Report(final_data)
14+
return report
15+
16+
17+
if __name__ == "__main__":
18+
19+
print(
20+
"Please don't call the `do_report` function. "
21+
"It's just and example."
22+
)

ch04/docstrings.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# docstrings.py
2+
def square(n):
3+
"""Return the square of a number n. """
4+
return n ** 2
5+
6+
def get_username(userid):
7+
"""Return the username of a user given their id. """
8+
return db.get(user_id=userid).username
9+
10+
11+
def connect(host, port, user, password):
12+
"""Connect to a database.
13+
14+
Connect to a PostgreSQL database directly, using the given
15+
parameters.
16+
17+
:param host: The host IP.
18+
:param port: The desired port.
19+
:param user: The connection username.
20+
:param password: The connection password.
21+
:return: The connection object.
22+
"""
23+
# body of the function here...
24+
return connection

ch04/filter.lambda.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# filter.lambda.py
2+
def get_multiples_of_five(n):
3+
return list(filter(lambda k: not k % 5, range(n)))
4+
5+
6+
print(get_multiples_of_five(50))

0 commit comments

Comments
 (0)