better __all__

This commit is contained in:
AF 2022-12-28 07:37:52 +00:00
parent 06db9220f9
commit 18001ff340
8 changed files with 39 additions and 46 deletions

View File

@ -1,6 +1,9 @@
from typing import Callable, Generic, TypeVar from typing import Callable, Generic, TypeVar
from .context import Implicit from .context import *
__all__ = ('AtOf',)
K = TypeVar('K') K = TypeVar('K')
V = TypeVar('V') V = TypeVar('V')

View File

@ -1,10 +1,10 @@
import asyncio
import time
from io import StringIO from io import StringIO
from typing import Awaitable, Callable, Optional, Union from typing import Awaitable, Callable, Optional, Union
import discord import discord
__all__ = ('usertype', 'Context', 'escape', 'command_type', 'Explicit', 'Implicit')
usertype = Union[discord.abc.User, discord.user.BaseUser, discord.Member, discord.User] usertype = Union[discord.abc.User, discord.user.BaseUser, discord.Member, discord.User]
@ -57,30 +57,3 @@ class Explicit(Exception):
class Implicit(Exception): class Implicit(Exception):
pass 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'])

View File

@ -2,8 +2,10 @@ from typing import Callable
import discord import discord
from v6d2ctx.context import Context, Explicit, Implicit, command_type from v6d2ctx.context import *
from v6d2ctx.handle_command import handle_command 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): async def handle_args(of: Callable[[str], command_type], message: discord.Message, args: list[str], client: discord.Client):

View File

@ -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: async def handle_command(of: Callable[[str], command_type], ctx: Context, name: str, args: list[str]) -> None:

View File

@ -3,9 +3,10 @@ from typing import Callable
import discord 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): async def handle_content(of: Callable[[str], command_type], message: discord.Message, content: str, prefix: str, client: discord.Client):

View File

@ -1,15 +1,23 @@
import asyncio import asyncio
from typing import Hashable 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: class Locks:
if key is None: def __init__(self) -> None:
raise Explicit(error_message) self.locks: dict[Hashable, asyncio.Lock] = {}
if key in locks:
return locks[key] def lock_for(self, key: Hashable, error_message: str) -> asyncio.Lock:
else: if key is None:
return locks.setdefault(key, asyncio.Lock()) 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

View File

@ -5,6 +5,8 @@ import time
from rainbowadn.instrument import Instrumentation from rainbowadn.instrument import Instrumentation
__all__ = ('ALog', 'SLog', 'FrameTrace', 'ABlockMonitor')
class ALog(Instrumentation): class ALog(Instrumentation):
start = time.time() start = time.time()
@ -52,7 +54,7 @@ class FrameTrace(Instrumentation):
class ABlockMonitor: class ABlockMonitor:
__task: asyncio.Future __task: asyncio.Future[None]
def __init__(self, *, threshold=0.0, delta=10.0, interval=0.0) -> None: def __init__(self, *, threshold=0.0, delta=10.0, interval=0.0) -> None:
self.threshold = threshold self.threshold = threshold

View File

@ -3,6 +3,8 @@ import signal
import discord import discord
__all__ = ('serve',)
def serve(main, client: discord.Client, loop: asyncio.AbstractEventLoop): def serve(main, client: discord.Client, loop: asyncio.AbstractEventLoop):
async def aclose(): async def aclose():