From 8878041c2ed9298b25b9999b90be05fc6a889ced Mon Sep 17 00:00:00 2001 From: judfs <39133604+judfs@users.noreply.github.com> Date: Mon, 2 Oct 2023 09:57:07 -0400 Subject: [PATCH] python: EventLog accept pathlib.Path (#468) * python: EventLog accept pathlib.Path * python: Attempt to make pathlib and type args backwards compatible * python: fix typed union --- lcm-python/lcm/__init__.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lcm-python/lcm/__init__.py b/lcm-python/lcm/__init__.py index f40240c68..e3e78caf9 100644 --- a/lcm-python/lcm/__init__.py +++ b/lcm-python/lcm/__init__.py @@ -1,4 +1,23 @@ import os +import sys + +# Attempt to be backwards compatible +if sys.version_info >= (3, 6): + from os import PathLike +elif sys.version_info >= (3, 4): + from pathlib import Path as PathLike +else: + # All we do to the Path is call str on it, so this is a harmless fallback. + PathLike = str + +if sys.version_info >= (3, 5): + import typing + # Not including `bytes` like other APIs in the union as the native interface does not accept a byte string. + # Makes more sense for the user to .decode(encoding) the bytes than us. + PathArgument = typing.Union[str, PathLike] +else: + PathArgument = object + from lcm import _lcm from lcm._lcm import LCM, LCMSubscription @@ -33,7 +52,7 @@ class EventLog(object): @undocumented: __iter__ """ - def __init__ (self, path, mode = "r", overwrite = False): + def __init__ (self, path:PathArgument, mode = "r", overwrite = False): """ Initializer @@ -53,6 +72,9 @@ def __init__ (self, path, mode = "r", overwrite = False): self.mode = mode + if isinstance(path, PathLike): + path = str(path) + self.c_eventlog = _lcm.EventLog (path, mode) self.f = None