Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
python-uga
py-training-2017
Commits
28ca6ad1
Commit
28ca6ad1
authored
Nov 26, 2019
by
paugier
Browse files
Use assert instead of unittest + improve presentation on for loops
parent
eabc7703
Pipeline
#32528
passed with stage
in 52 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
ipynb/pres02_basic_statements.ipynb
View file @
28ca6ad1
...
...
@@ -1046,23 +1046,41 @@
### Loops with the keyword `for`
%% Cell type:code id: tags:
```
python
values = range(5)
for value in values:
print(value, end=', ')
for index in range(5):
print(index, end=', ')
```
%% Cell type:code id: tags:
```
python
cities = ["grenoble", "paris", "berlin", "london"]
for city in cities:
print(city, end=", ")
```
%% Cell type:code id: tags:
```
python
# the built-in function enumerate is very useful
for index, value in enumerate(['a', 'b', 'c']):
print('({}, {})'.format(index, value))
for index, city in enumerate(cities):
print('({}, {})'.format(index, city))
cities[index] = city.capitalize()
```
%% Cell type:code id: tags:
```
python
cities
```
%%%% Output: execute_result
['Grenoble', 'Paris', 'Berlin', 'London']
%% Cell type:markdown id: tags:
### Loops: keywords `continue` and `break`
- `continue`: passes the block in the loop and continues the loop.
...
...
ipynb/pres032_functions_final.ipynb
View file @
28ca6ad1
...
...
@@ -224,24 +224,20 @@
nb_elem
=
0
for
e
in
arg0
:
if
e
==
elem
:
nb_elem
+=
1
return
nb_elem
```
import
unittest
class
TestMyStuff
(
unittest
.
TestCase
):
def
test_myfunc1
(
self
):
""" test myfunc1 """
self
.
assertEqual
(
3
,
my_count
(
"abb"
))
self
.
assertEqual
(
1
,
my_count
(
"abb"
,
"a"
))
self
.
assertEqual
(
2
,
my_count
(
"abb"
,
"b"
))
self
.
assertEqual
(
0
,
my_count
(
""
))
self
.
assertEqual
(
0
,
my_count
(
"abb"
,
"c"
))
self
.
assertEqual
(
None
,
None
)
_res
=
unittest
.
TextTestRunner
(
verbosity
=
2
).
run
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestMyStuff
))
%% Cell type:code id: tags:
```
python
assert
my_count
(
"abb"
)
==
3
assert
my_count
(
"abb"
,
"a"
)
==
1
assert
my_count
(
"abb"
,
"b"
)
==
2
assert
my_count
(
""
)
==
0
assert
my_count
(
"abb"
,
"c"
)
==
0
```
%% Cell type:markdown id: tags:
### Default arguments
...
...
@@ -289,36 +285,21 @@
if
l
is
None
:
l
=
[]
l
.
append
(
1
)
print
(
l
)
return
l
import
unittest
class
TestMyStuff
(
unittest
.
TestCase
):
def
test_myfunc1
(
self
):
""" test myfunc1 """
l1
=
[
1
,
2
,
3
]
how_to_use_list_as_default_arg
(
l1
)
self
.
assertEqual
(
l1
,
[
1
,
2
,
3
,
1
])
l
=
how_to_use_list_as_default_arg
()
self
.
assertEqual
(
l
,
[
1
])
_res
=
unittest
.
TextTestRunner
(
verbosity
=
2
).
run
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestMyStuff
))
print
(
"running function"
)
how_to_use_list_as_default_arg
()
how_to_use_list_as_default_arg
()
how_to_use_list_as_default_arg
()
l1
=
[
1
,
2
,
3
]
how_to_use_list_as_default_arg
(
l1
)
l1
```
%%
%% Output: execute_result
%%
Cell type:code id: tags:
[1, 2, 3, 1]
```
python
l1
=
[
1
,
2
,
3
]
how_to_use_list_as_default_arg
(
l1
)
assert
l1
==
[
1
,
2
,
3
,
1
]
assert
how_to_use_list_as_default_arg
()
==
[
1
]
assert
how_to_use_list_as_default_arg
()
==
[
1
]
```
%% Cell type:markdown id: tags:
### Variable-length arguments
...
...
@@ -376,29 +357,31 @@
def
multiply
(
l
,
factor
=
2
):
""" nput a list l and a number a and that multiplies all the elements
of the list by the number. If not set, number is defaulted to 2."""
pass
import
unittest
class
TestMyStuff
(
unittest
.
TestCase
):
def
test_
multiply
(
self
):
""" test multiply """
import
traceback
def
test_
func
(
func
):
try
:
l1
=
[
1
,
2
]
multiply
(
l1
)
self
.
assert
Equal
(
[
2
,
4
]
,
l1
)
multiply
(
l1
,
4
)
self
.
assert
Equal
(
[
8
,
16
]
,
l1
)
func
(
l1
)
assert
l1
==
[
2
,
4
]
func
(
l1
,
4
)
assert
l1
==
[
8
,
16
]
l1
=
[]
multiply
(
l1
)
self
.
assert
Equal
([],
l1
)
func
(
l1
)
assert
l1
==
[]
l1
=
[
'a'
,
'b'
]
multiply
(
l1
)
self
.
assertEqual
([
"aa"
,
"bb"
],
l1
)
_res
=
unittest
.
TextTestRunner
(
verbosity
=
2
).
run
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestMyStuff
))
func
(
l1
)
assert
l1
==
[
"aa"
,
"bb"
]
except
AssertionError
:
traceback
.
print_exc
()
else
:
print
(
"All tests pass!"
)
test_func
(
multiply
)
```
%% Cell type:markdown id: tags:
#### A possible solution:
...
...
@@ -408,40 +391,11 @@
```
python
def
multiply
(
l
,
a
=
2
):
for
i
,
val
in
enumerate
(
l
):
l
[
i
]
=
a
*
val
import
unittest
class
TestMyStuff
(
unittest
.
TestCase
):
def
test_multiply
(
self
):
""" test multiply """
l1
=
[
1
,
2
]
multiply
(
l1
)
self
.
assertEqual
([
2
,
4
],
l1
)
multiply
(
l1
,
4
)
self
.
assertEqual
([
8
,
16
],
l1
)
l1
=
[]
multiply
(
l1
)
self
.
assertEqual
([],
l1
)
l1
=
[
'a'
,
'b'
]
multiply
(
l1
)
self
.
assertEqual
([
"aa"
,
"bb"
],
l1
)
_res
=
unittest
.
TextTestRunner
(
verbosity
=
2
).
run
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestMyStuff
))
l
=
list
(
range
(
3
))
print
(
l
)
func
(
l
)
print
(
l
)
func
(
l
,
4
)
print
(
l
)
l
=
[
'a'
,
'b'
]
func
(
l
,
4
)
print
(
l
)
test_func
(
multiply
)
```
%% Cell type:markdown id: tags:
## `lambda` keyword and anonymous functions
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment