How do I access my database-level functions in other classes / files in Tornado?

I'm new to Tornado, and now I'm trying to get past this recent stumbling block. I currently have specific database variables and I instantiate the handlers, settings, and database connection information when initializing the Application class. I also have a base handler class (named BaseHandler) that provides a simple database interface for other classes. I would like to split some of my classes into other files and have most of my database logic in these other class methods and save application.py for routes and, if necessary, create these other classes and pass the necessary data to them for the database. How can I access this self.db function from these other files / classes?

application.py:

import tornado.database import tornado.httpserver import tornado.ioloop import tornado.options import tornado.web from tornado.options import define, options from user import User # Define some startup settings, can be changed through the command line define("port", default=8888, help="Run on the given HTTP port", type=int) define("db_host", default="localhost:3306", help="Database host") define("db_name", default="database_name", help="Database name") define("db_user", default="user", help="Database username") define("db_pass", default="password", help="Database password") class Application(tornado.web.Application): def __init__(self): handlers = [ (r"/", MainHandler) ] settings = dict( application_title = u"Test Application", template_path = os.path.join(os.path.dirname(__file__), "templates"), static_path = os.path.join(os.path.dirname(__file__), "static"), autoescape = None ) tornado.web.Application.__init__(self, handlers, **settings) self.db = tornado.database.Connection( host=options.db_host, database=options.db_name, user=options.db_user, password=options.db_pass) class BaseHandler(tornado.web.RequestHandler): @property def db(self): return self.application.db class MainHandler(BaseHandler): def get(self): u = User() self.write(tornado.escape.json_encode(u.get_user(" test@test.com ))) 

user.py:

 class User(object): def get_user(self, email): result = self.db.get("SELECT first_name, last_name FROM users WHERE email = %s", email) if not result: return False return True, result 

This logic works fine when I did not split the logic into a separate file, so I am obviously doing something wrong / something is missing.

+7
source share
1 answer

I meet the same situation just now, then go through some examples on github and do it.

I split my files as follows:

  • server.py: run the application
  • urls.py: define handlers and ui_modules
  • da.py: identify useful methods for accessing data.

Below is a brief description of each file, I think this will help you solve your problem.

urls.py

 import main import topic handlers=[] handlers.extend(main.handlers) handlers.extend(topic.handlers) ui_modules={} 

da.py

import tornado.database

 from tornado.options import define,options define("mysql_host", default="127.0.0.1:3306", help="database host") define("mysql_database", default="forum", help="database name") define("mysql_user", default="root", help="database user") define("mysql_password", default="111111", help="database password") db = tornado.database.Connection( host=options.mysql_host, database=options.mysql_database, user=options.mysql_user, password=options.mysql_password) 

server.py

 import os import tornado.database import tornado.httpserver import tornado.ioloop import tornado.web from tornado.options import define, options define("port", default=8888, help="run on the given port", type=int) import da class Application(tornado.web.Application): def __init__(self): from urls import handlers,ui_modules settings = dict( template_path=os.path.join(os.path.dirname(__file__), "templates"), static_path=os.path.join(os.path.dirname(__file__), "static"), xsrf_cookies=True, cookie_secret="11oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=", login_url="/signin", ui_modules=ui_modules, debug=True, ) super(Application,self).__init__(handlers,**settings) # tornado.web.Application.__init__(self, handlers, **settings) # Have one global connection to the blog DB across all handlers self.db = da.db def runserver(): tornado.options.parse_command_line() http_server = tornado.httpserver.HTTPServer(Application()) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start() if __name__ == "__main__": runserver() 

You can use db only 'from da import *', and then everything will be fine, or you can write BaseHandler extends tornado.web.RequestHandler and define the property:

 class BaseHandler(tornado.web.RequestHandler): @property def db(self): return self.application.db 

Each handler that extends BaseHandler can use self.db to perform database operations.

+10
source

All Articles