forked from gadomski/pyisd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructure library into record and batch
- Loading branch information
Showing
18 changed files
with
340 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,8 @@ Most useful functions and classes are contained in submodules. | |
|
||
errors | ||
io | ||
pandas | ||
record | ||
batch | ||
|
||
isd | ||
--- | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from isd.errors import IsdError | ||
from isd.batch import Batch | ||
from isd.record import Record | ||
|
||
__all__ = ["IsdError", "Record"] | ||
__all__ = ["IsdError", "Batch", "Record"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import gzip | ||
import json | ||
from io import BytesIO | ||
from pathlib import Path | ||
from dataclasses import dataclass | ||
from typing import List, Union, Optional, Dict, Any, Iterator | ||
import datetime as dt | ||
|
||
from isd.record import Record | ||
|
||
import pandas as pd | ||
|
||
|
||
@dataclass | ||
class Batch: | ||
records: List[Record] | ||
|
||
def __len__(self) -> int: | ||
return len(self.records) | ||
|
||
def __getitem__(self, index: int) -> Record: | ||
return self.records[index] | ||
|
||
def __iter__(self) -> Iterator[Record]: | ||
return iter(self.records) | ||
|
||
@classmethod | ||
def parse(cls, lines: Union[str, BytesIO]) -> "Batch": | ||
"""Reads records from a text io stream.""" | ||
if isinstance(lines, BytesIO): | ||
lines = lines.read().decode("utf-8") | ||
return cls([Record.parse(line) for line in lines.splitlines()]) | ||
|
||
@classmethod | ||
def from_path(cls, path: Union[str, Path]) -> "Batch": | ||
"""Opens a local ISD file and returns an iterator over its records. | ||
If the path has a .gz extension, this function will assume it has gzip | ||
compression and will attempt to open it using `gzip.open`. | ||
""" | ||
path = Path(path) | ||
if path.suffix == ".gz": | ||
with gzip.open(path) as gzip_file: | ||
return cls( | ||
[Record.parse(gzip_line.decode("utf-8")) for gzip_line in gzip_file] | ||
) | ||
else: | ||
with open(path) as uncompressed_file: | ||
return cls( | ||
[ | ||
Record.parse(uncompressed_line) | ||
for uncompressed_line in uncompressed_file | ||
] | ||
) | ||
|
||
def filter_by_datetime( | ||
self, | ||
start_date: Optional[dt.datetime] = None, | ||
end_date: Optional[dt.datetime] = None, | ||
) -> "Batch": | ||
"""Returns an iterator over records filtered by start and end datetimes (both optional).""" | ||
return Batch( | ||
[ | ||
record | ||
for record in self.records | ||
if (not start_date or record.datetime() >= start_date) | ||
and (not end_date or record.datetime() < end_date) | ||
] | ||
) | ||
|
||
def to_dict(self) -> List[Dict[str, Any]]: | ||
"""Returns a list of dictionaries, one for each record.""" | ||
return [record.to_dict() for record in self.records] | ||
|
||
def to_json(self, indent: int = 4) -> str: | ||
"""Returns a JSON line of all records.""" | ||
data = [] | ||
for d in self.to_dict(): | ||
d["datetime"] = d["datetime"].isoformat() | ||
data.append(d) | ||
return json.dumps(data, indent=indent) | ||
|
||
def to_df(self) -> pd.DataFrame: | ||
"""Reads a local ISD file into a DataFrame.""" | ||
import pandas as pd | ||
|
||
return pd.DataFrame([record.to_dict() for record in self.records]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.