From 551363b9b71a122c1934c1c78920d70ac8f844b9 Mon Sep 17 00:00:00 2001 From: Jeremy Saklad Date: Sun, 2 Jan 2022 17:57:53 -0600 Subject: [PATCH] Replace unnecessary variables with lambdas Variables that are only used in one line lend undue complexity to code. Using a one-shot lambda makes the use of a declaration much more obvious. --- bonemarketsolver/objects/bone_market_model.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/bonemarketsolver/objects/bone_market_model.py b/bonemarketsolver/objects/bone_market_model.py index 761d025..540250d 100644 --- a/bonemarketsolver/objects/bone_market_model.py +++ b/bonemarketsolver/objects/bone_market_model.py @@ -13,10 +13,7 @@ class BoneMarketModel(cp_model.CpModel): __slots__: tuple[()] = () def AddAllowedAssignments(self, variables: Iterable[Iterable], tuples_list: Iterable[Iterable]) -> tuple: - # Used for variable names - invocation: Final[str] = repr((variables, tuples_list)) - - intermediate_variables, constraints = zip(*(self.NewIntermediateIntVar(variable, f'{invocation}: {variable}') for variable in variables)) + intermediate_variables, constraints = (lambda invocation : zip(*(self.NewIntermediateIntVar(variable, f'{invocation}: {variable}') for variable in variables)))(repr((variables, tuples_list))) super().AddAllowedAssignments(intermediate_variables, tuples_list) return constraints @@ -108,13 +105,11 @@ Each parameter is interpreted as a BoundedLinearExpression, and a layer of indir if isinstance(expression, cp_model.IntVar) and (lambda domain : domain == [lb, ub] or domain[0] == domain[1])(cp_model.IntVar.Proto(expression).domain): return (expression, ()) else: - intermediate: Final[cp_model.IntVar] = super().NewIntVar(lb, ub, name) - return (intermediate, self.Add(intermediate == expression)) + return (lambda intermediate : (intermediate, self.Add(intermediate == expression)))(super().NewIntVar(lb, ub, name)) @NewIntermediateIntVar.register def _(self, expression: partialmethod, name: str, *, lb: Integral = cp_model.INT32_MIN, ub: Integral = cp_model.INT32_MAX) -> tuple: - intermediate: Final[cp_model.IntVar] = super().NewIntVar(lb, ub, name) - return (intermediate, expression.__get__(self)(intermediate)) + return (lambda intermediate : (intermediate, expression.__get__(self)(intermediate)))(super().NewIntVar(lb, ub, name)) @NewIntermediateIntVar.register def _(self, expression: Integral, *args, **kwargs) -> tuple: