weather_backend.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:Weather-App 作者: Tomasz-Kluczkowski 项目源码 文件源码
def __init__(self, controller):
        """Initialize Report class.

        Args:
            controller (Controller): controller object which will 
            store all the data required by each segment of the 
            application.

        :Attributes:
        :v_link (dict): Link to access variables in controller.
        :conn (sqlite3.Connection): Database object
        :cur (sqlite3.Cursor): Database cursor.
        :data_dirs (dict[str, str]): Data directories for the
            application.

        """
        self.controller = controller
        self.v_link = self.controller.app_data
        """:type : dict[str, any]"""

        # Create necessary application folders in 
        # C:\Users\User\AppData\Local
        user_dirs = appdirs.AppDirs("Weather_App", "")
        local_app_dir = user_dirs.user_data_dir
        self.data_dirs = {"Database": "",
                          "Debug": ""}
        for sub_dir in self.data_dirs:
            path = os.path.join(local_app_dir, sub_dir)
            self.data_dirs[sub_dir] = path
            os.makedirs(path, exist_ok=True)

        # Establish database connection.
        self.conn = sqlite3.connect(os.path.join(self.data_dirs["Database"],
                                                 "locations.db"))
        self.cur = self.conn.cursor()
        self.cur.execute("CREATE TABLE IF NOT EXISTS locations("
                         "Id INTEGER NOT NULL PRIMARY KEY , "
                         "Location TEXT NOT NULL UNIQUE, "
                         "Num_of_calls INTEGER NOT NULL DEFAULT 0, "
                         "Units TEXT)")
        # Read the last used units from the database and set in
        # controller.
        try:
            self.cur.execute("SELECT Units FROM locations WHERE Id=1")
            last_units = self.cur.fetchall()[0][0]
            if last_units != "":
                self.v_link["var_units"].set(last_units)
        # In case of the initial run with an empty database, default
        # units are metric.
        except IndexError:
            self.v_link["var_units"].set("metric")
        self.conn.commit()
        # Initial list of locations from previous use of the app for
        # loc_combobox ordered by amount of previous calls.
        self.combo_drop_menu()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号