Skip to content

Commit

Permalink
Use plain dict instead of OrderedDict (#28)
Browse files Browse the repository at this point in the history
We were using `OrderedDict` mostly for documentation purposes that the
returned dict preserves the insertion order.

However, such gurantee is a language feature rather than a CPython
implementation detail since python 3.7.

Considering (1) it has been quite some time since python 3.7 for this
language feature to be well-known, and (2) our minimum supported version
is python 3.8. We can safely return a plain `dict` instead of
`OrderedDict` now.
  • Loading branch information
yuxincs authored Mar 3, 2024
1 parent 95c0491 commit 67b1ae7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 63 deletions.
7 changes: 3 additions & 4 deletions src/aioapcaccess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

import asyncio
from collections import OrderedDict

_STATUS_CMD = b"\x00\x06status"
_EOF = b"\x00\x00"
Expand All @@ -29,7 +28,7 @@
}


async def request_status(host: str = "localhost", port: int = 3551) -> OrderedDict[str, str]:
async def request_status(host: str = "localhost", port: int = 3551) -> dict[str, str]:
"""Connect to the APCUPSd NIS and request its status and return the parsed dict.
:param host: the host which apcupsd is listening on.
Expand All @@ -40,14 +39,14 @@ async def request_status(host: str = "localhost", port: int = 3551) -> OrderedDi
return parse_raw_status(await request_raw_status(host, port))


def parse_raw_status(raw_status: bytes) -> OrderedDict[str, str]:
def parse_raw_status(raw_status: bytes) -> dict[str, str]:
"""Parse the raw status and return an ordered dict.
:param raw_status: raw status string retrieved from apcupsd.
:return: an ordered dict, where key is the field (e.g., "NOMINV") and value is the
value. For example, {"NOMINV": "12.0 Volts", ..., "SERIALNO": "XXXXXXX"}
"""
result = OrderedDict()
result = {}

# Strip EOF from the status and decode to string.
if not raw_status.endswith(_EOF):
Expand Down
112 changes: 54 additions & 58 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Sample raw status and its parsed dict."""

from collections import OrderedDict

SAMPLE_STATUS = (
b"\x00"
b"\x18APC : 001,051,1148\n\x00"
Expand Down Expand Up @@ -60,59 +58,57 @@
b"\x00"
)

PARSED_DICT = OrderedDict(
[
("APC", "001,051,1148"),
("DATE", "2016-02-09 21:13:11 +0000"),
("HOSTNAME", "localhost"),
("VERSION", "3.14.12 (29 March 2014) redhat"),
("UPSNAME", "netrack"),
("CABLE", "Custom Cable Smart"),
("DRIVER", "APC Smart UPS (any)"),
("UPSMODE", "Stand Alone"),
("STARTTIME", "2016-02-09 16:06:47 +0000"),
("MODEL", "SMART-UPS 1400"),
("STATUS", "TRIM ONLINE"),
("LINEV", "247.0 Volts"),
("LOADPCT", "9.3 Percent Load Capacity"),
("BCHARGE", "100.0 Percent"),
("TIMELEFT", "128.0 Minutes"),
("MBATTCHG", "5 Percent"),
("MINTIMEL", "3 Minutes"),
("MAXTIME", "0 Seconds"),
("MAXLINEV", "250.9 Volts"),
("MINLINEV", "247.0 Volts"),
("OUTPUTV", "218.4 Volts"),
("SENSE", "High"),
("DWAKE", "0 Seconds"),
("DSHUTD", "180 Seconds"),
("DLOWBATT", "2 Minutes"),
("LOTRANS", "196.0 Volts"),
("HITRANS", "253.0 Volts"),
("RETPCT", "15.0 Percent"),
("ITEMP", "30.6 C Internal"),
("ALARMDEL", "Low Battery"),
("BATTV", "27.6 Volts"),
("LINEFREQ", "50.0 Hz"),
("LASTXFER", "High line voltage"),
("NUMXFERS", "0"),
("TONBATT", "0 Seconds"),
("CUMONBATT", "0 Seconds"),
("XOFFBATT", "N/A"),
("SELFTEST", "NO"),
("STESTI", "7 days"),
("STATFLAG", "0x0500000A"),
("DIPSW", "0x00"),
("REG1", "0x00"),
("REG2", "0x00"),
("REG3", "0x00"),
("MANDATE", "07/13/99"),
("SERIALNO", "GS9229021308"),
("BATTDATE", "13/11/15"),
("NOMOUTV", "230 Volts"),
("NOMBATTV", "24.0 Volts"),
("EXTBATTS", "0"),
("FIRMWARE", "70.11.I"),
("END APC", "2016-02-09 21:13:26 +0000"),
]
)
PARSED_DICT = {
"APC": "001,051,1148",
"DATE": "2016-02-09 21:13:11 +0000",
"HOSTNAME": "localhost",
"VERSION": "3.14.12 (29 March 2014) redhat",
"UPSNAME": "netrack",
"CABLE": "Custom Cable Smart",
"DRIVER": "APC Smart UPS (any)",
"UPSMODE": "Stand Alone",
"STARTTIME": "2016-02-09 16:06:47 +0000",
"MODEL": "SMART-UPS 1400",
"STATUS": "TRIM ONLINE",
"LINEV": "247.0 Volts",
"LOADPCT": "9.3 Percent Load Capacity",
"BCHARGE": "100.0 Percent",
"TIMELEFT": "128.0 Minutes",
"MBATTCHG": "5 Percent",
"MINTIMEL": "3 Minutes",
"MAXTIME": "0 Seconds",
"MAXLINEV": "250.9 Volts",
"MINLINEV": "247.0 Volts",
"OUTPUTV": "218.4 Volts",
"SENSE": "High",
"DWAKE": "0 Seconds",
"DSHUTD": "180 Seconds",
"DLOWBATT": "2 Minutes",
"LOTRANS": "196.0 Volts",
"HITRANS": "253.0 Volts",
"RETPCT": "15.0 Percent",
"ITEMP": "30.6 C Internal",
"ALARMDEL": "Low Battery",
"BATTV": "27.6 Volts",
"LINEFREQ": "50.0 Hz",
"LASTXFER": "High line voltage",
"NUMXFERS": "0",
"TONBATT": "0 Seconds",
"CUMONBATT": "0 Seconds",
"XOFFBATT": "N/A",
"SELFTEST": "NO",
"STESTI": "7 days",
"STATFLAG": "0x0500000A",
"DIPSW": "0x00",
"REG1": "0x00",
"REG2": "0x00",
"REG3": "0x00",
"MANDATE": "07/13/99",
"SERIALNO": "GS9229021308",
"BATTDATE": "13/11/15",
"NOMOUTV": "230 Volts",
"NOMBATTV": "24.0 Volts",
"EXTBATTS": "0",
"FIRMWARE": "70.11.I",
"END APC": "2016-02-09 21:13:26 +0000",
}
2 changes: 1 addition & 1 deletion tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def test_request_raw_status():


def test_parse_raw_status():
"""Test parsing raw status"""
"""Test parsing raw status."""
assert parse_raw_status(SAMPLE_STATUS) == PARSED_DICT


Expand Down

0 comments on commit 67b1ae7

Please sign in to comment.