Skip to content

Commit

Permalink
Using f-strings and log on import
Browse files Browse the repository at this point in the history
* added target names

* updated function to recognize l0 files

also includes bug fix

* added tests and fixed bug

* fstrings everywhere

* added log of version on import

* Update hermes_core/__init__.py

Co-authored-by: Damian Barrous Dume <damianbarrous@gmail.com>

* fix based on comment

* Update .gitignore

* formatting and file extension

* log message on import

* setting path to black explicitly

Co-authored-by: Damian Barrous Dume <damianbarrous@gmail.com>
  • Loading branch information
ehsteve and dbarrous committed Oct 13, 2022
1 parent 4e4a029 commit ed6eb01
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 76 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -130,4 +130,7 @@ dmypy.json
hermes_core/version.py

# Mac hidden files
.DS_Store
.DS_Store

# docs/api
docs/api
3 changes: 2 additions & 1 deletion .vscode/settings.json
@@ -1,3 +1,4 @@
{
"esbonio.server.enabled": true
"esbonio.server.enabled": true,
"python.formatting.blackPath": "/usr/local/bin/black"
}
2 changes: 1 addition & 1 deletion docs/dev-guide/code_standards.rst
Expand Up @@ -16,7 +16,7 @@ Language Standard
* All code must be compatible with Python 3.7 and later.

* The new Python 3 formatting style should be used (i.e.
``"{0:s}".format("spam")`` instead of ``"%s" % "spam"``).
``f"{spam:s}"`` instead of ``"%s" % "spam"``).

Coding Style/Conventions
========================
Expand Down
2 changes: 2 additions & 0 deletions hermes_core/__init__.py
Expand Up @@ -23,3 +23,5 @@
INST_TARGETNAMES = ["EEA", "MAG", "MERIT", "SPANI"]
INST_TO_SHORTNAME = dict(zip(INST_NAMES, INST_SHORTNAMES))
INST_TO_TARGETNAME = dict(zip(INST_NAMES, INST_TARGETNAMES))

log.info(f"hermes_core version: {__version__}")
95 changes: 54 additions & 41 deletions hermes_core/tests/test_util_util.py
Expand Up @@ -9,14 +9,12 @@


