Skip to content

Commit

Permalink
python: EventLog accept pathlib.Path (#468)
Browse files Browse the repository at this point in the history
* python: EventLog accept pathlib.Path

* python: Attempt to make pathlib and type args backwards compatible

* python: fix typed union
  • Loading branch information
judfs committed Oct 2, 2023
1 parent 24ec55a commit 8878041
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lcm-python/lcm/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 8878041

Please sign in to comment.