Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ec8a37af1c | ||
|
|
4e6a69c038 | ||
|
|
fbb0e61b04 | ||
|
|
715b83f929 | ||
|
|
2d90f4aa31 | ||
|
|
fd28fa3fde |
@@ -1,8 +1,15 @@
|
||||
# Docs
|
||||
|
||||
Converting mov to gif:
|
||||
|
||||
```bash
|
||||
ffmpeg -i in.mov -filter:v "setpts=0.5*PTS" out.gif
|
||||
ffmpeg -i in.mkv out.mov
|
||||
```
|
||||
|
||||
Converting mkv to mp4 with 20Mbit bitrate
|
||||
|
||||
```bash
|
||||
ffmpeg -i in.mkv -b:v 20M out.mov
|
||||
```
|
||||
|
||||
```bash
|
||||
|
||||
2
converter.py
Normal file → Executable file
@@ -1,3 +1,5 @@
|
||||
#!/usr/bin/env/python3
|
||||
|
||||
import subprocess
|
||||
import os
|
||||
import time
|
||||
|
||||
352
docs/day3.md
Normal 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
|
||||
```
|
||||
|
||||

|
||||
|
||||
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:
|
||||
|
||||

|
||||
|
||||
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:
|
||||
|
||||

|
||||
|
||||
10. Type `python api.py` and press ++enter++
|
||||
|
||||

|
||||
|
||||
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++
|
||||
|
||||

|
||||
|
||||
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++
|
||||
|
||||

|
||||
|
||||
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:
|
||||
|
||||

