Skip to content

Commit

Permalink
Classes inherit parent's docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
KristianJensen committed Dec 9, 2015
1 parent 062acc9 commit 4b29264
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
8 changes: 6 additions & 2 deletions optlang/cplex_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from sympy.core.singleton import S
import cplex
from optlang import interface
from optlang.util import inheritdocstring
Zero = S.Zero
One = S.One

Expand Down Expand Up @@ -146,8 +147,8 @@ def _constraint_lb_and_ub_to_cplex_sense_rhs_and_range_value(lb, ub):
return sense, rhs, range_value


@six.add_metaclass(inheritdocstring)
class Variable(interface.Variable):
"""CPLEX variable interface."""

def __init__(self, name, *args, **kwargs):
super(Variable, self).__init__(name, **kwargs)
Expand Down Expand Up @@ -195,8 +196,8 @@ def dual(self):
return None


@six.add_metaclass(inheritdocstring)
class Constraint(interface.Constraint):
"""CPLEX solver interface"""

_INDICATOR_CONSTRAINT_SUPPORT = True

Expand Down Expand Up @@ -308,6 +309,7 @@ def __iadd__(self, other):
return self


@six.add_metaclass(inheritdocstring)
class Objective(interface.Objective):
def __init__(self, *args, **kwargs):
super(Objective, self).__init__(*args, **kwargs)
Expand All @@ -327,6 +329,7 @@ def __setattr__(self, name, value):
super(Objective, self).__setattr__(name, value)


@six.add_metaclass(inheritdocstring)
class Configuration(interface.MathematicalProgrammingConfiguration):

def __init__(self, lp_method='primal', tolerance=1e-9, presolve=False, verbosity=0, timeout=None,
Expand Down Expand Up @@ -500,6 +503,7 @@ def qp_method(self, value):
self._qp_method = value


@six.add_metaclass(inheritdocstring)
class Model(interface.Model):
def __init__(self, problem=None, *args, **kwargs):

Expand Down
12 changes: 8 additions & 4 deletions optlang/glpk_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from sympy.core.add import _unevaluated_Add
from sympy.core.mul import _unevaluated_Mul

from optlang.util import inheritdocstring

import logging
log = logging.getLogger(__name__)

Expand Down Expand Up @@ -67,8 +69,8 @@
)


@six.add_metaclass(inheritdocstring)
class Variable(interface.Variable):
"""..."""

def __init__(self, name, index=None, *args, **kwargs):
super(Variable, self).__init__(name, **kwargs)
Expand Down Expand Up @@ -137,8 +139,8 @@ def __setattr__(self, name, value):
glp_set_col_name(self.problem.problem, glp_find_col(self.problem.problem, old_name), str(value))


@six.add_metaclass(inheritdocstring)
class Constraint(interface.Constraint):
"""GLPK solver interface"""

_INDICATOR_CONSTRAINT_SUPPORT = False

Expand Down Expand Up @@ -284,6 +286,7 @@ def __idiv__(self, other):
return self


@six.add_metaclass(inheritdocstring)
class Objective(interface.Objective):
def __init__(self, *args, **kwargs):
super(Objective, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -349,8 +352,8 @@ def __idiv__(self, other):
return self


@six.add_metaclass(inheritdocstring)
class Configuration(interface.MathematicalProgrammingConfiguration):
"""docstring for Configuration"""

def __init__(self, presolve=False, verbosity=0, timeout=None, *args, **kwargs):
super(Configuration, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -433,8 +436,9 @@ def timeout(self, value):
self._set_timeout(value)
self._timeout = value


@six.add_metaclass(inheritdocstring)
class Model(interface.Model):
"""GLPK solver interface"""

def __init__(self, problem=None, *args, **kwargs):

Expand Down
15 changes: 15 additions & 0 deletions optlang/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

log = logging.getLogger(__name__)
import tempfile
import inspect
from subprocess import check_output


Expand Down Expand Up @@ -134,6 +135,20 @@ def list_available_solvers():
return solvers


def inheritdocstring(name, bases, attrs):
"""Use as metaclass to inherit parent class' docstring.
From http://stackoverflow.com/questions/13937500/inherit-a-parent-class-docstring-as-doc-attribute"""
if '__doc__' not in attrs or not attrs["__doc__"]:
# create a temporary 'parent' to (greatly) simplify the MRO search
temp = type('temporaryclass', bases, {})
for cls in inspect.getmro(temp):
if cls.__doc__ is not None:
attrs['__doc__'] = cls.__doc__
break

return type(name, bases, attrs)


if __name__ == '__main__':
from swiglpk import glp_create_prob, glp_read_lp, glp_get_num_rows

Expand Down

0 comments on commit 4b29264

Please sign in to comment.