Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 100 additions & 3 deletions pulp/pulp.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,56 @@ def fromDataclass(cls, mps: mpslp.MPSVariable):
return var

def toDict(self) -> dict[str, Any]:
"""
Exports a variable into a dict with its relevant information.

:return: a :py:class:`dict` with the variable information
:rtype: :dict
"""
return dataclasses.asdict(self.toDataclass())

to_dict = toDict
def to_dict(self) -> dict[str, Any]:
"""
Exports a variable into a dict with its relevant information.

This method is deprecated and :py:class:`LpVariable.toDict` should be used instead.

:return: a :py:class:`dict` with the variable information
:rtype: :dict
"""
warnings.warn(
"LpVariable.to_dict is deprecated, use LpVariable.toDict instead",
category=DeprecationWarning,
)
return self.toDict()

@classmethod
def fromDict(cls, data: dict[str, Any]):
"""
Initializes a variable object from information that comes from a dict.

:param data: a dict with the variable information
:return: a :py:class:`LpVariable`
:rtype: :LpVariable
"""
return cls.fromDataclass(mpslp.MPSVariable.fromDict(data))

@classmethod
def from_dict(cls, data: dict[str, Any]):
"""
Initializes a variable object from information that comes from a dict.

This method is deprecated and :py:class:`LpVariable.fromDict` should be used instead.

:param data: a dict with the variable information
:return: a :py:class:`LpVariable`
:rtype: :LpVariable
"""
warnings.warn(
"LpVariable.from_dict is deprecated, use LpVariable.fromDict instead",
category=DeprecationWarning,
)
return cls.fromDataclass(mpslp.MPSVariable.fromDict(data))

def add_expression(self, e):
self.expression = e
Expand Down Expand Up @@ -1004,6 +1051,30 @@ def toDataclass(self) -> list[mpslp.MPSCoefficient]:
"""
return [mpslp.MPSCoefficient(name=k.name, value=v) for k, v in self.items()]

def toDict(self) -> list[dict[str, Any]]:
"""
exports the :py:class:`LpAffineExpression` into a list of dictionaries with the coefficients
it does not export the constant

:return: list of dictionaries with the coefficients
:rtype: list
"""
return [{"name": k.name, "value": v} for k, v in self.items()]

def to_dict(self) -> list[dict[str, Any]]:
"""
exports the :py:class:`LpAffineExpression` into a list of dictionaries with the coefficients
it does not export the constant

:return: list of dictionaries with the coefficients
:rtype: list
"""
warnings.warn(
"LpAffineExpression.to_dict is deprecated, use LpAffineExpression.toDataclass instead",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here it should say: "... use LpAffineExpression.toDict instead"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

category=DeprecationWarning,
)
return self.toDict()


class LpConstraint:
"""An LP constraint"""
Expand Down Expand Up @@ -1531,10 +1602,25 @@ def fromDataclass(cls, mps: mpslp.MPS) -> tuple[dict[str, LpVariable], LpProblem
def toDict(self):
return dataclasses.asdict(self.toDataclass())

def to_dict(self):
warnings.warn(
"LpProblem.to_dict is deprecated, use LpProblem.toDict instead",
category=DeprecationWarning,
)
return self.toDict()

@classmethod
def fromDict(cls, data: dict[Any, Any]):
return cls.fromDataclass(mpslp.MPS.fromDict(data))

@classmethod
def from_dict(cls, data: dict[Any, Any]):
warnings.warn(
"LpProblem.from_dict is deprecated, use LpProblem.fromDict instead",
category=DeprecationWarning,
)
return cls.fromDict(data)

def toJson(self, filename: str, *args: Any, **kwargs: Any):
"""
Creates a json file from the LpProblem information
Expand All @@ -1547,7 +1633,12 @@ def toJson(self, filename: str, *args: Any, **kwargs: Any):
with open(filename, "w") as f:
json.dump(self.toDict(), f, *args, **kwargs)

to_json = toJson
def to_json(self, filename: str, *args: Any, **kwargs: Any):
warnings.warn(
"LpProblem.to_json is deprecated, use LpProblem.toJson instead",
category=DeprecationWarning,
)
return self.toJson(filename, *args, **kwargs)

@classmethod
def fromJson(cls, filename: str) -> tuple[dict[str, LpVariable], LpProblem]:
Expand All @@ -1562,7 +1653,13 @@ def fromJson(cls, filename: str) -> tuple[dict[str, LpVariable], LpProblem]:
data = json.load(f)
return cls.fromDict(data)

from_json = fromJson
@classmethod
def from_json(cls, filename: str):
warnings.warn(
"LpProblem.from_json is deprecated, use LpProblem.fromJson instead",
category=DeprecationWarning,
)
return cls.fromJson(filename)

@classmethod
def fromMPS(
Expand Down
Loading