Skip to content

Commit

Permalink
switch .format() to f' '
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Jul 10, 2023
1 parent 73b081d commit eb13ff7
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions ortools/sat/python/cp_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,17 +602,15 @@ def __init__(self, expressions, constant=0):
raise TypeError("Not an linear expression: " + str(x))

def __str__(self):
exprs_str = " + ".join(map(repr, self.__expressions))
if self.__constant == 0:
return "({})".format(" + ".join(map(str, self.__expressions)))
return f"({exprs_str})"
else:
return "({} + {})".format(
" + ".join(map(str, self.__expressions)), self.__constant
)
return f"({exprs_str} + {self.__constant})"

def __repr__(self):
return "SumArray({}, {})".format(
", ".join(map(repr, self.__expressions)), self.__constant
)
exprs_str = ", ".join(map(repr, self.__expressions))
return f"SumArray({exprs_str}, {self.__constant})"

def Expressions(self):
return self.__expressions
Expand Down Expand Up @@ -654,29 +652,27 @@ def __str__(self):
elif not output and cmh.is_minus_one(coeff):
output = "-" + str(expr)
elif not output:
output = "{} * {}".format(coeff, str(expr))
output = f"{coeff} * {expr}"
elif cmh.is_one(coeff):
output += " + {}".format(str(expr))
output += f" + {expr}"
elif cmh.is_minus_one(coeff):
output += " - {}".format(str(expr))
output += f" - {expr}"
elif coeff > 1:
output += " + {} * {}".format(coeff, str(expr))
output += f" + {coeff} * {expr}"
elif coeff < -1:
output += " - {} * {}".format(-coeff, str(expr))
output += f" - {-coeff} * {expr}"
if self.__constant > 0:
output += " + {}".format(self.__constant)
output += f" + {self.__constant}"
elif self.__constant < 0:
output += " - {}".format(-self.__constant)
output += f" - {-self.__constant}"
if output is None:
output = "0"
return output

def __repr__(self):
return "WeightedSum([{}], [{}], {})".format(
", ".join(map(repr, self.__expressions)),
", ".join(map(repr, self.__coefficients)),
self.__constant,
)
exprs_str = ", ".join(map(repr, self.__expressions))
coeffs_str = ", ".join(map(repr, self.__coefficients))
return f"WeightedSum([{exprs_str}], [{coeffs_str}], {self.__constant})"

def Expressions(self):
return self.__expressions
Expand Down Expand Up @@ -2266,10 +2262,7 @@ def _SetObjective(self, obj: ObjLinearExprT, minimize: bool):
else:
self.__model.objective.scaling_factor = -1
self.__model.objective.offset = -constant
for (
v,
c,
) in coeffs_map.items():
for v, c in coeffs_map.items():
self.__model.objective.coeffs.append(c)
if minimize:
self.__model.objective.vars.append(v.Index())
Expand All @@ -2278,10 +2271,7 @@ def _SetObjective(self, obj: ObjLinearExprT, minimize: bool):
else:
self.__model.floating_point_objective.maximize = not minimize
self.__model.floating_point_objective.offset = constant
for (
v,
c,
) in coeffs_map.items():
for v, c in coeffs_map.items():
self.__model.floating_point_objective.coeffs.append(c)
self.__model.floating_point_objective.vars.append(v.Index())
elif cmh.is_integral(obj):
Expand Down

0 comments on commit eb13ff7

Please sign in to comment.