-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a74e3b
commit cdeed11
Showing
8 changed files
with
143 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |