Skip to content

Commit

Permalink
feat: ✨ Add a basic method to_dict to StrikeLineDescriptor
Browse files Browse the repository at this point in the history
The function does not encode the associated instance, for simplicity
  • Loading branch information
egrelier committed Dec 14, 2023
1 parent 92b80a8 commit baa841b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
12 changes: 12 additions & 0 deletions thermal_events/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from marshmallow import fields
from .thermal_event_instance import ThermalEventInstance
from .thermal_event import ThermalEvent
from .strike_line_descriptor import StrikeLineDescriptor


class ThermalEventInstanceSchema(SQLAlchemyAutoSchema):
Expand Down Expand Up @@ -31,3 +32,14 @@ class Meta:
include_fk = True
load_instance = True
transient = True


class StrikeLineDescriptorSchema(SQLAlchemyAutoSchema):
"""Schema for the StrikeLineDescriptor model."""

class Meta:
model = StrikeLineDescriptor
include_relationships = True
include_fk = True
load_instance = True
transient = True
11 changes: 10 additions & 1 deletion thermal_events/strike_line_descriptor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import deepcopy
from sqlalchemy import (
Boolean,
Column,
Expand All @@ -6,7 +7,7 @@
)
from sqlalchemy.dialects import sqlite
from sqlalchemy.dialects.mysql import DOUBLE
from sqlalchemy.orm import relationship
from sqlalchemy.orm import make_transient, relationship

from .base import Base
from .thermal_event_instance import (
Expand Down Expand Up @@ -107,6 +108,14 @@ def __init__(
else:
setattr(self, key, value)

def to_dict(self):
from .schemas import StrikeLineDescriptorSchema

out = deepcopy(self)
make_transient(out)
make_transient(out.instance)
return {"0": StrikeLineDescriptorSchema().dump(out)}

def return_segmented_points(self) -> list:
"""Return the segmented points as a list.
Expand Down
8 changes: 6 additions & 2 deletions thermal_events/thermal_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,17 @@ def compute(self) -> None:

self._computed = True

def to_dict(self):
def to_dict(self, use_id_as_key=False):
from .schemas import ThermalEventSchema

out = deepcopy(self)
make_transient(out)
[make_transient(x) for x in out.instances]
return {"0": ThermalEventSchema().dump(out)}
if use_id_as_key:
key = str(self.id)
else:
key = "0"
return {key: ThermalEventSchema().dump(out)}

def to_json(self, path_to_file):
"""
Expand Down
1 change: 1 addition & 0 deletions thermal_events/thermal_event_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ class ThermalEventInstance(Base):
"StrikeLineDescriptor",
back_populates="instance",
cascade="all, delete, delete-orphan",
uselist=False,
)

_buffer = None # image bugger used to draw polygons
Expand Down

0 comments on commit baa841b

Please sign in to comment.