Skip to content

Commit 7be6f04

Browse files
author
Jose Salvatierra
committed
Added code sections 2 to 6.
0 parents  commit 7be6f04

33 files changed

+1233
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.pyc
2+
.idea/
3+
__pycache__/
4+
*.db
5+
.DS_Store

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# REST APIs with Flask and Python
2+
3+
This repository contains code that is created in my course, REST APIs with Flask and Python.
4+
5+
The code is divided in folders, one for each section of the course which contains code.
6+
7+
## Section 2
8+
9+
The code is in files numbered between 1 and 11, covering concepts ranging from beginner to advanced.
10+
11+
1. Variables
12+
2. Methods
13+
3. Lists, tuples, and sets
14+
4. If statements
15+
5. List comprehension
16+
6. Dictionaries
17+
7. Classes and objects
18+
8. Static and class methods
19+
9. Args and Kwargs
20+
10. Passing functions as arguments
21+
11. Decorators
22+
23+
## Section 3
24+
25+
The code in this section includes a simple Flask app and a HTML and JavaScript file which calls the Flask app endpoints.
26+
27+
## Section 4
28+
29+
The code in this section includes a Flask app which is an API that represents items. It also includes user registration and authentication.
30+
31+
## Section 5
32+
33+
The code in this section extends the last section by adding persistent storage of Items to a SQLite database.
34+
35+
## Section 6
36+
37+
The code in this section extends the previous section by replacing the manual integration with SQLite, with SQLAlchemy—an ORM (Object-Relational Mapping)—which allows us to easily replace SQLite with something like PostgreSQL or MySQL.

section2/10_args_and_kwargs.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
def my_method(arg1, arg2):
2+
return arg1 + arg2
3+
4+
def my_really_long_addition(arg1, arg2, arg3, arg4, arg5):
5+
return arg1 + arg2 + arg3 + arg4 + arg5
6+
7+
my_really_long_addition(13, 45, 66, 3, 4)
8+
9+
def adding_simplified(arg_list):
10+
return sum(arg_list)
11+
12+
adding_simplified([13, 45, 66, 3, 4])
13+
14+
# But you need a list :(
15+
16+
def what_are_args(*args):
17+
print(args)
18+
19+
what_are_args(12, 35, 64, 'hello')
20+
21+
def adding_more_simplified(*args):
22+
return sum(args) # args is a tuple of arguments passed
23+
24+
adding_more_simplified(13, 45, 66, 3, 4)
25+
26+
###
27+
28+
# As well as a tuple of args, we can pass kwargs
29+
30+
def what_are_kwargs(*args, **kwargs):
31+
print(args)
32+
print(kwargs)
33+
34+
what_are_kwargs(name='Jose', location='UK')
35+
what_are_kwargs(12, 35, 66, name='Jose', location='UK')
36+
37+
# args are a tuple
38+
# kwargs is a dictionary
39+
# This will come in handy!

section2/11_passing_functions.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def methodception(another):
2+
return another()
3+
4+
def add_two_numbers():
5+
return 35 + 77
6+
7+
methodception(add_two_numbers)
8+
9+
###
10+
11+
methodception(lambda: 35 + 77)
12+
13+
my_list = [13, 56, 77, 484]
14+
list(filter(lambda x: x != 13, my_list)) # A lambda function is just a short, one-line function that has no name.
15+
16+
# We could also do
17+
18+
def not_thirteen(x):
19+
return x != 13
20+
21+
list(filter(not_thirteen, my_list))
22+
23+
# filter() passes each element of my_list as a parameter to the function.
24+
# Pretty neat, eh?

section2/12_decorators.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# A decorator is just a function that gets called before another function
2+
3+
import functools # function tools
4+
5+
def my_decorator(f):
6+
@functools.wraps(f)
7+
def function_that_runs_f():
8+
print("Hello!")
9+
f()
10+
print("After!")
11+
return function_that_runs_f
12+
13+
@my_decorator
14+
def my_function():
15+
print("I'm in the function.")
16+
17+
my_function()
18+
19+
20+
###
21+
22+
def my_decorator(f):
23+
@functools.wraps(f)
24+
def function_that_runs_f(*args, **kwargs):
25+
print("Hello!")
26+
f(*args, **kwargs)
27+
print("After!")
28+
return function_that_runs_f
29+
30+
@my_decorator
31+
def my_function(arg1, arg2):
32+
print(arg1 + arg2)
33+
34+
my_function(56, 89)
35+
36+
###
37+
38+
def decorator_arguments(number):
39+
def my_decorator(f):
40+
@functools.wraps(f)
41+
def function_that_runs_f(*args, **kwargs):
42+
print("Hello!")
43+
if number == 56:
44+
print("Not running!")
45+
else:
46+
f(*args, **kwargs)
47+
print("After")
48+
return function_that_runs_f
49+
return my_decorator
50+
51+
@decorator_arguments(56)
52+
def my_function():
53+
print("Hello!")
54+
55+
my_function()