|
||||
|
||||
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!
|
||||
441
docs/day4.md
Normal file
@@ -0,0 +1,441 @@
|
||||
# Day 4
|
||||
|
||||
## "While"
|
||||
|
||||
The last section covered the for loop - a useful loop when you have a finite number of things you need to do something with. A grocery list, api data, and application arguments could be massive but will always have a definite size. Let's talk about times when you don't know how many times to loop.
|
||||
|
||||
### Example 1: A Game
|
||||
|
||||
Let's imagine you're coding a checkers app. You might approach it like this:
|
||||
|
||||
1. Game Start
|
||||
2. Player 1 moves
|
||||
3. Check if player 2 has no remaining pieces
|
||||
4. Player 2 moves
|
||||
5. Check if player 1 has no remaining pieces
|
||||
6. Player 1 moves
|
||||
7. Check if player 2 has no remaining pieces
|
||||
8. Player 2 moves
|
||||
9. Check if player 1 has no remaining pieces
|
||||
10. ""
|
||||
11. ""
|
||||
12. ""
|
||||
13. ...
|
||||
|
||||
You'll need to do a lot of the same stuff over and over - that's great for a loop. Since you'll have no idea how long the game will go on you can't use a for loop because there aren't a finite number of things to loop through (although you could simple use [the longest checkers game](http://www.wylliedraughts.com/LongGame.htm#:~:text=The%20longest%20game%20that%20Wyllie,of%203%2Dmove%20restriction%20checkers.) as your starting point I suppose).
|
||||
|
||||
What we need is a loop that run indefinitey until a condition is met - namely that one player wins the game.
|
||||
|
||||
### Example 2: User Input
|
||||
|
||||
Let's build a menu system for a text-based app.
|
||||
|
||||
1. Open your python interpretor by typing `python` and pressing ++enter++
|
||||
2. In your python interpretor paste the following:
|
||||
|
||||
```python
|
||||
program_choices = ["print", "count", "add", "exit"]
|
||||
while True:
|
||||
choice = input(f"Please choose one of the following options: [{', '.join(program_choices)}]: ")
|
||||
if choice == "print":
|
||||
print("hello")
|
||||
elif choice == "count":
|
||||
print(", ".join(["1","2","3","4","5"]))
|
||||
elif choice == "add":
|
||||
num1 = input("first number: ")
|
||||
num2 = input("second number: ")
|
||||
try:
|
||||
print(f"Answer: {int(num1) + int(num2)}")
|
||||
except ValueError:
|
||||
print("You did not provide 2 valid numbers")
|
||||
elif choice == "exit":
|
||||
break
|
||||
else:
|
||||
print("invalid choice!")
|
||||
```
|
||||
|
||||
3. Exit the menu by typing 'exit'
|
||||
4. Type exit() to exit the python interpretor
|
||||
|
||||
This lets a user select from a variety of options and returns them to the start of the menu upon a selection. If their selection is invalid we tell them it's invalid and return them to the start anyway.
|
||||
|
||||
Since we don't know how many things a user will want to do with our menu we should use a while loop to loop indefinitely.
|
||||
|
||||
Note the line that says `break`. This is how you break out of a loop in python. as soon as you call `break` it will stop the loop.
|
||||
|
||||
### Easy looping
|
||||
|
||||
Now that we've seen a few examples let's run through the core of a while loop:
|
||||
|
||||
1. Type `python` and press ++enter++ to open the python interpretor.
|
||||
2. We can use a while loop to do something indefinitely. Type the following and press ++enter++ twice:
|
||||
|
||||
```python
|
||||
while True:
|
||||
print("hello")
|
||||
```
|
||||
|
||||
3. Press ++ctrl+c++ to stop the loop
|
||||
4. We can use a while loop like a for loop (though this isn't recommended - it's too easy to get stuck in an infinite loop). Type the following and press ++enter++ twice:
|
||||
|
||||
```python
|
||||
count_to = 10
|
||||
start_at = 1
|
||||
while start_at <= count_to:
|
||||
print(start_at)
|
||||
start_at += 1
|
||||
```
|
||||
|
||||
5. We can use a while loop to check multiple conditions. Type the following and press ++enter++:
|
||||
|
||||
```python
|
||||
import random
|
||||
day = 1
|
||||
raining = True
|
||||
temperature = "cold"
|
||||
while temperature == "cold" or raining == True:
|
||||
print(f"Day {day}: Stay inside")
|
||||
day += 1
|
||||
temperature = random.choice(["cold", "warm"])
|
||||
raining = random.choice([True, False])
|
||||
|
||||
print(f"Day {day}: It's safe")
|
||||
```
|
||||
|
||||
6. We can put while loops inside while loops (notice how we break out of each loop - this requires 2 break statements). Type the following and press ++enter++:
|
||||
|
||||
```python
|
||||
while True:
|
||||
print("loop 1")
|
||||
while True:
|
||||
print("loop 2")
|
||||
break
|
||||
break
|
||||
```
|
||||
|
||||
## Let's build a menu
|
||||
|
||||
We're going to build a piece of software that lets us interact with our people API. We should be able to:
|
||||
|
||||
1. List the people from our API
|
||||
2. Update the list by calling the API
|
||||
3. Clear the list
|
||||
4. Exit
|
||||
|
||||
Let's get started:
|
||||
|
||||
1. Create a new file in your root directory called "menu.py"
|
||||
2. Add the following to the top:
|
||||
|
||||
```python
|
||||
import requests
|
||||
import json
|
||||
```
|
||||
|
||||
3. Create the list_people function by adding the following:
|
||||
|
||||
```python hl_lines="5-7"
|
||||
import requests
|
||||
import json
|
||||
|
||||
def list_people(people):
|
||||
print(people)
|
||||
return people
|
||||
```
|
||||
|
||||
4. Now create the update_people function by adding the following:
|
||||
|
||||
```python hl_lines="5-12"
|
||||
def list_people(people):
|
||||
print(people)
|
||||
return people
|
||||
|
||||
def update_people(people):
|
||||
try:
|
||||
response = requests.get("http://localhost:8000/people")
|
||||
people = response.json()["data"]
|
||||
print("successfully updated people.")
|
||||
except requests.exceptions.ConnectionError:
|
||||
print("Server is not running. Failed to update.")
|
||||
return people
|
||||
```
|
||||
|
||||
5. Now create the clear_people function by adding the following:
|
||||
|
||||
```python hl_lines="10-12"
|
||||
def update_people(people):
|
||||
try:
|
||||
response = requests.get("http://localhost:8000/people")
|
||||
people = response.json()["data"]
|
||||
print("successfully updated people.")
|
||||
except requests.exceptions.ConnectionError:
|
||||
print("Server is not running. Failed to update.")
|
||||
return people
|
||||
|
||||
def clear_people(people):
|
||||
people = None
|
||||
return people
|
||||
```
|
||||
|
||||
6. Finally add the main loop:
|
||||
|
||||
```python hl_lines="5-19"
|
||||
def clear_people(people):
|
||||
people = None
|
||||
return people
|
||||
|
||||
if __name__ == "__main__":
|
||||
people = None
|
||||
choices = ["list", "update", "clear", "exit"]
|
||||
while True:
|
||||
choice = input(f"Please choose an option [{', '.join(choices)}]: ")
|
||||
if choice == "list":
|
||||
people = list_people(people)
|
||||
elif choice == "update":
|
||||
people = update_people(people)
|
||||
elif choice == "clear":
|
||||
people = clear_people(people)
|
||||
elif choice == "exit":
|
||||
break
|
||||
else:
|
||||
print("Invalid choice. Please try again.")
|
||||
```
|
||||
|
||||
7. Check your work. Your whole program should be 35 lines long and look like this:
|
||||
|
||||
```python
|
||||
import requests
|
||||
import json
|
||||
|
||||
def list_people(people):
|
||||
print(people)
|
||||
return people
|
||||
|
||||
def update_people(people):
|
||||
try:
|
||||
response = requests.get("http://localhost:8000/people")
|
||||
people = response.json()["data"]
|
||||
print("successfully updated people.")
|
||||
except requests.exceptions.ConnectionError:
|
||||
print("Server is not running. Failed to update.")
|
||||
return people
|
||||
|
||||
def clear_people(people):
|
||||
people = None
|
||||
return people
|
||||
|
||||
if __name__ == "__main__":
|
||||
people = None
|
||||
choices = ["list", "update", "clear", "exit"]
|
||||
while True:
|
||||
choice = input(f"Please choose an option [{', '.join(choices)}]: ")
|
||||
if choice == "list":
|
||||
people = list_people(people)
|
||||
elif choice == "update":
|
||||
people = update_people(people)
|
||||
elif choice == "clear":
|
||||
people = clear_people(people)
|
||||
elif choice == "exit":
|
||||
break
|
||||
else:
|
||||
print("Invalid choice. Please try again.")
|
||||
```
|
||||
|
||||
8. Run the program by typing `python menu.py` and pressing ++enter++.
|
||||
9. Type "list" and press ++enter++. You should see "None" printed to the screen. We haven't updated our people yet!
|
||||
10. Type "update" and press ++enter++. You should see "Server is not running. Failed to update.". We didn't run our server!
|
||||
11. Open a new terminal by clicking the plus icon.
|
||||
12. Type `python manage.py runserver` and press ++enter++.
|
||||
13. Flip back over to your other terminal by using the dropdown menu.
|
||||
14. Type "update" and press ++enter++. You should see "successfully updated people."
|
||||
15. Type "list" again and press ++enter++. You should see your people print out.
|
||||
16. Type "clear" and press ++enter++.
|
||||
17. Type "list" again and press ++enter++. Your people should be cleared.
|
||||
18. Type "exit" to exit.
|
||||
19. Flip back over to your django terminal with the dropdown menu
|
||||
20. Press ++ctrl+c++ to stop the server
|
||||
|
||||
## Threading
|
||||
|
||||
One of the more difficult concepts in programming is threading and multiprocessing. It's rarely taught at an intro level but it's fairly easy to use.
|
||||
|
||||
A program runs in a "thread". When you run `python menu.py` it creates one thread that executes all code in order. Code at the end of your file can't run before code at the beginning of your file.
|
||||
|
||||
...unless it could. What if you have a super slow internet connection and you need to make an api call? You don't want it to slow down your whole menu.
|
||||
|
||||
Here's the idea: we tell our computer to make the slow call to our API in the background and continue letting the user mess with the menu.
|
||||
|
||||
Let's make that a reality by simulating a slow internet connection.
|
||||
|
||||
1. Open views.py
|
||||
2. For this part we're going to need the time library. "time" lets us pause code execution for a bit, simulating a slow internet. At the top of views.py add the following:
|
||||
|
||||
```python hl_lines="2 2"
|
||||
import random
|
||||
import time
|
||||
from django.shortcuts import render
|
||||
from django.http import JsonResponse
|
||||
```
|
||||
|
||||
3. Jump to the very bottom and add the highlighted code:
|
||||
|
||||
```python hl_lines="10-23"
|
||||
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})
|
||||
|
||||
def slow_api(request):
|
||||
people = []
|
||||
first_names = ["Liam", "Noah", "Oliver", "William", "Olivia", "Emma", "Ava", "Sophia", "Isabella"]
|
||||
last_names = ["Smith", "Johnson", "Anderson", "Brown", "Garcia", "Miller", "Martinez", "Chavez"]
|
||||
num_people = random.randint(1,100)
|
||||
|
||||
for person in range(num_people):
|
||||
time.sleep(.1)
|
||||
first_name = random.choice(first_names)
|
||||
last_name = random.choice(last_names)
|
||||
age = random.randint(1,100)
|
||||
people.append({"first_name": first_name, "last_name": last_name, "age": age})
|
||||
|
||||
return JsonResponse({"data": people})
|
||||
```
|
||||
|
||||
4. Save with ++ctrl+s++
|
||||
5. We need to add a URL, open urls.py
|
||||
6. Add the highlighted code:
|
||||
|
||||
```python hl_lines="8 8"
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.index),
|
||||
path('weather/', views.weather),
|
||||
path('people/', views.api),
|
||||
path('slow/', views.slow_api),
|
||||
]
|
||||
```
|
||||
|
||||
7. Save with ++ctrl+s++
|
||||
8. Open a new terminal by clicking the plus icon.
|
||||
9. Type `python manage.py runserver` and press ++enter++.
|
||||
10. Flip back over to your other terminal by using the dropdown menu.
|
||||
11. Navigate to <http://localhost:8000/slow/> to see your api results. Notice how long the page takes to load.
|
||||
12. Let's try our menu.py with the new API. Open menu.py and update line 10:
|
||||
|
||||
```python hl_lines="3 3"
|
||||
def update_people(people):
|
||||
try:
|
||||
response = requests.get("http://localhost:8000/slow")
|
||||
people = response.json()["data"]
|
||||
print("successfully updated people.")
|
||||
```
|
||||
|
||||
13. Save with ++ctrl+s++
|
||||
14. Open a new terminal by clicking the plus icon
|
||||
15. Type `python menu.py` and press ++enter++
|
||||
16. Type "list" and press ++enter++. You should see `None`
|
||||
17. Type "update" and press ++enter++. Notice the delay. While we're waiting for our api to respond our menu won't respond to any thing we type.
|
||||
18. Type "exit" to exit.
|
||||
19. Let's add the magic that will unblock our menu during an API call. At the top of menu.py add the following:
|
||||
|
||||
```python hl_lines="3 3"
|
||||
import requests
|
||||
import json
|
||||
import concurrent.futures import ThreadPoolExecutor
|
||||
```
|
||||
|
||||
20. We need to modify our main program to call the update_people function in a different thread. To do this we need to keep track of a "future" object.
|
||||
|
||||
A future is something that hasn't finished executing yet. We don't know when it will finish but we assume that it will.
|
||||
|
||||
```python hl_lines="3 8 10"
|
||||
if __name__ == "__main__":
|
||||
people = None
|
||||
future = None
|
||||
choices = ["list", "update", "clear", "exit"]
|
||||
while True:
|
||||
choice = input(f"Please choose an option [{', '.join(choices)}]: ")
|
||||
if choice == "list":
|
||||
people = list_people(future, people)
|
||||
elif choice == "update":
|
||||
future = ThreadPoolExecutor().submit(update_people, people)
|
||||
elif choice == "clear":
|
||||
people = clear_people(people)
|
||||
elif choice == "exit":
|
||||
break
|
||||
else:
|
||||
print("Invalid choice. Please try again.")
|
||||
```
|
||||
|
||||
21. We need to modify the `list_people` function to handle a future. This ensures that new people will print after an update. Change the following:
|
||||
|
||||
```python hl_lines="1-4"
|
||||
def list_people(future, people):
|
||||
if future is not None and future.done():
|
||||
people = future.result()
|
||||
future = None
|
||||
print(people)
|
||||
return people
|
||||
```
|
||||
|
||||
22. Save with ++ctrl+s++
|
||||
23. Your menu.py should look like this:
|
||||
|
||||
```python
|
||||
import requests
|
||||
import json
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
def list_people(future, people):
|
||||
if future is not None and future.done():
|
||||
people = future.result()
|
||||
future = None
|
||||
print(people)
|
||||
return people
|
||||
|
||||
def update_people(people):
|
||||
try:
|
||||
response = requests.get("http://localhost:8000/slow")
|
||||
people = response.json()["data"]
|
||||
print("successfully updated people.")
|
||||
except requests.exceptions.ConnectionError:
|
||||
print("Server is not running. Failed to update.")
|
||||
return people
|
||||
|
||||
def clear_people(people):
|
||||
people = None
|
||||
return people
|
||||
|
||||
if __name__ == "__main__":
|
||||
people = None
|
||||
future = None
|
||||
choices = ["list", "update", "clear", "exit"]
|
||||
while True:
|
||||
choice = input(f"Please choose an option [{', '.join(choices)}]: ")
|
||||
if choice == "list":
|
||||
people = list_people(future, people)
|
||||
elif choice == "update":
|
||||
future = ThreadPoolExecutor().submit(update_people, people)
|
||||
elif choice == "clear":
|
||||
people = clear_people(people)
|
||||
elif choice == "exit":
|
||||
break
|
||||
else:
|
||||
print("Invalid choice. Please try again.")
|
||||
```
|
||||
|
||||
24. Run
|
||||
25. Now run `python menu.py`
|
||||
26. Type `update` and press ++enter++. Notice the menu returns instantly.
|
||||
27. In a moment you'll see "successfully updated people" print. Type `list` and press ++enter++.
|
||||
28. Type `clear` and press ++enter++
|
||||
29. Now type `update` and press ++enter++ and quickly type `list` and press ++enter++ before it updates. Notice you can interact with the menu before the result returns!
|
||||
30. You've successfully written a multithreaded program. Type `exit` to exit.
|
||||
31. Flip back to your django server terminal with the dropdown
|
||||
32. Press ++ctrl+c++ to stop the server.
|
||||
BIN
docs/img/day3/api.gif
Normal file
|
After Width: | Height: | Size: 915 KiB |
BIN
docs/img/day3/index.gif
Normal file
|
After Width: | Height: | Size: 625 KiB |
BIN
docs/img/day3/install_requests.gif
Normal file
|
After Width: | Height: | Size: 258 KiB |
BIN
docs/img/day3/open_terminal.gif
Normal file
|
After Width: | Height: | Size: 770 KiB |
BIN
docs/img/day3/people.gif
Normal file
|
After Width: | Height: | Size: 591 KiB |
BIN
docs/img/day3/read_api.gif
Normal file
|
After Width: | Height: | Size: 738 KiB |
BIN
docs/img/day3/switch_term.gif
Normal file
|
After Width: | Height: | Size: 1.0 MiB |
@@ -21,4 +21,16 @@
|
||||
|
||||
- Back to the basics: and, or, not, if
|
||||
- Building a Terrible Weather App
|
||||
- Pulling it all together with some Django
|
||||
- Pulling it all together with some Django
|
||||
|
||||
### [Day 3](day3.md): for
|
||||
|
||||
- "For"
|
||||
- Creating an API
|
||||
- Reading an API with Python
|
||||
|
||||
### [Day 4](day4.md): while
|
||||
|
||||
- "While"
|
||||
- Let's build a menu
|
||||
- Threading
|
||||
@@ -4,6 +4,8 @@ nav:
|
||||
- Day 0: day0.md
|
||||
- Day 1: day1.md
|
||||
- Day 2: day2.md
|
||||
- Day 3: day3.md
|
||||
- Day 4: day4.md
|
||||
theme:
|
||||
name: material
|
||||
markdown_extensions:
|
||||
|
||||