Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/functions/test_function_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ def breakfast(ham: str, eggs: str = 'eggs') -> str:
"""
return ham + ' and ' + eggs

def number(n: int) -> bool:
return n > 10

def test_function_annotations():
"""Function Annotations."""

assert breakfast.__annotations__ == {'eggs': str, 'ham': str, 'return': str}
assert number.__annotations__ == {'n': int, 'return': bool}
1 change: 1 addition & 0 deletions src/functions/test_function_arbitrary_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ def concat(*args, sep='/'):

assert concat('earth', 'mars', 'venus') == 'earth/mars/venus'
assert concat('earth', 'mars', 'venus', sep='.') == 'earth.mars.venus'
assert concat('earth', 'mars', 'venus', 'mercury', 'jupyter', sep=' ') == 'earth mars venus mercury jupyter'
4 changes: 2 additions & 2 deletions src/functions/test_function_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def greeting_with_div_p(name):
return "Hello, {0}!".format(name)

assert greeting_with_div_p('John') == '<div><p>Hello, John!</p></div>'

# One important thing to notice here is that the order of setting our decorators matters.
# If the order was different in the example above, the output would have been different.

Expand All @@ -83,9 +82,10 @@ def func_wrapper(name):
return func_wrapper
return tags_decorator

@tags('body')
@tags('div')
@tags('p')
def greeting_with_tags(name):
return "Hello, {0}!".format(name)

assert greeting_with_tags('John') == '<div><p>Hello, John!</p></div>'
assert greeting_with_tags('John') == '<body><div><p>Hello, John!</p></div></body>'
1 change: 1 addition & 0 deletions src/functions/test_function_default_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ def test_default_function_arguments():
# We may also want to override the second argument by using the following function calls.
assert power_of(3, 2) == 9
assert power_of(3, 3) == 27
assert power_of(2) == 4
6 changes: 4 additions & 2 deletions src/functions/test_function_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ def greet_one_more(name):

def call_func(func):
other_name = 'John'
return func(other_name)
output = 'Welcome! '
output += func(other_name)
return output

assert call_func(greet_one_more) == 'Hello, John'
assert call_func(greet_one_more) == 'Welcome! Hello, John'

# Functions can return other functions. In other words, functions generating other functions.

Expand Down
6 changes: 4 additions & 2 deletions src/functions/test_function_keyword_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,19 @@ def function_with_one_argument(number):
# (*name must occur before **name.) For example, if we define a function like this:
def test_function(first_param, *arguments, **keywords):
"""This function accepts its arguments through "arguments" tuple and keywords dictionary."""
assert first_param == 'first param'
assert arguments == ('second param', 'third param')
assert first_param == 'new param'
assert arguments == ('first param','second param', 'third param')
assert keywords == {
'fourth_param_name': 'fourth named param',
'fifth_param_name': 'fifth named param'
}

test_function(
'new param',
'first param',
'second param',
'third param',
fourth_param_name='fourth named param',
fifth_param_name='fifth named param',

)
8 changes: 4 additions & 4 deletions src/functions/test_function_unpacking_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def test_function_unpacking_arguments():
assert list(range(*arguments_list)) == [3, 4, 5]

# In the same fashion, dictionaries can deliver keyword arguments with the **-operator:
def function_that_receives_names_arguments(first_word, second_word):
return first_word + ', ' + second_word + '!'
def function_that_receives_names_arguments(first_word, second_word, third_word):
return first_word + ', ' + second_word + '! ' + third_word

arguments_dictionary = {'first_word': 'Hello', 'second_word': 'World'}
assert function_that_receives_names_arguments(**arguments_dictionary) == 'Hello, World!'
arguments_dictionary = {'first_word': 'Hello', 'second_word': 'World', 'third_word': 'Bye'}
assert function_that_receives_names_arguments(**arguments_dictionary) == 'Hello, World! Bye'
4 changes: 2 additions & 2 deletions src/functions/test_lambda_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ def make_increment_function(delta):
# Another use of lambda is to pass a small function as an argument.
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
# Sort pairs by text key.
pairs.sort(key=lambda pair: pair[1])
pairs.sort(key=lambda pair: pair[0], reverse = True)

assert pairs == [(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
assert pairs == [(4, 'four'), (3, 'three'), (2, 'two'), (1, 'one')]