• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

freqtrade / freqtrade / 9394559170

26 Apr 2024 06:36AM UTC coverage: 94.656% (-0.02%) from 94.674%
9394559170

push

github

xmatthias
Loader should be passed as kwarg for clarity

20280 of 21425 relevant lines covered (94.66%)

0.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.19
/freqtrade/rpc/api_server/ws/serializer.py
1
import logging
1✔
2
from abc import ABC, abstractmethod
1✔
3
from typing import Any, Dict, Union
1✔
4

5
import orjson
1✔
6
import rapidjson
1✔
7
from pandas import DataFrame
1✔
8

9
from freqtrade.misc import dataframe_to_json, json_to_dataframe
1✔
10
from freqtrade.rpc.api_server.ws.proxy import WebSocketProxy
1✔
11
from freqtrade.rpc.api_server.ws_schemas import WSMessageSchemaType
1✔
12

13

14
logger = logging.getLogger(__name__)
1✔
15

16

17
class WebSocketSerializer(ABC):
1✔
18
    def __init__(self, websocket: WebSocketProxy):
1✔
19
        self._websocket: WebSocketProxy = websocket
1✔
20

21
    @abstractmethod
1✔
22
    def _serialize(self, data):
1✔
23
        raise NotImplementedError()
×
24

25
    @abstractmethod
1✔
26
    def _deserialize(self, data):
1✔
27
        raise NotImplementedError()
×
28

29
    async def send(self, data: Union[WSMessageSchemaType, Dict[str, Any]]):
1✔
30
        await self._websocket.send(self._serialize(data))
1✔
31

32
    async def recv(self) -> bytes:
1✔
33
        data = await self._websocket.recv()
1✔
34
        return self._deserialize(data)
1✔
35

36

37
class HybridJSONWebSocketSerializer(WebSocketSerializer):
1✔
38
    def _serialize(self, data) -> str:
1✔
39
        return str(orjson.dumps(data, default=_json_default), "utf-8")
1✔
40

41
    def _deserialize(self, data: str):
1✔
42
        # RapidJSON expects strings
43
        return rapidjson.loads(data, object_hook=_json_object_hook)
1✔
44

45

46
# Support serializing pandas DataFrames
47
def _json_default(z):
1✔
48
    if isinstance(z, DataFrame):
1✔
49
        return {
1✔
50
            '__type__': 'dataframe',
51
            '__value__': dataframe_to_json(z)
52
        }
53
    raise TypeError
×
54

55

56
# Support deserializing JSON to pandas DataFrames
57
def _json_object_hook(z):
1✔
58
    if z.get('__type__') == 'dataframe':
1✔
59
        return json_to_dataframe(z.get('__value__'))
×
60
    return z
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc