def rel_href(base_href, href):
 if urlparse(href).scheme:
  return href
 if ('/' not in base_href):
  return href
 base = filter((lambda x: (x and (x != '.'))), os.path.dirname(os.path.normpath(base_href)).replace(os.sep, '/').split('/'))
 while True:
  try:
   idx = base.index('..')
  except ValueError:
   break
  if (idx > 0):
   del base[(idx - 1):(idx + 1)]
  else:
   break
 if (not base):
  return href
 (target, frag) = urldefrag(href)
 target = target.split('/')
 index = 0
 for index in xrange(min(len(base), len(target))):
  if (base[index] != target[index]):
   break
 else:
  index += 1
 relhref = ((['..'] * (len(base) - index)) + target[index:])
 relhref = '/'.join(relhref)
 if frag:
  relhref = '#'.join((relhref, frag))
 return relhref

def rel_href ( base_href , href ) : if urlparse ( href ) . scheme : return href if ( '/' not in base_href ) : return href base = filter ( ( lambda x : ( x and ( x != '.' ) ) ) , os . path . dirname ( os . path . normpath ( base_href ) ) . replace ( os . sep , '/' ) . split ( '/' ) ) while True : try : idx = base . index ( '..' ) except ValueError : break if ( idx > 0 ) : del base [ ( idx - 1 ) : ( idx + 1 ) ] else : break if ( not base ) : return href ( target , frag ) = urldefrag ( href ) target = target . split ( '/' ) index = 0 for index in xrange ( min ( len ( base ) , len ( target ) ) ) : if ( base [ index ] != target [ index ] ) : break else : index += 1 relhref = ( ( [ '..' ] * ( len ( base ) - index ) ) + target [ index : ] ) relhref = '/' . join ( relhref ) if frag : relhref = '#' . join ( ( relhref , frag ) ) return relhref
for src=foo/bar .