From 165e01ec6fe54788ef5dbed4f4ddf7e547c285d4 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 11 Nov 2020 17:08:25 -0800 Subject: [PATCH] Fix for old Python versions. --- benchmarks/gen_synthetic_protos.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/benchmarks/gen_synthetic_protos.py b/benchmarks/gen_synthetic_protos.py index c90e4e92bc..2d28aa885d 100644 --- a/benchmarks/gen_synthetic_protos.py +++ b/benchmarks/gen_synthetic_protos.py @@ -44,6 +44,13 @@ field_freqs = [ population = [item[0] for item in field_freqs] weights = [item[1] for item in field_freqs] +def choices(k): + if sys.version_info >= (3, 6): + return random.choices(population=population, weights=weights, k=k) + else: + print("WARNING: old Python version, field types are not properly weighted!") + return [random.choice(population) for _ in range(k)] + with open(base + "/benchmarks/100_msgs.proto", "w") as f: f.write('syntax = "proto3";\n') f.write('package upb_benchmark;\n') @@ -65,7 +72,7 @@ with open(base + "/benchmarks/100_fields.proto", "w") as f: f.write('message Message {\n') i = 1 random.seed(a=0, version=2) - for field in random.choices(population=population, weights=weights, k=100): + for field in choices(100): field_type, label = field f.write(' {label} {field_type} field{i} = {i};\n'.format(i=i, label=label, field_type=field_type)) i += 1 @@ -78,7 +85,7 @@ with open(base + "/benchmarks/200_fields.proto", "w") as f: f.write('message Message {\n') i = 1 random.seed(a=0, version=2) - for field in random.choices(population=population, weights=weights, k=200): + for field in choices(200): field_type, label = field f.write(' {label} {field_type} field{i} = {i};\n'.format(i=i, label=label,field_type=field_type)) i += 1