code: def mkpath(*segments, **query):
      
      # Remove empty segments (e.g. no key specified)
      segments = [bytes_to_str(s) for s in segments if s is not None]
      # Join the segments into a path
      pathstring = '/'.join(segments)
      # Remove extra slashes
      pathstring = re.sub('/+', '/', pathstring)
  
      # Add the query string if it exists
      _query = {}
      for key in query:
          if query[key] in [False, True]:
              _query[key] = str(query[key]).lower()
          elif query[key] is not None:
              if PY2 and isinstance(query[key], unicode):  # noqa
                  _query[key] = query[key].encode('utf-8')
              else:
                  _query[key] = query[key]
  
      if len(_query) > 0:
          pathstring += "?" + urlencode(_query)
  
      if not pathstring.startswith('/'):
          pathstring = '/' + pathstring
  
      return pathstring
docs: Constructs the path & query portion of a URI from path segments
      and a dict.