diff --git a/dht11/__init__.py b/dht11/__init__.py index e274a0e..2e4a24a 100644 --- a/dht11/__init__.py +++ b/dht11/__init__.py @@ -114,9 +114,7 @@ def __parse_data_pull_up_lengths(self, data): lengths = [] # will contain the lengths of data pull up periods current_length = 0 # will contain the length of the previous period - for i in range(len(data)): - - current = data[i] + for current in data: current_length += 1 if state == STATE_INIT_PULL_DOWN: @@ -173,26 +171,16 @@ def __calculate_bits(self, pull_up_lengths): # use the halfway to determine whether the period it is long or short halfway = shortest_pull_up + (longest_pull_up - shortest_pull_up) / 2 - bits = [] - - for i in range(0, len(pull_up_lengths)): - bit = False - if pull_up_lengths[i] > halfway: - bit = True - bits.append(bit) - return bits + return [pull_up > halfway for pull_up in pull_up_lengths] def __bits_to_bytes(self, bits): the_bytes = [] byte = 0 - for i in range(0, len(bits)): + for i, bit in enumerate(bits): byte = byte << 1 - if (bits[i]): - byte = byte | 1 - else: - byte = byte | 0 + byte |= bit if ((i + 1) % 8 == 0): the_bytes.append(byte) byte = 0