def leg_from_proto_v2(
    leg_proto: ir_swap.SwapLeg
) -> Union[coupon_specs.FixedCouponSpecs, coupon_specs.FloatCouponSpecs]:
    """Initialized coupon specifications from a proto instance."""
    if leg_proto.HasField("fixed_leg"):
        leg = leg_proto.fixed_leg
        coupon_freq = leg.coupon_frequency.type
        coupon_freq, coupon_freq_multiplier = _frequency_and_multiplier(
            coupon_freq)
        return coupon_specs.FixedCouponSpecs(
            currency=[currencies.from_proto_value(leg.currency)],
            coupon_frequency=(coupon_freq, [
                coupon_freq_multiplier * leg.coupon_frequency.amount
            ]),
            notional_amount=[
                instrument_utils.decimal_to_double(leg.notional_amount)
            ],
            fixed_rate=[instrument_utils.decimal_to_double(leg.fixed_rate)],
            settlement_days=[leg.settlement_days],
            businessday_rule=business_days.convention_from_proto_value(
                leg.business_day_convention),
            daycount_convention=daycount_conventions.from_proto_value(
                leg.daycount_convention),
            calendar=business_days.holiday_from_proto_value(leg.bank_holidays))
    else:
        leg = leg_proto.floating_leg
        # Get the index rate object
        rate_index = leg.floating_rate_type
        rate_index = rate_indices.RateIndex.from_proto(rate_index)
        rate_index.name = [rate_index.name]
        rate_index.source = [rate_index.source]
        coupon_freq = leg.coupon_frequency.type
        coupon_freq, coupon_freq_multiplier = _frequency_and_multiplier(
            coupon_freq)
        reset_freq = leg.reset_frequency.type
        reset_freq, reset_freq_multiplier = _frequency_and_multiplier(
            reset_freq)
        return coupon_specs.FloatCouponSpecs(
            currency=[currencies.from_proto_value(leg.currency)],
            coupon_frequency=(coupon_freq, [
                coupon_freq_multiplier * leg.coupon_frequency.amount
            ]),
            reset_frequency=(reset_freq, [
                reset_freq_multiplier * leg.reset_frequency.amount
            ]),
            notional_amount=[
                instrument_utils.decimal_to_double(leg.notional_amount)
            ],
            floating_rate_type=[rate_index],
            settlement_days=[leg.settlement_days],
            businessday_rule=business_days.convention_from_proto_value(
                leg.business_day_convention),
            daycount_convention=daycount_conventions.from_proto_value(
                leg.daycount_convention),
            spread=[instrument_utils.decimal_to_double(leg.spread)],
            calendar=business_days.holiday_from_proto_value(leg.bank_holidays))
def _get_keys(leg: ir_swap.SwapLeg) -> Tuple[List[Any], List[Any]]:
    """Computes key values for a function that partitions swaps into batches."""
    if leg.HasField("fixed_leg"):
        fixed_leg = leg.fixed_leg
        key_1 = _fixed_leg_key(fixed_leg)
        key_2 = 7 * [None]
    else:
        floating_leg = leg.floating_leg
        key_1 = 4 * [None]
        key_2 = _floating_leg_key(floating_leg)
    # len(key_1) + len(key_2) = 11 - this is the number of features involved into
    # the batching procedure
    return key_1, key_2
def leg_from_proto(
    leg_proto: ir_swap.SwapLeg) -> Union[coupon_specs.FixedCouponSpecs,
                                         coupon_specs.FloatCouponSpecs]:
  """Initialized coupon specifications from a proto instance."""
  if leg_proto.HasField("fixed_leg"):
    leg = leg_proto.fixed_leg
    return coupon_specs.FixedCouponSpecs(
        currency=currencies.from_proto_value(leg.currency),
        coupon_frequency=leg.coupon_frequency,
        notional_amount=[instrument_utils.decimal_to_double(
            leg.notional_amount)],
        fixed_rate=[instrument_utils.decimal_to_double(
            leg.fixed_rate)],
        settlement_days=[leg.settlement_days],
        businessday_rule=business_days.convention_from_proto_value(
            leg.business_day_convention),
        daycount_convention=daycount_conventions.from_proto_value(
            leg.daycount_convention),
        calendar=business_days.holiday_from_proto_value(leg.bank_holidays))
  else:
    leg = leg_proto.floating_leg
    return coupon_specs.FloatCouponSpecs(
        currency=currencies.from_proto_value(leg.currency),
        coupon_frequency=leg.coupon_frequency,
        reset_frequency=leg.reset_frequency,
        notional_amount=[instrument_utils.decimal_to_double(
            leg.notional_amount)],
        floating_rate_type=[rate_indices.from_proto_value(
            leg.floating_rate_type)],
        settlement_days=[leg.settlement_days],
        businessday_rule=business_days.convention_from_proto_value(
            leg.business_day_convention),
        daycount_convention=daycount_conventions.from_proto_value(
            leg.daycount_convention),
        spread=[instrument_utils.decimal_to_double(leg.spread)],
        calendar=business_days.holiday_from_proto_value(leg.bank_holidays))