Skip to content

Commit

Permalink
Tables - Simplify inheritance structure
Browse files Browse the repository at this point in the history
Moving all common declaration into the `Base` class, which removes the
`SnowData` class.
  • Loading branch information
jomey committed Aug 22, 2024
1 parent 09a5328 commit 0ca7f4f
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion snowexsql/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sqlalchemy import MetaData, create_engine
from sqlalchemy.orm import sessionmaker

from snowexsql.tables import Base
from snowexsql.tables.base import Base


def initialize(engine):
Expand Down
5 changes: 0 additions & 5 deletions snowexsql/tables/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
from .base import Base, Measurement, SingleLocationData, SnowData
from .image_data import ImageData
from .layer_data import LayerData
from .point_data import PointData
from .site_data import SiteData

__all__ = [
'Base',
'ImageData',
'LayerData',
'Measurement',
'PointData',
'SingleLocationData',
'SiteData'
'SnowData',
]
23 changes: 14 additions & 9 deletions snowexsql/tables/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,31 @@

from geoalchemy2 import Geometry
from sqlalchemy import Column, Date, DateTime, Float, Integer, String, Time
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.sql import func

Base = declarative_base()


class SnowData(object):
class Base(DeclarativeBase):
"""
Base class for which all data will have these attributes
"""
site_name = Column(String(250))
date = Column(Date)
# SQL Alchemy
__table_args__ = {"schema": "public"}

# Primary Key
id = Column(Integer, primary_key=True)

# Standard table columns
time_created = Column(DateTime(timezone=True), server_default=func.now())
time_updated = Column(DateTime(timezone=True), onupdate=func.now())
id = Column(Integer, primary_key=True)
doi = Column(String(50))

date_accessed = Column(Date)
site_name = Column(String(250))
date = Column(Date)
doi = Column(String(50))


class SingleLocationData(SnowData):
class SingleLocationData:
"""
Base class for points and profiles
"""
Expand Down
5 changes: 2 additions & 3 deletions snowexsql/tables/image_data.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from geoalchemy2 import Raster
from sqlalchemy import Column, String

from .base import Base, Measurement, SnowData
from .base import Base, Measurement


class ImageData(SnowData, Measurement, Base):
class ImageData(Base, Measurement):
"""
Class representing the images table. This table holds all images/rasters
"""
__tablename__ = 'images'
__table_args__ = {"schema": "public"}
raster = Column(Raster)
description = Column(String(1000))
1 change: 0 additions & 1 deletion snowexsql/tables/layer_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class LayerData(SingleLocationData, Measurement, Base):
temperature etc...
"""
__tablename__ = 'layers'
__table_args__ = {"schema": "public"}

depth = Column(Float)
site_id = Column(String(50))
Expand Down
1 change: 0 additions & 1 deletion snowexsql/tables/point_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class PointData(SingleLocationData, Measurement, Base):
e.g. snow depths
"""
__tablename__ = 'points'
__table_args__ = {"schema": "public"}

version_number = Column(Integer)
equipment = Column(String(50))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from sqlalchemy import Table

from snowexsql.db import *
from snowexsql.db import get_db, get_table_attributes
from snowexsql.tables import ImageData, LayerData, PointData, SiteData
from .sql_test_base import DBSetup

Expand Down

0 comments on commit 0ca7f4f

Please sign in to comment.