# code cell
name = "Jonathan"

"""
Explanation: Markdown Cell
Sub heading this is big text
Smaller subheadings
This is just a paragraph and this is bolded.
End of explanation
"""


import numpy as np

# don't do:
# from numpy import *

max("a")

np.max("a")

"""
Explanation: Tips and Tricks
A markdown cell lets you do many useful things.
End of explanation
"""


# %matplotlib inline
# %config InlineBackend.figure_format='retina'

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
from pivottablejs import pivot_ui
import sys
import warnings

warnings.filterwarnings("ignore")

"""
Explanation: Imports
End of explanation
"""


first = 1

second = 2

third = 3

"""
Explanation: Keyboard shortcuts
Practice doing these a few times and try to force yourself to use them whenever you can. 
Help (h)
For help in the Notebook, ESC + h
(doesn't work in Lab)
Select multiple cells
j, k, and arrow keys while holding shift selects multiple cells.
End of explanation
"""


canadian_politics = pd.read_csv("../data/mps2.csv")

# recommend using .head()
canadian_politics.head(10)

sns.distplot(canadian_politics["Age"].dropna());

sns.set_context("poster", font_scale=1.3)

fig, ax = plt.subplots(figsize=(12, 8))
sns.distplot(canadian_politics["Age"].dropna())
fig.tight_layout()

"""
Explanation: Split a cell with -
Well, ctrl + shift + -, but remember it by horizontal.
Add a new cell (a)bove
Add a new cell (b)elow
(d)(d)elete
unD(z)o
(c)opy cells
(v)aste cells
Pivot Tables w/ pandas
A library and example from: http://nicolas.kruchten.com/content/2015/09/jupyter_pivottablejs/
End of explanation
"""


# Province, Party, (deselect) Average, Age, Heatmap

pivot_ui(canadian_politics)

newdf = pd.read_clipboard(sep='\t')
newdf.fillna("")

canadian_politics['Age-bin'] = pd.cut(canadian_politics['Age'], [x for x in range(10, 100, 5)])
# pd.qcut# neat!

"""
Explanation: Enhanced Pandas Dataframe Display
End of explanation
"""


import numpy as np

from numpy.random import chisquare, choice

np.random.chisquare()

# pure tab right ↓ less useful
np.random.choice()

"""
Explanation: Tab -- Your Friend
End of explanation
"""


# shift-tab right ↓ more useful
np.linspace(start=50, stop=100, endpoint=False)

"""
Explanation: shift-tab
End of explanation
"""


np.linspace(start=50, end=120)

"""
Explanation: shift-tab-tab
End of explanation
"""


np.linspace(start=50, stop=150, num=100, endpoint=False)

"""
Explanation: shift-tab-tab-tab
End of explanation
"""


plt.plot(np.linspace(start, stop, num=50, ))

"""
Explanation: shift-tab-tab-tab-tab
End of explanation
"""


np.linspace?

?np.linspace

"""
Explanation: DO NOT TRY shift-tab-tab-tab-tab-tab
?
End of explanation
"""


np.linspace??

!code ~/miniconda3/envs/dspy3/lib/python3.6/site-packages/numpy/core/function_base.py

"""
Explanation: ??
(Lab can scroll if you click)
End of explanation
"""


import textwrap
def example_function():
    """Docstring for example function"""
    
    print(textwrap.dedent("""
    This is a multi-lined string
    that I want to write inside of a function.
    Notice what happens when I print this.
        And when something is indented more."""))


example_function()

# python3.6+

name

f"{name}'s name is not Alex."

age = 37
f"{age} plus 2 = {age + 2}"

# Note: 
# f"""{example_dictionary["key"]}"""

"""
Explanation: Random stuff
End of explanation
"""


# But first find and replace
def silly_function(xval):
    """Takes a value and returns the value."""
    xval_sq = xval ** 2.0
    3 + 1
    xval_abs = np.sqrt(xval_sq)
    return xval_abs

silly_function(-2,)

silly_function?

silly_function??

!ls ../data/

coal_years = !ls ../data/coal_prod_20*.csv

coal_years

from glob import glob 

for filename in glob("../data/coal_prod_20*.csv"):
    print(filename)

"""
Explanation: Inspect everything and Find and Replace
End of explanation
"""


ex_dictionary = {}

# Indent/dedent/comment

for index in range(5):
    ex_dictionary["float_one"] = 1
    ex_dictionary["float_two"] = 2
    ex_dictionary["float_three"] = 3
    ex_dictionary["float_four"] = 4

ex_dictionary

"""
Explanation: Line numbers (lowercase "L")
Type lowercase "L" to have line numbers; shift-L for line numbers notebook-wide.
Move blocks of code around
Indent/dedent
Cmd + [
Cmd + ]

Comment
Cmd + /
End of explanation
"""


example["one_better_neat"] = 1
example["two_better_neat"] = 2
example["three_better_neat"] = 3
example["four_better_neat"] = 4

"""
Explanation: Multicursor magic
Hold down option, click and drag (for big cursor).
cmd + click == wherever you click. 
Shift command P -- command palette Classic Notebook
Shift command C -- command palette in JupyterLab
Hide left bar (CMD + B)
Full screen CMD + Shift + D (but CTRL + CMD F for browser fullscreen)
Move cells around mouse
End of explanation
"""


%%latex

If you want to get crazier$\ldots$

\begin{equation}
\oint_S {E_n dA = \frac{1}{{\varepsilon _0 }}} Q_\textrm{inside}
\end{equation}

%%python2
print "hi"

%%bash
wget http://www.ast.cam.ac.uk/%7Erfc/vpfit12.2.tar.gz
mkdir -p vpfit12
cd vpfit12
tar -xvzf ../vpfit12.2.tar.gz

"""
Explanation: Monospace
You can also get monospaced fonts by indenting 4 spaces:
mkdir toc
cd toc

Syntax Highlighting
Wrap with triple-backticks and language:
bash
mkdir toc
cd toc
wget https://repo.continuum.io/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
SQL
SELECT first_name,
       last_name,
       year_of_birth
FROM presidents
WHERE year_of_birth &gt; 1800;
Headings and LaTeX
With text and $\LaTeX$ support.
$$\begin{align}
        B'&=-\nabla \times E,\
        E'&=\nabla \times B - 4\pi j
\end{align}$$
Magics

% $\equiv$ inline magic
%% $\equiv$ cell magic

Read through the documentation: https://ipython.readthedocs.io/en/stable/interactive/magics.html
End of explanation
"""


normal_argument = 12.4
second_argument = 98.4

arg_with_spaces = "the secret to life"

%%bash -s {normal_argument} {second_argument}
echo "This script knows the value of the argument: $1"
echo "It also has no trouble with the second argument: $2"

%%bash -s "$arg_with_spaces"
echo "This bash script knows $1."

# %%R -i df -o df2
# df2 <- 

ls vpfit10/

tailthing = "*.ipynb"

tailthing

!ls {tailthing}

output = !ls 

output

"""
Explanation: Scripting
End of explanation
"""


%env 

"""
Explanation: Need to set or change environment variables
End of explanation
"""


!pwd

a = 3
a

print(canadian_politics.head().to_latex())

5 * 83

_

3 + 7

_

print(_81)

saved = _25

saved

%history 

%history -opf alex.txt

"""
Explanation: Danger zone
_, _N and _iN
End of explanation
"""


