From fb1f1c6926b6e4e121fc530798fdce75b7844d0b Mon Sep 17 00:00:00 2001 From: Roland Date: Fri, 31 Dec 2021 20:33:47 -0700 Subject: [PATCH] Removing duplicate sorting code. --- wrolpi/db.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/wrolpi/db.py b/wrolpi/db.py index 8a1a210d..4f23bf1e 100644 --- a/wrolpi/db.py +++ b/wrolpi/db.py @@ -117,12 +117,14 @@ def get_ranked_models(ranked_ids: List[int], model: Base, session: Session = Non """ Get all objects whose ids are in the `ranked_ids`, order them by their position in `ranked_ids`. """ + + def _(s): + r = s.query(model).filter(model.id.in_(ranked_ids)).all() + r = sorted(r, key=lambda i: ranked_ids.index(i.id)) + return r + if session: - results = session.query(model).filter(model.id.in_(ranked_ids)).all() - results = sorted(results, key=lambda i: ranked_ids.index(i.id)) - return results + return _(session) with get_db_session() as session: - results = session.query(model).filter(model.id.in_(ranked_ids)).all() - results = sorted(results, key=lambda i: ranked_ids.index(i.id)) - return results + return _(session)