84. flask signal and mateclass Meta, flaskmateclass
Navigate to this article:
- Flask instantiation Parameters
- Signal
- Metaclass metadata Parsing
1. flask instantiation Parameters
Instance_path and instance_relative_config are used together;
These two parameters are used to find the configuration file. They are used when you use app. config. from_pyfile ('settings. py') to import the configuration file.
From flask import Flask, requestapp = Flask (_ name __, instance_path = None, instance_relative_config = True) app. config. from_pyfile ('settings. py ') # C: \ Users \ Administrator \ PycharmProjects \ s6day120 \ 1. instantiation supplement # instsnce_path: # If instance_path is configured, you will find the file in the instance # instance_relative_config: # if it is set to True, the configuration file cannot be found, you will find the settings in the instance. pyapp. open_sessionprint (app. config. get ("NNN") @ app. route ('/Index') # app. route ('/Index') f (index) def index (): print (request) return "xx" if _ name _ =' _ main __': app. _ call _ app. run ()
If instance_releative_config = True is set, the settings. py file cannot be found. Solution: Create a folder for the instance manually.
2. Signal (blinker)
1. flask built-in Signal
The signal in the Flask framework is based on blinker, which mainly enables developers to customize some user behaviors during the flask request process. To put it bluntly, flask is in the list.
A few empty lists are reserved to store items in them. Signals help you decouple applications by sending notifications. In short, a signal allows a sender to notify the recipient of something happening ;,
10 signals: 2. request_started = _ signals. signal ('request-started') # execute 5 before the request arrives. request_finished = _ signals. signal ('request-finished') # execute after the request ends 3. before_render_template = _ signals. signal ('before-render-template') # execute the template before rendering. template_rendered = _ signals. signal ('template-rendered') # got_request_exception = _ signals. signal ('got-request-exception') # When an exception occurs during request execution 6. request_tearing_down = _ signals. signal ('request-tearing-lower') # Automatic execution after the request is executed (whether successful or not) 7. appcontext_tearing_down = _ signals. signal ('appcontext-tearing-low') # Automatic execution after the request context is executed (whether successful or not) 1. appcontext_pushed = _ signals. signal ('appcontext-pushed') #8. appcontext_popped = _ signals. signal ('appcontext-poped') # execute message_flashed = _ signals when requesting context pop. signal ('message-flashed') # It is automatically triggered when you call flask to add data to it.
1) Question 1:
What is the difference between a special decoration device (@ app. before_first_request; @ app. before_request; @ app. after_request) and a signal?
-The trigger signal has no return value. It doesn't matter if you do not write the return value.
-The special decorator makes sense to the return value. When before_request has a return value, it will not execute subsequent view functions. If no return value is available, it will execute subsequent functions, after_request must return values. Therefore, the function of the Special decorator is more powerful than that of the signal.
2) Question 2:
Can I use signals for permissions?
-We can't do it ourselves. If we want to use other mechanisms together, it will be very free and troublesome to do so, so we should choose middleware.
3) Question 3:
What is the signal used?
-Only perform custom operations without returning values.
-Reduced Code Coupling
2. flask built-in signal source code details
From flask import Flask, render_template, flashapp = Flask (_ name _) @ app. route ('/Index') def index (): flash () return render_template () if _ name _ =' _ main _ ': app. _ call _ app. run () #1 appcontext_pushed = _ signals. signal ('appcontext-pushed') # execute the command when requesting context push # def wsgi_app (self, environ, start_response): # def push (self ): #2 request_started = _ signals. signal ('request-started') # execution before request arrival # def wsgi_app (self, environ, start_response): # def full_dispatch_request (self): # request_started.send (self) #3 before_render_template = _ signals. signal ('before-render-template') # execute the template before rendering #4 template_rendered = _ signals. signal ('template-rendered') # Run the template after rendering # def render_template (template_name_or_list, ** context): # def _ render (template, context, app ): #5 request_finished = _ signals. signal ('request-finished') # Run the command after the request ends # def wsgi_app (self, environ, start_response): # def full_dispatch_request (self): # def finalize_request (self, rv, from_error_handler = False): # request_finished.send (self, response = response) #2 \ 3 \ 4 \ 5 where an error occurs and no error occurs. Do not execute # got_request_exception = _ signals. signal ('got-request-exception') # execute when an exception occurs during request execution # def wsgi_app (self, environ, start_response): # def handle_exception (self, e ): #6 request_tearing_down = _ signals. signal ('request-tearing-lower') # Automatic execution after the request is executed (whether successful or not) # def wsgi_app (self, environ, start_response): # def auto_pop (self, exc): # def pop (self, exc = _ sentinel): # def do_teardown_request (self, exc = _ sentinel): #7 appcontext_tearing_down = _ signals. signal ('appcontext-tearing-lower') # Automatic execution after the request context is executed (whether successful or not) # def wsgi_app (self, environ, start_response): # def auto_pop (self, exc): # def pop (self, exc = _ sentinel): # AppContext # def pop (self, exc = _ sentinel): # def do_teardown_appcontext (self, exc = _ sentinel): #8 appcontext_popped = _ signals. signal ('appcontext-poped') # execute when requesting context pop # def wsgi_app (self, environ, start_response): # def auto_pop (self, exc): # def pop (self, exc = _ sentinel): ## AppContext # def pop (self, exc = _ sentinel): # message_flashed = _ signals. signal ('message-flashed') # automatically triggered when calling flask to add data to it # def flash (message, category = 'message '):
View Code
3. metaclass metadata Parsing
1. Create a class Process
2. What is a metabase?
In Python3, the inherited type is the Meta class.
Metadata example
1) Example 1
# Method 1 class MyType (type): ''' the inherited type is the metaclass ''' def _ init _ (self, * args, ** kwargs ): print ("object created by MyType", self) # Foo super (MyType, self ). _ init _ (* args, ** kwargs) def _ call _ (self, * args, ** kwargs): obj = super (MyType, self ). _ call _ (* args, ** kwargs) print ("class creation object", self, obj) # Fooclass Foo (object, metaclass = MyType ): # The _ call _ method is executed in parentheses of the object, and the _ call _ method of type inherits from the _ call _ method of the object, in the _ call _ method of type, the _ new _ method is executed first, and then the _ init _ method is executed __ Method. Therefore, Foo is the user = "haiyan" age = 18obj = Foo () created with type ()
View Code
2) Example 2
# Method 2 class MyType (type): def _ init _ (self, * args, ** kwargs): print ("ssss") super (MyType, self ). _ init _ (* args, ** kwargs) def _ call _ (cls, * args, ** kwargs): v = dir (cls) obj = super (MyType, cls ). _ call _ (* args, ** kwargs) return obj # The _ call _ method class Foo (MyType ('zcc ', (object,), {})): # MyType ('zcc', (object,), {}) is equivalent to class Zcc (object): pass, that is, a Zcc class user = 'haiyan 'age = 18obj = Foo () is created ()
View Code
3) Example 3
# Method 3 class MyType (type): def _ init _ (self, * args, ** kwargs): print ("ssss") super (MyType, self ). _ init _ (* args, ** kwargs) def _ call _ (cls, * args, ** kwargs): v = dir (cls) obj = super (MyType, cls ). _ call _ (* args, ** kwargs) return obj # The _ call _ method def with_metaclass (arg, base) is executed when the object is enclosed in brackets ): print ("Class Object", MyType ('zcc', (base,), {}) return arg ('zcc', (base ,),{}) # Return A class Object <class '_ main __. zcc'> class Foo (with_metaclass (MyType, object): # MyType ('zcc', (object,), {}) is equivalent to class Zcc (object): pass, that is, a Zcc class user = 'haiyan 'age = 18obj = Foo () is created ()
View Code
4) Others
Class ASD (type): passqqq = ASD ("qwe", (object,), {}) # Use the ASD Meta class to create a (qwe, and inherit from the object class) class # class ASD (qwe): # passobj = qqq () # Metadata classes can be created # print (obj) classes can be created for objects # <__ main __. qwe object at 0x00000000024FFBA8> print (obj. _ class _) # <class '_ main __. qwe '> print (obj. _ class __. _ class _) # <class '_ main __. ASD '> print (obj. _ class __. _ class __. _ class _) # <class 'type'> print (obj. _ class __. _ class __. _ class __. _ class _) # <class 'type'>
View Code