Skip to content

Commit 3ed246e

Browse files
committedMar 10, 2021
Update Day-54
1 parent 185f05e commit 3ed246e

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed
 
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import time
2+
3+
def speed_calc_decorator(function):
4+
def wrapper_function():
5+
start_time = time.time()
6+
function()
7+
end_time = time.time()
8+
print(f"{function.__name__} run speed: {end_time-start_time}s")
9+
return wrapper_function
10+
11+
@speed_calc_decorator
12+
def fast_function():
13+
for i in range(10000000):
14+
i * i
15+
16+
@speed_calc_decorator
17+
def slow_function():
18+
for i in range(100000000):
19+
i * i
20+
21+
fast_function()
22+
slow_function()

‎Day-54-IntroWebDevelopment/hello.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Run a Flask server
2+
from flask import Flask
3+
app = Flask(__name__)
4+
# __name__ is a special built-in Python attribute ie a name of
5+
# class, function, method, descriptor or generator instance
6+
# '__main__' is name of scope, which means executing code in particular module.
7+
8+
@app.route("/")
9+
def hello_world():
10+
return "Hello, World!"
11+
12+
@app.route("/bye")
13+
def hello_bye():
14+
return "Bye!"
15+
16+
# On Command Prompt:
17+
# set FLASK_APP=hello.py
18+
# flask run
19+
20+
# Alternative to `flask run`
21+
if __name__=="__main__":
22+
app.run()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import time
2+
# Python Decorator Function
3+
4+
# It wraps another function, modifies or add some additional functionality
5+
def delay_decorator(function):
6+
def wrapper_function():
7+
# Do something
8+
time.sleep(2)
9+
function()
10+
return wrapper_function
11+
12+
@delay_decorator
13+
def say_hello():
14+
print("Hello")
15+
say_hello()
16+
17+
def say_greeting():
18+
print("Greeting")
19+
# `@delay_decorator` Alternative for
20+
decorated_function = delay_decorator(say_greeting)
21+
decorated_function()
22+
23+
def say_bye():
24+
print("Bye")

0 commit comments

Comments
 (0)
Please sign in to comment.