Skip to content

Commit

Permalink
Shorten if/else with ternary
Browse files Browse the repository at this point in the history
  • Loading branch information
pvcraven committed Dec 16, 2021
1 parent f9b2f9a commit d6fce99
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
7 changes: 3 additions & 4 deletions bitshift_example_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
# Use a single 1, and bit shift it with << to the
# spot we are interested in.
bit = (1 << bit_pos) & number_to_encode
if bit != 0:
bit_value = 1
else:
bit_value = 0

# Convert to a 1 or 0, as our 1 might not be in the one's place
bit_value = 0 if bit == 0 else 1

print(f"Bit position {bit_pos:2} is {bit_value} which is worth {bit:2}.")
2 changes: 1 addition & 1 deletion manchester_decoding_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def data_callback(channel):
cur_time = time.time()
time_interval = cur_time - data_callback.last_call

dl = "L->H" if data_line else "H->L"
dl = "low->high" if data_line else "high->low"
print(f" Change: {dl} Interval: {time_interval:.3f}")

data_callback.last_call = cur_time
Expand Down

0 comments on commit d6fce99

Please sign in to comment.