Uncategorized

Python
Import module or package in Python

Suppose that a main script of [cci]main.py[/cci] (or a corresponding [cci]Jupyter Notebook[/cci]) exists in the directory of [cci]myproject[/cci], and its subdirectory [cci]sub[/cci] contains modules of [cci]mod_a.py[/cci] (a function of [cci]func_a ()[/cci] included) and [cci]mod_b.py[/cci] (a function of [cci]func_b()[/cci] included) where [cci]main.py[/cci] imports the module [cci]mod_a[/cci] and the module [cci]mod_a[/cci] imports the module [cci]mod_b[/cci]. First, creating an empty file [cci]__init__.py[/cci] in the directory [cci]sub[/cci] so that [cci]sub[/cci] is recognized as a package as shown below: [cc] myproject/ main.py sub/ __init__.py mod_a.py (func_a()) mod_b.py (func_b()) [/cc] The sub-directory, [cci]sub[/cci], containing two modules, becomes a package when [cci]__init__.py[/cci] is included. Import [cci]mod_a.py[/cci] in [cci]main.py[/cci] The following is how to import the module [cci]mod_a.py[/cci] […]

Read more
Uncategorized
Handling SQLite database using Python

Suppose that the file name of SQLite's database is mpos_S.sqlite, its table (like a sheet of Excel) name is mpos_S, and mpos_S contains column data. If the column name of water temperature data is tp, this column tp contains water temperature data. In order to handle it from Python, you need to import sqlite3. First connect to the database and get the cursor cur. [cc] >>> import sqlite3 >>> conn = sqlite3.connect('mpos_S.sqlite') >>> cur = conn.cursor() [/cc] Check information of columns. [cc] >>> cur.execute("PRAGMA TABLE_INFO(mpos_S)") ### Gets information about columns of table mpos_S >>> cols = cur.fetchall() ### Information of a column is a tuple containing 6 elements. >>> print(cols) […]

Read more