Skip to content
Permalink
Browse files

replacing capturer with abduct where possible and making capturer opt…

…ional
  • Loading branch information
jonrkarr committed Aug 31, 2018
1 parent 1bfae67 commit b4058be9979e709dbec64a700630f9da7d846a23
@@ -9,6 +9,10 @@

import abc
import attrdict
try:
import capturer
except ModuleNotFoundError: # pragma: no cover
capturer = None # pragma: no cover
import enum
import numpy
import os
@@ -129,9 +133,14 @@ class ExportFormat(enum.Enum):
try:
import gurobipy
try:
import capturer
with capturer.CaptureOutput(merged=False, relay=False):
gurobipy.Model()
if capturer:
capture_output = capturer.CaptureOutput(merged=False, relay=False)
capture_output.start_capture()

gurobipy.Model()

if capturer:
capture_output.finish_capture()
ENABLED_SOLVERS.append(Solver.gurobi)
except gurobipy.GurobiError: # pragma: no cover
pass # pragma: no cover
@@ -11,7 +11,7 @@
SolveOptions, Solver, StatusCode, VariableType, Verbosity,
Constraint, LinearTerm, Model, QuadraticTerm, Term, Variable, Result, ConvOptError,
SolverModel)
import capturer
import abduct
try:
import gurobipy
except ImportError: # pragma: no cover
@@ -37,7 +37,7 @@ def load(self, conv_opt_model):
an objective has an unsupported term, a constraint has
an unsupported term, or a constraint is unbounded
"""
with capturer.CaptureOutput(relay=False):
with abduct.captured(abduct.out(tee=False)):
solver_model = gurobipy.Model(conv_opt_model.name or '')

# variables
@@ -148,7 +148,7 @@ def solve(self):
if self._options.verbosity.value >= Verbosity.status.value:
model.tune()
else:
with capturer.CaptureOutput(relay=False):
with abduct.captured(abduct.out(tee=False)):
model.tune()

# set presolve
@@ -218,9 +218,9 @@ def get_stats(self):
"""
model = self._model

with capturer.CaptureOutput(merged=False, relay=False) as captured:
with abduct.captured(abduct.out(tee=False)) as stdout:
model.printStats()
stats = captured.stdout.get_text()
stats = stdout.getvalue()

return {
'stats': stats,
@@ -16,7 +16,10 @@
Constraint, LinearTerm, Model, Term, Variable, Result, ConvOptError,
SolverModel)
import attrdict
import capturer
try:
import capturer
except ModuleNotFoundError: # pragma: no cover
capturer = None # pragma: no cover
import copy
try:
from qminospy import qwarmLP
@@ -256,7 +259,7 @@ def solve(self):
model.inform = numpy.array(0)

# solve model
if self._options.verbosity == Verbosity.status.off:
if capturer and self._options.verbosity == Verbosity.status.off:
capture_output = capturer.CaptureOutput(merged=False, relay=False)
capture_output.start_capture()

@@ -269,7 +272,7 @@ def solve(self):
int_opt_vals, float_opt_vals,
nstropts=num_str_opts, nintopts=num_int_opts, nrealopts=num_float_opts)

if self._options.verbosity == Verbosity.status.off:
if capturer and self._options.verbosity == Verbosity.status.off:
capture_output.finish_capture()

tmp = model.inform
@@ -11,7 +11,10 @@
SolveOptions, Solver, StatusCode, VariableType, Verbosity,
Constraint, LinearTerm, Model, QuadraticTerm, Term, Variable, Result, ConvOptError,
SolverModel)
import capturer
try:
import capturer
except ModuleNotFoundError: # pragma: no cover
capturer = None # pragma: no cover
import numpy
import sys
try:
@@ -142,15 +145,18 @@ def solve(self):

# presolve
if self._options.presolve == Presolve.on:
if self._options.verbosity == Verbosity.off:
with capturer.CaptureOutput(merged=False, relay=False) as captured:
model.presolve()
elif self._options.verbosity == Verbosity.error:
with capturer.CaptureOutput(merged=False, relay=False) as captured:
model.presolve()
err = captured.stderr.get_text()
if err:
sys.stderr.write(err)
if capturer and self._options.verbosity.value <= Verbosity.error.value:
capture_output = capturer.CaptureOutput(merged=False, relay=False)
capture_output.start_capture()

model.presolve()

if capturer and self._options.verbosity.value <= Verbosity.error.value:
capture_output.finish_capture()

err = capture_output.stderr.get_text()
if err and self._options.verbosity == Verbosity.error:
sys.stderr.write(err)
else:
model.presolve()
elif self._options.presolve != Presolve.off:
@@ -1,3 +1,6 @@
[capture_output]
capturer

[cbc]
git+https://github.com/jjhelmus/CyLP.git@py3#egg=cylp-0.7.4

@@ -1,5 +1,5 @@
abduct
attrdict
capturer
cython
enum34
mock
@@ -214,7 +214,7 @@ def test_verbose_presolve(self):
with capturer.CaptureOutput(merged=False, relay=False) as captured:
model.solve(options=options)

self.assertRegexpMatches(captured.stdout.get_text(), '^Maximizing LP ')
self.assertRegexpMatches(captured.stdout.get_text(), 'Maximizing LP ')
self.assertEqual(captured.stderr.get_text(), '')

options = conv_opt.SolveOptions(solver=conv_opt.Solver.xpress,

0 comments on commit b4058be

Please sign in to comment.
You can’t perform that action at this time.