Skip to content

Commit

Permalink
feat: optimize get_flag_name_c (#403)
Browse files Browse the repository at this point in the history
* feat: optimize get_flag_name_c.py

* chore: `black .`

* fix: circular import

* fix: .pxd discrepancy

* fix: cant assign to const

* fix: circular import

* fix: circular import

* chore: `black .`

* fix: NameError _await

* fix: incorrect definition _await

* chore: `black .`

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
BobTheBuidler and github-actions[bot] authored Nov 20, 2024
1 parent 2ef1091 commit 067aa6a
Show file tree
Hide file tree
Showing 15 changed files with 390 additions and 39 deletions.
3 changes: 2 additions & 1 deletion a_sync/a_sync/_helpers.pxd
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
cpdef object _await(object awaitable)
cpdef object get_event_loop()
cdef object _await(object awaitable)
cdef object _asyncify(object func, object executor)
4 changes: 3 additions & 1 deletion a_sync/a_sync/_helpers.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from a_sync._typing import *
from a_sync import exceptions as exceptions
def _await(awaitable: Awaitable[T]) -> T:...

def get_event_loop() -> asyncio.AbstractEventLoop: ...
def _await(awaitable: Awaitable[T]) -> T: ...
16 changes: 13 additions & 3 deletions a_sync/a_sync/_helpers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ and converting synchronous functions to asynchronous ones.
import asyncio
import functools

import a_sync.asyncio
from a_sync import exceptions
from a_sync._typing import *

cdef object get_event_loop = a_sync.asyncio.get_event_loop

cpdef object _await(object awaitable):
cpdef object get_event_loop():
cdef object loop
try:
return asyncio.get_event_loop()
except RuntimeError as e: # Necessary for use with multi-threaded applications.
if not str(e).startswith("There is no current event loop in thread"):
raise
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop


cdef object _await(object awaitable):
"""
Await an awaitable object in a synchronous context.
Expand Down
2 changes: 1 addition & 1 deletion a_sync/a_sync/_kwargs.pxd
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cdef object get_flag_name_c(dict kwargs)
cdef str get_flag_name_c(dict kwargs)
cdef bint is_sync(str flag, dict kwargs, bint pop_flag)
6 changes: 3 additions & 3 deletions a_sync/a_sync/_kwargs.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def get_flag_name(kwargs: dict) -> Optional[str]:
>>> get_flag_name({})
None
"""
return get_flag_name_c(kwargs)
return get_flag_name_c(kwargs) or None


cdef object get_flag_name_c(dict kwargs):
cdef str get_flag_name_c(dict kwargs):
cdef list present_flags = [flag for flag in VIABLE_FLAGS if flag in kwargs]
cdef uint8_t flags_count = len(present_flags)
if flags_count == 0:
return None
return "" # indicates no flag is present
elif flags_count == 1:
return present_flags[0]
raise exceptions.TooManyFlags("kwargs", present_flags)
Expand Down
8 changes: 4 additions & 4 deletions a_sync/a_sync/abstract.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ class ASyncABC(metaclass=ASyncMeta):
False
"""

cdef object flag
if flag := _kwargs.get_flag_name_c(kwargs):
cdef str flag = _kwargs.get_flag_name_c(kwargs)
if flag:
return _kwargs.is_sync(<str>flag, kwargs, pop_flag=True)

cdef ShouldAwaitCache cache
Expand Down Expand Up @@ -176,9 +176,9 @@ class ASyncABC(metaclass=ASyncMeta):
cls.__name__,
)

cdef object flag
cdef str flag = _kwargs.get_flag_name_c(kwargs)
cdef bint sync
if flag := _kwargs.get_flag_name_c(kwargs):
if flag:
sync = _kwargs.is_sync(<str>flag, kwargs, pop_flag=False) # type: ignore [arg-type]
logger.debug(
"kwargs indicate the new instance created with args %s %s is %ssynchronous",
Expand Down
4 changes: 2 additions & 2 deletions a_sync/a_sync/function.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ cdef bint _run_sync(object function, dict kwargs):
See Also:
- :func:`_kwargs.get_flag_name`
"""
cdef object flag
if flag := _kwargs.get_flag_name_c(kwargs):
cdef str flag = _kwargs.get_flag_name_c(kwargs)
if flag:
# If a flag was specified in the kwargs, we will defer to it.
return _kwargs.is_sync(<str>flag, kwargs, pop_flag=True)
else:
Expand Down
4 changes: 2 additions & 2 deletions a_sync/a_sync/method.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -769,8 +769,8 @@ class ASyncBoundMethod(ASyncFunction[P, T], Generic[I, P, T]):
>>> bound_method = ASyncBoundMethod(instance, my_function, True)
>>> should_await = bound_method._should_await(kwargs)
"""
cdef object flag
if flag := _kwargs.get_flag_name_c(kwargs):
cdef str flag = _kwargs.get_flag_name_c(kwargs)
if flag:
return _kwargs.is_sync(<str>flag, kwargs, pop_flag=True) # type: ignore [arg-type]
elif self.default:
return self.default == "sync"
Expand Down
2 changes: 1 addition & 1 deletion a_sync/asyncio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
- `asyncio <https://docs.python.org/3/library/asyncio.html>`_: The standard asyncio library documentation for more details on the original functions.
"""

from a_sync.a_sync._helpers import get_event_loop
from a_sync.asyncio.create_task import create_task
from a_sync.asyncio.as_completed import as_completed
from a_sync.asyncio.gather import gather
from a_sync.asyncio.utils import get_event_loop

__all__ = ["create_task", "gather", "as_completed", "get_event_loop"]

Expand Down
12 changes: 0 additions & 12 deletions a_sync/asyncio/utils.py

This file was deleted.

Loading

0 comments on commit 067aa6a

Please sign in to comment.