Module nemo.utils

Utility functions (eg, plotting).

Functions

def currency(value)
Expand source code
def currency(value):
    """Format a value into currency with thousands separator(s).

    If there are zero cents, remove .00 for brevity.  No doctest
    provided as the result will be locale specific.
    """
    cents = locale.localeconv()['mon_decimal_point'] + '00'
    return locale.currency(round(value), grouping=True).replace(cents, '')

Format a value into currency with thousands separator(s).

If there are zero cents, remove .00 for brevity. No doctest provided as the result will be locale specific.

def plot(context, filename=None, xlim=None, *, spills=False, showlegend=True)
Expand source code
def plot(context, filename=None, xlim=None, *, spills=False, showlegend=True):
    """Produce a pretty plot of supply and demand."""
    if xlim is not None and not isinstance(xlim, tuple):
        raise ValueError(xlim)

    if xlim is None:
        starttime = context.demand.index[0]
        ninety_days = 24 * 90
        if context.timesteps() > ninety_days:
            endtime = starttime + timedelta(days=90)
        else:
            endtime = context.demand.index[-1]
        timerange = (starttime, endtime)
    else:
        if len(xlim) != 2:
            raise ValueError(xlim)
        timerange = xlim

    _figure(context, spills, showlegend, timerange)
    if not filename:
        plt.show()
    else:
        plt.savefig(filename)

Produce a pretty plot of supply and demand.

def thousands(value)
Expand source code
def thousands(value):
    """Format a value with thousands separator(s).

    No doctest provided as the result will be locale specific.
    """
    return locale.format_string('%d', value, grouping=True)

Format a value with thousands separator(s).

No doctest provided as the result will be locale specific.

Classes

class MultiSetter (*args)
Expand source code
class MultiSetter:
    """A setter that broadcasts its value to any number of setters.

    This is useful for generator pairs such as a Battery and a
    BatteryLoad, where there should only be one (tied) capacity for
    both objects.

    >>> setter1 = (lambda x: print("one", x), 0, 40)
    >>> setter2 = (lambda x: print("two", x), 0, 40)
    >>> ms = MultiSetter(setter1, setter2)
    >>> ms.set_capacity(1.2)
    one 1.2
    two 1.2

    >>> setter1 = (None, 0, 40)  # not callable
    >>> ms = MultiSetter(setter1)
    Traceback (most recent call last):
        ...
    TypeError: setter not callable
    """

    def __init__(self, *args):
        """Initialise a MultiSetter with any number of setters."""
        self.setters = [setter[0] for setter in args]
        for setter in self.setters:
            if not callable(setter):
                msg = "setter not callable"
                raise TypeError(msg)

    def set_capacity(self, value):
        """Broadcast value to all setters."""
        for setter in self.setters:
            setter(value)

A setter that broadcasts its value to any number of setters.

This is useful for generator pairs such as a Battery and a BatteryLoad, where there should only be one (tied) capacity for both objects.

>>> setter1 = (lambda x: print("one", x), 0, 40)
>>> setter2 = (lambda x: print("two", x), 0, 40)
>>> ms = MultiSetter(setter1, setter2)
>>> ms.set_capacity(1.2)
one 1.2
two 1.2
>>> setter1 = (None, 0, 40)  # not callable
>>> ms = MultiSetter(setter1)
Traceback (most recent call last):
    ...
TypeError: setter not callable

Initialise a MultiSetter with any number of setters.

Methods

def set_capacity(self, value)
Expand source code
def set_capacity(self, value):
    """Broadcast value to all setters."""
    for setter in self.setters:
        setter(value)

Broadcast value to all setters.