Render this :class:`_engine.URL` object as a string. This method is used when the ``__str__()`` or ``__repr__()`` methods are used. The method directly includes additional options. :param hide_password: Defaults to True. The password is not shown in the string
(self, hide_password: bool = True)
| 628 | return self.render_as_string(hide_password=hide_password) |
| 629 | |
| 630 | def render_as_string(self, hide_password: bool = True) -> str: |
| 631 | """Render this :class:`_engine.URL` object as a string. |
| 632 | |
| 633 | This method is used when the ``__str__()`` or ``__repr__()`` |
| 634 | methods are used. The method directly includes additional options. |
| 635 | |
| 636 | :param hide_password: Defaults to True. The password is not shown |
| 637 | in the string unless this is set to False. |
| 638 | |
| 639 | """ |
| 640 | s = self.drivername + "://" |
| 641 | if self.username is not None: |
| 642 | s += quote(self.username, safe=" +") |
| 643 | if self.password is not None: |
| 644 | s += ":" + ( |
| 645 | "***" |
| 646 | if hide_password |
| 647 | else quote(str(self.password), safe=" +") |
| 648 | ) |
| 649 | s += "@" |
| 650 | if self.host is not None: |
| 651 | if ":" in self.host: |
| 652 | s += f"[{self.host}]" |
| 653 | else: |
| 654 | s += self.host |
| 655 | if self.port is not None: |
| 656 | s += ":" + str(self.port) |
| 657 | if self.database is not None: |
| 658 | s += "/" + quote(self.database, safe=" +/") |
| 659 | if self.query: |
| 660 | keys = list(self.query) |
| 661 | keys.sort() |
| 662 | s += "?" + "&".join( |
| 663 | f"{quote_plus(k)}={quote_plus(element)}" |
| 664 | for k in keys |
| 665 | for element in util.to_list(self.query[k]) |
| 666 | ) |
| 667 | return s |
| 668 | |
| 669 | def __repr__(self) -> str: |
| 670 | return self.render_as_string() |