# fmt: off
@pytest.mark.parametrize(
"instrument, time, level, version, result",
[
("eea", time, "l1", "1.2.3", "hermes_eea_l1_{}_v1.2.3.cdf".format(time_formatted)),
("merit", time, "l2", "2.4.5", "hermes_mrt_l2_{}_v2.4.5.cdf".format(time_formatted)),
("nemisis", time, "l2", "1.3.5", "hermes_nms_l2_{}_v1.3.5.cdf".format(time_formatted)),
("spani", time, "l3", "2.4.5", "hermes_spn_l3_{}_v2.4.5.cdf".format(time_formatted)),
],
@pytest.mark.parametrize("instrument,time,level,version,result", [
("eea", time, "l1", "1.2.3", f"hermes_eea_l1_{time_formatted}_v1.2.3.cdf"),
("merit", time, "l2", "2.4.5", f"hermes_mrt_l2_{time_formatted}_v2.4.5.cdf"),
("nemisis", time, "l2", "1.3.5", f"hermes_nms_l2_{time_formatted}_v1.3.5.cdf"),
("spani", time, "l3", "2.4.5", f"hermes_spn_l3_{time_formatted}_v2.4.5.cdf"),
]
)
def test_science_filename_output_a(instrument, time, level, version, result):
"""Test simple cases with expected output"""
Expand All @@ -31,43 +29,58 @@ def test_science_filename_output_b():
"""Test more complex cases of expected output"""

# mode
assert util.create_science_filename(
"spani", time, level="l3", mode="2s", version="2.4.5"
) == "hermes_spn_2s_l3_{}_v2.4.5.cdf".format(time_formatted)
assert (
util.create_science_filename(
"spani", time, level="l3", mode="2s", version="2.4.5"
)
== f"hermes_spn_2s_l3_{time_formatted}_v2.4.5.cdf"
)
# test
assert util.create_science_filename(
"spani", time, level="l1", version="2.4.5", test=True
) == "hermes_spn_l1test_{}_v2.4.5.cdf".format(time_formatted)
assert (
util.create_science_filename(
"spani", time, level="l1", version="2.4.5", test=True
)
== f"hermes_spn_l1test_{time_formatted}_v2.4.5.cdf"
)
# all options
assert util.create_science_filename(
"spani",
time,
level="l3",
mode="2s",
descriptor="burst",
version="2.4.5",
test=True,
) == "hermes_spn_2s_l3test_burst_{}_v2.4.5.cdf".format(time_formatted)
assert (
util.create_science_filename(
"spani",
time,
level="l3",
mode="2s",
descriptor="burst",
version="2.4.5",
test=True,
)
== f"hermes_spn_2s_l3test_burst_{time_formatted}_v2.4.5.cdf"
)
# Time object instead of str
assert util.create_science_filename(
"spani",
Time(time),
level="l3",
mode="2s",
descriptor="burst",
version="2.4.5",
test=True,
) == "hermes_spn_2s_l3test_burst_{}_v2.4.5.cdf".format(time_formatted)
assert (
util.create_science_filename(
"spani",
Time(time),
level="l3",
mode="2s",
descriptor="burst",
version="2.4.5",
test=True,
)
== f"hermes_spn_2s_l3test_burst_{time_formatted}_v2.4.5.cdf"
)
# Time object but created differently
assert util.create_science_filename(
"spani",
Time(2460407.004409722, format="jd"),
level="l3",
mode="2s",
descriptor="burst",
version="2.4.5",
test=True,
) == "hermes_spn_2s_l3test_burst_{}_v2.4.5.cdf".format(time_formatted)
assert (
util.create_science_filename(
"spani",
Time(2460407.004409722, format="jd"),
level="l3",
mode="2s",
descriptor="burst",
version="2.4.5",
test=True,
)
== f"hermes_spn_2s_l3test_burst_{time_formatted}_v2.4.5.cdf"
)


def test_parse_science_filename_output():
Expand Down
2 changes: 1 addition & 1 deletion hermes_core/util/config.py
Expand Up @@ -134,7 +134,7 @@ def print_config():
for section in hermes_core.config.sections():
print(f" [{section}]")
for option in hermes_core.config.options(section):
print(" {} = {}".format(option, hermes_core.config.get(section, option)))
print(f" {option} = {hermes_core.config.get(section, option)}")
print("")


Expand Down
43 changes: 12 additions & 31 deletions hermes_core/util/util.py
Expand Up @@ -13,6 +13,7 @@
TIME_FORMAT_L0 = "%Y%j-%H%M%S"
TIME_FORMAT = "%Y%m%d_%H%M%S"
VALID_DATA_LEVELS = ["l0", "l1", "ql", "l2", "l3", "l4"]
FILENAME_EXTENSION = ".cdf"


def create_science_filename(
Expand Down Expand Up @@ -50,29 +51,23 @@ def create_science_filename(

if instrument not in hermes_core.INST_NAMES:
raise ValueError(
"Instrument, {inst}, is not recognized. Must be one of {valid}.".format(
inst=instrument, valid=hermes_core.INST_NAMES
)
f"Instrument, {instrument}, is not recognized. Must be one of {hermes_core.INST_NAMES}."
)
if level not in VALID_DATA_LEVELS[1:]:
raise ValueError(
"Level, {level}, is not recognized. Must be one of {valid}.".format(
level=level, valid=VALID_DATA_LEVELS[1:]
)
f"Level, {level}, is not recognized. Must be one of {VALID_DATA_LEVELS[1:]}."
)
# check that version is in the right format with three parts
if len(version.split(".")) != 3:
raise ValueError(
"Version, {version}, is not formatted correctly. Should be X.Y.Z".format(
version=version
)
f"Version, {version}, is not formatted correctly. Should be X.Y.Z"
)
# check that version has integers in each part
for item in version.split("."):
try:
int_value = int(item)
except ValueError:
raise ValueError("Version, {version}, is not all integers.")
raise ValueError(f"Version, {version}, is not all integers.")

if test is True:
test_str = "test"
Expand All @@ -83,20 +78,10 @@ def create_science_filename(
"The underscore symbol _ is not allowed in mode or descriptor."
)

filename = (
"hermes_{inst}_{mode}_{level}{test}_{descriptor}_{time}_v{version}".format(
inst=hermes_core.INST_TO_SHORTNAME[instrument],
mode=mode,
level=level,
test=test_str,
descriptor=descriptor,
time=time_str,
version=version,
)
)
filename = f"hermes_{hermes_core.INST_TO_SHORTNAME[instrument]}_{mode}_{level}{test_str}_{descriptor}_{time_str}_v{version}"
filename = filename.replace("__", "_") # reformat if mode or descriptor not given

return filename + ".cdf"
return filename + FILENAME_EXTENSION


def parse_science_filename(filename):
Expand Down Expand Up @@ -129,20 +114,16 @@ def parse_science_filename(filename):
filename_components = file_name.split("_")

if filename_components[0] != hermes_core.MISSION_NAME:
raise ValueError(
"File {} not recognized. Not a valid mission name.".format(filename)
)
raise ValueError(f"File {filename} not recognized. Not a valid mission name.")

if file_ext == ".bin":
if filename_components[1] not in hermes_core.INST_TARGETNAMES:
raise ValueError(
"File {} not recognized. Not a valid target name.".format(filename)
f"File {filename} not recognized. Not a valid target name."
)
if filename_components[2] != VALID_DATA_LEVELS[0]:
raise ValueError(
"Data level {} is not correct for this file extension.".format(
filename_components[2]
)
f"Data level {filename_components[2]} is not correct for this file extension."
)
else:
result["level"] = filename_components[2]
Expand All @@ -154,7 +135,7 @@ def parse_science_filename(filename):
elif file_ext == ".cdf":
if filename_components[1] not in hermes_core.INST_SHORTNAMES:
raise ValueError(
"File {} not recognized. Not a valid instrument name.".format(filename)
"File {filename} not recognized. Not a valid instrument name."
)

# reverse the dictionary to look up instrument name from the short name
Expand All @@ -180,7 +161,7 @@ def parse_science_filename(filename):
if len(filename_components) == 7:
result["descriptor"] = filename_components[3]
else:
raise ValueError("File extension {} not recognized.".format(file_ext))
raise ValueError(f"File extension {file_ext} not recognized.")

result["instrument"] = from_shortname[filename_components[1]]
result["version"] = filename_components[-1][1:] # remove the v
Expand Down

0 comments on commit ed6eb01

Please sign in to comment.