10 Commits
0.0.4 ... 0.0.9

Author SHA1 Message Date
ducoterra
fbb0e61b04 update headers for day 3 2020-11-23 19:56:41 -05:00
ducoterra
715b83f929 finish day 3 2020-11-22 19:31:52 -05:00
ducoterra
2d90f4aa31 finish part 1 day 3 2020-11-22 18:52:56 -05:00
ducoterra
fd28fa3fde begin day 3 2020-11-17 19:26:20 -05:00
ducoterra
3696da7ae4 fix instructions 2020-11-16 18:48:14 -05:00
ducoterra
d60a9f538d add start.sh 2020-11-09 21:21:40 -05:00
ducoterra
20fa53960d add day 2 to index 2020-11-09 21:21:05 -05:00
ducoterra
d58ebc1ebe add debug to convert 2020-11-09 21:20:58 -05:00
ducoterra
09b4bc8f5b day 2 2020-11-09 21:12:58 -05:00
ducoterra
f2b35db671 add git tag pager info to README 2020-11-05 14:50:27 -05:00
35 changed files with 772 additions and 1 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
venv/ venv/
site/ site/
videos/

View File

@@ -2,4 +2,10 @@
```bash ```bash
ffmpeg -i in.mov -filter:v "setpts=0.5*PTS" out.gif ffmpeg -i in.mov -filter:v "setpts=0.5*PTS" out.gif
ffmpeg -i in.mkv out.mov
```
```bash
git config --global pager.branch false
git config --global pager.tag false
``` ```

30
converter.py Executable file
View File

@@ -0,0 +1,30 @@
#!/usr/bin/env/python3
import subprocess
import os
import time
convert_dir = "/Users/ducoterra/Desktop/"
debug = False
while True:
# get a list of all .mov or .gif files
movs = list(filter(lambda item: item.endswith(".mov"), os.listdir(convert_dir)))
if debug:
print(f"Found {len(movs)} .mov files.")
gifs = list(filter(lambda item: item.endswith(".gif"), os.listdir(convert_dir)))
if debug:
print(f"Found {len(gifs)} .gif files.")
# find files that are not already gifs and aren't default
not_done = list(filter(lambda mov: mov.replace(".mov",".gif") not in gifs and not mov.startswith("Screen Recording"), movs))
if debug:
print(f"Found {len(not_done)} to convert.")
for mov in not_done:
print(f"converting {mov}. Ctrl + C to stop...")
if debug:
time.sleep(5)
subprocess.run(['/usr/local/bin/ffmpeg', '-i', convert_dir + mov, '-filter:v', 'setpts=0.5*PTS', convert_dir + mov.replace(".mov",".gif")], capture_output=True)
time.sleep(1)

241
docs/day2.md Normal file
View File

