-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
executable file
·52 lines (47 loc) · 1.74 KB
/
Copy pathfunctions.py
File metadata and controls
executable file
·52 lines (47 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Example of a function
# def get_at_content(dna):
# length = len(dna)
# a_count = dna.count("A")
# t_count = dna.count("T")
# at_content = (a_count + t_count) / length
# return at_content
# 'dna' holds whatever value it is given when the function is called
# a_count, t_count, at_content only exist within the body of the function
# improve the function
# def get_at_content(dna):
# length = len(dna)
#
# # force upper case, so function works with lower case letters
# a_count = dna.upper().count('A')
# t_count = dna.upper().count('T')
# at_content = (a_count + t_count) / length
#
# # round the output number, so it is always readable
# return round(at_content, 2)
# generalize the function
# introduce additional argument, (number of significant figures)
# set a default number of significant figures (sig_figs=2)
def get_at_content(dna, sig_figs=2):
length = len(dna)
a_count = dna.upper().count('A')
t_count = dna.upper().count('T')
at_content = (a_count + t_count) / length
# additional argument is called here
return round(at_content, sig_figs)
# call a function very explicitly
at = get_at_content(dna="ATCGTGACTCG", sig_figs=4)
print(str(at))
# here the default is overwritten with a requested argument
# remember to execute this function with python3 or higher!
# division doesnt work with vanilla python2
# test a function with 'assert'
assert get_at_content("ATGC") == 0.5
# this answer is wrong, which produces
# AssertionError
assert get_at_content("A") == 1
assert get_at_content("G") == 0
assert get_at_content("ATGC") == 0.5
assert get_at_content("AGG") == 0.33
assert get_at_content("AGG", 1) == 0.3
assert get_at_content("AGG", 5) == 0.33333
# the above is a test suite of function tests