Skip to content
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

Uso de context manager en patrón repetido #29

Open
nico-mac opened this issue Jul 11, 2022 · 0 comments
Open

Uso de context manager en patrón repetido #29

nico-mac opened this issue Jul 11, 2022 · 0 comments

Comments

@nico-mac
Copy link
Member

Si este patrón se repite mucho, demás se podría hacer un context para manejarlo:

# ./utils.py
from contextlib import asynccontextmanager

@asynccontextmanager
async def catch_exc(msg: str, *cleanup_fns: list[Callable]):
    try:
        yield
    except Exception:
          log.error(msg, code, exc_info=True)
          errors.add(code)
          for fn in cleanup_fns:
              fn()

# Ejemplos
with catch_exc(f"Cannot get requirements and syllabus for {code}", db_session.rollback):
   ...

No creo que esta versión que deje (y que no he probado) funcione para todos los casos, pero creo que se podría ver para simplificar el código 🤔

Originally posted by @benjavicente in #19 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant