From d12c676955cda24e72fc3631f414227d07848124 Mon Sep 17 00:00:00 2001 From: akash5100 Date: Wed, 24 Aug 2022 20:53:14 +0530 Subject: [PATCH 1/9] add create event string function --- hvpy/tests/test_utils.py | 29 ++++++++++++++++++++++++----- hvpy/utils.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/hvpy/tests/test_utils.py b/hvpy/tests/test_utils.py index 7f3d93a..4cd6ccc 100644 --- a/hvpy/tests/test_utils.py +++ b/hvpy/tests/test_utils.py @@ -1,18 +1,22 @@ import pytest -from hvpy import DataSource -from hvpy.utils import _create_layer_string, _to_datasource, create_layers +from hvpy import DataSource, EventType +from hvpy.utils import ( + _create_events_string, + _create_layer_string, + _to_datasource, + _to_event_type, + create_events, + create_layers, +) def test_create_layers(): assert create_layers([(9, 100), (11, 50)]) == "[9,1,100],[11,1,50]" - with pytest.raises(ValueError, match="999 is not a valid DataSource"): create_layers([(9, 100), (999, 100)]) - with pytest.raises(ValueError, match="must be between 0 and 100"): create_layers([(9, 101), (14, 100)]) - with pytest.raises(ValueError, match="must be between 0 and 100"): create_layers([(9, 100), (14, -1)]) @@ -30,3 +34,18 @@ def test_create_layers_string(): _create_layer_string(DataSource.AIA_131, 101) with pytest.raises(ValueError, match="must be between 0 and 100"): _create_layer_string(DataSource.AIA_131, -1) + + +def test_to_event_type(): + assert _to_event_type("AR") == EventType.ACTIVE_REGION + assert _to_event_type(EventType.ACTIVE_REGION) == EventType.ACTIVE_REGION + + +def test_create_events(): + assert create_events([(EventType.ACTIVE_REGION), (EventType.ERUPTION)]) == "[AR,all,1],[ER,all,1]" + with pytest.raises(ValueError, match="STRING is not a valid EventType"): + create_events([(EventType.ACTIVE_REGION), (EventType.ERUPTION), ("STRING")]) + + +def test_create_events_string(): + assert _create_events_string(EventType.ACTIVE_REGION) == "[AR,all,1]" diff --git a/hvpy/utils.py b/hvpy/utils.py index 4956f39..0e4841e 100644 --- a/hvpy/utils.py +++ b/hvpy/utils.py @@ -2,11 +2,13 @@ from datetime import datetime from hvpy.datasource import DataSource +from hvpy.event import EventType __all__ = [ "convert_date_to_isoformat", "convert_date_to_unix", "create_layers", + "create_events", ] @@ -75,3 +77,40 @@ def create_layers(layer: list) -> str: '[3,1,50],[10,1,50]' """ return ",".join([_create_layer_string(_to_datasource(s), o) for s, o in layer]) + + +def _to_event_type(val: Union[str, EventType]) -> EventType: + """ + Validates the input and converts it to a EventType enum. + """ + if isinstance(val, str) and val in [x.value for x in EventType]: + return EventType(val) + elif isinstance(val, EventType): + return val + else: + raise ValueError(f"{val} is not a valid EventType") + + +def _create_events_string(event_type: EventType) -> str: + """ + Generates a string of the form "[event_type,all,1]" for a event. + """ + return f"[{event_type.value},all,1]" + + +def create_events(event: list) -> str: + """ + Creates a string of events separated by commas. + + Parameters + ---------- + event + A list of tuples of the form (``event_type``). + + Examples + -------- + >>> from hvpy import create_event_string, EventType + >>> create_event_string([(EventType.ACTIVE_REGION), (EventType.ERUPTION)]) + '[AR,all,1],[ER,all,1]' + """ + return ",".join([_create_events_string(_to_event_type(e)) for e in event]) From 8d89d4b723ca4e4e4f9a42a7a48c5fd475ec9c3d Mon Sep 17 00:00:00 2001 From: akash5100 Date: Wed, 24 Aug 2022 20:58:45 +0530 Subject: [PATCH 2/9] fix docstr typo --- hvpy/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hvpy/utils.py b/hvpy/utils.py index 0e4841e..9e0f012 100644 --- a/hvpy/utils.py +++ b/hvpy/utils.py @@ -109,8 +109,8 @@ def create_events(event: list) -> str: Examples -------- - >>> from hvpy import create_event_string, EventType - >>> create_event_string([(EventType.ACTIVE_REGION), (EventType.ERUPTION)]) + >>> from hvpy import create_events, EventType + >>> create_events([(EventType.ACTIVE_REGION), (EventType.ERUPTION)]) '[AR,all,1],[ER,all,1]' """ return ",".join([_create_events_string(_to_event_type(e)) for e in event]) From 7ea5f3673f526cb53553e5cbee95b3ad79ce3e6c Mon Sep 17 00:00:00 2001 From: akash5100 Date: Wed, 24 Aug 2022 21:09:10 +0530 Subject: [PATCH 3/9] Bring the function to top --- hvpy/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hvpy/__init__.py b/hvpy/__init__.py index dbb1156..6fe14b1 100644 --- a/hvpy/__init__.py +++ b/hvpy/__init__.py @@ -2,5 +2,5 @@ from .datasource import * from .event import * from .facade import * -from .utils import create_layers +from .utils import create_layers, create_events from .version import __version__ From 75318051e907578560e4fde0db5a228fedeb3085 Mon Sep 17 00:00:00 2001 From: akash5100 Date: Fri, 26 Aug 2022 20:07:17 +0530 Subject: [PATCH 4/9] Function accept recognition method string --- hvpy/tests/test_utils.py | 13 ++++++++++--- hvpy/utils.py | 32 +++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/hvpy/tests/test_utils.py b/hvpy/tests/test_utils.py index 4cd6ccc..8d2e6ff 100644 --- a/hvpy/tests/test_utils.py +++ b/hvpy/tests/test_utils.py @@ -42,10 +42,17 @@ def test_to_event_type(): def test_create_events(): - assert create_events([(EventType.ACTIVE_REGION), (EventType.ERUPTION)]) == "[AR,all,1],[ER,all,1]" - with pytest.raises(ValueError, match="STRING is not a valid EventType"): - create_events([(EventType.ACTIVE_REGION), (EventType.ERUPTION), ("STRING")]) + assert create_events([(EventType.ACTIVE_REGION, "SPoCA;NOAA_SWPC_Observer")]) == "[AR,SPoCA;NOAA_SWPC_Observer,1]" + assert create_events([(EventType.ACTIVE_REGION)]) == "[AR,all,1]" + assert create_events([("ER")]) == "[ER,all,1]" + with pytest.raises(ValueError, match="XYZ is not a valid EventType"): + create_events([("XYZ")]) + assert ( + create_events([(EventType.ACTIVE_REGION, "SPoCA"), ("ER", "NOAA_SWPC_Observer")]) + == "[AR,SPoCA,1],[ER,NOAA_SWPC_Observer,1]" + ) def test_create_events_string(): assert _create_events_string(EventType.ACTIVE_REGION) == "[AR,all,1]" + assert _create_events_string(EventType.ACTIVE_REGION, "xyz") == "[AR,xyz,1]" diff --git a/hvpy/utils.py b/hvpy/utils.py index 9e0f012..df119f4 100644 --- a/hvpy/utils.py +++ b/hvpy/utils.py @@ -1,4 +1,4 @@ -from typing import Any, Union, Callable +from typing import Any, Union, Callable, Optional from datetime import datetime from hvpy.datasource import DataSource @@ -91,11 +91,14 @@ def _to_event_type(val: Union[str, EventType]) -> EventType: raise ValueError(f"{val} is not a valid EventType") -def _create_events_string(event_type: EventType) -> str: +def _create_events_string(event_type: EventType, recognition_method: Optional[str] = "all") -> str: """ - Generates a string of the form "[event_type,all,1]" for a event. + Generates a string of the form "[event_type,recognition_method,1]" for a + event. """ - return f"[{event_type.value},all,1]" + if recognition_method is None: + recognition_method = "all" + return f"[{event_type.value},{recognition_method},1]" def create_events(event: list) -> str: @@ -105,12 +108,23 @@ def create_events(event: list) -> str: Parameters ---------- event - A list of tuples of the form (``event_type``). + A list of tuples of the form (``event_type``, ``recognition_methods``). Examples -------- >>> from hvpy import create_events, EventType - >>> create_events([(EventType.ACTIVE_REGION), (EventType.ERUPTION)]) - '[AR,all,1],[ER,all,1]' - """ - return ",".join([_create_events_string(_to_event_type(e)) for e in event]) + >>> create_events([("AR"), ("ER", "SPoCA;NOAA_SWPC_Observer")]) + '[AR,all,1],[ER,SPoCA;NOAA_SWPC_Observer,1]' + """ + buff = "" + try: + if not isinstance(event[0], str): # If unpacking results in a tuple. + for e, frm in event: + buff += _create_events_string(_to_event_type(e), frm) + "," + else: # if unpacking results in a string. (e.g. "AR") + for e in event: + buff += _create_events_string(_to_event_type(e)) + "," + except TypeError: + for e in event: # If unpacking directly results in a EventType object. + buff += _create_events_string(_to_event_type(e)) + "," + return buff[:-1] # remove the last comma From 68eedd96846b41293f9e099a8363e96a594e718a Mon Sep 17 00:00:00 2001 From: Akash Verma Date: Thu, 25 Aug 2022 12:12:33 +0530 Subject: [PATCH 5/9] Update hvpy/tests/test_utils.py Co-authored-by: Nabil Freij --- hvpy/tests/test_utils.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/hvpy/tests/test_utils.py b/hvpy/tests/test_utils.py index 8d2e6ff..a5a85e0 100644 --- a/hvpy/tests/test_utils.py +++ b/hvpy/tests/test_utils.py @@ -37,8 +37,7 @@ def test_create_layers_string(): def test_to_event_type(): - assert _to_event_type("AR") == EventType.ACTIVE_REGION - assert _to_event_type(EventType.ACTIVE_REGION) == EventType.ACTIVE_REGION + assert _to_event_type(EventType.ACTIVE_REGION) == _to_event_type("AR") == EventType.ACTIVE_REGION def test_create_events(): From a5a2f3c9e2bfbfd0708a10749ad4e374d7f93e59 Mon Sep 17 00:00:00 2001 From: akash5100 Date: Fri, 26 Aug 2022 20:12:30 +0530 Subject: [PATCH 6/9] correction in comments --- hvpy/utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/hvpy/utils.py b/hvpy/utils.py index df119f4..9d4e421 100644 --- a/hvpy/utils.py +++ b/hvpy/utils.py @@ -118,11 +118,11 @@ def create_events(event: list) -> str: """ buff = "" try: - if not isinstance(event[0], str): # If unpacking results in a tuple. - for e, frm in event: + if not isinstance(event[0], str): # if event is a list of tuples + for e, frm in event: # If unpacking results in a tuple. buff += _create_events_string(_to_event_type(e), frm) + "," - else: # if unpacking results in a string. (e.g. "AR") - for e in event: + else: + for e in event: # if unpacking results in a string. (e.g. "AR") buff += _create_events_string(_to_event_type(e)) + "," except TypeError: for e in event: # If unpacking directly results in a EventType object. From 31b8790c4a2b1d4773ea51e5e49b30d7f8e2d490 Mon Sep 17 00:00:00 2001 From: akash5100 Date: Fri, 26 Aug 2022 22:29:13 +0530 Subject: [PATCH 7/9] Fix tuple unpacking bug --- hvpy/tests/test_utils.py | 18 ++++++++++++++---- hvpy/utils.py | 18 ++++++++---------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/hvpy/tests/test_utils.py b/hvpy/tests/test_utils.py index a5a85e0..08e5645 100644 --- a/hvpy/tests/test_utils.py +++ b/hvpy/tests/test_utils.py @@ -41,15 +41,25 @@ def test_to_event_type(): def test_create_events(): - assert create_events([(EventType.ACTIVE_REGION, "SPoCA;NOAA_SWPC_Observer")]) == "[AR,SPoCA;NOAA_SWPC_Observer,1]" - assert create_events([(EventType.ACTIVE_REGION)]) == "[AR,all,1]" assert create_events([("ER")]) == "[ER,all,1]" - with pytest.raises(ValueError, match="XYZ is not a valid EventType"): - create_events([("XYZ")]) + assert create_events([(EventType.ACTIVE_REGION)]) == "[AR,all,1]" + assert create_events([(EventType.ACTIVE_REGION, "SPoCA;NOAA_SWPC_Observer")]) == "[AR,SPoCA;NOAA_SWPC_Observer,1]" + assert create_events([(EventType.ACTIVE_REGION), (EventType.CORONAL_DIMMING)]) == "[AR,all,1],[CD,all,1]" + assert ( + create_events([(EventType.ACTIVE_REGION, "ZXCV"), (EventType.CORONAL_DIMMING, "ZXCV")]) + == "[AR,ZXCV,1],[CD,ZXCV,1]" + ) + assert create_events([(EventType.ACTIVE_REGION), (EventType.CORONAL_DIMMING, "ASDF")]) == "[AR,all,1],[CD,ASDF,1]" + assert create_events([(EventType.ACTIVE_REGION, "ASDF"), (EventType.CORONAL_DIMMING)]) == "[AR,ASDF,1],[CD,all,1]" assert ( create_events([(EventType.ACTIVE_REGION, "SPoCA"), ("ER", "NOAA_SWPC_Observer")]) == "[AR,SPoCA,1],[ER,NOAA_SWPC_Observer,1]" ) + assert create_events([("AR", "SPoCA"), ("ER", "NOAA_SWPC_Observer")]) == "[AR,SPoCA,1],[ER,NOAA_SWPC_Observer,1]" + with pytest.raises(ValueError, match="XYZ is not a valid EventType"): + create_events([("XYZ")]) + with pytest.raises(ValueError, match="not a valid EventType or tuple"): + create_events([("AR", "SPoCA", 123)]) def test_create_events_string(): diff --git a/hvpy/utils.py b/hvpy/utils.py index 9d4e421..cf4fbde 100644 --- a/hvpy/utils.py +++ b/hvpy/utils.py @@ -117,14 +117,12 @@ def create_events(event: list) -> str: '[AR,all,1],[ER,SPoCA;NOAA_SWPC_Observer,1]' """ buff = "" - try: - if not isinstance(event[0], str): # if event is a list of tuples - for e, frm in event: # If unpacking results in a tuple. - buff += _create_events_string(_to_event_type(e), frm) + "," - else: - for e in event: # if unpacking results in a string. (e.g. "AR") - buff += _create_events_string(_to_event_type(e)) + "," - except TypeError: - for e in event: # If unpacking directly results in a EventType object. + array = [e for e in event] + for e in array: + if isinstance(e, EventType) or isinstance(e, str): buff += _create_events_string(_to_event_type(e)) + "," - return buff[:-1] # remove the last comma + elif isinstance(e, tuple) and len(e) == 2: + buff += _create_events_string(_to_event_type(e[0]), e[1]) + "," + else: + raise ValueError(f"{e} is not a valid EventType or tuple") + return buff[:-1] From 9b2bbf5b277a71f7d7ce15a27421370d331a10f1 Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Sun, 28 Aug 2022 17:23:31 +0100 Subject: [PATCH 8/9] random tweaks --- .pre-commit-config.yaml | 2 +- hvpy/tests/test_utils.py | 14 +++++++------- hvpy/utils.py | 39 ++++++++++++++++++++------------------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e640ff5..74ff6d3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,7 @@ repos: - id: docformatter args: [--in-place, --pre-summary-newline, --make-summary-multi] - repo: https://github.com/myint/autoflake - rev: v1.4 + rev: v1.5.1 hooks: - id: autoflake args: ['--in-place', '--remove-all-unused-imports', '--remove-unused-variable'] diff --git a/hvpy/tests/test_utils.py b/hvpy/tests/test_utils.py index 08e5645..78a335d 100644 --- a/hvpy/tests/test_utils.py +++ b/hvpy/tests/test_utils.py @@ -41,24 +41,24 @@ def test_to_event_type(): def test_create_events(): - assert create_events([("ER")]) == "[ER,all,1]" - assert create_events([(EventType.ACTIVE_REGION)]) == "[AR,all,1]" + assert create_events(["ER"]) == "[ER,all,1]" + assert create_events([EventType.ACTIVE_REGION]) == "[AR,all,1]" assert create_events([(EventType.ACTIVE_REGION, "SPoCA;NOAA_SWPC_Observer")]) == "[AR,SPoCA;NOAA_SWPC_Observer,1]" - assert create_events([(EventType.ACTIVE_REGION), (EventType.CORONAL_DIMMING)]) == "[AR,all,1],[CD,all,1]" + assert create_events([EventType.ACTIVE_REGION, EventType.CORONAL_DIMMING]) == "[AR,all,1],[CD,all,1]" assert ( create_events([(EventType.ACTIVE_REGION, "ZXCV"), (EventType.CORONAL_DIMMING, "ZXCV")]) == "[AR,ZXCV,1],[CD,ZXCV,1]" ) - assert create_events([(EventType.ACTIVE_REGION), (EventType.CORONAL_DIMMING, "ASDF")]) == "[AR,all,1],[CD,ASDF,1]" - assert create_events([(EventType.ACTIVE_REGION, "ASDF"), (EventType.CORONAL_DIMMING)]) == "[AR,ASDF,1],[CD,all,1]" + assert create_events([EventType.ACTIVE_REGION, (EventType.CORONAL_DIMMING, "ASDF")]) == "[AR,all,1],[CD,ASDF,1]" + assert create_events([(EventType.ACTIVE_REGION, "ASDF"), EventType.CORONAL_DIMMING]) == "[AR,ASDF,1],[CD,all,1]" assert ( create_events([(EventType.ACTIVE_REGION, "SPoCA"), ("ER", "NOAA_SWPC_Observer")]) == "[AR,SPoCA,1],[ER,NOAA_SWPC_Observer,1]" ) assert create_events([("AR", "SPoCA"), ("ER", "NOAA_SWPC_Observer")]) == "[AR,SPoCA,1],[ER,NOAA_SWPC_Observer,1]" with pytest.raises(ValueError, match="XYZ is not a valid EventType"): - create_events([("XYZ")]) - with pytest.raises(ValueError, match="not a valid EventType or tuple"): + create_events(["XYZ"]) + with pytest.raises(ValueError, match="is not a EventType or str or two-length tuple"): create_events([("AR", "SPoCA", 123)]) diff --git a/hvpy/utils.py b/hvpy/utils.py index cf4fbde..901d88d 100644 --- a/hvpy/utils.py +++ b/hvpy/utils.py @@ -1,4 +1,4 @@ -from typing import Any, Union, Callable, Optional +from typing import Any, List, Union, Callable from datetime import datetime from hvpy.datasource import DataSource @@ -83,7 +83,8 @@ def _to_event_type(val: Union[str, EventType]) -> EventType: """ Validates the input and converts it to a EventType enum. """ - if isinstance(val, str) and val in [x.value for x in EventType]: + # This is an undocumented attribute of Enums + if isinstance(val, str) and val in EventType._value2member_map_: return EventType(val) elif isinstance(val, EventType): return val @@ -91,38 +92,38 @@ def _to_event_type(val: Union[str, EventType]) -> EventType: raise ValueError(f"{val} is not a valid EventType") -def _create_events_string(event_type: EventType, recognition_method: Optional[str] = "all") -> str: +def _create_events_string(event_type: EventType, recognition_method: str = "all") -> str: """ Generates a string of the form "[event_type,recognition_method,1]" for a event. """ - if recognition_method is None: - recognition_method = "all" return f"[{event_type.value},{recognition_method},1]" -def create_events(event: list) -> str: +def create_events(events: List[Union[EventType, str, tuple]]) -> str: """ Creates a string of events separated by commas. Parameters ---------- - event - A list of tuples of the form (``event_type``, ``recognition_methods``). + events + Either a list of tuples of the form (``event_type``, ``recognition_methods``), + or a list of `str` or `.EventType`, e.g., ``["AR", EventType.CORONAL_CAVITY]`` + If ``recognition_methods`` is missing, it will use "ALL". Examples -------- - >>> from hvpy import create_events, EventType - >>> create_events([("AR"), ("ER", "SPoCA;NOAA_SWPC_Observer")]) + >>> from hvpy import create_events + >>> create_events(["AR", ("ER", "SPoCA;NOAA_SWPC_Observer")]) '[AR,all,1],[ER,SPoCA;NOAA_SWPC_Observer,1]' """ - buff = "" - array = [e for e in event] - for e in array: - if isinstance(e, EventType) or isinstance(e, str): - buff += _create_events_string(_to_event_type(e)) + "," - elif isinstance(e, tuple) and len(e) == 2: - buff += _create_events_string(_to_event_type(e[0]), e[1]) + "," + constructed_events = "" + for event in events: + if isinstance(event, (str, EventType)): + constructed_events += _create_events_string(_to_event_type(event)) + "," + elif isinstance(event, tuple) and len(event) == 2: + constructed_events += _create_events_string(_to_event_type(event[0]), event[1]) + "," else: - raise ValueError(f"{e} is not a valid EventType or tuple") - return buff[:-1] + raise ValueError(f"{event} is not a EventType or str or two-length tuple") + # Strips the final comma + return constructed_events[:-1] From 84e098fba16fb1374e6ff6dc571819f8912d069d Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Sun, 28 Aug 2022 17:27:24 +0100 Subject: [PATCH 9/9] doc tweaks --- hvpy/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hvpy/utils.py b/hvpy/utils.py index 901d88d..fcc2469 100644 --- a/hvpy/utils.py +++ b/hvpy/utils.py @@ -107,8 +107,8 @@ def create_events(events: List[Union[EventType, str, tuple]]) -> str: Parameters ---------- events - Either a list of tuples of the form (``event_type``, ``recognition_methods``), - or a list of `str` or `.EventType`, e.g., ``["AR", EventType.CORONAL_CAVITY]`` + Either a `list` of `tuple` of the form (``event_type``, ``recognition_methods``), + or a `list` of `str` or `~hvpy.EventType`, e.g., ``["AR", EventType.CORONAL_CAVITY]`` If ``recognition_methods`` is missing, it will use "ALL". Examples