-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriters.py
More file actions
33 lines (26 loc) · 997 Bytes
/
writers.py
File metadata and controls
33 lines (26 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import datetime
import json
import os
from typing import List
class DataTypeNotSupportedForIngestionException(Exception):
def __init__(self, data):
self.data = data
self.message = f"Data type {type(data)} is not supported for ingestion"
super().__init__(self.message)
class DataWriter:
def __init__(self, coin: str, api: str) -> None:
self.api = api
self.coin = coin
self.filename = f"{self.api}/{self.coin}/{datetime.datetime.now()}.json"
def _write_row(self, row: str) -> None:
os.makedirs(os.path.dirname(self.filename), exist_ok=True)
with open(self.filename, "a") as f:
f.write(row)
def write(self, data: [List, dict]):
if isinstance(data, dict):
self._write_row(json.dumps(data) + "\n")
elif isinstance(data, List):
for element in data:
self.write(element)
else:
raise DataTypeNotSupportedForIngestionException(data)