def _to_dsn(hosts):
p = urlparse(hosts)<EOL>try:<EOL><INDENT>user_and_pw, netloc = p.netloc.split('<STR_LIT:@>', maxsplit=<NUM_LIT:1>)<EOL><DEDENT>except ValueError:<EOL><INDENT>netloc = p.netloc<EOL>user_and_pw = '<STR_LIT>'<EOL><DEDENT>try:<EOL><INDENT>host, port = netloc.split('<STR_LIT::>', maxsplit=<NUM_LIT:1>)<EOL><DEDENT>except ValueError:<EOL><INDENT>host = netloc<EOL>port = <NUM_LIT><EOL><DEDENT>dbname = p.path[<NUM_LIT:1>:] if p.path else '<STR_LIT>'<EOL>dsn = f'<STR_LIT>'<EOL>if p.query:<EOL><INDENT>dsn += '<STR_LIT:?>' + '<STR_LIT:&>'.join(k + '<STR_LIT:=>' + v[<NUM_LIT:0>] for k, v in parse_qs(p.query).items())<EOL><DEDENT>return dsn<EOL>
Convert a host URI into a dsn for aiopg.

    >>> _to_dsn('aiopg://myhostname:4242/mydb')
    'postgres://crate@myhostname:4242/mydb'

    >>> _to_dsn('aiopg://myhostname:4242')
    'postgres://crate@myhostname:4242/doc'

    >>> _to_dsn('aiopg://hoschi:pw@myhostname:4242/doc?sslmode=require')
    'postgres://hoschi:pw@myhostname:4242/doc?sslmode=require'

    >>> _to_dsn('aiopg://myhostname')
    'postgres://crate@myhostname:5432/doc'