Skip to content

Create awaitme_ismeteren_yurtcu.py #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Week05/awaitme_ismeteren_yurtcu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import asyncio

def awaitme(f):
"""
A decorator to handle both synchronous and asynchronous functions seamlessly.

If the decorated function is asynchronous, it awaits the function's result.
If the decorated function is synchronous, it directly returns the result.

Args:
f (function): The function to be decorated. It can be either synchronous or asynchronous.

Returns:
function: An asynchronous wrapper that handles both sync and async functions gracefully.
"""
async def wrapper(*args, **kwargs):
"""
Wrapper function that calls the original function and checks whether it's a coroutine.

If the result is a coroutine, it awaits the result. Otherwise, it returns the result directly.

Args:
*args: Positional arguments to pass to the original function.
**kwargs: Keyword arguments to pass to the original function.

Returns:
Any: The result of the original function, either awaited (if async) or direct (if sync).
"""
fn = f(*args, **kwargs)
if asyncio.iscoroutine(fn):
return await fn
return fn

return wrapper
Loading