From 3c0b13e39f210aca9a542a219c4899b18c1e2e47 Mon Sep 17 00:00:00 2001 From: Alina Lenk Date: Wed, 2 Mar 2022 15:21:10 +0100 Subject: [PATCH 2/5] generate_packets.py: Replace get_choices with itertools powerset recipe See osdn #44008 Signed-off-by: Alina Lenk --- common/generate_packets.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/common/generate_packets.py b/common/generate_packets.py index e994586e62..03700c50d9 100755 --- a/common/generate_packets.py +++ b/common/generate_packets.py @@ -21,6 +21,7 @@ import argparse from pathlib import Path from contextlib import contextmanager from functools import partial +from itertools import chain, combinations ###################### Parsing Command Line Arguments ###################### @@ -231,18 +232,13 @@ def lazy_overwrite_open(path, suffix=".tmp"): tmp_path.replace(path) -def get_choices(population): - def helper(index, so_far): - if index >= len(population): - return [so_far] - t0=so_far[:] - t1=so_far[:] - t1.append(list(population)[index]) - return helper(index + 1, t1) + helper(index + 1, t0) +######################### General helper functions ######################### - result=helper(0, []) - assert len(result) == 2**len(population) - return result +# Taken from https://docs.python.org/3.4/library/itertools.html#itertools-recipes +def powerset(iterable): + "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" + s = list(iterable) + return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) def without(seq, part): result=[] @@ -1582,9 +1578,8 @@ class Packet: if f.remove_cap: all_caps[f.remove_cap]=1 all_caps=all_caps.keys() - choices=get_choices(all_caps) self.variants=[] - for i, poscaps in enumerate(choices): + for i, poscaps in enumerate(powerset(all_caps)): negcaps=without(all_caps,poscaps) fields=[] for field in self.fields: -- 2.17.1