def __split_unit(self, readable_value): if RealNumber(readable_value).is_type(): if self._default_unit is None: raise UnitNotFoundError( "unit not found", value=readable_value, available_units=_get_unit_msg(self._text_units), ) return (readable_value, self._default_unit) if not String(readable_value).is_type(): raise TypeError("readable_value must be a string") for unit in self._units: try: if unit.regexp.search(readable_value): number = unit.regexp.split(readable_value)[0] if not RealNumber(number).is_type(): continue return (number, unit) except TypeError: continue raise UnitNotFoundError( "unit not found", value=readable_value, available_units=_get_unit_msg(self._text_units), )
def validate_netem_parameter(self): self.validate_bandwidth_rate() self.__validate_network_delay() self.__validate_packet_loss_rate() self.__validate_packet_duplicate_rate() self.__validate_corruption_rate() self.__validate_reordering_rate() self.__validate_reordering_and_delay() netem_param_value_list = [ self.bandwidth_rate, self.__packet_loss_rate, self.__packet_duplicate_rate, self.__corruption_rate, self.__reordering_rate, ] if all([ not RealNumber(netem_param_value).is_type() or netem_param_value <= 0 for netem_param_value in netem_param_value_list ] + [ self.__latency_time <= HumanReadableTime( Tc.ValueRange.LatencyTime.MIN) ]): raise ParameterError( "there are no valid net emulation parameters found. " "at least one or more following parameters are required: " "--rate, --delay, --loss, --duplicate, --corrupt, --reordering" )
def validate_netem_parameter(self): self.validate_bandwidth_rate() self.__validate_network_delay() self.__validate_packet_loss_rate() self.__validate_packet_duplicate_rate() self.__validate_corruption_rate() self.__validate_reordering_rate() self.__validate_reordering_and_delay() netem_param_values = [ self.__packet_loss_rate, self.__packet_duplicate_rate, self.__corruption_rate, self.__reordering_rate, ] if self.__bandwidth_rate: netem_param_values.append(self.__bandwidth_rate.kilo_bps) check_results = [ not RealNumber(netem_param_value).is_type() or netem_param_value <= 0 for netem_param_value in netem_param_values ] if self.__latency_time: check_results.append(self.__latency_time <= hr.Time(Tc.ValueRange.LatencyTime.MIN)) if all(check_results): raise hr.ParameterError( "there are no valid net emulation parameters found. " "at least one or more following parameters are required: " "--rate, --delay, --loss, --duplicate, --corrupt, --reordering" )
def __exeute_method(self, method, value): try: result = getattr(self.typeclass(value, self.strict_level), method)() if method == "validate": result = "-" except (TypeError, typepy.TypeConversionError): return '"E"' # for string tests if NullString(result, StrictLevel.MAX).is_type(): return '""' strict_level = StrictLevel.MAX typeobj = Integer(result, strict_level) if typeobj.is_type(): return typeobj.convert() typeobj = RealNumber(result, strict_level) if typeobj.is_type(): return typeobj.convert() if String(result, strict_level).is_type(): return '"{}"'.format(result) if Infinity(result, strict_level).is_type(): return '"inf"' if Nan(result, strict_level).is_type(): return '"nan"' return result
def get_integer_digit(value): from typepy import RealNumber, TypeConversionError float_type = RealNumber(value) try: abs_value = abs(float_type.convert()) except TypeConversionError: raise ValueError( "the value must be a number: value='{}' type='{}'".format(value, type(value)) ) if abs_value.is_zero(): return 1 try: return len(text_type(abs_value.quantize(Decimal("1."), rounding=decimal.ROUND_DOWN))) except decimal.InvalidOperation as e: raise ValueError(e)
def __get_additional_format_len(self) -> int: if not RealNumber(self.data, strip_ansi_escape=False).is_type(): return 0 format_len = 0 if Decimal(self.data) < 0: # for minus character format_len += 1 return format_len
def test_normal_send_parse(self, transmitter, ping_parser, host, count): transmitter.destination = host transmitter.count = count result = transmitter.ping() ping_parser.parse(result.stdout) assert ping_parser.packet_transmit >= count assert RealNumber(ping_parser.packet_receive).is_type() assert RealNumber(ping_parser.packet_loss_rate).is_type() assert RealNumber(ping_parser.packet_loss_count).is_type() assert RealNumber(ping_parser.packet_duplicate_rate).is_type() assert RealNumber(ping_parser.packet_duplicate_count).is_type() assert RealNumber(ping_parser.rtt_min).is_type() assert RealNumber(ping_parser.rtt_avg).is_type() assert RealNumber(ping_parser.rtt_max).is_type() assert RealNumber(ping_parser.rtt_mdev).is_type()
def test_normal_send_parse(self, transmitter, ping_parser, host, count): transmitter.destination = host transmitter.count = count result = transmitter.ping() stats = ping_parser.parse(result.stdout) assert stats.packet_transmit >= count assert RealNumber(stats.packet_receive).is_type() assert RealNumber(stats.packet_loss_rate).is_type() assert RealNumber(stats.packet_loss_count).is_type() assert RealNumber(stats.packet_duplicate_rate).is_type() assert RealNumber(stats.packet_duplicate_count).is_type() assert RealNumber(stats.rtt_min).is_type() assert RealNumber(stats.rtt_avg).is_type() assert RealNumber(stats.rtt_max).is_type() assert RealNumber(stats.rtt_mdev).is_type() assert IcmpReplyKey.TIMESTAMP not in stats.icmp_replies[0]
def get_integer_digit(value): from typepy import RealNumber, TypeConversionError float_type = RealNumber(value) try: abs_value = abs(float_type.convert()) except TypeConversionError: raise ValueError( "the value must be a number: value='{}' type='{}'".format( value, type(value))) if abs_value.is_zero(): return 1 try: return len( text_type( abs_value.quantize(Decimal("1."), rounding=decimal.ROUND_DOWN))) except decimal.InvalidOperation as e: raise ValueError(e)
def is_invalid_param(rate, delay, packet_loss, packet_duplicate, corrupt, reordering): param_values = [packet_loss, packet_duplicate, corrupt, reordering] print("rate={}, params={}".format(rate, param_values)) is_invalid = all([ not RealNumber(param_value).is_type() or param_value <= 0 for param_value in param_values ] + [hr.Time(delay, hr.Time.Unit.MILLISECOND).milliseconds <= 0]) try: hr.BitPerSecond(rate).bps except (TypeError, ValueError): pass else: is_invalid = False return is_invalid
def is_invalid_param(rate, delay, packet_loss, packet_duplicate, corrupt, reordering): param_value_list = [packet_loss, packet_duplicate, corrupt, reordering] print(param_value_list) is_invalid = all([ not RealNumber(param_value).is_type() or param_value <= 0 for param_value in param_value_list ] + [HumanReadableTime(delay) <= HumanReadableTime("0ms")]) try: Humanreadable(rate, kilo_size=1000).to_bit() except (TypeError, ValueError): pass else: is_invalid = False return is_invalid
def validate_bandwidth_rate(self): if typepy.is_null_string(self.__bandwidth_rate): return # convert bandwidth string [K/M/G bit per second] to a number bandwidth_rate = Humanreadable(self.__bandwidth_rate, kilo_size=KILO_SIZE).to_kilo_bit() if not RealNumber(bandwidth_rate).is_type(): raise ParameterError("bandwidth_rate must be a number", value=bandwidth_rate) if bandwidth_rate <= 0: raise ParameterError("bandwidth_rate must be greater than zero", value=bandwidth_rate) no_limit_kbits = get_no_limit_kbits(self.__device) if bandwidth_rate > no_limit_kbits: raise ParameterError( "exceed bandwidth rate limit", value="{} kbps".format(bandwidth_rate), expected="less than {} kbps".format(no_limit_kbits), )
def update(self, value): store_value = RealNumber(value).try_convert() if store_value is None: return self.__value_list.append(store_value)