Section thirty-fourth regular route add features and functionality added attention

Import Re
 Import pymysql 

URL_DICT = dict () 

'' ' 
to a regular route to add a reason: When the actual development, URL would often with a lot of parameters, for example: /add/0000007.html, wherein 
000007 (stock code means: can for extracting the corresponding database record) is the parameter, 
if this time is not regular, then we should write N times @route, add the corresponding function to the dictionary, then the dictionary of key-value pairs with N, a waste of space, 
If regular, then only need to write one @route to complete multiple URL, such as /add/00007.html,add/000036.html correspond to the same function, when the dictionary key-value pairs 
will be much less 
'' ' 
DEF route (URL):
     DEF set_func (FUNC): 
        URL_DICT [URL] = FUNC
         DEF call_func (): 
            FUNC () 
        return call_func
     return set_func 

@route (R & lt ' /center.html' )
 DEF center_p (SET):
     Pass 

@route (R & lt ' /add/(\d+)\.html ' )
 DEF add_focus (RET):
     # Get ticker 
    stock_code ret.group = (. 1 )
     # determines whether the stock Code 
    Conn = pymysql.connect ( ' localhost ' , ' the root ' , '' , ' python_test ' ) 
    Cursor = conn.cursor () 
    SQL = " SELECT * WHERE info from code =% S "
    cursor.execute (SQL, (stock_code,)) 
    IF  not cursor.fetchone (): 
        cursor.close () 
        conn.Close () 
        return  ' does not run the stock, we are start-up companies, please popular men ' 
    the else :
         # judge whether the stock code is already concerned about 
        SQL = ' the SELECT * focus from the WHERE info_id =% S ' 
        cursor.execute (SQL, (stock_code,)) 
        iF cursor.fetchone (): 
            cursor.close () 
            conn.Close () 
            return  ' has been a concern, do not repeat concerned ' 
        the else :
             # Add concern 
            SQL = 'Focus INTO INSERT (info_id) SELECT from ID code =% S WHERE info ' 
            the cursor.execute (SQL, (stock_code,)) 
            conn.commit () 
            cursor.close () 
            conn.Close () 
    return  ' the Add (% S) OK .... ' % stock_code 

@route (R & lt ' index.html ' )
 DEF index_p (RET):
     Pass 

DEF file application (the env, start_response):
     ' '' the env is an empty dictionary, start_response a web server in a reference method , the function return body '' ' 
    the start_response ( ' 200 is the OK ' , [( 'Content-Type', 'text/html;charset=utf-8')])
    file_name = env['PATH INFO']

    try:
        # return URL_DICT[file_name]()
        for url, func in URL_DICT.items():
            '''
            {r"/index.html":index,
            r"/center.html":center,
            r"/add/\d+\.html":add_focus
            } 
            '''
            ret = re.match(url, file_name)
            if ret:
                return func(ret)
    exceptAS RET Exception:
         return  ' an abnormality S% ' % STR (RET)

 

Guess you like

Origin www.cnblogs.com/kogmaw/p/12602582.html