Skip to content

Commit

Permalink
Merge branch 'main' into ms/handle-missing
Browse files Browse the repository at this point in the history
  • Loading branch information
staadecker committed Mar 27, 2024
2 parents cc858c3 + 2571fec commit 5b8b943
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/pyoframe/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def __init__(self, data: pl.DataFrame):

super().__init__(data)

def sum(self, over: str | Iterable[str]):
def sum(self, over: Union[str, Iterable[str]]):
"""
Examples
--------
Expand Down Expand Up @@ -668,16 +668,16 @@ def __repr__(self) -> str:


@overload
def sum(over: str | Sequence[str], expr: SupportsToExpr): ...
def sum(over: Union[str, Sequence[str]], expr: SupportsToExpr): ...


@overload
def sum(over: SupportsToExpr): ...


def sum(
over: str | Sequence[str] | SupportsToExpr,
expr: SupportsToExpr | None = None,
over: Union[str, Sequence[str], SupportsToExpr],
expr: Optional[SupportsToExpr] = None,
) -> "Expression":
if expr is None:
assert isinstance(over, SupportsMath)
Expand All @@ -693,7 +693,7 @@ def sum(
return expr.to_expr().sum(over)


def sum_by(by: str | Sequence[str], expr: SupportsToExpr) -> "Expression":
def sum_by(by: Union[str, Sequence[str]], expr: SupportsToExpr) -> "Expression":
if isinstance(by, str):
by = [by]
expr = expr.to_expr()
Expand Down
4 changes: 2 additions & 2 deletions src/pyoframe/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from io import TextIOWrapper
from tempfile import NamedTemporaryFile
from pathlib import Path
from typing import TYPE_CHECKING, Iterable, TypeVar
from typing import TYPE_CHECKING, Iterable, Optional, TypeVar, Union

from pyoframe.constants import VAR_KEY
from pyoframe.var_mapping import DEFAULT_MAP, VariableMapping
Expand Down Expand Up @@ -98,7 +98,7 @@ def create_section(iterable: Iterable[T], f, section_header) -> Iterable[T]:


def to_file(
m: "Model", fn: str | Path | None, integer_label="general", use_var_names=False
m: "Model", fn: Optional[Union[str, Path]], integer_label="general", use_var_names=False
) -> Path:
"""
Write out a model to a lp file.
Expand Down
4 changes: 2 additions & 2 deletions src/pyoframe/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Iterable, List
from typing import Any, Iterable, List, Optional
from pyoframe.constants import ObjSense, VType
from pyoframe.constraints import SupportsMath
from pyoframe.model_element import ModelElement
Expand All @@ -18,7 +18,7 @@ class Model:
def __init__(self, name="model"):
self._variables: List[Variable] = []
self._constraints: List[Constraint] = []
self._objective: Objective | None = None
self._objective: Optional[Objective] = None
self.var_map = NamedVariables(self)
self.name = name

Expand Down
3 changes: 2 additions & 1 deletion src/pyoframe/objective.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import Union
from pyoframe.constants import ObjSense, ObjSenseValue
from pyoframe.constraints import SupportsMath, Expression


class Objective(Expression):
def __init__(self, expr: SupportsMath, sense: ObjSense | ObjSenseValue) -> None:
def __init__(self, expr: SupportsMath, sense: Union[ObjSense, ObjSenseValue]) -> None:
"""
Examples
--------
Expand Down
5 changes: 3 additions & 2 deletions src/pyoframe/solvers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from pathlib import Path
from typing import Optional


def solve(m, solver, output_dir: Path | None = None, **kwargs):
def solve(m, solver, output_dir: Optional[Path] = None, **kwargs):
if output_dir is not None and not output_dir.exists():
output_dir.mkdir(parents=True)

Expand All @@ -11,7 +12,7 @@ def solve(m, solver, output_dir: Path | None = None, **kwargs):
raise ValueError(f"Solver {solver} not recognized or supported.")


def gurobi_solve(model, dir_path: Path | None = None, use_var_names=True):
def gurobi_solve(model, dir_path: Optional[Path] = None, use_var_names=True):
import gurobipy as gp

if dir_path is None:
Expand Down
8 changes: 4 additions & 4 deletions src/pyoframe/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
File containing utility functions.
"""

from typing import Any, Iterable
from typing import Any, Iterable, Optional, Union
import polars as pl
import pandas as pd

Expand All @@ -22,7 +22,7 @@ def get_obj_repr(obj: object, _props: Iterable[str] = (), **kwargs):


def parse_inputs_as_iterable(
*inputs: Any | Iterable[Any],
*inputs: Union[Any, Iterable[Any]],
) -> Iterable[Any]:
"""
Converts a parameter *x: Any | Iteraable[Any] to a single Iterable[Any] object.
Expand All @@ -42,7 +42,7 @@ def parse_inputs_as_iterable(
return inputs


def _is_iterable(input: Any | Iterable[Any]) -> bool:
def _is_iterable(input: Union[Any, Iterable[Any]]) -> bool:
# Inspired from the polars library
return isinstance(input, Iterable) and not isinstance(
input,
Expand All @@ -52,7 +52,7 @@ def _is_iterable(input: Any | Iterable[Any]) -> bool:

def concat_dimensions(
df: pl.DataFrame,
prefix: str | None = None,
prefix: Optional[str] = None,
keep_dims: bool = True,
ignore_columns=RESERVED_COL_KEYS,
replace_spaces: bool = True,
Expand Down
3 changes: 2 additions & 1 deletion tests/examples/facility_problem/model.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# pyright: reportAttributeAccessIssue=false
import os
from typing import Union
import pandas as pd
from pathlib import Path

from pyoframe import Model, Variable, sum


def main(input_dir, output_dir: Path | str):
def main(input_dir, output_dir: Union[Path, str]):
plants = pd.read_csv(input_dir / "plants.csv").set_index("plant")
warehouses = pd.read_csv(input_dir / "wharehouses.csv").set_index("wharehouse")
transport_costs = (
Expand Down

0 comments on commit 5b8b943

Please sign in to comment.