Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Python examples in Chapter 1 #840

Merged
merged 4 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/Python/hwclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

# Do 10 requests, waiting each time for a response
for request in range(10):
print("Sending request %s ..." % request)
socket.send(b"Hello")
print(f"Sending request {request} ...")
socket.send_string("Hello")

# Get the reply.
message = socket.recv()
print("Received reply %s [ %s ]" % (request, message))
print(f"Received reply {request} [ {message} ]")
4 changes: 2 additions & 2 deletions examples/Python/hwserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
while True:
# Wait for next request from client
message = socket.recv()
print("Received request: %s" % message)
print(f"Received request: {message}")

# Do some 'work'
time.sleep(1)

# Send reply back to client
socket.send(b"World")
socket.send_string("World")
2 changes: 1 addition & 1 deletion examples/Python/tasksink.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@

# Calculate and report duration of batch
tend = time.time()
print("Total elapsed time: %d msec" % ((tend-tstart)*1000))
print(f"Total elapsed time: {(tend-tstart)*1000} msec")
11 changes: 3 additions & 8 deletions examples/Python/taskvent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
import random
import time

try:
raw_input
except NameError:
# Python 3
raw_input = input

context = zmq.Context()

Expand All @@ -25,7 +20,7 @@
sink.connect("tcp://localhost:5558")

print("Press Enter when the workers are ready: ")
_ = raw_input()
_ = input()
print("Sending tasks to workers...")

# The first message is "0" and signals start of batch
Expand All @@ -42,9 +37,9 @@
workload = random.randint(1, 100)
total_msec += workload

sender.send_string(u'%i' % workload)
sender.send_string(f"{workload}")

print("Total expected cost: %s msec" % total_msec)
print(f"Total expected cost: {total_msec} msec")

# Give 0MQ time to deliver
time.sleep(1)
4 changes: 2 additions & 2 deletions examples/Python/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

import zmq

print("Current libzmq version is %s" % zmq.zmq_version())
print("Current pyzmq version is %s" % zmq.__version__)
print(f"Current libzmq version is {zmq.zmq_version()}")
print(f"Current pyzmq version is {zmq.__version__}")
9 changes: 2 additions & 7 deletions examples/Python/wuclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

# Subscribe to zipcode, default is NYC, 10001
zip_filter = sys.argv[1] if len(sys.argv) > 1 else "10001"

# Python 2 - ascii bytes to unicode str
if isinstance(zip_filter, bytes):
zip_filter = zip_filter.decode('ascii')
socket.setsockopt_string(zmq.SUBSCRIBE, zip_filter)

# Process 5 updates
Expand All @@ -30,6 +26,5 @@
zipcode, temperature, relhumidity = string.split()
total_temp += int(temperature)

print("Average temperature for zipcode '%s' was %dF" % (
zip_filter, total_temp / (update_nbr+1))
)
print((f"Average temperature for zipcode "
f"'{zip_filter}' was {total_temp / (update_nbr+1)} F"))
2 changes: 1 addition & 1 deletion examples/Python/wuserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
temperature = randrange(-80, 135)
relhumidity = randrange(10, 60)

socket.send_string("%i %i %i" % (zipcode, temperature, relhumidity))
socket.send_string(f"{zipcode} {temperature} {relhumidity}")