Skip to content

Commit 81ce8ea

Browse files
committed
remove content that was accidentally included
1 parent 1fb86be commit 81ce8ea

File tree

1 file changed

+1
-68
lines changed

1 file changed

+1
-68
lines changed

doc/newsletters/2023/WEEK_26.rst

+1-68
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ Ellipses in Python serve multiple purposes, ranging from placeholders for unwrit
382382
7. 🧩 Understanding `*args` and `**kwargs` in Python
383383
*****************************************************
384384

385-
In Python, \*args and \**kwargs are special syntaxes used to pass a variable number of arguments to functions. These notations provide flexibility when working with functions that can accept an arbitrary number of arguments.
385+
In Python, \*args and \**kwargs are special syntaxes used to pass a variable number of arguments to functions. These notations provide flexibility when working with functions that can accept an arbitrary number of arguments.
386386
This article dives into the details of \*args and \**kwargs and explores their uses and benefits.
387387

388388
**🌟 *args: Variable-Length Arguments**
@@ -648,74 +648,7 @@ Async and await provide several advantages:
648648

649649
non-blocking I/O, async and await help create responsive applications that can handle multiple tasks simultaneously.
650650

651-
**💡 Writing Your Own Async Context Managers**
652651

653-
In addition to writing asynchronous functions, Python also provides the ability to create async context managers using the `async with` statement. Async context managers are useful for managing resources that require asynchronous setup and teardown.
654-
655-
To create an async context manager, you need to define an asynchronous context manager class that implements the `__aenter__()` and `__aexit__()` methods. These methods specify the setup and teardown actions for acquiring and releasing resources.
656-
657-
Here's an example of an async context manager for managing a database connection:
658-
659-
660-
.. code:: python
661-
662-
import asyncio
663-
664-
class DatabaseConnection:
665-
def __init__(self, connection_string):
666-
self.connection_string = connection_string
667-
668-
async def __aenter__(self):
669-
self.connection = await asyncio.sleep(1) # Simulating asynchronous setup
670-
return self.connection
671-
672-
async def __aexit__(self, exc_type, exc_val, exc_tb):
673-
await asyncio.sleep(1) # Simulating asynchronous teardown
674-
self.connection.close()
675-
676-
async def main():
677-
async with DatabaseConnection("example_connection_string") as connection:
678-
# Perform database operations using the connection
679-
pass
680-
681-
asyncio.run(main())
682-
683-
684-
In this example, the `DatabaseConnection` class is defined as an async context manager by implementing the `__aenter__()` and `__aexit__()` methods. The `__aenter__()` method is responsible for setting up the connection, while the `__aexit__()` method handles the teardown.
685-
686-
**🌟 Decorators that Take Arguments in Async Functions**
687-
688-
Similar to synchronous functions, async functions can also be decorated to modify their behavior. Decorators that take arguments can be particularly useful when working with async functions.
689-
690-
To create decorators that take arguments for async functions, you can follow the same principles as with synchronous functions. The only difference is that the decorator itself needs to be an async function or an async context manager.
691-
692-
Here's an example of a decorator that measures the execution time of an async function:
693-
694-
695-
.. code:: python
696-
697-
import time
698-
import asyncio
699-
700-
def measure_time_async(func):
701-
async def wrapper(*args, **kwargs):
702-
start_time = time.time()
703-
result = await func(*args, **kwargs)
704-
end_time = time.time()
705-
execution_time = end_time - start_time
706-
print(f"Execution time: {execution_time} seconds")
707-
return result
708-
return wrapper
709-
710-
@measure_time_async
711-
async def process_data_async(data):
712-
await asyncio.sleep(1) # Simulating some async processing
713-
return data
714-
715-
asyncio.run(process_data_async("example_data"))
716-
717-
718-
In this example, the `measure_time_async` decorator is defined as a regular function that takes an async function as an argument. It measures the execution time of the async function and prints the result.
719652

720653
**🚀 Unlocking the Power of Async and Await**
721654

0 commit comments

Comments
 (0)