From 4bc747858ba10add5a87f69efab36a26e9b1ad94 Mon Sep 17 00:00:00 2001 From: Edward Burnell Date: Fri, 14 Aug 2015 22:35:48 -0700 Subject: [PATCH 1/2] get to a low level of python3 compatibility --- gpkit/_mosek/cli_expopt.py | 4 ++-- gpkit/geometric_program.py | 4 ++-- gpkit/model.py | 10 +++++----- gpkit/nomial_data.py | 3 ++- gpkit/nomials.py | 5 +++++ gpkit/solution_array.py | 3 ++- gpkit/tools.py | 2 +- gpkit/variables.py | 6 ++++++ 8 files changed, 25 insertions(+), 12 deletions(-) diff --git a/gpkit/_mosek/cli_expopt.py b/gpkit/_mosek/cli_expopt.py index afe24825a..4fcc1de6e 100644 --- a/gpkit/_mosek/cli_expopt.py +++ b/gpkit/_mosek/cli_expopt.py @@ -101,8 +101,8 @@ def imize(c, A, p_idxs, *args, **kwargs): for x in t_j_Atj]) log = check_output(["mskexpopt", filename]) - for logline in log.split("\n"): - print logline + for logline in log.split(b"\n"): + print(logline) with open(filename+".sol") as f: assert_line(f, "PROBLEM STATUS : PRIMAL_AND_DUAL_FEASIBLE\n") diff --git a/gpkit/geometric_program.py b/gpkit/geometric_program.py index a9043480e..85a2e21f2 100644 --- a/gpkit/geometric_program.py +++ b/gpkit/geometric_program.py @@ -58,7 +58,7 @@ def __init__(self, cost, constraints, verbosity=1): p_idxs = [] m_idxs = [] for i, p_len in enumerate(self.k): - m_idxs.append(range(len(p_idxs), len(p_idxs) + p_len)) + m_idxs.append(list(range(len(p_idxs), len(p_idxs) + p_len))) p_idxs += [i]*p_len self.p_idxs = np.array(p_idxs) self.m_idxs = m_idxs @@ -215,7 +215,7 @@ def _almost_equal(num1, num2): "local almost equal test" return num1 == num2 or abs((num1 - num2) / (num1 + num2)) < tol cost = sol["cost"] - primal_sol = np.log(sol["variables"].values()) + primal_sol = np.log(list(sol["variables"].values())) nu = sol["sensitivities"]["monomials"] la = sol["sensitivities"]["posynomials"] A = self.A.tocsr() diff --git a/gpkit/model.py b/gpkit/model.py index 2d5bfd92d..4fe2e1226 100644 --- a/gpkit/model.py +++ b/gpkit/model.py @@ -291,7 +291,7 @@ def _solve(self, programType, solver, verbosity, skipfailures, if sweep: if len(sweep) == 1: - sweep_grids = np.array(sweep.values()) + sweep_grids = np.array(list(sweep.values())) else: sweep_grids = np.meshgrid(*list(sweep.values())) @@ -351,7 +351,7 @@ def solve_pass(i): solution.toarray() self.solution = solution # NOTE: SIDE EFFECTS if verbosity > 0: - print solution.table() + print(solution.table()) return solution # TODO: add sweepgp(index)? @@ -433,7 +433,7 @@ def feasibility(self, max_gp = self.gp().feasibility_search("max") infeasibility = max_gp.solve(verbosity=verbosity-1)["cost"] if verbosity > 0: - print " overall : %.2f" % infeasibility + print(" overall : %.2f" % infeasibility) feasibilities["overall"] = infeasibility if "constraints" in search: @@ -442,7 +442,7 @@ def feasibility(self, result = prod_gp.solve(verbosity=verbosity-1) con_infeas = [result["variables"][sv] for sv in slackvars] if verbosity > 0: - print " constraints : %s" % con_infeas + print(" constraints : %s" % con_infeas) feasibilities["constraints"] = con_infeas constants = get_constants(unsubbed, allsubs) @@ -479,7 +479,7 @@ def feasibility(self, var_infeas = {addvalue[constvarkeys[i]]: feasible_constvalues[i] for i in np.where(changed_vals)} if verbosity > 0: - print " constants : %s" % var_infeas + print(" constants : %s" % var_infeas) feasibilities["constants"] = var_infeas if verbosity > 0: diff --git a/gpkit/nomial_data.py b/gpkit/nomial_data.py index b06904e11..786b52824 100644 --- a/gpkit/nomial_data.py +++ b/gpkit/nomial_data.py @@ -38,9 +38,10 @@ def __init__(self, exps=None, cs=None, nomials=None, simplify=True): if "value" in vk.descr} # confirm lengths before calling zip assert len(self.exps) == len(self.cs) - self._hash = hash(tuple(zip(self.exps, tuple(self.cs)))) def __hash__(self): + if not hasattr(self, "_hash"): + self._hash = hash(tuple(zip(self.exps, tuple(self.cs)))) return self._hash def __repr__(self): diff --git a/gpkit/nomials.py b/gpkit/nomials.py index bca247a8f..cf3832ceb 100644 --- a/gpkit/nomials.py +++ b/gpkit/nomials.py @@ -118,6 +118,11 @@ def __init__(self, exps=None, cs=1, require_positive=True, simplify=True, self.exp = self.exps[0] self.c = self.cs[0] + def __hash__(self): + if not hasattr(self, "_hash"): + self._hash = hash(tuple(zip(self.exps, tuple(self.cs)))) + return self._hash + @property def value(self): """Self, with values substituted for variables that have values diff --git a/gpkit/solution_array.py b/gpkit/solution_array.py index d285d375c..1cb60e828 100644 --- a/gpkit/solution_array.py +++ b/gpkit/solution_array.py @@ -182,7 +182,8 @@ def results_table(data, title, minval=0, printunits=True, fixedcols=True, if not fixedcols: maxlens = [maxlens[0], 0, 0, 0] dirs = ['>', '<', '<', '<'] - assert len(dirs) == len(maxlens) # check lengths before using zip + # check lengths before using zip + assert len(list(dirs)) == len(list(maxlens)) fmts = ['{0:%s%s}' % (direc, L) for direc, L in zip(dirs, maxlens)] lines = [[fmt.format(s) for fmt, s in zip(fmts, line)] for line in lines] lines = [title] + ["-"*len(title)] + [''.join(l) for l in lines] + [""] diff --git a/gpkit/tools.py b/gpkit/tools.py index f1062ab4f..01bce5d0f 100644 --- a/gpkit/tools.py +++ b/gpkit/tools.py @@ -35,7 +35,7 @@ def te_exp_minus1(posy, nterm): raise ValueError("Unexpected number of terms, nterm=%s" % nterm) res = 0 factorial_denom = 1 - for i in xrange(1, nterm + 1): + for i in range(1, nterm + 1): factorial_denom *= i res += posy**i / factorial_denom return res diff --git a/gpkit/variables.py b/gpkit/variables.py index cbc2d23e6..5d28bf900 100644 --- a/gpkit/variables.py +++ b/gpkit/variables.py @@ -3,6 +3,7 @@ from .varkey import VarKey from .nomials import Monomial +from .nomial_data import NomialData from .posyarray import PosyArray from .small_classes import Strings, Numbers, Quantity from .small_scripts import is_sweepvar @@ -45,6 +46,11 @@ def __init__(self, *args, **descr): Monomial.__init__(self, **descr) self.__class__ = Variable + + def __hash__(self): + if not hasattr(self, "_hash"): + self._hash = hash(tuple(zip(self.exps, tuple(self.cs)))) + return self._hash @property def varkey(self): From 68eddfc8e541857e0e52722a9c2d24f9a1c3dabf Mon Sep 17 00:00:00 2001 From: Edward Burnell Date: Fri, 14 Aug 2015 22:48:28 -0700 Subject: [PATCH 2/2] reduce hash duplication --- gpkit/nomial_data.py | 4 ++-- gpkit/nomials.py | 5 +---- gpkit/variables.py | 7 ++----- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/gpkit/nomial_data.py b/gpkit/nomial_data.py index 786b52824..c51659f56 100644 --- a/gpkit/nomial_data.py +++ b/gpkit/nomial_data.py @@ -36,11 +36,11 @@ def __init__(self, exps=None, cs=None, nomials=None, simplify=True): self.varlocs, self.varstrs = locate_vars(self.exps) self.values = {vk: vk.descr["value"] for vk in self.varlocs if "value" in vk.descr} - # confirm lengths before calling zip - assert len(self.exps) == len(self.cs) def __hash__(self): if not hasattr(self, "_hash"): + # confirm lengths before calling zip + assert len(self.exps) == len(self.cs) self._hash = hash(tuple(zip(self.exps, tuple(self.cs)))) return self._hash diff --git a/gpkit/nomials.py b/gpkit/nomials.py index cf3832ceb..b8fc27176 100644 --- a/gpkit/nomials.py +++ b/gpkit/nomials.py @@ -118,10 +118,7 @@ def __init__(self, exps=None, cs=1, require_positive=True, simplify=True, self.exp = self.exps[0] self.c = self.cs[0] - def __hash__(self): - if not hasattr(self, "_hash"): - self._hash = hash(tuple(zip(self.exps, tuple(self.cs)))) - return self._hash + __hash__ = NomialData.__hash__ @property def value(self): diff --git a/gpkit/variables.py b/gpkit/variables.py index 5d28bf900..fc8793ce4 100644 --- a/gpkit/variables.py +++ b/gpkit/variables.py @@ -46,11 +46,8 @@ def __init__(self, *args, **descr): Monomial.__init__(self, **descr) self.__class__ = Variable - - def __hash__(self): - if not hasattr(self, "_hash"): - self._hash = hash(tuple(zip(self.exps, tuple(self.cs)))) - return self._hash + + __hash__ = NomialData.__hash__ @property def varkey(self):