Skip to content

Commit

Permalink
Modernize ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
taldcroft committed Nov 2, 2024
1 parent 0bfd046 commit 1b8103d
Show file tree
Hide file tree
Showing 23 changed files with 220 additions and 161 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.6.1
rev: v0.7.2
hooks:
# Run the linter.
- id: ruff
Expand Down
2 changes: 1 addition & 1 deletion kadi/cmds/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from .cmds import * # noqa
from .cmds import *
35 changes: 17 additions & 18 deletions kadi/cmds/cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ def load_pars_dict():

def filter(start=None, stop=None, **kwargs):
"""
Get commands with ``start`` <= date < ``stop``. Additional ``key=val`` pairs
can be supplied to further filter the results. Both ``key`` and ``val``
are case insensitive. In addition to the any of the command parameters
such as TLMSID, MSID, SCS, STEP, or POS, the ``key`` can be:
Get commands with ``start`` <= date < ``stop``.
date : Exact date of command e.g. '2013:003:22:11:45.530'
type : Command type e.g. COMMAND_SW, COMMAND_HW, ACISPKT, SIMTRANS
Additional ``key=val`` pairs can be supplied to further filter the results. Both
``key`` and ``val`` are case insensitive. In addition to the any of the command
parameters such as TLMSID, MSID, SCS, STEP, or POS, the ``key`` can be:
- date : Exact date of command e.g. '2013:003:22:11:45.530'
- type : Command type e.g. COMMAND_SW, COMMAND_HW, ACISPKT, SIMTRANS
Examples::
Expand Down Expand Up @@ -107,13 +108,14 @@ def filter(start=None, stop=None, **kwargs):

def _find(start=None, stop=None, **kwargs):
"""
Get commands ``start`` <= date < ``stop``. Additional ``key=val`` pairs
can be supplied to further filter the results. Both ``key`` and ``val``
are case insensitive. In addition to the any of the command parameters
such as TLMSID, MSID, SCS, STEP, or POS, the ``key`` can be:
Get commands ``start`` <= date < ``stop``.
Additional ``key=val`` pairs can be supplied to further filter the results. Both
``key`` and ``val`` are case insensitive. In addition to the any of the command
parameters such as TLMSID, MSID, SCS, STEP, or POS, the ``key`` can be:
date : Exact date of command e.g. '2013:003:22:11:45.530'
type : Command type e.g. COMMAND_SW, COMMAND_HW, ACISPKT, SIMTRANS
- date : Exact date of command e.g. '2013:003:22:11:45.530'
- type : Command type e.g. COMMAND_SW, COMMAND_HW, ACISPKT, SIMTRANS
Examples::
Expand Down Expand Up @@ -215,12 +217,9 @@ def __getitem__(self, item):
if item in cmds.colnames:
return cmds[item]

out = []
for idx in cmds["idx"]:
# Find the parameters dict for this command from the reverse
# lookup table which maps index to the params tuple.
out.append(dict(rev_pars_dict[idx]).get(item))
out = np.array(out)
# Find the parameters dict for this command from the reverse lookup table
# which maps index to the params tuple.
out = np.array([dict(rev_pars_dict[idx]).get(item) for idx in cmds["idx"]])

elif isinstance(item, int):
out = Cmd(cmds[item])
Expand Down
4 changes: 2 additions & 2 deletions kadi/commands/commands_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ def manvr_duration(q1, q2):
q_manvr_3 = abs(-q2[0] * q1[0] - q2[1] * q1[1] - q2[2] * q1[2] + q2[3] * -q1[3])

# 4th component is cos(theta/2)
if q_manvr_3 > 1: # noqa PLR1730
if q_manvr_3 > 1: # noqa: PLR1730
q_manvr_3 = 1
phimax = 2 * math.acos(q_manvr_3)

Expand All @@ -764,7 +764,7 @@ def manvr_duration(q1, q2):
np.sqrt(MANVR_DELTA**2 + 4 * phimax / MANVR_ALPHAMAX) / 2
- 1.5 * MANVR_DELTA
)
if eps < 0: # noqa PLR1730
if eps < 0: # noqa: PLR1730
eps = 0

tm = 4 * MANVR_DELTA + 2 * eps + tau
Expand Down
11 changes: 7 additions & 4 deletions kadi/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,14 +459,17 @@ def get_starcat_keys_types():


def encode_starcat_params(params_dict):
assert set(params_dict.keys()) == set(STARCAT_KEYS)
if set(params_dict.keys()) != set(STARCAT_KEYS):
raise ValueError(
f"Star catalog keys must be {STARCAT_KEYS}, got {set(params_dict.keys())}"
)
args = tuple(params_dict[key] for key in STARCAT_KEYS)
return struct.pack(STARCAT_TYPES, *args)


