Commit 5b4930c2 authored by christian.foerster's avatar christian.foerster
Browse files

intital tutorial files

parent acd227c6
This diff is collapsed.
This diff is collapsed.
%% Cell type:markdown id: tags:
# Python Tutorial Part 2 - Advanced Topics
## Object Oriented Programming (OOP)
OOP is a programming paradigm based on the concept of "objects", which can contain data, in the form of attributes and code, in the form of methods.
It's a different way of you thinking about your model/problem. Instead of thinking of a problem as a sequence of commands that need to be executed (procedural, **linear**), you identify single processes/actors and define their attributes and functionalities, so that they can interact with other actors (**nonlinear**).
**Example Problem:
Create a plotting library**
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
## Structering your code
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
## Performance
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
## Large Projects
Pycharm, Git, Debugging, Naming conventions, goot practice
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
This diff is collapsed.
# PythonTutorial
The tutorial consists of multiple parts.
- Python Basics
- Python Advanced
- Pandas and Plotting
\ No newline at end of file
- Pandas and Plotting
## Requierements
You must have python3 installed on your computer. Additionally we'll be using a few packages in the tutorial what we still need to install.
### Installation Python
***
Go to the [official python website](https://www.python.org) click on downloads and select your operating system.
Then go ahead and download the latest stable release. Windows users most likely need the 64 bit version, alternatively you can check your system version in the system information.
Run the downloaded file and make sure you select the add to Path option!
In case you need further assistance please check out this [website](https://realpython.com/installing-python/#step-2-run-the-installer).
**Check the Installation**
After you finished installing python open a console and type `pip`. This commands output should look something like this:
```bash
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
search Search PyPI for packages.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
help Show help for commands.
General Options:
-h, --help Show help.
--isolated Run pip in an isolated mode, ignoring environment variables and user configuration.
-v, --verbose Give more output. Option is additive, and can be used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR,
and CRITICAL logging levels).
--log <path> Path to a verbose appending log.
--proxy <proxy> Specify a proxy in the form [user:passwd@]proxy.server:port.
--retries <retries> Maximum number of retries each connection should attempt (default 5 times).
--timeout <sec> Set the socket timeout (default 15 seconds).
--exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
--trusted-host <hostname> Mark this host as trusted, even though it does not have valid or any HTTPS.
--cert <path> Path to alternate CA bundle.
--client-cert <path> Path to SSL client certificate, a single file containing the private key and the certificate in PEM
format.
--cache-dir <dir> Store the cache data in <dir>.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine whether a new version of pip is available for download.
Implied with --no-index.
```
### Install Python Packages
***
We'll be using:
- jupyter notebook -> lightweight IDE
- pandas -> a library for working with data
- numpy -> library for efficient matrix, array,... manipulation
- matplotlib -> standard plotting library
- plotly -> dynamic plots
- cufflinks -> pandas adapter to plotly
- seaborn -> fancy plots out of the box
- sklearn -> machine learning library
To install the packages open a console and run the following command.
(Linux users should use pip3 instead of pip since they do have Python2 and Python3 installed by default.)
```bash
pip install pandas numpy matplotlib plotly cufflinks seaborn sklearn jupyter notebook
```
They're many dependencies involved, so the installation might take a minute.
## Starting Jupyter Notebook
I choose *jupyter notebook* (*JN*) as the appropriate IDE for this tutorial, as it is light weight, so it's easy to understand and does not need much resources. Additionally *JN* is great for plotting!
One way to start *JN* is:
- to open a console and navigate to your working directory (using the cd command)
- now you simply type jupyter notebook
A browser tab will open and this is your IDE. If you don't know what to do from this point on forward check out this comprehensive [tutorial](https://realpython.com/jupyter-notebook-introduction/).
### The rest we'll figure out together in the tutorial!
%% Cell type:markdown id: tags:
# Solutions
%% Cell type:markdown id: tags:
Read in some of your data with pandas, plot it and get the main statistics on the data.
%% Cell type:code id: tags:
``` python
import pandas as pd
%matplotlib inline
import cufflinks as cf
cf.go_offline()
fp=r"M:\Projekte\2013_UWRM\3_messen und daten\Messstellen\Pegel Kreischa\Q_Kreischa.txt"
my_df=pd.read_csv(fp,parse_dates=True,index_col=0,sep=",")
```
%% Cell type:code id: tags:
``` python
my_df.iplot(secondary_y="q")
```
%% Cell type:code id: tags:
``` python
print(my_df.describe())
```
%% Cell type:markdown id: tags:
Write a class, that takes a filepath as an argument when it is initialized. Create a method, that takes a string as an argument and returns the number of occurances of that string in that file.
%% Cell type:code id: tags:
``` python
import re
class StrCount():
def __init__(self,filepath):
self.filepath=filepath
return
def count(self,string):
count=0
with open(self.filepath) as file:
for line in file:
count+=len(line.split(string))-1
return count
```
%% Cell type:code id: tags:
``` python
import matplotlib.pyplot as plt
%matplotlib inline
```
%% Cell type:code id: tags:
``` python
fig,ax=plt.subplots(2,2)
fig.text(0,0.5,r"\sum$",rotation=90, verticalalignment='center')
```
%% Cell type:code id: tags:
``` python
filepath=r"C:\Users\Gabi\Documents\Python\NN\Inputdata\PCM_MEZ_good_shape.txt"
count=StrCount(filepath).count(";")
print(count)
# if this takes too long it might be the server connection... (depending on the file size)
```
%% Cell type:markdown id: tags:
Create a function that takes an arbitrary number of inputs and prints them.
%% Cell type:code id: tags:
``` python
#this function only takes list like entries
def arbitrary_list(*arguments):
print(arguments)
arbitrary_list(2,3,4,5,4,3,2,4,[432],[{2:3}])
```
%% Cell type:code id: tags:
``` python
#this function only takes list like entries
def arbitrary_dict(**dictarguments):
print(dictarguments)
arbitrary_dict(a=3,b=4,wer=88)
```
%% Cell type:code id: tags:
``` python
#this function can do all list arguments must come before dict arguments
def arbitrary(*list_args,**dict_args):
print(list_args)
print(dict_args)
arbitrary(2,3,4,5,6,a=3,b=33,d="dd")
```
%% Cell type:markdown id: tags:
Write a function, that creates the fibonacci sequence up to the n'th term.
%% Cell type:code id: tags:
``` python
def fib(n):
fibo=[1,1]
if n==1:
return fibo[0]
elif n==2:
return fibo
else:
for run in range(n-2):
fibo.append(fibo[-1]+fibo[-2])
return fibo
fib(16)
```
%% Cell type:markdown id: tags:
Make python list of all files in the current directory and sort them by their file type.
%% Cell type:code id: tags:
``` python
import os
# list all contest