purl
purl copied to clipboard
Custom scheme is not respected in as_string
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"
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"