Skip to content

Commit

Permalink
Update code to use print formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
pvcraven committed Nov 10, 2021
1 parent a21613f commit e586caf
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bitshift_example_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
else:
bit_value = 0

print("Bit position {:2} is {} which is worth {:2}".format(bit_pos, bit_value, bit))
print(f"Bit position {bit_pos:2} is {bit_value} which is worth {bit:2}.")
13 changes: 6 additions & 7 deletions bitshift_example_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
# Now pull each bit out of the letter.
# Start from bit 7, and count down to 0
for bit_pos in range(bits_to_encode - 1, -1, -1):


# Use bitwise and to pull out the bit we are interested in
bit = 1 << bit_pos & my_byte
if bit != 0:
bit_value = 1
else:
bit_value = 0

# Convert to a 1 or 0, as the 1 may not be in the 1's place
bit_value = 0 if bit == 0 else 1
# Print, while staying on our current line.
print(bit_value, end="")

# Done with this letter. Go to the next line.
print(" - {:3} - {:}".format(my_byte, chr(my_byte)))
print(f" - {my_byte:3} - {chr(my_byte)}")
10 changes: 3 additions & 7 deletions encode_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
for my_byte in my_message:

# Show what we are encoding
print("{} = {:3} = ".format(chr(my_byte), my_byte), end="")
print(f"{chr(my_byte)} = {my_byte:3} = ", end="")

# Loop for each bit of the byte, starting with the most
# significant bit 7, down to 0.
Expand All @@ -41,12 +41,8 @@
# & = 0100 0000 = 64 (result of bit-wise 'and')
bit = (1 << bit_pos) & my_byte

if bit != 0:
# There was a value
bit_value = 1
else:
# There was not a value
bit_value = 0
# Make sure bit_value is a 1 or 0, not some power of 2.
bit_value = 0 if bit == 0 else 1

print(bit_value, end="")

Expand Down
4 changes: 2 additions & 2 deletions read_wire_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
# or low/high change in the signal.
def my_callback(channel):
if GPIO.input(channel):
print("Channel {} is high".format(channel))
print(f"Channel {channel} is high.")
else:
print("Channel {} is low".format(channel))
print(f"Channel {channel} is low.")

# Set pin 12 up for input
GPIO.setmode(GPIO.BCM)
Expand Down

0 comments on commit e586caf

Please sign in to comment.