@@ -0,0 +1,241 @@
# Day 2
## Back to the basics: and, or, not, if
Before we do anything more interesting we're going to have to cover our logical basics. In Python these are `not`, `and`, `or`, and `if`.
1. Stop your server with ++ctrl++ + C
2. Type `python` in the terminal to open your python interpretor
![python_prompt](img/day1/python_prompt.gif)
### not
1. Type `x = True` and press enter.
2. Type `x` and press enter to see that x is true.
3. Type `not x` and press enter. `not` inverts the value of `x`. So if `x` is True, `not x` is False.
4. Type `not not x` and press enter. `not not x` inverts `not x`. You can chain `not` as many times as you want.
5. Let's imagine we're building a weather app and we want to know if it's raining. Type `is_raining = True` and press enter.
6. Type `print(f"should I wear a raincoat? {is_raining}")` and press enter.
7. Today it's not raining. We could create a variable called `is_not_raining` but we could also just `not`. Type `print(f"should I wear a raincoat? {not is_raining}")`
8. Type `not 'hello'` and press enter. This might seem strange but Python interprets a string as `True`. `not 'hello'` will return `False`.
9. Type `not ''` and press enter. An empty string in Python is False. `not ''` returns `True`.
### and
1. In your terminal type `x = True` and press enter.
2. Type `x` and press enter to see that x is True.
3. Type `y = True` and press enter.
4. Type `y` and press enter to see that y is True.
5. Type `y and x` and press enter. `y` is true *and* `x` is true, so `y` and `x` is true.
6. We're building our weather app to be more useful. We want to tell people if it's rainy and windy. Type `rainy = True` and press enter.
7. Type `windy = True` and press enter.
8. Type `rainy and windy` and press enter. We get True because it's rainy and windy.
9. Today it's not windy. Type `rainy and not windy` and press enter. We get False because it isn't rainy *and* windy.
10. Tomorrow it won't be windy and it also won't be rainy. Type `not rainy and not windy` and press enter. We get False because neither weather conditions are met.
### or
1. In your terminal type `x = True` and press enter.
2. Type `x` and press enter to see that x is True.
3. Type `y = False` and press enter.
4. Type `y` and press enter to see that y is False.
5. Type `y or x` and press enter. `x` is True so asking if x or y is true will result in `True`.
6. Our weather app has millions of downloads. Our data suggests people don't like when it's cold or raining. Let's tell them when it's cold or raining.
7. Type `cold = True` and press enter.
8. Type `raining = True` and press enter.
9. Type `cold or raining` and press enter. We get `True`, it is cold and it is raining.
10. Today it isn't cold but it is raining. Type `not cold or raining`. We get `True`, it is still raining outside.
11. Tomorrow it won't be cold and it won't be raining. Type `not cold or not raining`. We get `False` because neither of our disliked weather conditions are met.
### if
1. In your terminal type `x = True` and press enter.
2. Type `x` and press enter to see that x is True.
3. Type `y = False` and press enter.
4. Type `y` and press enter to see that y is False.
5. Type `if x:` and press enter. You'll see a `...`. Python is waiting for another input.
6. Type ++tab++ `print('x is true!')` and press enter. You should see another `...`.
7. Press enter again. 'x is true!' will print to the terminal.
![if_interpreter](img/day2/if_interpreter.gif)
8. Type `if y:` and press enter. Now type ++tab++ `print('y is true!')` and press enter twice. You won't see anything print.
![y_is_false](img/day2/y_is_false.gif)
9. Type `if not y:` and press enter. Now type ++tab++ `print('y is false!')` and press enter twice. You'll see 'y is false!') printed.
10. Now that our weather app is sentient let's give it some decision making capability.
11. Type `raining = True` and press enter.
12. Type `if raining:` and press enter.
13. Type ++tab++ `print('You should wear a rain jacket')` and press enter twice. Your weather app can now tell you to wear a jacket if it's raining outside.
![rain_jacket](img/day2/rain_jacket.gif)
14. Type `exit()` to exit
## Building a Terrible Weather App
1. Create a new file called `weather_app.py` by clicking the "add file" icon in the top left of VSCode.
![create_file](img/day2/create_file.gif)
2. We're going to create an app where the user has to guess the weather. We're going to need the python random module to generate random weather conditions. Add the following to the top of the page:
```python
import random
```
![import random](img/day2/import_random.png)
This tells python we intend to use the `random` package
3. We'll generate 4 weather conditions. Add the following after `import random`
```python
warm = random.choice([True, False])
cold = not warm
raining = random.choice([True, False])
snowing = not raining
```
![conditions](img/day2/conditions.png)
random.choice([True, False]) selects either True or False randomly.
4. Let's print our weather conditions in a cryptic way. Add the following below our variables:
```python
if warm or cold:
print("It's warm or cold.")
if raining or warm:
print("It's raining or warm.")
if raining or snowing:
print("It's raining or snowing.")
if cold or snowing:
print("It's cold or snowing.")
```
![if_prints](img/day2/if_prints.png)
This will print weather conditions in pairs to make it more challenging for our users to guess the weather.
5. Now let's get our user input. Since we have 4 possible weather conditions we'll need 4 "guessing modules". Let's build the first one:
```python
warm_guess = input("Is it warm? (y/n) ")
if warm_guess == 'y' and warm:
print('Correct!')
elif warm_guess == 'n' and not warm:
print('Correct!')
else:
print('Wrong!')
```
![warm_module](img/day2/warm_module.png)
We ask the user if it's warm. If they say 'y' and it is warm we tell them they got it right! If they guess wrong we tell them they are wrong.
6. We'll just repeat the guess module 3 more times for cold, raining, and snowing.
```python
cold_guess = input("Is it cold? (y/n) ")
if cold_guess == 'y' and cold:
print('Correct!')
elif cold_guess == 'n' and not cold:
print('Correct!')
else:
print('Wrong!')
raining_guess = input("Is it raining? (y/n) ")
if raining_guess == 'y' and raining:
print('Correct!')
elif raining_guess == 'n' and not raining:
print('Correct!')
else:
print('Wrong!')
snowing_guess = input("Is it snowing? (y/n) ")
if snowing_guess == 'y' and snowing:
print('Correct!')
elif snowing_guess == 'n' and not snowing:
print('Correct!')
else:
print('Wrong!')
```
![modules](img/day2/modules.png)
7. Save with ++ctrl++ + S and run your weather app by typing `python weather_app.py`. Try to guess the weather.
![run_app](img/day2/run_app.gif)
## Pulling it all together with some Django
1. Let's make a weather app! Open "views.py" by clicking on it.
![open_views](img/day2/open_views.gif)
2. At the top of views.py add the following:
```python
import random
```
![django_random](img/day2/django_random.gif)
3. Now under all of our existing code in views.py add the following:
```python
def weather(request):
warm = random.choice([True, False])
cold = not warm
raining = random.choice([True, False])
snowing = not raining
if warm:
temperature_report = "It's warm out! Don't wear a jacket."
else:
temperature_report = "It's cold out! Wear a jacket."
if raining:
precipitation_report = "It's raining! Bring an umbrella."
else:
precipitation_report = "It's snowing! Bring your skis."
return render(
request,
"mysite/index.html",
{"data": temperature_report + " " + precipitation_report}
)
```
![add_weather](img/day2/add_weather.gif)
This creates a new "view" in our django app.
4. Open mysite/urls.py by clicking on it.
![open_urls](img/day2/open_urls.gif)
5. Under `path('', views.index),` add:
```python
path('weather/', views.weather),
```
![add_weather_path](img/day2/add_weather_path.gif)
6. Run your server with `python manage.py runserver` (if it isn't already running)
![python_manage_run](img/day2/python_manage_run.gif)
7. Navigate to <http://localhost:8000/weather>
![navigate_to_weather](img/day2/navigate_to_weather.gif)
Congratulations! You've added a new page to your app!

352
docs/day3.md Normal file
View File

@@ -0,0 +1,352 @@
# Day 3
## "For"
Let's cover our first loop - the "for" loop. In Python the "for" loop is absurdly powerful. Let me show you why.
### Looping without code
"For" loops in Python make intuitive sense even without code. I can demonstrate with this basket of fruit:
```text
| |
| apple |
| banana |
\ pear /
\_____/
```
For each fruit I want you to take it out of the basket say its name. You start with the apple, since it's at the top, and say "apple". Then you'd take out the banana and say "banana". Finally you'd take our the pear and say "pear".
In Python our "basket" is a list. Lists look like this:
```python
["apple", "banana", "pear"]
```
They're very similar to tuples. A list is "ordered", the first item in the list will always be the apple - much like the top item in the basket will always be the apple.
If I asked you to repeat the basket exercise above but with our python list - remove each item and say its name - you'd start with the apple, then remove the banana, and finally remove the pear.
### Looping with code
You saying the names of fruit out loud is funny but not very practical. Instead I want you to print the names of the fruit from our basket in the Python interpretor.
1. Start you interpretor by typing `python` and pressing ++enter++
2. In your interpretor type the following:
```python
fruits = ["apple", "banana", "pear"]
for fruit in fruits:
print(fruit)
```
You should see the fruit print out:
```python
>>> fruits = ["apple", "banana", "pear"]
>>> for fruit in fruits:
... print(fruit)
...
apple
banana
pear
>>>
```
you've just used a for loop!
3. So we can do something with each item in a list. But what if we need the index of each item? Type the following:
```python
fruits = ["apple", "banana", "pear"]
for index, fruit in enumerate(fruits):
print(f"fruit {fruit} is at index {index}")
```
with `enumerate` we can capture the position of the item and the item itself in the list.
Why is this useful?
4. We can modify elements in a list if we have their position. Paste the following and press enter:
```python
fruits = ["apple", "banana", "pear"]
print(f"Our original list is: {fruits}")
```
Now let's modify our list. Type the following and press enter:
```python
for index, fruit in enumerate(fruits):
print(f"fruit {fruit} is at index {index}")
if fruits[index] == "apple":
fruits[index] = "apricot"
print(f"fruit is still {fruit} but {fruits[index]} is at position {index} now")
```
Now paste this:
```python
print(f"Our modified list is: {fruits}")
```
5. We can also loop through the contents of a dictionary. Let's recreate our dictionary from before:
```python
vocabulary = {"apple": "the usually round, red or yellow, edible fruit of a small tree, Malus sylvestris, of the rose family.", "banana": "a tropical plant of the genus Musa, certain species of which are cultivated for their nutritious fruit."}
```
Now we'll loop through the keys in the dictionary:
```python
for word in vocabulary:
print(word)
```
We can access any item in the dictionary with its key:
```python
for word in vocabulary:
print(vocabulary[word])
```
And we can access both the key and value by using the `items()` function:
```python
for word, definition in vocabulary.items():
print(f"{word}: {definition}")
```
6. One common data format you'll run into is a list of dictionaries. You can think of this like spreadsheet columns:
| first name | last name | age |
|-|-|-|
| Jim | Fowler | 24 |
| Bob | Jones | 36 |
| Alice | Appleseed | 52 |
Behind the scenes this table data might look like:
```json
[
{"first name" : "Jim", "last name": "Fowler", "age": 24},
{"first name" : "Bob", "last name": "Jones", "age": 36},
{"first name" : "Alice", "last name": "Appleseed", "age": 52}
]
```
Notice how the headers are copied each time - this is the only way to ensure headers are preserved when translated to this format. This is called "json" and is the current preferred way to send data over the web.
Let's create this data in Python
```python
people = [
{"first name" : "Jim", "last name": "Fowler", "age": 24},
{"first name" : "Bob", "last name": "Jones", "age": 36},
{"first name" : "Alice", "last name": "Appleseed", "age": 52}
]
```
We're building a program that checks each person and lets you know if they're over 30 years old. This is easy with for loops:
```python
for person in people:
if person["age"] > 30:
print(f"{person['first name']} {person['last name']} is over 30.")
```
7. Another common data format is a dictionary of lists. You can think of this like acceptible responses on a web form:
The form below has 3 valid states and 4 valid emotions:
<label>Choose a state:</label>
<select>
<option>Ohio</option>
<option>Virginia</option>
<option>Alask</option>
</select>
<label>How you are feeling today:</label>
<select>
<option>Happy</option>
<option>Sad</option>
<option>Excited</option>
<option>Nope</option>
</select>
These could be represented by the following data:
```json
{
"states": ["Ohio", "Virginia", "Alaska"],
"emotions": ["Happy", "Sad", "Excited", "Nope"]
}
```
Let's add this data to python:
```python
choices = {
"states": ["Ohio", "Virginia", "Alaska"],
"emotions": ["Happy", "Sad", "Excited", "Nope"]
}
```
Now imagine you have to check if someone entered the right answer when filling out the form. Type the following:
```python
state = input("Type the state you live in: ")
```
```python
emotion = input("How are you feeling: ")
```
```python
if state in choices["states"]:
print("Good state")
else:
print("I don't know where that is.")
```
```python
if emotion in choices["emotions"]:
print("Same")
else:
print("Not a valid emotion")
```
8. Exit your terminal with `exit()`
## Creating an API
You've got enough at this point to make a substantial API. Usually we'd write a python script at this point but we're going to build a Django API first this time. We'll use the people data we have above.
1. Open mysite/views.py
2. At the top of views.py add the following:
```python hl_lines="3 3"
import random
from django.shortcuts import render
from django.http import JsonResponse
```
3. At the very bottom (below your weather function) add another function like so:
```python
def api(request):
people = [
{"first name" : "Jim", "last name": "Fowler", "age": 24},
{"first name" : "Bob", "last name": "Jones", "age": 36},
{"first name" : "Alice", "last name": "Appleseed", "age": 52}
]
return JsonResponse({"data": people})
```
We're returning a "JsonResponse" this time. Remember how json is the current preferred way to send data over the web? Django has a formatter built in for it.
4. let's add a url that points to our api endpoint. Open mysite/urls.py.
5. Add the following:
```python hl_lines="4 4"
urlpatterns = [
path('', views.index),
path('weather/', views.weather),
path('people/', views.api),
]
```
6. Start your server with `python manage.py runserver`
7. Navigate to <http://localhost:8000/people>
8. You should see people data!
9. Stop your server with ++ctrl+c++
## Reading an API with python
You have an API! Now let's read it with python. This is one of the most common applications for python in a work environment.
1. First we need a pip package called `requests`. `requests` can make web requests on our behalf. Install it by typing the following in your terminal:
```bash
pip install requests
```
![install_requests](img/day3/install_requests.gif)
2. Let's make sure that install worked. Open the python prompt by typing `python`
3. In your interpretor type:
```python
import requests
requests.get("https://google.com")
```
You should see a `<Response [200]>`. That means everything worked!
4. Type `exit()` to exit.
5. Create a new file called `api.py` in your my_website folder:
![api](img/day3/api.gif)
6. Add the following to `api.py`
```python
import requests
response = requests.get("http://localhost:8000")
print(response.text)
```
7. Save with ++ctrl+s++
8. Run your server by typing `python manage.py runserver`
9. Open a new terminal window by click the plus icon:
![open_terminal](img/day3/open_terminal.gif)
10. Type `python api.py` and press ++enter++
![index](img/day3/index.gif)
We got our index page! Now let's get our API call:
11. Modify api.py by changing our web call to <http://localhost:8000/people>
```python hl_lines="3 3"
import requests
response = requests.get("http://localhost:8000/people")
print(response.text)
```
12. Type `python api.py` and press ++enter++
![people](img/day3/people.gif)
13. Now we want to save our api response as python data. Let's add the following to `api.py`
```python hl_lines="2 5-8"
import requests
import json
response = requests.get("http://localhost:8000/people")
people = response.json()["data"]
for person in people:
if person["age"] > 30:
print(f"{person['first name']} {person['last name']} is over 30.")
```
14. Type `python api.py` and press ++enter++
![read_api](img/day3/read_api.gif)
You've just done one of the most important things in Python programming: used the results of an api call to do something!
15. Click the terminal dropdown and switch back to the terminal running your web server:
![switch_term](img/day3/switch_term.gif)
16. Stop your server with ++ctrl+c++
17. Type `python api.py` and press ++enter++. Notice how you get an error that says "failed to establish new connection"? If requests can't connect to the web server you'll see this error. Remember it!

Binary file not shown.

After

Width:  |  Height:  |  Size: 367 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 465 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 123 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 359 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 342 KiB

BIN
docs/img/day2/if_prints.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 183 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 KiB

BIN
docs/img/day2/modules.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 254 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 KiB

BIN
docs/img/day2/open_urls.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 204 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 260 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 231 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 407 KiB

BIN
docs/img/day2/run_app.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 386 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 209 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 412 KiB

BIN
docs/img/day3/api.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 915 KiB

BIN
docs/img/day3/index.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 625 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 258 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 770 KiB

BIN
docs/img/day3/people.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 591 KiB

BIN
docs/img/day3/read_api.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 738 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB

View File

@@ -15,3 +15,16 @@
- The Interactive Python Prompt - The Interactive Python Prompt
- A Python Program - A Python Program
- A Django Project
### [Day 2](day2.md): not, and, or, if
- Back to the basics: and, or, not, if
- Building a Terrible Weather App
- Pulling it all together with some Django
### [Day 3](day3.md): for
- "For"
- Creating an API
- Reading an API with Python

58
ffmpeg.yaml Normal file
View File

@@ -0,0 +1,58 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: ffmpeg
spec:
selector:
matchLabels:
app: ffmpeg
template:
metadata:
labels:
app: ffmpeg
spec:
containers:
- image: debian:latest
name: ffmpeg
command:
- sleep
- infinity
volumeMounts:
- mountPath: /in
name: in
- mountPath: /out
name: out
resources:
limits:
memory: "16Gi"
cpu: "16"
requests:
memory: "1Mi"
cpu: "1m"
restartPolicy: Always
volumes:
- name: out
persistentVolumeClaim:
claimName: ffmpeg
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: ffmpeg-in
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 32Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: ffmpeg-out
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 32Gi

View File

@@ -3,6 +3,8 @@ nav:
- Home: index.md - Home: index.md
- Day 0: day0.md - Day 0: day0.md
- Day 1: day1.md - Day 1: day1.md
- Day 2: day2.md
- Day 3: day3.md
theme: theme:
name: material name: material
markdown_extensions: markdown_extensions:

16
my_program.py Normal file
View File

@@ -0,0 +1,16 @@
def print_hello():
print('hello, world!')
def print_goodbye():
print('goodbye!')
def middle_stuff():
x = 1
y = 2
print(x + y)
my_name = "Reese"
print(my_name)
print_hello()
middle_stuff()
print_goodbye()

1
start.sh Executable file
View File

@@ -0,0 +1 @@
mkdocs serve -a localhost:8080

51
weather_app.py Normal file
View File

@@ -0,0 +1,51 @@
import random
warm = random.choice([True, False])
cold = not warm
raining = random.choice([True, False])
snowing = not raining
if warm or cold:
print("It's warm or snowing.")
if raining or warm:
print("It's raining or warm.")
if raining or snowing:
print("It's raining or cold.")
if cold or snowing:
print("It's cold or snowing.")
warm_guess = input("Is it warm? (y/n) ")
if warm_guess == 'y' and warm:
print('Correct!')
elif warm_guess == 'n' and not warm:
print('Correct!')
else:
print('Wrong!')
cold_guess = input("Is it cold? (y/n) ")
if cold_guess == 'y' and cold:
print('Correct!')
elif cold_guess == 'n' and not cold:
print('Correct!')
else:
print('Wrong!')
raining_guess = input("Is it raining? (y/n) ")
if raining_guess == 'y' and raining:
print('Correct!')
elif raining_guess == 'n' and not raining:
print('Correct!')
else:
print('Wrong!')
snowing_guess = input("Is it snowing? (y/n) ")
if snowing_guess == 'y' and snowing:
print('Correct!')
elif snowing_guess == 'n' and not snowing:
print('Correct!')
else:
print('Wrong!')