section2/1_variables_methods.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
a = 5
2+
b = 10
3+
my_variable = 56
4+
any_variable_name = 100
5+
6+
string_variable = "hello"
7+
single_quotes = 'strings can have single quotes'
8+
9+
print(string_variable)
10+
print(my_variable)
11+
12+
# print is a method with one parameter—what we want to print
13+
14+
def my_print_method(my_parameter):
15+
print(my_parameter)
16+
17+
my_print_method(string_variable)
18+
19+
def my_multiplication_method(number_one, number_two):
20+
return number_one * number_two
21+
22+
result = my_multiplication_method(a, b)
23+
print(result)
24+
25+
print(my_multiplication_method(56, 75))
26+
27+
my_print_method(my_multiplication_method('b', 5)) # What would this do?

section2/2_lists_tuples_sets.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
my_variable = 'hello'
2+
my_list_variable = ['hello', 'hi', 'nice to meet you']
3+
my_tuple_variable = ('hello', 'hi', 'nice to meet you')
4+
my_set_variable = {'hello', 'hi', 'nice to meet you'}
5+
6+
print(my_list_variable)
7+
print(my_tuple_variable)
8+
print(my_set_variable)
9+
10+
my_short_tuple_variable = ("hello",)
11+
another_short_tuple_variable = "hello",
12+
13+
print(my_list_variable[0])
14+
print(my_tuple_variable[0])
15+
print(my_set_variable[0]) # This won't work, because there is no order. Which one is element 0?
16+
17+
my_list_variable.append('another string')
18+
print(my_list_variable)
19+
20+
my_tuple_variable.append('a string') # This won't work, because a tuple is not a list.
21+
my_tuple_variable = my_tuple_variable + ("a string",)
22+
print(my_tuple_variable)
23+
my_tuple_variable[0] = 'can I change this?' # No, you can't
24+
25+
my_set_variable.add('hello')
26+
print(my_set_variable)
27+
my_set_variable.add('hello')
28+
print(my_set_variable)
29+
30+
31+
###### Set Operations
32+
33+
set_one = {1, 2, 3, 4, 5}
34+
set_two = {1, 3, 5, 7, 9, 11}
35+
36+
print(set_one.intersection(set_two)) # {1, 3, 5}
37+
38+
print({1, 2}.union({2, 3})) # {1, 2, 3}
39+
40+
print({1, 2, 3, 4}.difference({2, 4})) # {1, 3}

section2/3_loops.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
my_string = "hello"
2+
3+
for character in my_string:
4+
print(character)
5+
6+
for asdf in my_string:
7+
print(asdf)
8+
9+
my_list = [1, 2, 5, 3, 67]
10+
11+
for number in my_list:
12+
print(number)
13+
14+
for number in my_list:
15+
print(number ** 2)
16+
17+
should_continue = True
18+
while should_continue:
19+
print("I'm continuing!")
20+
21+
user_input = input("Should we continue? (y/n)")
22+
if user_input == 'n':
23+
should_continue = False

section2/4_if_statements.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
my_known_people = ["John", "Rolf", "Anne"]
2+
user_name = input("Enter your name: ")
3+
if user_name in my_known_people:
4+
print("Hello, I know you!")
5+
6+
7+
if user_name in my_known_people:
8+
print("Hello {}, I know you!".format(user_name))
9+
10+
11+
if user_name in my_known_people:
12+
print("Hello {name}, I know you!".format(name=user_name))
13+
14+
"Hello {name}, I know you {}!".format("well", name=user_name)
15+
"Hello {}, I know you {}!".format("John", "well")
16+
17+
#### Exercise
18+
19+
def who_do_you_know():
20+
names = input("Enter the names of people you know, separated by commas: ")
21+
names_list = names.split(",")
22+
return names_list
23+
24+
def ask_user():
25+
# Ask user for their name
26+
# See if their name is in list of people
27+
# Print something if it is
28+
29+
user_name = input("Enter your name: ")
30+
if user_name in who_do_you_know():
31+
print("Hello {}, I know you!".format(user_name))

section2/5_list_comprehension.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
my_list = [0, 1, 2, 3, 4]
2+
an_equal_list = [x for x in range(5)]
3+
4+
for my_number in range(10):
5+
print(my_number)
6+
7+
[my_number for my_number in range(10)]
8+
9+
[my_number * 2 for my_number in range(10)]
10+
11+
1 % 2
12+
2 % 2
13+
5 % 2
14+
8 % 3
15+
16+
[n for n in range(10) if n % 2 == 0]
17+
18+
names_list = ["John", "Rolf", "Anne"]
19+
lowercase_names = [name.lower() for name in names_list]
20+
print(lowercase_names)

0 commit comments

Comments
 (0)