-
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue/2 #30
Merged
Merged
Issue/2 #30
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ dependencies = [ | |
"netcdf4", | ||
"h5netcdf", | ||
"h5py", | ||
"pvlive-api", | ||
] | ||
|
||
[project.optional-dependencies] | ||
|
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,46 @@ | ||
from datetime import datetime | ||
from pvlive_api import PVLive | ||
import logging | ||
import pytz | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class PVLiveData: | ||
def __init__(self): | ||
self.pvl = PVLive() | ||
|
||
def get_latest_data(self, period, entity_type="gsp", entity_id=0, extra_fields=""): | ||
""" | ||
Get the latest data from PVlive | ||
""" | ||
try: | ||
df = self.pvl.latest(entity_type=entity_type, entity_id=entity_id, extra_fields=extra_fields, period=period, dataframe=True) | ||
return df | ||
except Exception as e: | ||
logger.error(e) | ||
return None | ||
|
||
def get_data_between(self, start, end, entity_type="gsp", entity_id=0, extra_fields=""): | ||
""" | ||
Get the data between two dates | ||
""" | ||
try: | ||
df = self.pvl.between(start=start, end=end, entity_type=entity_type, entity_id=entity_id, extra_fields=extra_fields, dataframe=True) | ||
return df | ||
except Exception as e: | ||
logger.error(e) | ||
return None | ||
|
||
def get_data_at_time(self, dt): | ||
""" | ||
Get data at a specific time | ||
""" | ||
try: | ||
df = self.pvl.at_time(dt, entity_type="pes", entity_id=0, extra_fields="", period=30, dataframe=True) | ||
return df | ||
except Exception as e: | ||
logger.error(e) | ||
return None | ||
|
||
|
Empty file.
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,60 @@ | ||
import pytest | ||
from unittest.mock import MagicMock | ||
from datetime import datetime | ||
import pytz | ||
from open_data_pvnet.scripts.fetch_pvlive_data import PVLiveData | ||
|
||
@pytest.fixture | ||
def pvlive_mock(): | ||
""" | ||
Fixture to create a PVLiveData instance with mocked PVLive methods. | ||
""" | ||
pvlive = PVLiveData() | ||
pvlive.pvl.latest = MagicMock() | ||
pvlive.pvl.between = MagicMock() | ||
pvlive.pvl.at_time = MagicMock() | ||
return pvlive | ||
|
||
def test_get_latest_data(pvlive_mock): | ||
""" | ||
Test the get_latest_data method. | ||
""" | ||
mock_data = {"column1": [1, 2], "column2": [3, 4]} | ||
pvlive_mock.pvl.latest.return_value = mock_data | ||
|
||
result = pvlive_mock.get_latest_data(period=30) | ||
pvlive_mock.pvl.latest.assert_called_once_with( | ||
entity_type="gsp", entity_id=0, extra_fields="", period=30, dataframe=True | ||
) | ||
assert result == mock_data | ||
|
||
def test_get_data_between(pvlive_mock): | ||
""" | ||
Test the get_data_between method. | ||
""" | ||
mock_data = {"column1": [5, 6], "column2": [7, 8]} | ||
pvlive_mock.pvl.between.return_value = mock_data | ||
|
||
start = datetime(2021, 1, 1, 12, 0, tzinfo=pytz.utc) | ||
end = datetime(2021, 1, 2, 12, 0, tzinfo=pytz.utc) | ||
|
||
result = pvlive_mock.get_data_between(start, end) | ||
pvlive_mock.pvl.between.assert_called_once_with( | ||
start=start, end=end, entity_type="gsp", entity_id=0, extra_fields="", dataframe=True | ||
) | ||
assert result == mock_data | ||
|
||
def test_get_data_at_time(pvlive_mock): | ||
""" | ||
Test the get_data_at_time method. | ||
""" | ||
mock_data = {"column1": [9, 10], "column2": [11, 12]} | ||
pvlive_mock.pvl.at_time.return_value = mock_data | ||
|
||
dt = datetime(2021, 1, 1, 12, 0, tzinfo=pytz.utc) | ||
|
||
result = pvlive_mock.get_data_at_time(dt) | ||
pvlive_mock.pvl.at_time.assert_called_once_with( | ||
dt, entity_type="pes", entity_id=0, extra_fields="", period=30, dataframe=True | ||
) | ||
assert result == mock_data |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry i missed this, but entity_type should be "gsp"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about that, I'll create a pull request to change the default entity type to gsp