Skip to content
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

Allow passing a tuple of sources #421

Merged
merged 4 commits into from Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions documentation/changelog.rst
Expand Up @@ -17,6 +17,7 @@ Infrastructure / Support
----------------------
* Unit conversion prefers shorter units in general [see `PR #415 <http://www.github.com/FlexMeasures/flexmeasures/pull/415>`_]
* Shorter CI builds in Github Actions by caching Python environment [see `PR #361 <http://www.github.com/FlexMeasures/flexmeasures/pull/361>`_]
* Allow to filter data by source using a tuple instead of a list [see `PR #421 <http://www.github.com/FlexMeasures/flexmeasures/pull/421>`_]


v0.9.3 | April 15, 2022
Expand Down
26 changes: 18 additions & 8 deletions flexmeasures/data/models/parsing_utils.py
@@ -1,4 +1,6 @@
from typing import Optional, Union, List
from __future__ import annotations

from collections.abc import Sequence

from flask import current_app
from flexmeasures.data import db
Expand All @@ -7,18 +9,26 @@


def parse_source_arg(
source: Optional[
Union[DataSource, List[DataSource], int, List[int], str, List[str]]
]
) -> Optional[List[DataSource]]:
"""Parse the "source" argument by looking up DataSources corresponding to any given ids or names."""
source: DataSource
| int
| str
| Sequence[DataSource]
| Sequence[int]
| Sequence[str]
| None,
) -> list[DataSource] | None:
"""Parse the "source" argument by looking up DataSources corresponding to any given ids or names.

Passes None as is (i.e. no source argument is given).
Accepts ids and names as list or tuples, always converting them to a list.
"""
if source is None:
return source
if not isinstance(source, list):
if isinstance(source, (DataSource, str, int)):
sources = [source]
else:
sources = source
parsed_sources: List[DataSource] = []
parsed_sources: list[DataSource] = []
Flix6x marked this conversation as resolved.
Show resolved Hide resolved
for source in sources:
if isinstance(source, int):
parsed_source = (
Expand Down