Included repository for persisting building

This commit is contained in:
Peter Yefi 2022-11-03 15:29:09 -04:00
parent e6ecac528f
commit 4f4f9fd6ec
11 changed files with 109 additions and 10 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@
/data/energy_systems/heat_pumps/*.csv /data/energy_systems/heat_pumps/*.csv
/data/energy_systems/heat_pumps/*.insel /data/energy_systems/heat_pumps/*.insel
.DS_Store .DS_Store
.env

View File

@ -6,7 +6,7 @@ Project Coder Peter Yefi peteryefi@gmail.com
""" """
from sqlalchemy import create_engine from sqlalchemy import create_engine
from db_config import BaseConfiguration from persistence.db_config import BaseConfiguration
from persistence.models import Building from persistence.models import Building
from persistence.models import City from persistence.models import City

17
persistence/Base.py Normal file
View File

@ -0,0 +1,17 @@
"""
Base repository class to establish db connection
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Peter Yefi peteryefi@gmail.com
"""
from persistence.db_config import BaseConfiguration
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
class BaseRepo:
def __init__(self):
config = BaseConfiguration()
engine = create_engine(config.conn_string())
self.session = Session(engine)

View File

@ -0,0 +1,3 @@
from .Base import BaseRepo
from .repositories.CityRepo import CityRepo
from .repositories.BuildingRepo import BuildingRepo

View File

@ -7,13 +7,23 @@ Project Coder Peter Yefi peteryefi@gmail.com
from sqlalchemy import Column, Integer, String, Sequence, Float, ForeignKey from sqlalchemy import Column, Integer, String, Sequence, Float, ForeignKey
from sqlalchemy.dialects.postgresql import JSONB from sqlalchemy.dialects.postgresql import JSONB
from db_config import Base from persistence.db_config import Base
class Building(Base): class Building(Base):
"""A model representation of a building
Attributes:
city_id A reference to the city which has this building.
name The name of the building.
construction_year The year of construction of the building.
function The function (e.g. residential) of the building.
floor_area The computed area of the floor of the building.
data A JSON object which contain other data (like roof, walls, zones, etc) of the building
"""
__tablename__ = "building" __tablename__ = "building"
id = Column(Integer, Sequence('building_id_seq'), primary_key=True) id = Column(Integer, Sequence('building_id_seq'), primary_key=True)
city_id = Column(Integer, ForeignKey('models.id'), nullable=False) city_id = Column(Integer, ForeignKey('city.id'), nullable=False)
name = Column(String, nullable=False) name = Column(String, nullable=False)
construction_year = Column(Integer, nullable=False) construction_year = Column(Integer, nullable=False)
function = Column(String, nullable=False) function = Column(String, nullable=False)

View File

@ -6,16 +6,28 @@ Project Coder Peter Yefi peteryefi@gmail.com
""" """
from sqlalchemy import Column, Integer, String, Sequence, Float from sqlalchemy import Column, Integer, String, Sequence, Float
from db_config import Base from persistence.db_config import Base
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
class City(Base): class City(Base):
__tablename__ = "models" """A model representation of a city
Attributes:
name The name of the city.
uid A unique identifier for each city object
location The location of the building.
country_code The country where the city is located.
latitude The GPS latitude location of the city.
longitude The GPS longitude location of the city.
building A relationship for fetching all buildings in the city
"""
__tablename__ = "city"
id = Column(Integer, Sequence('city_id_seq'), primary_key=True) id = Column(Integer, Sequence('city_id_seq'), primary_key=True)
uid = Column(String, nullable=False, unique=True)
name = Column(String, nullable=False) name = Column(String, nullable=False)
location = Column(String, nullable=False) time_zone = Column(String, nullable=True)
country_code = Column(String, nullable=False) country_code = Column(String, nullable=False)
latitude = Column(Float, nullable=False) latitude = Column(Float)
longitude = Column(Float, nullable=False) longitude = Column(Float)
building = relationship('Building', backref='models', lazy=True, cascade="all, delete-orphan") building = relationship('Building', backref='city', lazy=True, cascade="all, delete-orphan")

View File

@ -0,0 +1,25 @@
"""
Building repository with database CRUD operations
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Peter Yefi peteryefi@gmail.com
"""
import json
from city_model_structure.building import Building
from persistence.models import City
from persistence import BaseRepo
class BuildingRepo(BaseRepo):
def __init__(self):
super().__init__()
def insert_building(self, building: [Building], city: City):
pass
@staticmethod
def _extract_building_data(building: Building):
json_building = json.dumps(building.__dict__)
print(json_building)

View File

@ -0,0 +1,28 @@
"""
City repository with database CRUD operations
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Peter Yefi peteryefi@gmail.com
"""
from city_model_structure.city import City
from persistence.models import City as ModelCity
from persistence import BaseRepo
class CityRepo(BaseRepo):
def __init__(self):
super().__init__()
def insert(self, city: City):
model_city = ModelCity()
model_city.uid = city.uid
model_city.name = city.name
model_city.longitude = city.longitude
model_city.latitude = city.latitude
model_city.country_code = city.country_code
model_city.time_zone = city.time_zone
self.session.add(model_city)
self.session.flush()
self.session.commit()
return model_city

View File

@ -0,0 +1 @@

View File

@ -16,3 +16,5 @@ rhino3dm==7.7.0
scipy scipy
PyYAML PyYAML
pyecore==0.12.2 pyecore==0.12.2
python-dotenv
SQLAlchemy