Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Programming course
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
14
Issues
14
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
Operations
Operations
Incidents
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
courses
Programming course
Commits
6383bfaa
Commit
6383bfaa
authored
Nov 26, 2018
by
Mihai Lefter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Started day2 tex
parent
3ef62844
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
347 additions
and
0 deletions
+347
-0
more_python/more_01/Makefile
more_python/more_01/Makefile
+1
-0
more_python/more_01/beamerthemelumc.sty
more_python/more_01/beamerthemelumc.sty
+1
-0
more_python/more_01/logos
more_python/more_01/logos
+1
-0
more_python/more_01/more_01.tex
more_python/more_01/more_01.tex
+299
-0
more_python/more_01/seq_toolbox.py
more_python/more_01/seq_toolbox.py
+13
-0
more_python/more_01/seq_toolbox_02.py
more_python/more_01/seq_toolbox_02.py
+16
-0
more_python/more_01/seq_toolbox_03.py
more_python/more_01/seq_toolbox_03.py
+16
-0
No files found.
more_python/more_01/Makefile
0 → 120000
View file @
6383bfaa
../../.presentation/Makefile
\ No newline at end of file
more_python/more_01/beamerthemelumc.sty
0 → 120000
View file @
6383bfaa
../../.presentation/beamerthemelumc.sty
\ No newline at end of file
more_python/more_01/logos
0 → 120000
View file @
6383bfaa
../../.presentation/logos
\ No newline at end of file
more_python/more_01/more_01.tex
0 → 100644
View file @
6383bfaa
\documentclass
[aspectratio=1610,slidestop]
{
beamer
}
\author
{
Mihai Lefter
}
\title
{
Python Programming
}
\providecommand
{
\mySubTitle
}{
String methods, error and exceptions
}
\providecommand
{
\myConference
}{
Programming Course
}
\providecommand
{
\myDate
}{
27-11-2018
}
\providecommand
{
\myGroup
}{}
\providecommand
{
\myDepartment
}{}
\providecommand
{
\myCenter
}{}
\usetheme
{
lumc
}
\usepackage
{
minted
}
\usepackage
{
tikz
}
\usepackage
[many]
{
tcolorbox
}
\definecolor
{
monokaibg
}{
HTML
}{
272822
}
\definecolor
{
emailc
}{
HTML
}{
1e90FF
}
\definecolor
{
scriptback
}{
HTML
}{
CDECF0
}
\newenvironment
{
ipython
}
{
\begin{tcolorbox}
[title=IPython,
title filled=false,
fonttitle=
\scriptsize
,
fontupper=
\footnotesize
,
enhanced,
colback=monokaibg,
drop small lifted shadow,
boxrule=0.1mm,
left=0.1cm,
arc=0mm,
colframe=black]
}
{
\end{tcolorbox}
}
\newenvironment
{
terminal
}
{
\begin{tcolorbox}
[title=terminal,
title filled=false,
fonttitle=
\scriptsize
,
fontupper=
\footnotesize
,
enhanced,
colback=monokaibg,
drop small lifted shadow,
boxrule=0.1mm,
left=0.1cm,
arc=0mm,
colframe=black]
}
{
\end{tcolorbox}
}
\newcommand
{
\hrefcc
}
[2]
{
\textcolor
{
#1
}{
\href
{
#2
}{
#2
}}}
\newcommand
{
\hrefc
}
[3]
{
\textcolor
{
#1
}{
\href
{
#2
}{
#3
}}}
\newcounter
{
cntr
}
\renewcommand
{
\thecntr
}{
\texttt
{
[
\arabic
{
cntr
}
]
}}
\newenvironment
{
pythonin
}
[1]
{
\VerbatimEnvironment
\begin{minipage}
[t]
{
0.11
\linewidth
}
\textcolor
{
green
}{
\texttt
{{
\refstepcounter
{
cntr
}
\label
{
#1
}
In
\thecntr
:
}}}
\end{minipage}
\begin{minipage}
[t]
{
0.89
\linewidth
}
\begin{minted}
[
breaklines=true,style=monokai]
{
#1
}}
{
\end{minted}
\end{minipage}
}
\newenvironment
{
pythonout
}
[1]
{
\VerbatimEnvironment
\addtocounter
{
cntr
}{
-1
}
\begin{minipage}
[t]
{
0.11
\linewidth
}
\textcolor
{
red
}{
\texttt
{{
\refstepcounter
{
cntr
}
\label
{
#1
}
Out
\thecntr
:
}}}
\end{minipage}
\begin{minipage}
[t]
{
0.89
\linewidth
}
\begin{minted}
[
breaklines=true,style=monokai]
{
#1
}}
{
\end{minted}
\end{minipage}
}
\newenvironment
{
pythonerr
}
[1]
{
\VerbatimEnvironment
\begin{minted}
[
breaklines=true,style=monokai]
{
#1
}}
{
\end{minted}
}
\newenvironment
{
pythonfile
}
[1]
{
\begin{tcolorbox}
[title=#1,
title filled=false,
coltitle=LUMCDonkerblauw,
fonttitle=
\scriptsize
,
fontupper=
\footnotesize
,
enhanced,
drop small lifted shadow,
boxrule=0.1mm,
leftrule=5mm,
rulecolor=white,
left=0.1cm,
colback=white!92!black,
colframe=scriptback]
}
{
\end{tcolorbox}
}
\begin{document}
% This disables the \pause command, handy in the editing phase.
%\renewcommand{\pause}{}
% Make the title slide.
\makeTitleSlide
{
\includegraphics
[height=3.5cm]
{
../../images/Python.pdf
}}
% First page of the presentation.
\section
{
Introduction
}
\makeTableOfContents
\subsection
{
Let's start with a simple GC calculator
}
\begin{pframe}
\begin{pythonfile}
{
seq
\_
toolbox.py
}
\begin{minted}
[linenos]
{
python
}
def calc
_
gc
_
percent(seq):
at
_
count, gc
_
count = 0, 0
for char in seq:
if char in ('A', 'T'):
at
_
count += 1
elif char in ('G', 'C'):
gc
_
count += 1
return gc
_
count * 100.0 / (gc
_
count + at
_
count)
print("The sequence 'CAGG' has a
%GC of {:.2f}".format(
calc
_
gc
_
percent("CAGG")))
\end{minted}
\end{pythonfile}
\pause
Our script is nice and dandy, but we don't want to edit the source file everytime we calculate a sequence's GC.
\end{pframe}
\section
{
The standard library
}
\begin{pframe}
\begin{itemize}
\item
A collection of Python modules (or functions, for now) that comes packaged with a default Python installation.
\item
They're not part of the language per se, more like a batteries included thing.
\end{itemize}
\end{pframe}
\subsection
{
Our first standard library module: sys
}
\begin{pframe}
We'll start by using the simple sys module to make our script more flexible.
Standard library (and other modules, as we'll see later) can be used via the import statement, for example:
\end{pframe}
\subsection
{
Improving our script with sys.argv
}
\begin{pframe}
\begin{pythonfile}
{
seq
\_
toolbox.py
}
\begin{minted}
[linenos]
{
python
}
import sys
def calc
_
gc
_
percent(seq):
at
_
count, gc
_
count = 0, 0
for char in seq:
if char in ('A', 'T'):
at
_
count += 1
elif char in ('G', 'C'):
gc
_
count += 1
return gc
_
count * 100.0 / (gc
_
count + at
_
count)
input
_
seq = sys.argv[1]
print("The sequence '
{}
' has a
%GC of {:.2f}".format(
input
_
seq, calc
_
gc
_
percent(input
_
seq)))
\end{minted}
\end{pythonfile}
\end{pframe}
\section
{
String methods
}
\begin{pframe}
\end{pframe}
\subsection
{
Improving our script with upper()
}
\begin{pframe}
\begin{pythonfile}
{
seq
\_
toolbox.py
}
\begin{minted}
[linenos]
{
python
}
import sys
def calc
_
gc
_
percent(seq):
at
_
count, gc
_
count = 0, 0
for char in seq.upper():
if char in ('A', 'T'):
at
_
count += 1
elif char in ('G', 'C'):
gc
_
count += 1
return gc
_
count * 100.0 / (gc
_
count + at
_
count)
input
_
seq = sys.argv[1]
print("The sequence '
{}
' has a
%GC of {:.2f}".format(
input
_
seq, calc
_
gc
_
percent(input
_
seq)))
\end{minted}
\end{pythonfile}
\end{pframe}
\section
{
Improving our script with comments and docstrings
}
\begin{pframe}
\vspace
{
-1.2cm
}
\begin{pythonfile}
{
seq
\_
toolbox.py
}
\begin{tiny}
\begin{minted}
[linenos]
{
python
}
import sys
def calc
_
gc
_
percent(seq):
"""
Calculates the GC percentage of the given sequence.
Arguments:
- seq - the input sequence (string).
Returns:
- GC percentage (float).
The returned value is always <= 100.0
"""
at
_
count, gc
_
count = 0, 0
# Change input to all caps to allow for non-capital
# input sequence.
for char in seq.upper():
if char in ('A', 'T'):
at
_
count += 1
elif char in ('G', 'C'):
gc
_
count += 1
return gc
_
count * 100.0 / (gc
_
count + at
_
count)
input
_
seq = sys.argv[1]
print("The sequence '
{}
' has a
%GC of {:.2f}".format(
input
_
seq, calc
_
gc
_
percent(input
_
seq)))
\end{minted}
\end{scriptsize}
\end{pythonfile}
}
\end{pframe}
\section
{
Errors and exceptions
}
\subsection
{
Improving our script by handling corner cases
}
\begin{pframe}
\begin{pythonfile}
{
seq
\_
toolbox.py
}
\begin{tiny}
\begin{minted}
[linenos]
{
python
}
def calc
_
gc
_
percent(seq):
"""
Calculates the GC percentage of the given sequence.
...
The returned value is always <= 100.0
"""
at
_
count, gc
_
count = 0, 0
# Change input to all caps to allow for non-capital
# input sequence.
for char in seq.upper():
if char in ('A', 'T'):
at
_
count += 1
elif char in ('G', 'C'):
gc
_
count += 1
else:
raise ValueError(
"Unexpeced character found:
{}
. Only "
"ACTGs are allowed.".format(char))
# Corner case handling: empty input sequence.
try:
return gc
_
count * 100.0 / (gc
_
count + at
_
count)
except ZeroDivisionError:
return 0.0
\end{minted}
\end{scriptsize}
\end{pythonfile}
}
\end{pframe}
% Make the acknowledgements slide.
\makeAcknowledgementsSlide
{
\begin{tabular}
{
ll
}
Martijn Vermaat
\\
Jeroen Laros
\\
Jonathan Vis
\end{tabular}
}
\end{document}
more_python/more_01/seq_toolbox.py
0 → 100644
View file @
6383bfaa
def
calc_gc_percent
(
seq
):
at_count
,
gc_count
=
0
,
0
for
char
in
seq
:
if
char
in
(
'A'
,
'T'
):
at_count
+=
1
elif
char
in
(
'G'
,
'C'
):
gc_count
+=
1
return
gc_count
*
100.0
/
(
gc_count
+
at_count
)
print
(
"The sequence 'CAGG' has a %GC of {:.2f}"
.
format
(
calc_gc_percent
(
"CAGG"
)))
more_python/more_01/seq_toolbox_02.py
0 → 100644
View file @
6383bfaa
import
sys
def
calc_gc_percent
(
seq
):
at_count
,
gc_count
=
0
,
0
for
char
in
seq
:
if
char
in
(
'A'
,
'T'
):
at_count
+=
1
elif
char
in
(
'G'
,
'C'
):
gc_count
+=
1
return
gc_count
*
100.0
/
(
gc_count
+
at_count
)
input_seq
=
sys
.
argv
[
1
]
print
(
"The sequence '{}' has a %GC of {:.2f}"
.
format
(
input_seq
,
calc_gc_percent
(
input_seq
)))
more_python/more_01/seq_toolbox_03.py
0 → 100644
View file @
6383bfaa
import
sys
def
calc_gc_percent
(
seq
):
at_count
,
gc_count
=
0
,
0
for
char
in
seq
.
upper
():
if
char
in
(
'A'
,
'T'
):
at_count
+=
1
elif
char
in
(
'G'
,
'C'
):
gc_count
+=
1
return
gc_count
*
100.0
/
(
gc_count
+
at_count
)
input_seq
=
sys
.
argv
[
1
]
print
(
"The sequence '{}' has a %GC of {:.2f}"
.
format
(
input_seq
,
calc_gc_percent
(
input_seq
)))
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