def decode_starcat_params(params_bytes):
vals = struct.unpack(STARCAT_TYPES, params_bytes)
return {key: val for key, val in zip(STARCAT_KEYS, vals)}
return dict(zip(STARCAT_KEYS, vals))


class CommandRow(Row):
Expand Down Expand Up @@ -510,7 +513,7 @@ def values(self):
return [self[key] for key in self.keys()] # noqa: SIM118

def items(self):
return [(key, value) for key, value in zip(self.keys(), self.values())]
return list(zip(self.keys(), self.values()))

def __repr__(self):
out = f"<Cmd {str(self)}>"
Expand Down Expand Up @@ -1059,7 +1062,7 @@ def get_par_idx_update_pars_dict(pars_dict, cmd, params=None, rev_pars_dict=None
# Define a consistently ordered tuple that has all command parameter information
if params is None:
params = cmd["params"]
keys = set(params.keys()) - set(("SCS", "STEP", "TLMSID"))
keys = set(params.keys()) - {"SCS", "STEP", "TLMSID"}

if cmd["tlmsid"] == "AOSTRCAT":
pars_tup = encode_starcat_params(params) if params else ()
Expand Down
10 changes: 7 additions & 3 deletions kadi/commands/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -2366,9 +2366,13 @@ def get_continuity(
# that were beyond the stop time and did not get processed.
continuity_transitions.extend(states.meta["continuity_transitions"])

colnames = set(states.colnames) - set(
["datestart", "datestop", "tstart", "tstop", "trans_keys"]
)
colnames = set(states.colnames) - {
"datestart",
"datestop",
"tstart",
"tstop",
"trans_keys",
}
for colname in colnames:
if states[colname][-1] is not None:
# Reduce states to only the desired state_key
Expand Down
44 changes: 22 additions & 22 deletions kadi/commands/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,18 +372,18 @@ def test_get_cmds_v2_recent_only(stop_date_2020_12_03): # noqa: ARG001
assert np.all(cmds["idx"] == -1)
# fmt: off
assert cmds[:5].pformat_like_backstop() == [
"2020:336:00:08:38.610 | COMMAND_HW | CNOOP | NOV3020A | hex=7E00000, msid=CNOOPLR, scs=128", # noqa
"2020:336:00:08:39.635 | COMMAND_HW | CNOOP | NOV3020A | hex=7E00000, msid=CNOOPLR, scs=128", # noqa
"2020:336:00:12:55.214 | ACISPKT | AA00000000 | NOV3020A | cmds=3, words=3, scs=131", # noqa
"2020:336:00:12:55.214 | ORBPOINT | None | NOV3020A | event_type=XEF1000, scs=0", # noqa
"2020:336:00:12:59.214 | ACISPKT | AA00000000 | NOV3020A | cmds=3, words=3, scs=131", # noqa
"2020:336:00:08:38.610 | COMMAND_HW | CNOOP | NOV3020A | hex=7E00000, msid=CNOOPLR, scs=128",
"2020:336:00:08:39.635 | COMMAND_HW | CNOOP | NOV3020A | hex=7E00000, msid=CNOOPLR, scs=128",
"2020:336:00:12:55.214 | ACISPKT | AA00000000 | NOV3020A | cmds=3, words=3, scs=131",
"2020:336:00:12:55.214 | ORBPOINT | None | NOV3020A | event_type=XEF1000, scs=0",
"2020:336:00:12:59.214 | ACISPKT | AA00000000 | NOV3020A | cmds=3, words=3, scs=131",
]
assert cmds[-5:].pformat_like_backstop() == [
"2020:342:03:15:02.313 | COMMAND_SW | OFMTSNRM | NOV3020A | hex=8010A00, msid=OFMTSNRM, scs=130", # noqa
"2020:342:03:15:02.313 | COMMAND_SW | COSCSEND | NOV3020A | hex=C800000, msid=OBC_END_SCS, scs=130", # noqa
"2020:342:06:04:34.287 | ACISPKT | AA00000000 | NOV3020A | cmds=3, words=3, scs=133", # noqa
"2020:342:06:04:34.287 | COMMAND_SW | COSCSEND | NOV3020A | hex=C800000, msid=OBC_END_SCS, scs=133", # noqa
"2020:342:06:04:34.287 | LOAD_EVENT | None | NOV3020A | event_type=SCHEDULED_STOP_TIME, scs=0", # noqa
"2020:342:03:15:02.313 | COMMAND_SW | OFMTSNRM | NOV3020A | hex=8010A00, msid=OFMTSNRM, scs=130",
"2020:342:03:15:02.313 | COMMAND_SW | COSCSEND | NOV3020A | hex=C800000, msid=OBC_END_SCS, scs=130",
"2020:342:06:04:34.287 | ACISPKT | AA00000000 | NOV3020A | cmds=3, words=3, scs=133",
"2020:342:06:04:34.287 | COMMAND_SW | COSCSEND | NOV3020A | hex=C800000, msid=OBC_END_SCS, scs=133",
"2020:342:06:04:34.287 | LOAD_EVENT | None | NOV3020A | event_type=SCHEDULED_STOP_TIME, scs=0",
]
# fmt: on
# Same for no stop date
Expand Down Expand Up @@ -613,7 +613,7 @@ def test_command_set_bsh():
2000:001:00:00:01.025 | SIMTRANS | None | CMD_EVT | event=Bright_star_hold, event_date=2000:001:00:00:00, pos=-99616, scs=0
2000:001:00:01:06.685 | ACISPKT | AA00000000 | CMD_EVT | event=Bright_star_hold, event_date=2000:001:00:00:00, scs=0
2000:001:00:01:07.710 | ACISPKT | AA00000000 | CMD_EVT | event=Bright_star_hold, event_date=2000:001:00:00:00, scs=0
2000:001:00:01:17.960 | ACISPKT | WSPOW00000 | CMD_EVT | event=Bright_star_hold, event_date=2000:001:00:00:00, scs=0""" # noqa
2000:001:00:01:17.960 | ACISPKT | WSPOW00000 | CMD_EVT | event=Bright_star_hold, event_date=2000:001:00:00:00, scs=0"""

assert cmds.pformat_like_backstop(max_params_width=None) == exp.splitlines()
commands.clear_caches()
Expand All @@ -636,7 +636,7 @@ def test_command_set_safe_mode():
2000:001:00:01:06.685 | ACISPKT | AA00000000 | CMD_EVT | event=Safe_mode, event_date=2000:001:00:00:00, scs=0
2000:001:00:01:07.710 | ACISPKT | AA00000000 | CMD_EVT | event=Safe_mode, event_date=2000:001:00:00:00, scs=0
2000:001:00:01:17.960 | ACISPKT | WSPOW00000 | CMD_EVT | event=Safe_mode, event_date=2000:001:00:00:00, scs=0
2000:001:00:01:17.960 | COMMAND_SW | AODSDITH | CMD_EVT | event=Safe_mode, event_date=2000:001:00:00:00, scs=0""" # noqa
2000:001:00:01:17.960 | COMMAND_SW | AODSDITH | CMD_EVT | event=Safe_mode, event_date=2000:001:00:00:00, scs=0"""
assert cmds.pformat_like_backstop(max_params_width=None) == exp.splitlines()
commands.clear_caches()

Expand Down Expand Up @@ -1009,16 +1009,16 @@ def test_get_cmds_from_event_case(par_str):
)
cmd_events_all_exps = [
[
"2020:001:00:00:00.000 | LOAD_EVENT | None | CMD_EVT | event=Observing_not_run, event_date=2020:001:00:00:00, event_type=OBSERVING_NOT_RUN, load=FEB1422A, scs=0" # noqa
"2020:001:00:00:00.000 | LOAD_EVENT | None | CMD_EVT | event=Observing_not_run, event_date=2020:001:00:00:00, event_type=OBSERVING_NOT_RUN, load=FEB1422A, scs=0"
],
[
"2020:001:00:00:00.000 | LOAD_EVENT | None | CMD_EVT | event=Load_not_run, event_date=2020:001:00:00:00, event_type=LOAD_NOT_RUN, load=OCT2521A, scs=0" # noqa
"2020:001:00:00:00.000 | LOAD_EVENT | None | CMD_EVT | event=Load_not_run, event_date=2020:001:00:00:00, event_type=LOAD_NOT_RUN, load=OCT2521A, scs=0"
],
[
"2020:001:00:00:00.000 | ACISPKT | AA00000000 | CMD_EVT | event=Command, event_date=2020:001:00:00:00, cmds=3, words=3, scs=0" # noqa
"2020:001:00:00:00.000 | ACISPKT | AA00000000 | CMD_EVT | event=Command, event_date=2020:001:00:00:00, cmds=3, words=3, scs=0"
],
[
"2020:001:00:00:00.000 | NOT_RUN | 4OHETGIN | CMD_EVT | event=Command_not_run, event_date=2020:001:00:00:00, hex=8050300, msid=4OHETGIN, __type__=COMMAND_SW, scs=0" # noqa
"2020:001:00:00:00.000 | NOT_RUN | 4OHETGIN | CMD_EVT | event=Command_not_run, event_date=2020:001:00:00:00, hex=8050300, msid=4OHETGIN, __type__=COMMAND_SW, scs=0"
],
[
"2020:001:00:00:00.000 | COMMAND_SW | OORMPEN | CMD_EVT | event=RTS,"
Expand Down Expand Up @@ -1059,7 +1059,7 @@ def test_get_cmds_from_event_case(par_str):
" event_date=2020:001:00:00:00, scs=135",
],
[
"2020:001:00:00:00.000 | MP_OBSID | COAOSQID | CMD_EVT | event=Obsid, event_date=2020:001:00:00:00, id=65527, scs=0" # noqa
"2020:001:00:00:00.000 | MP_OBSID | COAOSQID | CMD_EVT | event=Obsid, event_date=2020:001:00:00:00, id=65527, scs=0"
],
[
"2020:001:00:00:00.000 | COMMAND_SW | AONMMODE | CMD_EVT |"
Expand Down Expand Up @@ -1185,9 +1185,9 @@ def test_get_cmds_from_event_case(par_str):
"2020:001:00:00:00.000 | COMMAND_SW | OORMPDS | CMD_EVT |"
" event=Bright_star_hold, event_date=2020:001:00:00:00, scs=0",
"2020:001:00:00:01.025 | COMMAND_HW | AFIDP | CMD_EVT |"
" event=Bright_star_hold, event_date=2020:001:00:00:00, msid=AFLCRSET, scs=0", # noqa
" event=Bright_star_hold, event_date=2020:001:00:00:00, msid=AFLCRSET, scs=0",
"2020:001:00:00:01.025 | SIMTRANS | None | CMD_EVT |"
" event=Bright_star_hold, event_date=2020:001:00:00:00, pos=-99616, scs=0", # noqa
" event=Bright_star_hold, event_date=2020:001:00:00:00, pos=-99616, scs=0",
"2020:001:00:01:06.685 | ACISPKT | AA00000000 | CMD_EVT |"
" event=Bright_star_hold, event_date=2020:001:00:00:00, scs=0",
"2020:001:00:01:07.710 | ACISPKT | AA00000000 | CMD_EVT |"
Expand All @@ -1196,7 +1196,7 @@ def test_get_cmds_from_event_case(par_str):
" event=Bright_star_hold, event_date=2020:001:00:00:00, scs=0",
],
[
"2020:001:00:00:00.000 | COMMAND_SW | AOENDITH | CMD_EVT | event=Dither, event_date=2020:001:00:00:00, scs=0" # noqa
"2020:001:00:00:00.000 | COMMAND_SW | AOENDITH | CMD_EVT | event=Dither, event_date=2020:001:00:00:00, scs=0"
],
]

Expand Down Expand Up @@ -1321,7 +1321,7 @@ def test_scenario_with_rts(monkeypatch, fast_sun_position_method):
2021:298:01:57:00.000 | ACISPKT | AA00000000 | OCT2521B | cmds=3, words=3, scs=131
2021:298:01:57:03.000 | ACISPKT | AA00000000 | OCT2521B | cmds=3, words=3, scs=131
2021:298:01:57:33.000 | COMMAND_SW | CODISASX | OCT2521B | msid=CODISASX, codisas1=135 , scs=131
2021:298:01:57:34.000 | COMMAND_SW | COCLRSX | OCT2521B | msid=COCLRSX, coclrs1=135 , scs=131""" # noqa: E501
2021:298:01:57:34.000 | COMMAND_SW | COCLRSX | OCT2521B | msid=COCLRSX, coclrs1=135 , scs=131"""

out = "\n".join(cmds.pformat_like_backstop(max_params_width=60))
assert out == exp
Expand Down Expand Up @@ -1578,7 +1578,7 @@ def test_command_not_run(case):
2023:351:13:30:33.849 | 4 0 | COMMAND_SW | TLMSID= COACTSX, HEX= 8402600, MSID= COACTSX, COACTS1=38 , COACTS2=0 , SCS= 128, STEP= 691
2023:351:13:30:55.373 | 5 0 | COMMAND_HW | TLMSID= 4MC5AEN, HEX= 4800012, MSID= 4MC5AEN, SCS= 131, STEP= 892
2023:351:19:38:41.550 | 6 0 | SIMTRANS | POS= 92904, SCS= 131, STEP= 1191
""" # noqa
"""
cmds = commands.read_backstop(backstop_text.strip().splitlines())
cmds["source"] = "DEC1123A"
cmds_exp = cmds.copy()
Expand Down
Loading

0 comments on commit 1b8103d

Please sign in to comment.