def _get_weight_or_default(weight_factory, weight=None, default_one=True):
    """Converts weight to an instance of the weight type.

    If weight is None, the weight is set to:
    * semiring one when default_one is True,
    * semiring zero when default_one is False.
    """
    if weight is None:
        return weight_factory.one() if default_one else weight_factory.zero()
    if isinstance(weight, weight_factory.__bases__):
        return weight
    return weight_factory(weight)