From 579d0c95a085a7cb796ae9476da8c48fca201601 Mon Sep 17 00:00:00 2001 From: Hemna Date: Tue, 5 Nov 2024 15:04:48 -0500 Subject: [PATCH] optimized Packet.get() --- aprsd/packets/core.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/aprsd/packets/core.py b/aprsd/packets/core.py index fa43f3f..52bea31 100644 --- a/aprsd/packets/core.py +++ b/aprsd/packets/core.py @@ -63,15 +63,11 @@ def _init_msgNo(): # noqa: N802 def _translate_fields(raw: dict) -> dict: - translate_fields = { - "from": "from_call", - "to": "to_call", - } - # First translate some fields - for key in translate_fields: - if key in raw: - raw[translate_fields[key]] = raw[key] - del raw[key] + # Direct key checks instead of iteration + if "from" in raw: + raw["from_call"] = raw.pop("from") + if "to" in raw: + raw["to_call"] = raw.pop("to") # addresse overrides to_call if "addresse" in raw: @@ -110,11 +106,7 @@ class Packet: via: Optional[str] = field(default=None, compare=False, hash=False) def get(self, key: str, default: Optional[str] = None): - """Emulate a getter on a dict.""" - if hasattr(self, key): - return getattr(self, key) - else: - return default + return getattr(self, key, default) @property def key(self) -> str: