7 Commits
0.0.9 ... 0.2.3

Author SHA1 Message Date
ducoterra
99e4fa3c79 add table of contents for day 5 2020-12-08 19:50:19 -05:00
ducoterra
f9cc52cc83 finish day 5 2020-12-08 19:43:39 -05:00
ducoterra
948996705e day 5 2020-12-07 22:08:59 -05:00
ducoterra
e09bd887b5 fix imports 2020-11-29 18:52:23 -05:00
ducoterra
7358db6429 fix major variable issue 2020-11-29 16:36:27 -05:00
ducoterra
ec8a37af1c fix menu 2020-11-29 14:55:57 -05:00
ducoterra
4e6a69c038 day 4 done 2020-11-29 14:46:34 -05:00
14 changed files with 822 additions and 2 deletions

View File

@@ -1,8 +1,15 @@
# Docs # Docs
Converting mov to gif:
```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 ```
Converting mkv to mp4 with 20Mbit bitrate
```bash
ffmpeg -i in.mkv -b:v 20M out.mov
``` ```
```bash ```bash

437
docs/day4.md Normal file
View File

@@ -0,0 +1,437 @@
# 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
```
3. Create the list_people function by adding the following:
```python hl_lines="3-5"
import requests
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
from 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":
future, 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 (future, people)
```
22. Save with ++ctrl+s++
23. Your menu.py should look like this:
```python
import requests
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 (future, 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":
future, 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. Now run `python menu.py`
25. Type `list` and press ++enter++. Notice how there's nothing in the list.
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.

362
docs/day5.md Normal file
View File

@@ -0,0 +1,362 @@
# Day 5
## "import"
In our last section we're going to talk about organization. Not a glamorous topic, but an important one nonetheless!
### What an import actually is
You've already done this quite a bit - but here's a recap
1. Type `python` in the terminal and press ++enter++ to start the interpretor
2. Type `requests.get("https://google.com")` and press ++enter++
3. You should see `NameError: name 'requests' is not defined` printed to the terminal
4. Type `import requests` and press ++enter++
5. Now type `requests.get("https://google.com")` again and press ++enter++
6. You should see `<Response [200]>` printed to the terminal. You've just imported a module.
7. Type `exit()` and press ++enter++ to exit
"importing" alerts python that you want to use some code that isn't in the file you're currently working in. But where is "requests"? Remember that "venv" folder we made? Look inside it:
![requests](img/day5/requests.gif)
Requests is just a bit of python code that lives in our venv. In fact, we can rename it and import it as something completely different (though this isn't recommended). Give this a try:
1. Rename the requests folder in your venv to "somethingelse"
2. Type `python` in the terminal and press ++enter++ to open the interpretor
3. Type `import somethingelse` and press ++enter++
4. Type `somethingelse.get("https://google.com")` and press ++enter++. You should see `<Response [200]>` printed to the terminal. Your rename worked! You just changed the name python references when we import the code.
5. Type `exit()` and press ++enter++ to exit.
6. Rename "somethingelse" back to " for now.
### Using "as" to rename imports
Sometimes we don't want to use the name of the package in our code. It might conflict with something we already named, it might be really long to type, or it might just be poorly named. Fortunately, we don't have to rename the folder to change the name.
1. Type `python` in the terminal and press ++enter++ to open the interpretor
2. Type `import requests as somethingelse` and press enter
3. Type `somethingelse.get("https://google.com")` and press ++enter++. You should see `<Response [200]>` printed to the terminal. You can use `as` to rename modules.
4. Type `import requests` and press ++enter++
5. Type `requests.get("https://google.com")` and press ++enter++. You should see `<Response [200]>` printed to the terminal. You can import requests like normal alongside your custom name.
6. Type `exit()` and press ++enter++ to exit
### dots
When you type `requests.get()` have you considered what the `.` means? Anytime you see a `.` it means you can split that thing apart. Example:
```text
requests . get
└──────┘ ^ └─┘
```
are two separate things.
`requests` is a "package". A package can container other packages, classes, functions, variables, etc.
`get` is a "function". That means it was created like this:
```python
def get(url, ...):
*code here*
```
Check around line 64 in `venv/lib/python3/site-packages/requests/api.py` to see the actual function.
Let's actually make use of that dot.
1. Type `python` in the terminal and press ++enter++
2. Type `from requests import get` and press ++enter++
3. Type `requests.get("https://google.com")` and press ++enter++
4. You should see `NameError: name 'requests' is not defined` printed to the terminal
5. Type `get("https://google.com")` and press ++enter++. You should see `<Response [200]>` printed to the terminal. You just split the `get` function from the `requests` package. You don't need to type `requests.` to use the `get` function.
6. Type `post("https://google.com")` and press ++enter++. You should see `NameError: name 'post' is not defined` printed to the terminal. `post` is a valid function, let's import it.
7. Type `from requests import *` and press ++enter++. We just used a "wildcard" import. It import everything into our interpretor from the package..
8. Type `post("https://google.com")` and press ++enter++. You should see `<Response [405]>` printed to the terminal. You didn't need to `from requests import post` to use the `post` function. You imported it when you used the `*` symbol.
9. Type `exit()` and press ++enter++ to exit.
## Breaking apart our terrible weather app
What better way to demonstrate the power of imports than with our terrible weather app?
1. Type `python weather_app.py` in your terminal and press ++enter++. You should run through your terrible weather app just like on day 2
2. Open "weather_app.py" in VSCode
3. Notice the `import random` at the top, you should know what's happening here.
4. Create a new file called `weather_config.py` in the root directory
![config](img/day5/config.gif)
5. Copy the 4 variables from the top of "weather_app.py" into "weather_config.py"
![copy](img/day5/copy.gif)
6. Save with ++ctrl+s++
7. Delete those variables from "weather_app.py".
![delete](img/day5/delete.gif)
8. Save with ++ctrl+s++
9. Delete `import random` from the top of "weather_app.py"
10. Save with ++ctrl+s++
11. Add `import random` to the top of "weather_config.py"
```python hl_lines="1 2"
import random
warm = random.choice([True, False])
cold = not warm
raining = random.choice([True, False])
snowing = not raining
```
12. Save with ++ctrl+s++
13. In your terminal type `python weather_app.py` and press ++enter++. You should see `NameError: name 'warm' is not defined` print to the terminal. But `warm` is in our weather_config.py! We need to import it!
14. At the top of "weather_app.py" add `from weather_config import warm`
```python hl_lines="1 2"
from weather_config import warm
if warm or cold:
print("It's warm or cold.")
if raining or warm:
```
15. Save with ++ctrl+s++
16. Type `python weather_app.py` again and press ++enter++. This time you should see `NameError: name 'cold' is not defined` printed to your terminal. Ah, we imported warm but none of the other variables. We need to import those as well. We could type everything out one by one, but instead:
17. At the top of "weather_app.py" change `from weather_config import warm` to `from weather_config import *`
18. Save with ++ctrl+s++
19. Now type `python weather_app.py` again and press ++enter++. Your weather app works as normal!
### Breaking it even further apart
We're going to turn each "chunk" of code in our weather app into a function that we can move to another file.
1. Starting at line 3, add the following (**indent the if statements!**):
```python hl_lines="1 1"
def print_clues():
--->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.")
```
![clues](img/day5/clues.png)
2. Now do the same to our "guess chunks" like so:
```python hl_lines="1 1"
def check_guesses():
--->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!')
```
![guesses](img/day5/guesses.png)
3. Save with ++ctrl+s++
4. Type `python weather_app.py` in the terminal and press ++enter++. Nothing happens!
5. Create a new file called "weather_run.py" in your root directory.
![run](img/day5/run.gif)
6. Add the following to "weather_run.py"
```python
from weather_app import print_clues, check_guesses
print_clues()
check_guesses()
```
7. Save with ++ctrl+s++
8. In your terminal type `python weather_run.py` and press ++enter++. Your weather app works again! You've broken it out into a bunch of modules.
9. Type `python` in your terminal and press ++enter++ to open the python interpretor
10. Type `import weather_run` and press ++enter++. Your weather app should run in the terminal
11. Press ++ctrl+c++ to stop your app from running
12. Type `exit()` to exit
13. We don't want your app to run when we import it. Let's add a check to "weather_run.py" to make sure that doesn't happen. Modify "weather_run.py" like so:
```python hl_lines="3 3"
from weather_app import print_clues, check_guesses
if __name__ == "__main__":
--->print_clues()
--->check_guesses()
```
![main](img/day5/main.png)
14. Type `python` in your terminal and press ++enter++ to open the python interpretor
15. Type `import weather_run` and press ++enter++. This time nothing should happen! That's good, we want to import our app without it running immediately. Just like importing `requests` doesn't immediately run the `get` function, we don't want anything to run on import of our programs.
16. Type `exit()` to exit
## Importing our menu
We can use imports to make our lives a lot easier. Let's use our menu as an example.
1. Open "menu.py" in VSCode
2. Type `python` in your terminal and press ++enter++ to open the python interpretor
3. Type `import menu` and press enter. You've just imported all our functions from "menu.py". Notice how the menu doesn't run though, that's because we added a `if __name__ == "__main__"` block! We were thinking ahead.
4. Open a new terminal windows by clicking the plus icon
5. Type `python manage.py runserver` and press ++enter++ to start your server
6. Use the dropdown to switch back to your other terminal window
7. Type `menu.update_people(None)` and press ++enter++. You should see people print to the terminal. But wait, that's not what update() was supposed to do - and why did we have to specify `(None)`?
If we want our menu functions to be useful outside our menu app we have some work to do:
1. First, I shouldn't have to pass a default value like `(None)` to our functions. Edit `list_people` like so:
```python hl_lines="1 1"
def list_people(future=None, people=None):
if future is not None and future.done():
people = future.result()
future = None
print(people)
return (future, people)
```
This specifies that unless a "future" or "people" is provided they are "None" by default.
2. Let's do that to our remaining functions:
```python hl_lines="1 10"
def update_people(people=None):
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=None):
people = None
return people
```
3. Now let's fix our update function. `update_people` is supposed to run in the background, but it just returned the list of people. Let's create a "decorator".
4. At the top of `menu.py` add the following:
```python hl_lines="5-8"
import requests
import json
from concurrent.futures import ThreadPoolExecutor
def run_in_background(function):
def wrapper(*args, **kwargs):
return ThreadPoolExecutor().submit(function, *args, **kwargs)
return wrapper
def list_people(future, people):
if future is not None and future.done():
people = future.result()
future = None
```
5. Now add the following above `update_people()`:
```python hl_lines="1 1"
@run_in_background
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
```
6. Remove the `ThreadPoolExecutor()` part at line 40:
```python hl_lines="10 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":
future, people = list_people(future, people)
elif choice == "update":
future = update_people(people)
elif choice == "clear":
people = clear_people(people)
elif choice == "exit":
break
else:
print("Invalid choice. Please try again.")
```
7. Let's make sure we didn't break our menu. Type `python menu.py` and press ++enter++
8. Type update, list, clear, and exit to test your program.
9. Type `python` and press ++enter++ to open the interpretor
10. Type `import menu` and press ++enter++
11. Type `people = menu.list_people()` and press ++enter++. You should see `None` print.
12. Now type `update = menu.update_people()` and press ++enter++. Nothing should print.
13. Wait a moment for "successfully updated people." to print to the terminal
14. Type `people = menu.list_people(update)`. Your people should list! You successfully turned your menu into an importable package!
15. Type `exit()` to exit.
### Using our imported menu
For our last trick we'll use our menu functions in a new program.
1. Create a new file named "print_people.py"
![print_people](img/day5/print.gif)
2. Add the following:
```python
from menu import update_people, list_people
from concurrent.futures import wait
update = update_people()
wait([update])
list_people(update)
```
3. Type `python print_people.py` and press ++enter++ to run your program. You should see your people print after a while.
Congratulations! You've just imported a program you wrote to communciate with an API and used it to do something automatically.

BIN
docs/img/day5/clues.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 668 KiB

BIN
docs/img/day5/config.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 990 KiB

BIN
docs/img/day5/copy.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 933 KiB

BIN
docs/img/day5/delete.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

BIN
docs/img/day5/guesses.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 692 KiB

BIN
docs/img/day5/main.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 578 KiB

BIN
docs/img/day5/print.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

BIN
docs/img/day5/requests.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 MiB

BIN
docs/img/day5/run.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 768 KiB

View File

@@ -27,4 +27,16 @@
- "For" - "For"
- Creating an API - Creating an API
- Reading an API with Python - Reading an API with Python
### [Day 4](day4.md): while
- "While"
- Let's build a menu
- Threading
### [Day 5](day5.md): import
- "import"
- Breaking apart our terrible weather app
- Importing our menu

View File

@@ -5,6 +5,8 @@ nav:
- Day 1: day1.md - Day 1: day1.md
- Day 2: day2.md - Day 2: day2.md
- Day 3: day3.md - Day 3: day3.md
- Day 4: day4.md
- Day 5: day5.md
theme: theme:
name: material name: material
markdown_extensions: markdown_extensions: