diff --git a/.gitignore b/.gitignore
index 0072a13..53ada57 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,9 +1,9 @@
.DS_Store
-.idea/
-.vscode/
-.pytest_cache/
-__pycache__/
-/src/flameai/__pycache__/
-/tests/__pycache__/
-dist/
-.nox/
\ No newline at end of file
+.idea
+.vscode
+.pytest_cache
+.nox
+.ipynb_checkpoints
+__pycache__
+dist
+flameai.db
\ No newline at end of file
diff --git a/data/course.csv b/data/course.csv
new file mode 100644
index 0000000..5cb2d34
--- /dev/null
+++ b/data/course.csv
@@ -0,0 +1,11 @@
+name course classroom
+John Smith English 01
+John Smith Geography 02
+John Smith Art 03
+John Smith Design 04
+Emily Johnson English 01
+Emily Johnson Music 05
+Emily Johnson History 06
+Michael Brown English 01
+Michael Brown Computing 07
+Michael Brown Technology 08
\ No newline at end of file
diff --git a/data/student.csv b/data/student.csv
new file mode 100644
index 0000000..23a8b9a
--- /dev/null
+++ b/data/student.csv
@@ -0,0 +1,4 @@
+time name score age
+2023-12-16 17:23:00 John Smith 99.50 18
+2023-12-16 17:24:00 Emily Johnson 83.00 23
+2023-12-16 17:25:00 Michael Brown 55.00 23
\ No newline at end of file
diff --git a/notes/load_data.ipynb b/notebook/load_data.ipynb
similarity index 76%
rename from notes/load_data.ipynb
rename to notebook/load_data.ipynb
index d9e2dcb..fd9eb91 100644
--- a/notes/load_data.ipynb
+++ b/notebook/load_data.ipynb
@@ -13,7 +13,7 @@
"metadata": {},
"outputs": [],
"source": [
- "CSV_FILE = 'data/load_data.csv'"
+ "import flameai as fl"
]
},
{
@@ -22,7 +22,8 @@
"metadata": {},
"outputs": [],
"source": [
- "import flameai as fl"
+ "FILE_PATH = '../data'\n",
+ "CSV_FILE = 'student.csv'"
]
},
{
@@ -51,11 +52,13 @@
" \n",
" \n",
" | \n",
- " Score | \n",
- " Age | \n",
+ " name | \n",
+ " score | \n",
+ " age | \n",
"
\n",
" \n",
- " Time | \n",
+ " time | \n",
+ " | \n",
" | \n",
" | \n",
"
\n",
@@ -63,16 +66,19 @@
"
\n",
" \n",
" 2023-12-16 17:23:00 | \n",
+ " John Smith | \n",
" 99.5 | \n",
" 18 | \n",
"
\n",
" \n",
" 2023-12-16 17:24:00 | \n",
+ " Emily Johnson | \n",
" 83.0 | \n",
" 23 | \n",
"
\n",
" \n",
" 2023-12-16 17:25:00 | \n",
+ " Michael Brown | \n",
" 55.0 | \n",
" 23 | \n",
"
\n",
@@ -81,11 +87,11 @@
""
],
"text/plain": [
- " Score Age\n",
- "Time \n",
- "2023-12-16 17:23:00 99.5 18\n",
- "2023-12-16 17:24:00 83.0 23\n",
- "2023-12-16 17:25:00 55.0 23"
+ " name score age\n",
+ "time \n",
+ "2023-12-16 17:23:00 John Smith 99.5 18\n",
+ "2023-12-16 17:24:00 Emily Johnson 83.0 23\n",
+ "2023-12-16 17:25:00 Michael Brown 55.0 23"
]
},
"execution_count": 3,
@@ -94,11 +100,11 @@
}
],
"source": [
- "file_path = fl.gen_abspath('.', CSV_FILE)\n",
+ "file_path = fl.gen_abspath(FILE_PATH, CSV_FILE)\n",
"df = fl.read_csv(file_path,\n",
" sep='\\t',\n",
- " index_col='Time',\n",
- " parse_dates=['Time'],\n",
+ " index_col='time',\n",
+ " parse_dates=['time'],\n",
" date_format='%Y-%m-%d %H:%M:%S')\n",
"df"
]
diff --git a/notes/data/load_data.csv b/notes/data/load_data.csv
deleted file mode 100644
index e250a83..0000000
--- a/notes/data/load_data.csv
+++ /dev/null
@@ -1,4 +0,0 @@
-Time Score Age
-2023-12-16 17:23:00 99.50 18
-2023-12-16 17:24:00 83.00 23
-2023-12-16 17:25:00 55.00 23
\ No newline at end of file
diff --git a/pyproject.toml b/pyproject.toml
index 85785bc..1e3dd8a 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "flameai"
-version = "1.0.8"
+version = "1.0.9"
description = "Python Deep Learning Toolkit."
readme = "README.md"
keywords = [
diff --git a/src/flameai/sql.py b/src/flameai/sql.py
new file mode 100644
index 0000000..c486a40
--- /dev/null
+++ b/src/flameai/sql.py
@@ -0,0 +1,40 @@
+import os
+import sqlite3
+
+import pandas as pd
+
+from .util import gen_abspath, read_csv
+
+
+class SQLConnect:
+
+ def __init__(self,
+ db_name: str = 'flameai.db',
+ db_backup_path: str = './'):
+ base_path = '/tmp' if os.path.exists('/tmp') else db_backup_path
+ self.db_path = gen_abspath(base_path, db_name)
+ self.conn = sqlite3.connect(self.db_path)
+
+ def create_table(self,
+ table_name: str,
+ df: pd.DataFrame) -> None:
+ df.to_sql(table_name, self.conn, if_exists='replace', index=False)
+
+ def create_table_with_csv(self,
+ table_name: str,
+ csv_path: str,
+ sep: str = ',') -> None:
+ df = read_csv(file_path=csv_path,
+ sep=sep,
+ header=0)
+ self.create_table(table_name, df)
+
+ def sql(self, query) -> pd.DataFrame:
+ return pd.read_sql_query(query, self.conn)
+
+ def delete_database(self):
+ try:
+ os.remove(self.db_path)
+ except Exception as e:
+ print(f'Can not remove {self.db_path}')
+ print(f"Error: {e}")
diff --git a/tests/test_sql.py b/tests/test_sql.py
new file mode 100644
index 0000000..7a5fa1c
--- /dev/null
+++ b/tests/test_sql.py
@@ -0,0 +1,60 @@
+from flameai.sql import SQLConnect
+from flameai.util import gen_abspath
+import pandas as pd
+
+
+def test_create_table():
+ sc = SQLConnect()
+ df = pd.DataFrame({'a': ['a', 'b', 'c', 'a', 'b', 'c'],
+ 'b': [1, 2, 3, 2, 1, 0]})
+ sc.create_table(table_name='table_a', df=df)
+ result = sc.sql('select a, b from table_a')
+ sc.delete_database()
+
+ assert result['a'].to_list() == ['a', 'b', 'c', 'a', 'b', 'c']
+ assert result['b'].to_list() == [1, 2, 3, 2, 1, 0]
+
+
+def test_create_table_with_csv():
+ sc = SQLConnect()
+ csv_path = gen_abspath('../data', 'student.csv')
+ sc.create_table_with_csv(table_name='table_student',
+ csv_path=csv_path,
+ sep='\t')
+ result = sc.sql('select time, name, score, age from table_student')
+ sc.delete_database()
+
+ assert result['time'].to_list() == ['2023-12-16 17:23:00',
+ '2023-12-16 17:24:00',
+ '2023-12-16 17:25:00']
+ assert result['name'].to_list() == ['John Smith', 'Emily Johnson', 'Michael Brown']
+ assert result['score'].to_list() == [99.50, 83.00, 55.00]
+ assert result['age'].to_list() == [18, 23, 23]
+
+
+def test_table_join():
+ sc = SQLConnect()
+ student_path = gen_abspath('../data', 'student.csv')
+ course_path = gen_abspath('../data', 'course.csv')
+ sc.create_table_with_csv(table_name='table_student',
+ csv_path=student_path,
+ sep='\t')
+ sc.create_table_with_csv(table_name='table_course',
+ csv_path=course_path,
+ sep='\t')
+ query = """
+ SELECT
+ a.name,
+ a.age,
+ b.course
+ FROM table_student a
+ LEFT JOIN table_course b
+ ON a.name = b.name
+ """
+ result = sc.sql(query)
+ sc.delete_database()
+
+ assert result['name'].to_list() == ['John Smith'] * 4 + ['Emily Johnson'] * 3 + ['Michael Brown'] * 3
+ assert result['age'].to_list() == [18, 18, 18, 18, 23, 23, 23, 23, 23, 23]
+ assert result['course'].to_list() == ['Art', 'Design', 'English', 'Geography', 'English', 'History',
+ 'Music', 'Computing', 'English', 'Technology']