purl icon indicating copy to clipboard operation
purl copied to clipboard

Custom scheme is not respected in as_string

Open denizdogan opened this issue 6 years ago • 1 comments

ipython
Python 3.7.3 (default, Apr 12 2019, 14:40:22)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.3.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from purl import URL

In [2]: url = URL("foobar:///my_path")

In [3]: url.scheme()
Out[3]: 'foobar'

In [4]: url.as_string()
Out[4]: '/my_path'

I expect Out[4] to be "foobar:///my_path"

denizdogan avatar Aug 20 '19 14:08 denizdogan

from purl import URL

class CustomURL(URL):
    def as_string(self):
        # Get the string representation of the URL without the scheme
        url_string = super().as_string()[2:]
        # Add the custom scheme back to the URL string
        return f"{self.scheme()}://{url_string}"

# Test the CustomURL class
url = CustomURL("foobar:///my_path")
print(url.as_string())  # Output: "foobar:///my_path"

ljluestc avatar Feb 26 '24 00:02 ljluestc