Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Sep 19, 2024
1 parent b198403 commit 9da7142
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
28 changes: 24 additions & 4 deletions core/models/entity/_base.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import json
from dataclasses import fields
from dataclasses import dataclass, fields
from enum import Enum
from typing import Any, Dict, List


class Entity:
def Entity(cls):
cls = dataclass(frozen=True)(cls)

def to_dict(self) -> Dict[str, Any]:
field_dict = {f.name: getattr(self, f.name) for f in fields(self)}

property_dict = {
k: getattr(self, k)
for k in dir(self)
Expand All @@ -26,7 +27,6 @@ def to_dict(self) -> Dict[str, Any]:
v.to_dict() if hasattr(v, "to_dict") and callable(v.to_dict) else v
for v in value
]

return result

def to_json(self) -> str:
Expand Down Expand Up @@ -69,6 +69,8 @@ def format_value(value):
return f"{value:.8f}"
return str(value)

print(field_dict)

return ", ".join(
f"{key}={format_value(value)}" if value is not None else f"{key}=NA"
for key, value in field_dict.items()
Expand All @@ -79,3 +81,21 @@ def __repr__(self) -> str:

def __format__(self, format_spec: str) -> str:
return self.to_json() if format_spec == "json" else self.__str__()

cls_methods = {
"to_dict": to_dict,
"to_json": to_json,
"from_dict": from_dict,
"from_json": from_json,
"from_list": from_list,
}

for method_name, method in cls_methods.items():
if not hasattr(cls, method_name):
setattr(cls, method_name, method)

cls.__str__ = __str__
cls.__repr__ = __repr__
cls.__format__ = __format__

return cls
6 changes: 2 additions & 4 deletions core/models/entity/bar.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from dataclasses import dataclass

from ._base import Entity
from .ohlcv import OHLCV


@dataclass(frozen=True)
class Bar(Entity):
@Entity
class Bar:
ohlcv: OHLCV
closed: bool
5 changes: 2 additions & 3 deletions core/models/entity/ohlcv.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from dataclasses import dataclass
from typing import Any, Dict, List

from core.models.candle_type import CandleType

from ._base import Entity


@dataclass(frozen=True)
class OHLCV(Entity):
@Entity
class OHLCV:
timestamp: int
open: float
high: float
Expand Down
6 changes: 3 additions & 3 deletions core/models/entity/order.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import uuid
from dataclasses import dataclass, field
from dataclasses import field
from datetime import datetime

from core.models.order_type import OrderStatus, OrderType

from ._base import Entity


@dataclass(frozen=True)
class Order(Entity):
@Entity
class Order:
status: OrderStatus
price: float
size: float
Expand Down

0 comments on commit 9da7142

Please sign in to comment.