Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
luochang212 committed Aug 16, 2024
1 parent 6a74e3b commit cdeed11
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 26 deletions.
16 changes: 8 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.DS_Store
.idea/
.vscode/
.pytest_cache/
__pycache__/
/src/flameai/__pycache__/
/tests/__pycache__/
dist/
.nox/
.idea
.vscode
.pytest_cache
.nox
.ipynb_checkpoints
__pycache__
dist
flameai.db
11 changes: 11 additions & 0 deletions data/course.csv
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions data/student.csv
Original file line number Diff line number Diff line change
@@ -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
32 changes: 19 additions & 13 deletions notes/load_data.ipynb → notebook/load_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"metadata": {},
"outputs": [],
"source": [
"CSV_FILE = 'data/load_data.csv'"
"import flameai as fl"
]
},
{
Expand All @@ -22,7 +22,8 @@
"metadata": {},
"outputs": [],
"source": [
"import flameai as fl"
"FILE_PATH = '../data'\n",
"CSV_FILE = 'student.csv'"
]
},
{
Expand Down Expand Up @@ -51,28 +52,33 @@
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Score</th>\n",
" <th>Age</th>\n",
" <th>name</th>\n",
" <th>score</th>\n",
" <th>age</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Time</th>\n",
" <th>time</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2023-12-16 17:23:00</th>\n",
" <td>John Smith</td>\n",
" <td>99.5</td>\n",
" <td>18</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-12-16 17:24:00</th>\n",
" <td>Emily Johnson</td>\n",
" <td>83.0</td>\n",
" <td>23</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-12-16 17:25:00</th>\n",
" <td>Michael Brown</td>\n",
" <td>55.0</td>\n",
" <td>23</td>\n",
" </tr>\n",
Expand All @@ -81,11 +87,11 @@
"</div>"
],
"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,
Expand All @@ -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"
]
Expand Down
4 changes: 0 additions & 4 deletions notes/data/load_data.csv

This file was deleted.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
40 changes: 40 additions & 0 deletions src/flameai/sql.py
Original file line number Diff line number Diff line change
@@ -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}")
60 changes: 60 additions & 0 deletions tests/test_sql.py
Original file line number Diff line number Diff line change
@@ -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']

0 comments on commit cdeed11

Please sign in to comment.