Skip to content

Commit

Permalink
Merge pull request #38 from matt-telstra/35-performance
Browse files Browse the repository at this point in the history
  • Loading branch information
aguinane committed Feb 3, 2023
2 parents a196911 + 51e22cf commit c6b2621
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 31 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ ENV/
/*.csv
!examples/*.csv
*.db

# Mac metadata
.DS_Store

22 changes: 16 additions & 6 deletions nemreader/nem_objects.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from datetime import datetime
from typing import Dict, List, NamedTuple, Optional

from pydantic import BaseModel


class HeaderRecord(NamedTuple):
"""Header record (100)"""

Expand Down Expand Up @@ -112,21 +109,34 @@ class B2BDetails13(NamedTuple):
current_trans_code: str
current_ret_service_order: str


class NEMReadings(BaseModel):
class NEMReadings:
"""Represents a meter reading"""

readings: Dict[str, Dict[str, List[Reading]]]
transactions: Dict[str, Dict[str, list]]

def __init__(self, readings, transactions):
self.readings = readings
self.transactions = transactions

class NEMData(BaseModel):
class Config:
copy_on_model_validation = 'shallow' # faster

class NEMData:
"""Represents a meter reading"""

header: HeaderRecord
readings: Dict[str, Dict[str, List[Reading]]]
transactions: Dict[str, Dict[str, list]]

def __init__(self, header, readings, transactions):
self.header = header
self.readings = readings
self.transactions = transactions

@property
def nmis(self) -> List[str]:
return list(self.transactions.keys())

class Config:
copy_on_model_validation = 'shallow' # faster
38 changes: 19 additions & 19 deletions nemreader/nem_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,28 @@ def nem_data(self) -> NEMData:
# Zip file is open in binary mode
# So decode then convert back to list
nmi_file = csv_text.read().decode("utf-8").splitlines()
reads = self.parse_nem_file(nmi_file, file_name=csv_file)
for nmi in reads.transactions.keys():
self.nmis.add(nmi)
suffixes = list(reads.transactions[nmi].keys())
self.nmi_channels[nmi] = suffixes
return NEMData(
header=self.header,
readings=reads.readings,
transactions=reads.transactions,
)
reads = self.parse_nem_file(nmi_file, file_name=csv_file)
for nmi in reads.transactions.keys():
self.nmis.add(nmi)
suffixes = list(reads.transactions[nmi].keys())
self.nmi_channels[nmi] = suffixes
return NEMData(
header=self.header,
readings=reads.readings,
transactions=reads.transactions,
)

with open(self.file_path) as nmi_file:
reads = self.parse_nem_file(nmi_file)
for nmi in reads.transactions.keys():
self.nmis.add(nmi)
suffixes = list(reads.transactions[nmi].keys())
self.nmi_channels[nmi] = suffixes
return NEMData(
header=self.header,
readings=reads.readings,
transactions=reads.transactions,
)
for nmi in reads.transactions.keys():
self.nmis.add(nmi)
suffixes = list(reads.transactions[nmi].keys())
self.nmi_channels[nmi] = suffixes
return NEMData(
header=self.header,
readings=reads.readings,
transactions=reads.transactions,
)

def get_data_frame(
self, split_days: bool = False, set_interval: Optional[int] = None
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ classifiers = [
keywords = ["energy", "NEM12", "NEM13"]
requires-python = ">=3.8"
dynamic = ["version", "description"]
dependencies = ["pandas", "pydantic", "sqlite_utils", "typer"]
dependencies = ["pandas", "sqlite_utils", "typer"]

[project.optional-dependencies]
test = ["pytest >=2.7.3", "pytest-cov", "mypy"]
Expand Down
6 changes: 1 addition & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ pluggy==1.0.0
# via pytest
pycodestyle==2.10.0
# via flake8
pydantic==1.10.2
# via nemreader (pyproject.toml)
pyflakes==3.0.1
# via flake8
pytest==7.2.0
Expand Down Expand Up @@ -91,6 +89,4 @@ tomli==2.0.1
typer==0.7.0
# via nemreader (pyproject.toml)
typing-extensions==4.4.0
# via
# mypy
# pydantic
# via mypy

0 comments on commit c6b2621

Please sign in to comment.