From cdaa1db667ec99d303c34a7d3ed8d00eea023452 Mon Sep 17 00:00:00 2001 From: Rich Wareham Date: Tue, 21 Sep 2010 14:18:47 +0100 Subject: [PATCH] convert map/filter use to generator expressions Apparently using generator expressions and list comprehensions is more 'pythonic'. Who am I to disagree? :) --- generate_key.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/generate_key.py b/generate_key.py index 20c0449..7c7b00e 100755 --- a/generate_key.py +++ b/generate_key.py @@ -112,11 +112,11 @@ def do_test(key_matrix): # add sink keys together according to src ksv key1 = reduce(lambda x, y: (x+y) & 0xffffffffffffff, - map(lambda x: x[1], filter(lambda x: src_ksv & (1<> x) & 1, range(40)) + ksv_bits = ( (ksv >> x) & 1 for x in range(40) ) # zip the master key matrix and the ksv s.t. the LSB of KSV is associated with # the first row, etc. Then filter to only select those rows where the # appropriate bit of the KSV is 1. - master_rows = map(lambda x: x[1], filter(lambda x: x[0] == 1, zip(ksv_bits, key_matrix))) + master_rows = ( x[1] for x in zip(ksv_bits, key_matrix) if x[0] == 1 ) # now generate the key key = [0] * 40 for row in master_rows: # add row to key - key = map(lambda x: (x[0] + x[1]) & 0xffffffffffffff, zip(key, row)) + key = [ (x[0] + x[1]) & 0xffffffffffffff for x in zip(key, row) ] - return key + return tuple(key) def gen_sink_key(ksv, key_matrix): """Generate a sink key from the master key matrix passed. @@ -220,7 +220,7 @@ def output_human_readable(ksv, key, is_sink): print('KSV: %010x' % ksv) # output the key - key_strs = map(lambda x: '%014x' % x, key) + key_strs = [ '%014x' % x for x in key ] print('\n%s Key:' % ('Sink' if is_sink else 'Source')) for idx in range(0, 40, 5): @@ -233,7 +233,7 @@ def output_json(ksv, key, is_sink): print(json.dumps( { 'ksv': ('%010x' % ksv), - 'key': map(lambda x: '%014x' % x, key), + 'key': [ '%014x' % x for x in key ], 'type': 'sink' if is_sink else 'source' }, sort_keys=True, indent=True))