Skip to content

Commit a56ebfe

Browse files
akash5100nabobalis
andauthored
Add create_event function to create event string (#71)
* add create event string function * fix docstr typo * Bring the function to top * Function accept recognition method string * Update hvpy/tests/test_utils.py Co-authored-by: Nabil Freij <[email protected]> * correction in comments * Fix tuple unpacking bug * random tweaks * doc tweaks Co-authored-by: Nabil Freij <[email protected]>
1 parent 5ca472d commit a56ebfe

File tree

4 files changed

+95
-8
lines changed

4 files changed

+95
-8
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ repos:
55
- id: docformatter
66
args: [--in-place, --pre-summary-newline, --make-summary-multi]
77
- repo: https://github.com/myint/autoflake
8-
rev: v1.4
8+
rev: v1.5.1
99
hooks:
1010
- id: autoflake
1111
args: ['--in-place', '--remove-all-unused-imports', '--remove-unused-variable']

hvpy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
from .datasource import *
33
from .event import *
44
from .facade import *
5-
from .utils import create_layers
5+
from .utils import create_layers, create_events
66
from .version import __version__

hvpy/tests/test_utils.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import pytest
22

3-
from hvpy import DataSource
4-
from hvpy.utils import _create_layer_string, _to_datasource, create_layers
3+
from hvpy import DataSource, EventType
4+
from hvpy.utils import (
5+
_create_events_string,
6+
_create_layer_string,
7+
_to_datasource,
8+
_to_event_type,
9+
create_events,
10+
create_layers,
11+
)
512

613

714
def test_create_layers():
815
assert create_layers([(9, 100), (11, 50)]) == "[9,1,100],[11,1,50]"
9-
1016
with pytest.raises(ValueError, match="999 is not a valid DataSource"):
1117
create_layers([(9, 100), (999, 100)])
12-
1318
with pytest.raises(ValueError, match="must be between 0 and 100"):
1419
create_layers([(9, 101), (14, 100)])
15-
1620
with pytest.raises(ValueError, match="must be between 0 and 100"):
1721
create_layers([(9, 100), (14, -1)])
1822

@@ -30,3 +34,34 @@ def test_create_layers_string():
3034
_create_layer_string(DataSource.AIA_131, 101)
3135
with pytest.raises(ValueError, match="must be between 0 and 100"):
3236
_create_layer_string(DataSource.AIA_131, -1)
37+
38+
39+
def test_to_event_type():
40+
assert _to_event_type(EventType.ACTIVE_REGION) == _to_event_type("AR") == EventType.ACTIVE_REGION
41+
42+
43+
def test_create_events():
44+
assert create_events(["ER"]) == "[ER,all,1]"
45+
assert create_events([EventType.ACTIVE_REGION]) == "[AR,all,1]"
46+
assert create_events([(EventType.ACTIVE_REGION, "SPoCA;NOAA_SWPC_Observer")]) == "[AR,SPoCA;NOAA_SWPC_Observer,1]"
47+
assert create_events([EventType.ACTIVE_REGION, EventType.CORONAL_DIMMING]) == "[AR,all,1],[CD,all,1]"
48+
assert (
49+
create_events([(EventType.ACTIVE_REGION, "ZXCV"), (EventType.CORONAL_DIMMING, "ZXCV")])
50+
== "[AR,ZXCV,1],[CD,ZXCV,1]"
51+
)
52+
assert create_events([EventType.ACTIVE_REGION, (EventType.CORONAL_DIMMING, "ASDF")]) == "[AR,all,1],[CD,ASDF,1]"
53+
assert create_events([(EventType.ACTIVE_REGION, "ASDF"), EventType.CORONAL_DIMMING]) == "[AR,ASDF,1],[CD,all,1]"
54+
assert (
55+
create_events([(EventType.ACTIVE_REGION, "SPoCA"), ("ER", "NOAA_SWPC_Observer")])
56+
== "[AR,SPoCA,1],[ER,NOAA_SWPC_Observer,1]"
57+
)
58+
assert create_events([("AR", "SPoCA"), ("ER", "NOAA_SWPC_Observer")]) == "[AR,SPoCA,1],[ER,NOAA_SWPC_Observer,1]"
59+
with pytest.raises(ValueError, match="XYZ is not a valid EventType"):
60+
create_events(["XYZ"])
61+
with pytest.raises(ValueError, match="is not a EventType or str or two-length tuple"):
62+
create_events([("AR", "SPoCA", 123)])
63+
64+
65+
def test_create_events_string():
66+
assert _create_events_string(EventType.ACTIVE_REGION) == "[AR,all,1]"
67+
assert _create_events_string(EventType.ACTIVE_REGION, "xyz") == "[AR,xyz,1]"

hvpy/utils.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
from typing import Any, Union, Callable
1+
from typing import Any, List, Union, Callable
22
from datetime import datetime
33

44
from hvpy.datasource import DataSource
5+
from hvpy.event import EventType
56

67
__all__ = [
78
"convert_date_to_isoformat",
89
"convert_date_to_unix",
910
"create_layers",
11+
"create_events",
1012
]
1113

1214

@@ -75,3 +77,53 @@ def create_layers(layer: list) -> str:
7577
'[3,1,50],[10,1,50]'
7678
"""
7779
return ",".join([_create_layer_string(_to_datasource(s), o) for s, o in layer])
80+
81+
82+
def _to_event_type(val: Union[str, EventType]) -> EventType:
83+
"""
84+
Validates the input and converts it to a EventType enum.
85+
"""
86+
# This is an undocumented attribute of Enums
87+
if isinstance(val, str) and val in EventType._value2member_map_:
88+
return EventType(val)
89+
elif isinstance(val, EventType):
90+
return val
91+
else:
92+
raise ValueError(f"{val} is not a valid EventType")
93+
94+
95+
def _create_events_string(event_type: EventType, recognition_method: str = "all") -> str:
96+
"""
97+
Generates a string of the form "[event_type,recognition_method,1]" for a
98+
event.
99+
"""
100+
return f"[{event_type.value},{recognition_method},1]"
101+
102+
103+
def create_events(events: List[Union[EventType, str, tuple]]) -> str:
104+
"""
105+
Creates a string of events separated by commas.
106+
107+
Parameters
108+
----------
109+
events
110+
Either a `list` of `tuple` of the form (``event_type``, ``recognition_methods``),
111+
or a `list` of `str` or `~hvpy.EventType`, e.g., ``["AR", EventType.CORONAL_CAVITY]``
112+
If ``recognition_methods`` is missing, it will use "ALL".
113+
114+
Examples
115+
--------
116+
>>> from hvpy import create_events
117+
>>> create_events(["AR", ("ER", "SPoCA;NOAA_SWPC_Observer")])
118+
'[AR,all,1],[ER,SPoCA;NOAA_SWPC_Observer,1]'
119+
"""
120+
constructed_events = ""
121+
for event in events:
122+
if isinstance(event, (str, EventType)):
123+
constructed_events += _create_events_string(_to_event_type(event)) + ","
124+
elif isinstance(event, tuple) and len(event) == 2:
125+
constructed_events += _create_events_string(_to_event_type(event[0]), event[1]) + ","
126+
else:
127+
raise ValueError(f"{event} is not a EventType or str or two-length tuple")
128+
# Strips the final comma
129+
return constructed_events[:-1]

0 commit comments

Comments
 (0)