Skip to content

io_mappers

Defines various methods for mapping a variable or constraint to its string representation.

Base36Mapper(cls)

Bases: Mapper, ABC

Source code in pyoframe/io_mappers.py
def __init__(self, cls: Type["ModelElementWithId"]) -> None:
    self._ID_COL = cls.get_id_column_name()
    self.mapping_registry = pl.DataFrame(
        {self._ID_COL: [], Mapper.NAME_COL: []},
        schema={self._ID_COL: pl.UInt32, Mapper.NAME_COL: pl.String},
    )

Base36VarMapper(*args, **kwargs)

Bases: Base36Mapper

Examples:

>>> import polars as pl
>>> from pyoframe import Model, Variable
>>> from pyoframe.constants import VAR_KEY
>>> m = Model("min")
>>> m.x = Variable(pl.DataFrame({"t": range(1,63)}))
>>> (m.x.filter(t=11)+1).to_str()
'[11]: 1  + x[11]'
>>> (m.x.filter(t=11)+1).to_str(var_map=Base36VarMapper(Variable))
'[11]: 1  + xb'
>>> Base36VarMapper(Variable).apply(pl.DataFrame({VAR_KEY: []}))
shape: (0, 1)
┌───────────────┐
│ __variable_id │
│ ---           │
│ null          │
╞═══════════════╡
└───────────────┘
Source code in pyoframe/io_mappers.py
def __init__(self, *args, **kwargs) -> None:
    super().__init__(*args, **kwargs)
    df = pl.DataFrame(
        {self._ID_COL: [CONST_TERM]},
        schema={self._ID_COL: pl.UInt32},
    )
    df = self.apply(df, to_col=Mapper.NAME_COL)
    self._extend_registry(df)

NamedMapper(cls)

Bases: Mapper

Maps constraints or variables to a string representation using the object's name and dimensions.

Examples:

>>> import polars as pl
>>> import pyoframe as pf
>>> m = pf.Model("min")
>>> m.foo = pf.Variable(pl.DataFrame({"t": range(4)}))
>>> pf.sum(m.foo)
<Expression size=1 dimensions={} terms=4>
foo[0] + foo[1] + foo[2] + foo[3]
Source code in pyoframe/io_mappers.py
def __init__(self, cls: Type["ModelElementWithId"]) -> None:
    self._ID_COL = cls.get_id_column_name()
    self.mapping_registry = pl.DataFrame(
        {self._ID_COL: [], Mapper.NAME_COL: []},
        schema={self._ID_COL: pl.UInt32, Mapper.NAME_COL: pl.String},
    )