Skip to content

Commit 285b82d

Browse files
committed
sixth commit, refresher finished
1 parent 3832378 commit 285b82d

File tree

14 files changed

+153
-26
lines changed

14 files changed

+153
-26
lines changed

01_variables/code.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
a = 5
1616
b = a
1717
print(f"a:{a}")
18-
print(f"b:{b}")
18+
print(f"b:{b}")

02_string_formatting/code.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
name = "Alice"
88
print(greeting_format.format(name))
99

10-
print("{} {}".format("Hello", "Alice"))
10+
print("{} {}".format("Hello", "Alice"))

03_user_input/code.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
size = input("What is your shoe size? ")
66
american_size = int(size) - 30
7-
print(f"Your American shoe size is {american_size}.")
7+
print(f"Your American shoe size is {american_size}.")
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
friends = {"A", "B", "C"}
2+
abroad = {"A", "C"}
3+
4+
local = friends.difference(abroad)
5+
abroad = abroad.difference(friends)
6+
friends = friends.union(abroad)
7+
print(friends)
8+
print(local)
9+
print(abroad)
10+
11+
both = friends.intersection(abroad)
12+
not_both = friends.symmetric_difference(abroad)
13+
print(both)
14+
print(not_both)

05_collections/code.py

-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
l = ["B", "E", "A"]
32
t = ("C", "F", "X")
43
s = {"O", "G", "H"}
@@ -8,19 +7,3 @@
87
print(l, type(l))
98
print(t, type(t))
109
print(s, type(s))
11-
12-
13-
friends = {"A", "B", "C"}
14-
abroad = {"A", "C"}
15-
16-
local = friends.difference(abroad)
17-
abroad = abroad.difference(friends)
18-
friends = friends.union(abroad)
19-
print(friends)
20-
print(local)
21-
print(abroad)
22-
23-
both = friends.intersection(abroad)
24-
not_both = friends.symmetric_difference(abroad)
25-
print(both)
26-
print(not_both)

06_booleans/code.py

+3
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
print(a == b)
1111
print(a is b)
1212
print(c is b)
13+
print(True or False)
14+
print(True and False)
15+
print(not True)

09_in_in_if/code.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
elif int(user) in [number - 1, number + 1]:
88
print("You were close!")
99
else:
10-
print("You guessed it wrong!")
10+
print("You guessed it wrong!")

12_dicts/code.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
friend_ages = {"Rolf": 24, "Adam": 30, "Anne": 27}
2+
23
try:
34
print(friend_ages["Rolf"])
45
print(friend_ages["Bob"])
@@ -12,7 +13,9 @@
1213
{"name": "Adam", "age": 30},
1314
{"name": "Anne", "age": 27}
1415
]
16+
1517
agesum = 0
18+
1619
for friend in friends:
1720
agesum += friend["age"]
1821
for k,v in friend.items():

13_destructing_variables/code.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
x = (5, 11)
2-
y = 5, 11
2+
y = [5, 11]
33
z = [(5, 11)]
44

5-
f, g = x
5+
f, g = y
66

77
print(type(x))
88
print(type(y))
@@ -29,8 +29,8 @@
2929

3030
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
3131

32-
head, head2, *tail = numbers
32+
*rest, head1, head2 = numbers
3333

34-
print(head)
34+
print(rest)
35+
print(head1)
3536
print(head2)
36-
print(tail)

35_at_syntax_decorators/code.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import functools
2+
3+
user = {"access_level": "admin"}
4+
5+
def make_secure(func):
6+
# @functools.wraps(func)
7+
def secure_function():
8+
if user["access_level"] == "admin":
9+
return func()
10+
else:
11+
return f"No admin permissions for {user['access_level']}"
12+
return secure_function
13+
14+
@make_secure
15+
def get_admin_password():
16+
return "1234"
17+
18+
print(get_admin_password.__name__) # 1234

36_parameter_decorators/code.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import functools
2+
3+
user1 = {"name":"Josh","access_level": "admin"}
4+
user2 = {"name":"Diana","access_level": "user"}
5+
6+
def make_secure(func):
7+
@functools.wraps(func)
8+
def secure_function(*args, **kwargs):
9+
user_temp = args[0]
10+
if user_temp["access_level"] == "admin":
11+
return func(*args, **kwargs)
12+
else:
13+
return f"User {user_temp['name']} {user_temp['access_level']} doesn't have the admin access level."
14+
return secure_function
15+
16+
@make_secure
17+
def get_admin_password(user: dict = {"name":"guest","access_level": "guest"}):
18+
return user["name"]+" 1234"
19+
20+
print(get_admin_password(user1)) # 1234
21+
print(get_admin_password(user2)) # User Diana doesn't have the admin access level.
22+

37_decorators_with_parameters/code.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import functools
2+
3+
user1 = {"name":"Josh","access_level": "admin", "password": "1234"}
4+
user2 = {"name":"Diana","access_level": "user", "password": "4321"}
5+
6+
def make_secure(access_level):
7+
def decorator(func):
8+
@functools.wraps(func)
9+
def secure_function(*args, **kwargs):
10+
if args:
11+
user_temp = args[0]
12+
if user_temp["access_level"] == access_level or user_temp["access_level"] == "admin":
13+
return func(*args, **kwargs)
14+
else:
15+
return f"User {user_temp['name']} doesn't have the {access_level} access level."
16+
else:
17+
return f"Guest access is not allowed."
18+
return secure_function
19+
return decorator
20+
21+
@make_secure("admin")
22+
def get_admin_password(user: dict):
23+
return user["name"]+user["password"]
24+
25+
@make_secure("user")
26+
def get_user_password(user: dict):
27+
return user["name"]+user["password"]
28+
29+
30+
31+
print(get_admin_password(user1))
32+
print(get_admin_password(user2))
33+
34+
print(get_user_password(user1))
35+
print(get_user_password(user2))

38_mutability/code.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
a = []
2+
b = a
3+
c = []
4+
5+
print(id(a))
6+
print(id(b))
7+
print(id(c))
8+
9+
print(a is b)
10+
a.append(35)
11+
c.append(35)
12+
print(a)
13+
print(b)
14+
print(c)

39_mutabile_parameters/code.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from typing import List, Optional
2+
3+
class Student:
4+
def __init__(self, name: str, grades: Optional[List[int]] = None):
5+
self.name = name
6+
self.grades = grades or []
7+
8+
def take_exam(self, result: int) -> None:
9+
self.grades.append(result)
10+
11+
class StudentNotRecommended:
12+
def __init__(self, name: str, grades: List[int] = []):
13+
self.name = name
14+
self.grades = grades
15+
16+
def take_exam(self, result: int) -> None:
17+
self.grades.append(result)
18+
19+
bob1 = StudentNotRecommended("Bob")
20+
bob1.take_exam(85)
21+
grace2 = StudentNotRecommended("Grace")
22+
23+
print(id(bob1.grades))
24+
print(id(grace2.grades))
25+
26+
print(grace2.grades)
27+
print(bob1.grades)
28+
29+
bob = Student("Bob")
30+
bob.take_exam(85)
31+
grace = Student("Grace")
32+
print(id(bob.grades))
33+
print(id(grace.grades))
34+
print(grace.grades)
35+
print(bob.grades)

0 commit comments

Comments
 (0)