views:

49

answers:

3

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 = htt p://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.

A: 

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.lpartition('/')
    return '/'.join(parts[0], key)
  parts = current.rpartition('/')
  return '/'.join(parts[0], key)
g.d.d.c
+1  A: 

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.

Matt Luongo
A: 

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)
JAB
Thanks guys! I basically did it manually. I improved upon the above though in the else branch by doing an rfind for the last / and then concatenating from there the key. Also in the elif I searched for the index of the the first matching word as oppose to generalizing the index.
G Ullman