Hi! I want to share my new library: transfunctions.
It solves the problem of code duplication that arose as a result of the split between the sync and async halves of the Python ecosystem.
The most interesting part of the library is the superfunctions. They allow you to create functions that are both regular and asynchronous:
from asyncio import run
from transfunctions import (
superfunction,
sync_context,
async_context,
generator_context,
await_it,
)
@superfunction
def my_superfunction():
print('so, ', end='')
with sync_context:
print("it's just usual function!")
with async_context:
print("it's an async function!")
~my_superfunction()
#> so, it's just usual function!
run(my_superfunction())
#> so, it's an async function!
So, we get functions where the client can decide for himself whether to use them as synchronous or asynchronous. Under the hood, AST-level code generation is used here, which compiles synchronous and asynchronous versions of the original function on the fly.