Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import random
from typing import List, Optional, Tuple, Union
from common.proto.load_generator_pb2 import ScalarOrRange
# RegEx to validate strings formatted as: '1, 2.3, 4.5 .. 6.7 , .8...9, 10., .11'
# IMPORTANT: this regex just validates data, it does not extract the pieces of data!
RE_FLOAT = r'[\ ]*[0-9]*[\.]?[0-9]*[\ ]*'
RE_RANGE = RE_FLOAT + r'(\.\.' + RE_FLOAT + r')?'
RE_SCALAR_RANGE_LIST = RE_RANGE + r'(\,' + RE_RANGE + r')*'
Type_ListScalarRange = List[Union[float, Tuple[float, float]]]
def parse_list_scalar_range(value : str) -> Type_ListScalarRange:
str_value = str(value).replace(' ', '')
ranges = [[float(value) for value in item.split('..')] for item in str_value.split(',')]
return ranges
def list_scalar_range__to__grpc(list_scalar_range : Type_ListScalarRange, obj : List[ScalarOrRange]) -> None:
for i,scalar_or_range in enumerate(list_scalar_range):
if isinstance(scalar_or_range, (float, str)):
_scalar = obj.add()
_scalar.scalar = float(scalar_or_range)
elif isinstance(scalar_or_range, (list, tuple)):
if len(scalar_or_range) == 1:
_scalar = obj.add()
_scalar.scalar = float(scalar_or_range[0])
elif len(scalar_or_range) == 2:
_range = obj.add()
_range.range.minimum = float(scalar_or_range[0])
_range.range.maximum = float(scalar_or_range[1])
else:
MSG = 'List/tuple with {:d} items in item(#{:d}, {:s})'
raise NotImplementedError(MSG.format(len(scalar_or_range), i, str(scalar_or_range)))
else:
MSG = 'Type({:s}) in item(#{:d}, {:s})'
raise NotImplementedError(MSG.format(str(type(scalar_or_range), i, str(scalar_or_range))))
def grpc__to__list_scalar_range(obj : List[ScalarOrRange]) -> Type_ListScalarRange:
list_scalar_range = list()
for item in obj:
item_kind = item.WhichOneof('value')
if item_kind == 'scalar':
scalar_or_range = float(item.scalar)
elif item_kind == 'range':
scalar_or_range = (float(item.range.minimum), float(item.range.maximum))
else:
raise NotImplementedError('Unsupported ScalarOrRange kind({:s})'.format(str(item_kind)))
list_scalar_range.append(scalar_or_range)
return list_scalar_range
def generate_value(
list_scalar_range : Type_ListScalarRange, ndigits : Optional[int] = None
) -> float:
scalar_or_range = random.choice(list_scalar_range)
if isinstance(scalar_or_range, (float, str)):
value = float(scalar_or_range)
elif isinstance(scalar_or_range, (list, tuple)):
if len(scalar_or_range) == 1:
value = float(scalar_or_range[0])
elif len(scalar_or_range) == 2:
minimum = float(scalar_or_range[0])
maximum = float(scalar_or_range[1])
value = random.uniform(minimum, maximum)
else:
MSG = 'List/tuple with {:d} items in item({:s})'
raise NotImplementedError(MSG.format(len(scalar_or_range), str(scalar_or_range)))
else:
MSG = 'Type({:s}) in item({:s})'
raise NotImplementedError(MSG.format(str(type(scalar_or_range), str(scalar_or_range))))
if ndigits is None: return value
return round(value, ndigits=ndigits)