Domanda

E 'un po' brutto che molte righe di codice rientrano in "__main__". Qualcuno può darmi un consiglio su come muoversi in SessionMiddleware metodo manico? Dovrei notare che uso sessione CoreXmlParser. Grazie in anticipo!

def handle(environ, start_response):
        req = webob.Request(environ)
        c = CoreXmlParser(req)
        resp = webob.Response(body=c(), charset = 'utf-8', status='200 OK', \
        request=req, content_type='text/xml')
        resp(environ, start_response)
        return resp.app_iter

    if __name__ == '__main__':
        #parse config file for session options
        app = SessionMiddleware(handle, some_session_opts_here)
        from flup.server.fcgi import WSGIServer
        WSGIServer(app).run()
È stato utile?

Soluzione

Non sono sicuro capisco perché si sta cercando di spostare una sola riga. Se si vuole ridurre la quantità di roba in "__main__", perché non basta spostare tutta quella roba "#parse config file" in una funzione separata?

def handle(environ, start_response):
    # same as before

def create_app(config_file):
    #parse config file for session options
    return SessionMiddleWare(handle, some_session_opts_here)

if __name__ == '__main__':
    app = create_app(config_file)
    from flup.server.fcgi import WSGIServer
    WSGIServer(app).run()
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top