diff --git a/v6d2ctx/at_of.py b/v6d2ctx/at_of.py index c29339d..c39c244 100644 --- a/v6d2ctx/at_of.py +++ b/v6d2ctx/at_of.py @@ -1,6 +1,9 @@ from typing import Callable, Generic, TypeVar -from .context import Implicit +from .context import * + +__all__ = ('AtOf',) + K = TypeVar('K') V = TypeVar('V') diff --git a/v6d2ctx/context.py b/v6d2ctx/context.py index e61e120..9832a63 100644 --- a/v6d2ctx/context.py +++ b/v6d2ctx/context.py @@ -1,10 +1,10 @@ -import asyncio -import time from io import StringIO from typing import Awaitable, Callable, Optional, Union import discord +__all__ = ('usertype', 'Context', 'escape', 'command_type', 'Explicit', 'Implicit') + usertype = Union[discord.abc.User, discord.user.BaseUser, discord.Member, discord.User] @@ -57,30 +57,3 @@ class Explicit(Exception): class Implicit(Exception): pass - - -benchmarks: dict[str, dict[str, float]] = {} -_t = time.perf_counter() - - -class Benchmark: - def __init__(self, benchmark: str): - self.benchmark = benchmark - - def __enter__(self): - self.t = time.perf_counter() - - def __exit__(self, exc_type, exc_val, exc_tb): - d = (time.perf_counter() - self.t) - benchmarks.setdefault(self.benchmark, {'integral': 0.0, 'max': 0.0}) - benchmarks[self.benchmark]['integral'] += d - benchmarks[self.benchmark]['max'] = max(benchmarks[self.benchmark]['max'], d) - - -async def monitor(): - while True: - await asyncio.sleep(10) - dt = time.perf_counter() - _t - print('Benchmarks:') - for benchmark, metrics in benchmarks.items(): - print(benchmark, '=', metrics['integral'] / max(dt, .00001), ':', metrics['max']) diff --git a/v6d2ctx/handle_args.py b/v6d2ctx/handle_args.py index 0ad589c..c983419 100644 --- a/v6d2ctx/handle_args.py +++ b/v6d2ctx/handle_args.py @@ -2,8 +2,10 @@ from typing import Callable import discord -from v6d2ctx.context import Context, Explicit, Implicit, command_type -from v6d2ctx.handle_command import handle_command +from v6d2ctx.context import * +from v6d2ctx.handle_command import * + +__all__ = ('handle_args',) async def handle_args(of: Callable[[str], command_type], message: discord.Message, args: list[str], client: discord.Client): diff --git a/v6d2ctx/handle_command.py b/v6d2ctx/handle_command.py index 9071e6b..196499b 100644 --- a/v6d2ctx/handle_command.py +++ b/v6d2ctx/handle_command.py @@ -1,6 +1,8 @@ -from typing import Awaitable, Callable +from typing import Callable -from v6d2ctx.context import Context, command_type +from v6d2ctx.context import * + +__all__ = ('handle_command',) async def handle_command(of: Callable[[str], command_type], ctx: Context, name: str, args: list[str]) -> None: diff --git a/v6d2ctx/handle_content.py b/v6d2ctx/handle_content.py index 726dcf4..99bfeb5 100644 --- a/v6d2ctx/handle_content.py +++ b/v6d2ctx/handle_content.py @@ -3,9 +3,10 @@ from typing import Callable import discord -from v6d2ctx.handle_args import handle_args +from v6d2ctx.context import * +from v6d2ctx.handle_args import * -from v6d2ctx.context import command_type +__all__ = ('handle_content',) async def handle_content(of: Callable[[str], command_type], message: discord.Message, content: str, prefix: str, client: discord.Client): diff --git a/v6d2ctx/lock_for.py b/v6d2ctx/lock_for.py index 6c6edaf..7e39e5b 100644 --- a/v6d2ctx/lock_for.py +++ b/v6d2ctx/lock_for.py @@ -1,15 +1,23 @@ import asyncio from typing import Hashable -from v6d2ctx.context import Explicit +from v6d2ctx.context import * -locks: dict[Hashable, asyncio.Lock] = {} +__all__ = ('lock_for', 'Locks',) -def lock_for(key: Hashable, error_message: str) -> asyncio.Lock: - if key is None: - raise Explicit(error_message) - if key in locks: - return locks[key] - else: - return locks.setdefault(key, asyncio.Lock()) +class Locks: + def __init__(self) -> None: + self.locks: dict[Hashable, asyncio.Lock] = {} + + def lock_for(self, key: Hashable, error_message: str) -> asyncio.Lock: + if key is None: + raise Explicit(error_message) + if key in self.locks: + return self.locks[key] + else: + return self.locks.setdefault(key, asyncio.Lock()) + + +locks = Locks() +lock_for = locks.lock_for diff --git a/v6d2ctx/pain.py b/v6d2ctx/pain.py index ac6a103..c9fe58e 100644 --- a/v6d2ctx/pain.py +++ b/v6d2ctx/pain.py @@ -5,6 +5,8 @@ import time from rainbowadn.instrument import Instrumentation +__all__ = ('ALog', 'SLog', 'FrameTrace', 'ABlockMonitor') + class ALog(Instrumentation): start = time.time() @@ -52,7 +54,7 @@ class FrameTrace(Instrumentation): class ABlockMonitor: - __task: asyncio.Future + __task: asyncio.Future[None] def __init__(self, *, threshold=0.0, delta=10.0, interval=0.0) -> None: self.threshold = threshold diff --git a/v6d2ctx/serve.py b/v6d2ctx/serve.py index fae67da..a891cc2 100644 --- a/v6d2ctx/serve.py +++ b/v6d2ctx/serve.py @@ -3,6 +3,8 @@ import signal import discord +__all__ = ('serve',) + def serve(main, client: discord.Client, loop: asyncio.AbstractEventLoop): async def aclose():