diff --git a/.nojekyll b/.nojekyll new file mode 100644 index 000000000..e69de29bb diff --git a/404.html b/404.html new file mode 100644 index 000000000..5163d6229 --- /dev/null +++ b/404.html @@ -0,0 +1,2169 @@ + + + +
+ + + + + + + + + + + + + + +unsafe_
functionLearning objectives
+if
and assert
in function writingassert
within functions¶Within functions, assert
is used for:
assert
differs between debug and release¶$ cat assert.py
+assert 1 == 2
+
+$ python -O assert.py
+
+$ python assert.py
+Traceback (most recent call last):
+ File "/home/richel/assert.py", line 1, in <module>
+ assert 1 == 2
+AssertionError
+
assert
as a stub¶assert
in debug mode¶def align_two_dna_sequences(dna_sequences):
+ """Internal function to align two DNA sequences"""
+ assert len(dna_sequences) == 2 # TODO
+
Superior to documentation, as it cannot be ignored.
+++Assert liberally to document internal assumptions and invariants
+@sutter2004cpp chapter 68.
+
assert
to document assumptions a developer makes¶Learning objectives
+if
and assert
in function writingHow do I write functions [1] that are:
+References;
+algorithm == function
,
+ as the definition of an algorithm is
+ 'a step-by-step procedure for solving a problem or accomplishing some
+ end' [dictionary definition]
A good function ...
+. . .
+[Ram, 2013]
[tidyverse style guideline of functions]
[Martin, 2009]
+ @cpp_core_guidelines_functions
+ [tidyverse style guideline of functions]
[Martin, 2009]
+ @cpp_core_guidelines_functions [tidyverse style guideline of functions]
+ @pep_20 @hitchhikers_guide_to_python_general_concepts[Martin, 2009]
+ @cpp_core_guidelines_functions
+ [tidyverse style guideline of functions]
[tidyverse style guideline of functions]
[Martin, 2009]
+ @cpp_core_guidelines_functions
+ [tidyverse style guideline of functions]
@pep_20+ ++
def sort_1(x):
+ """Sort list `x` in-place.
+
+ Returns nothing
+ """
+
+def sort_2(x):
+ """Sort list `x`.
+
+ Returns the sorted list.
+ """
+
+assert sort_1.__doc__
+assert sort_2.__doc__
+
Mandatory in some contexts [Ram, 2013]
+[tidyverse style guideline of functions]
.
++ +There are only two hard things in Computer Science: +cache invalidation and naming things
+Phil Karlton
+
[tidyverse style guideline of functions]
[Martin, 2009]
[Martin, 2009]
[Martin, 2009]
[Martin, 2009]
[Martin, 2009]
Could you give examples of bad function names?
+. . .
+calculate
: calculates what?. . .
+calc_bfgt
: calculates what??. . .
+prime
: a prime number is a data type. What does this function do?. . .
+needleman_wunch
: this is a technique to get a DNA alignment.. . .
+Imagine two DNA sequences:
+ +How would you call the algorithm that detects the location
+of the *
(but not of the x
, as the *
comes earlier)?
get_first_mismatch_pos
, get_first_mismatch_locus
,
+ find_first_mismatch_pos
, find_first_mismatch_locus
Imagine two DNA sequences:
+ +How would you call the algorithm that detects all the locations of the *
s?
find_mismatch_positions
, find_mismatch_loci
,
+ get_mismatch_positions
, get_mismatch_loci
Imagine two DNA sequences:
+ +How would you call the algorithm that makes the sequences
+have as much similarities as possible, by possibly inserting a -
align_seqs
, align_sequences
, align_dna_sequences
, align_dna_seqs
,
+ calc_aligned_seqs
, get_aligned_seqs
Comment on this function from Pythonpool:
+i=2
+
+def Prime(no, i):
+ if no == i:
+ return True
+ elif no % i == 0:
+ return False
+ return Prime(no, i + 1)
+
. . .
+Function names start with lowercase character, name does not start with a +verb, input is not checked, clumsy interface:
+assert Prime(2, 2)
+assert Prime(3, 2)
+assert Prime(3, 3) # Nothing stops me!
+assert not Prime(4, 2)
+assert Prime(5, 2)
+
The classic on refactoring is @fowler2018refactoring.
+Comment on this function again:
+def is_prime(no, i = 2):
+ assert isinstance(no, int)
+ assert isinstance(i, int)
+ if no == i:
+ return True
+ elif no % i == 0:
+ return False
+ return is_prime(no, i + 1)
+
. . .
+assert is_prime(2)
+assert is_prime(2, 2) # Nothing stops me!
+assert is_prime(3)
+assert not is_prime(4)
+assert is_prime(5)
+
Comment on this function again:
+def is_prime(no):
+ if not isinstance(no, int):
+ raise TypeError("'no' must be integer")
+ return is_prime_internal(no)
+
+def is_prime_internal(no, i = 2):
+ assert isinstance(no, int)
+ assert isinstance(i, int)
+ if no == i:
+ return True
+ elif no % i == 0:
+ return False
+ return is_prime_internal(no, i + 1)
+
+assert is_prime(2)
+assert is_prime(3)
+assert not is_prime(4)
+assert is_prime(5)
+
. . .
+I think it is OK, please correct me :-)
+A function should perform a single logical operation [CppCore F.2], +hence don't:
+ +Do:
+ +You rarely need and
in a function name. Possible exception:
+mean and standard deviation
A good function:
+Imagine two DNA sequences:
+ +The function align_dna_seqs
aligns two DNA sequences to this:
Which tests would you write?
+assert align_dna_seqs(
+ "AAACCCGGGTTT", "ATACCCGGGTAT"
+ ) == {
+ "AAACCCGGGTTT", "ATACC-GGGTTT"
+ }
+assert align_dna_seqs(
+ { "AAACCCGGGTTT", "ATACCCGGGTAT" }
+) ==
+ { "AAACCCGGGTTT", "ATACC-GGGTTT" }
+
A good function:
+[dictionary definition]
+ https://www.merriam-webster.com/dictionary/algorithm[Martin, 2009]
Martin, Robert C.
+ Clean code: a handbook of agile software craftsmanship. Pearson Education, 2009.[Ram, 2013]
Ram, K. "rOpenSci-open tools for open science."
+ AGU Fall Meeting Abstracts. Vol. 2013. 2013.[tidyverse style guideline of functions]
+ https://style.tidyverse.org/functions.html[CppCore F.2]
F.2: A function should perform a single logical operation,
+ https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-logicalLearning objectives
+if
and assert
in function writingHow do I write functions [1] that are:
+References;
+algorithm == function
,
+ as the definition of an algorithm is
+ 'a step-by-step procedure for solving a problem or
+ accomplishing some end' @dictionary_algorithm[2] pick any vague definition
+assert
helpsDescription | +HTML | +QMD | +|
---|---|---|---|
Algorithms lecture | +algorithms_lecture.pdf |
+algorithms_lecture.html |
+here | +
the code layout
-ed items will become links in the future
Learning objectives
+if
and assert
in function writingn |
+n! |
+
---|---|
0 | +1 | +
1 | +1 | +
2 | +2 * 1 = 2 | +
3 | +3 * 2 * 1 = 6 | +
4 | +4 * 3 * 2 * 1 = 24 | +
5 | +5 * 4! |
+
n |
+n * (n-1)! |
+
Fibonacci sequence:
+N | +0 | +1 | +2 | +3 | +4 | +5 | +6 | +7 | +8 | +9 | +10 | +
---|---|---|---|---|---|---|---|---|---|---|---|
Fn |
+0 | +1 | +1 | +2 | +3 | +5 | +8 | +13 | +21 | +34 | +55 | +
N | +Fn |
+
---|---|
0 | +0 | +
1 | +1 | +
2 | +1 | +
3 | +Fn(1) + Fn(2) |
+
n |
+Fn(n - 2) + Fn(n - 1) |
+