| 47 | |
| 48 | @dataclass |
| 49 | class Serializer: |
| 50 | sql: "Union[str, Selectable]" |
| 51 | con: "Union[str, Connectable]" |
| 52 | chunksize: int = 10_000 |
| 53 | |
| 54 | def to_csv(self, file: StrOrBytesPath, progress=noop): |
| 55 | import pandas as pd |
| 56 | |
| 57 | idfs = pd.read_sql(self.sql, self.con, chunksize=self.chunksize) |
| 58 | with atomic_file(file) as f: |
| 59 | for i, df in enumerate(idfs): |
| 60 | df.to_csv(f, header=i == 0, index=False) |
| 61 | progress(len(df)) |
| 62 | |
| 63 | def to_json(self, file: StrOrBytesPath, progress=noop): # noqa: ARG002 |
| 64 | import pandas as pd |
| 65 | |
| 66 | df = pd.read_sql(self.sql, self.con) |
| 67 | with atomic_file(file) as f: |
| 68 | df.to_json(f, orient="records") |
| 69 | |
| 70 | def export(self, file: StrOrBytesPath, format: str = "csv", progress=noop): # noqa: A002 |
| 71 | if format == "json": |
| 72 | return self.to_json(file, progress=progress) |
| 73 | return self.to_csv(file, progress=progress) |
| 74 | |
| 75 | |
| 76 | @dataclass |
no outgoing calls
no test coverage detected
searching dependent graphs…