Skip to content

Commit 9f4f530

Browse files
authored
Merge pull request canbula#556 from Furk4nBulut/patch-6
Create awaitme_furkan_bulut.py
2 parents 157aabc + 01f9910 commit 9f4f530

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Week05/awaitme_furkan_bulut.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import asyncio
2+
3+
def awaitme(f):
4+
"""
5+
Decorator to handle synchronous and asynchronous functions.
6+
7+
This decorator allows a function to work seamlessly with both
8+
synchronous and asynchronous code. If the function `f` is a coroutine
9+
(asynchronous), it will await the result. Otherwise, it will treat
10+
it as a regular synchronous function and return the result directly.
11+
12+
:param f: The function to be decorated, which could be either synchronous or asynchronous.
13+
:type f: function
14+
:return: A wrapper function that handles the asynchronous behavior.
15+
:rtype: function
16+
17+
:Example:
18+
19+
#>>> @awaitme
20+
#>>> def sync_func():
21+
#>>> return 42
22+
#>>> sync_func()
23+
42
24+
25+
#>>> @awaitme
26+
#>>> async def async_func():
27+
#>>> return 42
28+
#>>> await async_func()
29+
42
30+
"""
31+
async def _awaitme(*args, **kwargs):
32+
func = f(*args, **kwargs)
33+
if asyncio.iscoroutine(func):
34+
return await func
35+
return func
36+
37+
return _awaitme

0 commit comments

Comments
 (0)