melobot.typ

class melobot.typ.LogicMode[源代码]

基类:Enum

逻辑模式枚举类型

AND = 'and'
OR = 'or'
NOT = 'not'
XOR = 'xor'
get_operator() Callable[[Any, Any], bool][源代码]
返回类型:

Callable[[Any, Any], bool]

class melobot.typ.Color[源代码]

基类:object

颜色表示类

__init__(arg1: Literal['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'bright_black', 'bright_red', 'bright_green', 'bright_yellow', 'bright_blue', 'bright_magenta', 'bright_cyan', 'bright_white', 'reset'] | str | int, arg2: int | None = None, arg3: int | None = None) None[源代码]

三种初始化模式:

  1. 通过常用颜色名称字符串初始化,例如 "red"、"bright_blue" 等。

  2. 通过十六进制颜色字符串初始化,例如 "#ff0000"

  3. 通过 RGB 三参数初始化,例如 255, 0, 0

参数:
  • arg1 (Literal['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'bright_black', 'bright_red', 'bright_green', 'bright_yellow', 'bright_blue', 'bright_magenta', 'bright_cyan', 'bright_white', 'reset'] | str | int) -- 第一参数,可以是常用颜色名称字符串、十六进制颜色字符串,或者 RGB 的 R 分量整数

  • arg2 (int | None) -- 第二参数,如果 arg1 是 RGB 的 R 分量,则 arg2 是 G 分量整数,否则为 None

  • arg3 (int | None) -- 第三参数,如果 arg1 是 RGB 的 R 分量,则 arg3 是 B 分量整数,否则为 None

返回类型:

None

property hex: str
property hsl: tuple[float, float, float]
black: Color = <melobot.typ._enum.Color object>
red: Color = <melobot.typ._enum.Color object>
green: Color = <melobot.typ._enum.Color object>
yellow: Color = <melobot.typ._enum.Color object>
blue: Color = <melobot.typ._enum.Color object>
magenta: Color = <melobot.typ._enum.Color object>
cyan: Color = <melobot.typ._enum.Color object>
white: Color = <melobot.typ._enum.Color object>
bright_black: Color = <melobot.typ._enum.Color object>
bright_red: Color = <melobot.typ._enum.Color object>
bright_green: Color = <melobot.typ._enum.Color object>
bright_yellow: Color = <melobot.typ._enum.Color object>
bright_blue: Color = <melobot.typ._enum.Color object>
bright_magenta: Color = <melobot.typ._enum.Color object>
bright_cyan: Color = <melobot.typ._enum.Color object>
bright_white: Color = <melobot.typ._enum.Color object>
reset: Color = <melobot.typ._enum.Color object>
melobot.typ.is_type(obj: T, hint: type[Any]) TypeIs[T][源代码]

检查 obj 是否是类型注解 hint 所表示的类型

参数:
  • obj (T) -- 任意对象

  • hint (type[Any]) -- 任意类型注解

返回:

布尔值

返回类型:

TypeIs[T]

class melobot.typ.BetterABCMeta[源代码]

基类:ABCMeta

更好的抽象元类,兼容 ABCMeta 的所有功能,但是额外支持 abstractattr()

class melobot.typ.BetterABC[源代码]

基类:object

更好的抽象类,兼容 ABC 的所有功能,但是额外支持 abstractattr()

melobot.typ.abstractattr(obj: Callable[[Any], T] | None = None) T[源代码]

抽象属性

abstractproperty 相比更灵活,abstractattr 不关心你以何种形式定义属性。只要子类在实例化后,该属性存在,即认为合法。

但注意它必须与 BetterABCBetterABCMetaSingletonBetterABCMeta 配合使用

这意味着可以在类层级、实例层级定义属性,或使用 property 定义属性:

class A(BetterABC):
    foo: int = abstractattr()  # 声明为抽象属性

    # 或者使用装饰器的形式声明,这与上面是等价的
    @abstractattr
    def bar(self) -> int: ...

# 以下实现都是合法的:

class B(A):
    foo = 2
    bar = 4

class C(A):
    foo = 3
    def __init__(self) -> None:
        self.bar = 5

class D(A):
    def __init__(self) -> None:
        self.foo = 8

    @property
    def bar(self) -> int:
        return self.foo + 10
参数:

obj (Callable[[Any], T] | None)

返回类型:

T

class melobot.typ.SingletonMeta[源代码]

基类:type

单例元类

相比单例装饰器,可以自动保证所有子类都为单例

class melobot.typ.SingletonBetterABCMeta[源代码]

基类:BetterABCMeta

单例抽象元类

相比普通的抽象元类,还可以自动保证所有子类都为单例

class melobot.typ.AsyncCallable[源代码]

基类:Protocol[P, T_co]

用法:AsyncCallable[P, T]

是该类型的等价形式:Callable[P, Awaitable[T]]

class melobot.typ.SyncOrAsyncCallable[源代码]

基类:Protocol[P, T_co]

用法:SyncOrAsyncCallable[P, T]

是该类型的等价形式:Callable[P, T | Awaitable[T]]

melobot.typ.T

泛型 T,无约束

melobot.typ.U

泛型 U,无约束

melobot.typ.V

泛型 V,无约束

melobot.typ.T_co

泛型 T_co,协变无约束

melobot.typ.P

ParamSpec 泛型 P,无约束