Question

Basically I've got this current url and this other key that I want to merge into a new url, but there are three different cases.

Suppose the current url is localhost:32401/A/B/foo

if key is bar then I want to return localhost:32401/A/B/bar

if key starts with a slash and is /A/bar then I want to return localhost:32401/A/bar

finally if key is its own independent url then I just want to return that key = http://foo.com/bar -> http://foo.com/bar

I assume there is a way to do at least the first two cases without manipulating the strings manually, but nothing jumped out at me immediately in the os.path module.

Was it helpful?

Solution

I assume there is a way to do at least the first two cases without manipulating the strings manually, but nothing jumped out at me immediately in the os.path module.

That's because you want to use urllib.parse (for Python 3.x) or urlparse (for Python 2.x) instead.

I don't have much experience with it, though, so here's a snippet using str.split() and str.join().

urlparts = url.split('/')

if key.startswith('http://'):
    return key
elif key.startswith('/'):
    return '/'.join(urlparts[:2], key[1:])
else:
    urlparts[len(urlparts) - 1] = key
    return '/'.join(urlparts)

OTHER TIPS

Have you checked out the urlparse module?

From the docs,

from urlparse import urljoin
urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')

Should help with your first case.

Obviously, you can always do basic string manipulation for the rest.

String objects in Python all have startswith and endswith methods that should be able to get you there. Something like this perhaps?

def merge(current, key):
  if key.startswith('http'):
    return key
  if key.startswith('/'):
    parts = current.partition('/')
    return '/'.join(parts[0], key)
  parts = current.rpartition('/')
  return '/'.join(parts[0], key)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top