From 094f51b3b4eea62e5d357680fdc052a9098d6348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gi=C3=B2=20Diani?= Date: Sat, 26 Oct 2024 17:21:52 +0200 Subject: [PATCH] rudimentary class for database interactions (download db from switchdrive: https://drive.switch.ch/index.php/apps/files/?dir=/Consultancy&fileid=7499599646#). --- .gitignore | 26 ++++++++++++++++++++++++++ src/data/__init__.py | 5 +++++ src/data/database.py | 15 +++++++++++++++ src/gio/test_duckdb.py | 5 +++++ 4 files changed, 51 insertions(+) create mode 100644 src/data/__init__.py create mode 100644 src/data/database.py create mode 100644 src/gio/test_duckdb.py diff --git a/.gitignore b/.gitignore index 0485192..e7714e2 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,32 @@ nbdist/ nbactions.xml nb-configuration.xml +# Sublime +*.sublime-* + # OS .DS_Store +# Python +.Python +.python-version +__pycache__/ +.pytest_cache +*.py[cod] +.env/ +.env* +.~env/ +.venv +env3.6/ +venv/ +env3.*/ +.dev +.denv +.pypyenv +.pytest_cache/ +.mypy_cache/ +.hypothesis/ + +# duckdb +*.duckdb + diff --git a/src/data/__init__.py b/src/data/__init__.py new file mode 100644 index 0000000..9368496 --- /dev/null +++ b/src/data/__init__.py @@ -0,0 +1,5 @@ +from data import database + + +def load(): + return database.Database() \ No newline at end of file diff --git a/src/data/database.py b/src/data/database.py new file mode 100644 index 0000000..5b9c82f --- /dev/null +++ b/src/data/database.py @@ -0,0 +1,15 @@ +import duckdb + + +class Database: + def __init__(self): + self.connection = duckdb.connect(database = '../data/consultancy.duckdb', read_only = True) + + def db_overview(self): + return self.connection.sql("DESCRIBE;").show() + + def seeds(self): + return self.connection.sql("SELECT regions.name, seeds.uri FROM consultancy_d.regions LEFT JOIN consultancy_d.seeds ON regions.id = seeds.region_id;").show() + + def properties_growth(self): + return self.connection.sql("SELECT strftime(created_at, '%Y-%m-%d') AS date, COUNT(*) as properties_count FROM consultancy_d.properties GROUP BY date;").show() \ No newline at end of file diff --git a/src/gio/test_duckdb.py b/src/gio/test_duckdb.py new file mode 100644 index 0000000..aac68b5 --- /dev/null +++ b/src/gio/test_duckdb.py @@ -0,0 +1,5 @@ +import data + +inst = data.load() +inst.seeds() +inst.properties_growth()