From ea0dd0dc21a659105f25f22fc8624849890de99e Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Fri, 2 Jun 2023 21:29:43 +0300 Subject: Create a function for stripping the last bit from a URL * gn3/db/rdf.py: Import unquote and urlparse. (strip_url): New function. Signed-off-by: Munyoki Kilyungi --- gn3/db/rdf.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/gn3/db/rdf.py b/gn3/db/rdf.py index 3e8d513..21a5121 100644 --- a/gn3/db/rdf.py +++ b/gn3/db/rdf.py @@ -5,6 +5,9 @@ This module is a collection of functions that handle SPARQL queries. """ from typing import Tuple from string import Template +from urllib.parse import unquote +from urllib.parse import urlparse + from SPARQLWrapper import JSON, SPARQLWrapper from pymonad.maybe import Just from gn3.monads import MonadicDict @@ -37,6 +40,14 @@ def sparql_query( return (MonadicDict(),) +def strip_url(string: str) -> str: + """Get the last item after a '/" from a URL""" + if string.startswith("http"): + url = urlparse(string) + return unquote(url.path).rpartition("/")[-1] + return string + + def get_dataset_metadata( sparql_conn: SPARQLWrapper, name: str ) -> MonadicDict: -- cgit v1.2.3