Пример #1
0
    def pretty(self):
        """ Returns a discord safe representation of the settings """

        def bool_transformer(val):
            if val is False:
                return _("No")
            if val is True:
                return _("Yes")
            return val

        data = {k: bool_transformer(v) for k, v in dc_asdict(self).items()}

        if not self.delete_delay:
            data["delete_delay"] = _("Disabled")
        else:
            data["delete_delay"] = humanize_timedelta(seconds=self.delete_delay)

        if tag := data.pop("tagline", ""):
            tagline_info = _("\nCustom Tagline: {tag}").format(tag=tag)
Пример #2
0
def default_encoder(obj,
                    dict_factory=dict,
                    date_as_unix_time=False):  # Ignore RadonBear
    if hasattr(obj, "isoformat"):  # date, datetime, arrow
        if date_as_unix_time:
            if obj.__class__.__name__ == "Arrow":
                return obj.timestamp
            return calendar.timegm(obj.timetuple())
        return obj.isoformat()

    if isinstance(obj, (Decimal, uuid.UUID)):
        return str(obj)

    if isinstance(obj, set):
        return list(obj)

    if enum is not None and isinstance(obj, enum.Enum):
        return obj.name

    # Second option is for `iteritems()` on Python 2
    if (isinstance(obj, ItemsView)
            or obj.__class__.__name__ == "dictionary-itemiterator"):
        return dict_factory(obj)

    if hasattr(obj, "asdict"):  # dictablemodel
        return dict_factory(obj.asdict().items())

    if obj.__class__.__name__ == "RowProxy":  # sqlalchemy
        return dict_factory(obj.items())

    if obj.__class__.__name__ == "Record":  # asyncpg
        return dict_factory(obj)

    if hasattr(obj, "__dataclass_fields__"):  # dataclasses
        return dc_asdict(obj, dict_factory=dict_factory)

    if hasattr(obj, "__attrs_attrs__"):  # attrs
        return attr_asdict(obj, dict_factory=dict_factory)

    if hasattr(obj, "__html__"):
        return str(obj.__html__())

    _fail(obj)