Skip to content

Commit

Permalink
feat(dataset): add "get_url()" method to "Data"
Browse files Browse the repository at this point in the history
PR Closed: #789
  • Loading branch information
edsn60 committed Jul 5, 2021
1 parent 16c309d commit 2ebecfb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
13 changes: 11 additions & 2 deletions tensorbay/dataset/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from http.client import HTTPResponse
from string import printable
from typing import Any, Callable, Dict, Optional, Type, TypeVar, Union
from urllib.parse import quote
from urllib.request import urlopen
from urllib.parse import quote, urljoin
from urllib.request import pathname2url, urlopen

from _io import BufferedReader

Expand Down Expand Up @@ -213,6 +213,15 @@ def dumps(self) -> Dict[str, Any]:
"""
return super()._dumps()

def get_url(self) -> str:
"""Return the url of the local data file.
Returns:
The url of the local data.
"""
return urljoin("file:", pathname2url(os.path.abspath(self.path)))


class RemoteData(DataBase):
"""RemoteData is a combination of a specific tensorbay dataset file and its label.
Expand Down
9 changes: 9 additions & 0 deletions tensorbay/dataset/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Copyright 2021 Graviti. Licensed under MIT License.
#

from pathlib import Path

import pytest

from ..data import Data, RemoteData
Expand Down Expand Up @@ -37,6 +39,13 @@ def test_target_remote_path(self):
data_object.target_remote_path = target_remote_path
assert data_object.target_remote_path == target_remote_path

def test_get_url(self):
local_relative_path = Path(__file__).relative_to(Path.cwd())
data = Data(str(local_relative_path))
assert data.get_url() == local_relative_path.resolve().as_uri()
data = Data(__file__)
assert data.get_url() == Path(__file__).resolve().as_uri()


class TestRemoteData:
def test_init(self):
Expand Down

0 comments on commit 2ebecfb

Please sign in to comment.