code: def build_url(base, seg, query=None):
      
  
      def clean_segment(segment):
          """
          Cleans the segment and encodes to UTF-8 if the segment is unicode.
          """
          segment = segment.strip('/')
          if isinstance(segment, basestring):
              segment = segment.encode('utf-8')
          return segment
  
      seg = (quote(clean_segment(s)) for s in seg)
      if query is None or len(query) == 0:
          query_string = ''
      else:
          query_string = "?" + urlencode(query)
      path = '/'.join(seg) + query_string
      adjusted_base = base.rstrip('/') + '/'
      return urljoin(str(adjusted_base), str(path))
docs: Create a URL from a list of path segments and an optional dict of query
      parameters.