Decorator function xử lý ảnh

11 5 0
Decorator function  xử lý ảnh

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

DECORATOR FUNCTION Decorator function, một trong những khái niệm phức tạp nhất và hữu dụng nhất trong lập trình Python Nội dung Decorator trong Python là gì và tác dụng Cách định nghĩa c.

DECORATOR FUNCTION Decorator functon, môt khai niêm phưc tap nhât va hưu dung nhât lâp trinh Python Nơi dung: • Decorator Python la gi va tac dung • Cach định nghĩa cac decorator • Ví du • Viết code tốt decorator Giới thiệu @decorator def functon_to_decorate(): pass Tiền tố @ Cac decorator đặc biệt hưu ích việc giư code khơng bị lặp lại, va chúng lam điều đồng thời cải thiện khả đọc code vi decorator la cac ham Python 01 def x_plus_2(x): 02 return x + 03 04 print(x_plus_2(2)) # + ==4 05 def x_squared(x): 06 return x * x 07 print(x_squared(3)) # ^ == 08 09 # Let's compose the two functons for x=2 10 11 12 print(x_squared(x_plus_2(2))) # (2 + 2) ^ == 16 13 print(x_squared(x_plus_2(3))) # (3 + 2) ^ == 25 14 print(x_squared(x_plus_2(4))) # (4 + 2) ^ == 36 15 16 Tao ham khac, ham x_plus_2_squared x_squared(x_plus_2) # TypeError: unsupported operand type(s) for *: 'functon' and 'functon' Không thể tổ hợp cac ham cach vi hai ham đều nhận cac số la cac đối số: # Let's now create a proper functon compositon without actually applying the functon x_plus_2_squared = lambda x: x_squared(x_plus_2(x)) print(x_plus_2_squared(2)) # (2 + 2) ^ == 16 print(x_plus_2_squared(3)) # (3 + 2) ^ == 25 print(x_plus_2_squared(4)) # (4 + 2) ^ == 36 Định nghĩa lai cach ham x_squared lam việc: Châp nhận ham đối số Trả về ham khac Đăt tên tên ham tai tổ hợp x_squared đơn giản la squared 1 def squared(func): return lambda x: func(x) * func(x) print(squared(x_plus_2)(2)) # (2 + 2) ^ == 16 print(squared(x_plus_2)(3)) # (3 + 2) ^ == 25 print(squared(x_plus_2)(4)) # (4 + 2) ^ == 36 Bây sử dung với bât kỳ ham nao khac Dưới la số ví du: def x_plus_3(x): return x + 3 def x_tmes_2(x): return x * print(squared(x_plus_3)(2)) # (2 + 3) ^ == 25 print(squared(x_tmes_2)(2)) # (2 * 2) ^ == 16 Có thể nói ham squared decorate cac ham x_plus_2, x_plus_3 va x_tmes_2 x_plus_2 = squared(x_plus_2) # We decorated x_plus_2 with squared print(x_plus_2(2)) (2 + 2) ^ 01 def x_plus_2(x): 02 03 return x + # x_plus_2 now returns the decorated squared result: 04 x_plus_2 = squared(x_plus_2) 05 06 # ^ This is completely equivalent with: 07 08 @squared 09 def x_plus_2(x): 10 return x + Ký hiệu @ la hinh thưc cú phap dễ đọc: 01 02 03 @squared def x_tmes_3(x): return * x 04 05 06 print(x_tmes_3(2)) # (3 * 2) ^ = 36 # It might be a bit confusing, but by decoratng it with squared, x_tmes_3 became in fact (3 * x) * (3 * x) 07 08 09 10 @squared def x_minus_1(x): return x - 11 12 print(x_minus_1(3)) # (3 - 1) ^ = Xây dựng Decorator Một decorator la ham nhận ham la đối số va trả về ham khac, định nghĩa decorator: def decorator(functon_to_decorate): # return decorated_functon Có thể định nghĩa cac ham bên cac ham khac Trong hầu hết cac trường hợp, decorated_functon định nghĩa bên decorator def decorator(functon_to_decorate): def decorated_functon(*args, **kwargs): # Since we decorate `functon_to_decorate`, we should use it somewhere inside here return decorated_functon 01 import pytz 02 from datetme import datetme 03 04 def to_utc(functon_to_decorate): 05 def decorated_functon(): 06 # Get the result of functon_to_decorate and transform the result to UTC 07 return functon_to_decorate().astmezone(pytz.utc) 08 return decorated_functon 09 10 @to_utc 11 def package_pickup_tme(): 12 13 14 """ This can come from a database or from an API """ tz = pytz.tmezone('US/Pacific') return tz.localize(datetme(2017, 8, 2, 12, 30, 0, 0)) 15 16 17 18 19 20 @to_utc def package_delivery_tme(): """ This can come from a database or from an API """ tz = pytz.tmezone('US/Eastern') return tz.localize(datetme(2017, 8, 2, 12, 30, 0, 0)) # What a coincidence, same tme different tmezone! 21 22 23 print("PICKUP: ", package_pickup_tme()) # '2017-08-02 19:30:00+00:00' print("DELIVERY: ", package_delivery_tme()) # '2017-08-02 16:30:00+00:00' Ví dụ Thực tế Một trường hợp sử dung rât phổ biến va rât kinh điển khac decorator la lưu cache kết ham: 01 import tme 02 03 def cached(functon_to_decorate): 04 _cache = {} # Where we keep the results 05 def decorated_functon(*args): 06 start_tme = tme.tme() 07 print('_cache:', _cache) 08 if args not in _cache: _cache[args] = functon_to_decorate(*args) # Perform the computaton and store it in cache 09 10 11 print('Compute tme: %ss' % round(tme.tme() - start_tme, 2)) return _cache[args] return decorated_functon 12 13 14 15 16 17 @cached def complex_computaton(x, y): print('Processing ') tme.sleep(2) return x + y 18 19 print(complex_computaton(1, 2)) # 3, Performing the expensive operaton 20 print(complex_computaton(1, 2)) # 3, SKIP performing the expensive 21 operaton 22 print(complex_computaton(4, 5)) # 9, Performing the expensive operaton 23 print(complex_computaton(4, 5)) # 9, SKIP performing the expensive operaton 24 print(complex_computaton(1, 2)) # 3, SKIP performing the expensive operaton Nếu ban nhin sơ qua code, ban thây khó chịu Decorator khơng thể sử dung lai được! Nếu decorate ham khac (gọi la another_complex_computaton) va gọi với cac tham số tương tự thi nhận cac kết cache từ ham complex_computaton Điều không xảy Decorator sử dung lai va la lý do: @cached def another_complex_computaton(x, y): print('Processing ') tme.sleep(2) return x * y print(another_complex_computaton(1, 2)) # 2, Performing the expensive operaton print(another_complex_computaton(1, 2)) # 2, SKIP performing the expensive operaton print(another_complex_computaton(1, 2)) # 2, SKIP performing the expensive operaton Ham chached gọi lần cho ham ma decorate, đó, biến _cache khac khởi tao lần va tồn tai ngư cảnh Hãy thử điều đó: print(complex_computaton(10, 20)) # -> 30 print(another_complex_computaton(10, 20)) # -> 200 Decorator Thực tế Decorator ma vừa mớ viết, ban nhận thây, rât hưu ích Nó hưu ích phiên phưc tap va manh mẽ tồn tai mơ đun functools têu chuẩn Nó đặt tên la lru_cache LRU la viết tắt Least Recently Used, ky thuât cahe 01 from functools import lru_cache 02 03 @lru_cache() 04 def complex_computaton(x, y): 05 print('Processing ') 06 tme.sleep(2) 07 return x + y 08 09 print(complex_computaton(1, 2)) # Processing 10 print(complex_computaton(1, 2)) # 11 print(complex_computaton(2, 3)) # Processing 12 print(complex_computaton(1, 2)) # 13 print(complex_computaton(2, 3)) # Một trường hợp sử dung decorator la framework Flask Nó thi gọn gang, la đoan code điều đầu tên ban nhin thây trang web Flask Đây la đoan code đó: 01 from flask import Flask 02 03 app = Flask( name ) 04 05 @app.route("/") 06 def hello(): 07 return "Hello World!" 08 09 if name == " main ": 10 app.run() Decorator app.route gan ham hello lam trinh xử lý yêu cầu cho tuyến "/" Thật đơn giản Một cach sử dung decorator gọn gang khac la bên Django Thông thường, cac ưng dung web có hai kiểu trang: cac trang ban xem ma không cần chưng thực (trang chủ, trang landing, bai blog, đăng nhập, đăng ký) cac trang ban cần phải chưng thực để xem (cai đặt tểu sử, hộp thư đến, bảng điều khiển) Nếu ban thử xem kiểu trang thư hai, ban thường chuyển hướng đến trang đăng nhập Dưới la cach thực điều Django: 01 from django.http import HttpResponse 02 from django.contrib.auth.decorators import login_required 03 04 # Public Pages 05 06 def home(request): 07 return HttpResponse("Home") 08 09 def landing(request): 10 return HttpResponse("Landing") 11 12 # Authentcated Pages 13 14 @login_required(login_url='/login') 15 def dashboard(request): 16 return HttpResponse("Dashboard") 17 18 @login_required(login_url='/login') 19 def profile_settings(request): 20 return HttpResponse("Profile Settings") Quan sat xem cac chế độ view riêng tư gọn gang nao đanh dâu login_required Khi xem qua code, rât rõ rang người đọc ma cac trang yêu cầu người dùng đăng nhập va trang khơng u cầu Phần tóm tắt Tơi hy vọng ban có buổi học thú vị về decorator vi chúng đai diện cho tính rât gọn gang Python Dưới la số điều cần nhớ: • Sử dung va thiết kế decorator cach lam cho code ban trở nên tốt hơn, sach va đẹp • Sử dung decorator giúp ban DRY code—di chuyển cac code giống từ bên cac ham vao decorator • Khi ban sử dung cac decorator nhiều hơn, ban thây cach tốt hơn, phưc tap để sử dung chúng ... 11 12 print(x_minus_1(3)) # (3 - 1) ^ = Xây dựng Decorator Một decorator la ham nhận ham la đối số va trả về ham khac, định nghĩa decorator: def decorator( functon_to_decorate): # return decorated_functon... khởi tao lần va tồn tai ngư cảnh Hãy thử điều đó: print(complex_computaton(10, 20)) # -> 30 print(another_complex_computaton(10, 20)) # -> 200 Decorator Thực tế Decorator ma vừa mớ viết, ban... World!" 08 09 if name == " main ": 10 app.run() Decorator app.route gan ham hello lam trinh xử lý yêu cầu cho tuyến "/" Thật đơn giản Một cach sử dung decorator gọn gang khac la bên Django Thông

Ngày đăng: 11/09/2022, 23:00

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan