Skip to content

Commit

Permalink
Refactored .format() to f-strings for floats.
Browse files Browse the repository at this point in the history
  • Loading branch information
baochunli committed Aug 21, 2024
1 parent 396b31a commit 5e797d2
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 65 deletions.
2 changes: 1 addition & 1 deletion examples/fattree_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

env = simpy.Environment()

n_flows = 64
n_flows = 512
finish_time = 10
k = 32
pir = 10000000000 # 10Gbps
Expand Down
7 changes: 2 additions & 5 deletions examples/real_traffic/tcp_echo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

try:
message = 'This is a new message. It will be echoed.'
print('Sent "{}" at time {:.2f}.'.format(message,
time.time() - init_time))
print(f'Sent "{message}" at time {(time.time() - init_time):.2f}')
sock.sendall(bytes(message, 'utf-8'))

amount_received = 0
Expand All @@ -27,9 +26,7 @@
while amount_received < amount_expected:
data = sock.recv(64)
amount_received += len(data)
print('Received "{}" at time {:.2f}.'.format(
data,
time.time() - init_time))
print(f'Received "{data}" at time {(time.time() - init_time):.2f}.')

finally:
print('Disconnecting.')
Expand Down
9 changes: 3 additions & 6 deletions examples/real_traffic/tcp_echo_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,15 @@
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(64)
print('received "{}" at time {:.2f}.'.format(
data,
time.time() - init_time))
print(f'received "{data}" at time {(time.time() - init_time):.2f}.')
if data:
print('sending "{}" back to the client at time {:.2f}'.
format(data,
time.time() - init_time))
connection.sendall(data)
else:
print('no more data from {} at time {:.2f}'.format(
client_address,
time.time() - init_time))
print(f"no more data from {client_address} at time "
f"{(time.time() - init_time):.2f}")
break

finally:
Expand Down
4 changes: 2 additions & 2 deletions examples/real_traffic/udp_echo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
(args.server_host, int(args.server_port)))
received = str(sock.recv(1024), "utf-8")

print("Sent: {}".format(message))
print("Received: {}".format(received))
print(f"Sent: {message}")
print(f"Received: {received}")
2 changes: 1 addition & 1 deletion examples/real_traffic/udp_echo_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MyUDPHandler(socketserver.BaseRequestHandler):
def handle(self):
data = self.request[0].strip()
socket = self.request[1]
print("{} wrote: ".format(self.client_address))
print(f"{self.client_address} wrote: ")
print(data)
socket.sendto(data, self.client_address)

Expand Down
42 changes: 15 additions & 27 deletions ns/packet/bbr_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,9 @@ def run(self):
self.packet_in_flight += packet.size
if self.debug:
print(
"Send packet {:d} with size {:d}, "
"flow_id {:d} at time {:.4f}, delivered_time {:.4f}.".format(
packet.packet_id,
packet.size,
packet.flow_id,
self.env.now,
packet.delivered_time,
)
f"Send packet {packet.packet_id} with size {packet.size}, "
f"flow_id {packet.flow_id} at time {self.env.now:.4f}, "
f"and the packet delivered time is {packet.delivered_time:.4f}."
)
assert packet.delivered_time > 0
self.out.put(packet)
Expand All @@ -165,8 +160,8 @@ def run(self):

if self.debug:
print(
"Setting a timer for packet {:d} with an RTO"
" of {:.4f}.".format(packet.packet_id, self.rto)
f"Setting a timer for packet {packet.packet_id} with an RTO"
f" of {self.rto:.4f}."
)

def timeout_callback(self, packet_id=0):
Expand All @@ -175,8 +170,8 @@ def timeout_callback(self, packet_id=0):
packet_id = self.max_ack
if self.debug:
print(
"Timer expired for packet {:d} {:d} at time {:.4f}.".format(
packet_id, self.flow.fid, self.env.now
f"Timer expired for packet {packet_id} {self.flow.fid} "
f"at time {self.env.now:.4f}."
)
)

Expand All @@ -203,12 +198,8 @@ def timeout_callback(self, packet_id=0):
self.rto = 60
if self.debug:
print(
"to Resending packet {:d} with flow_id {:d} at time {:.4f} with a timeout time {:4f}.".format(
resent_pkt.packet_id,
resent_pkt.flow_id,
self.env.now,
self.env.now + self.rto,
)
f"to Resending packet {resent_pkt.packet_id} with flow_id {resent_pkt.flow_id} "
f"at time {self.env.now:.4f} with a timeout time {self.env.now + self.rto:4f}."
)

# starting a new timer for this segment and doubling the retransmission timeout
Expand Down Expand Up @@ -295,9 +286,8 @@ def put(self, ack):

if self.debug:
print(
"dup Resending packet {:d} with flow_id {:d} at time {:.4f}.".format(
resent_pkt.packet_id, resent_pkt.flow_id, self.env.now
)
f"dup Resending packet {resent_pkt.packet_id} with flow_id "
f"{resent_pkt.flow_id} at time {self.env.now:.4f}."
)
self.out.put(resent_pkt)

Expand Down Expand Up @@ -358,14 +348,12 @@ def put(self, ack):

if self.debug:
print(
"Ack received till sequence number {:d} at time {:.4f}.".format(
ack.ack, self.env.now
)
f"Ack received till sequence number {ack.ack} at time "
f"{self.env.now:.4f}."
)
print(
"Congestion window size = {:.1f}, last ack = {:d}.".format(
self.congestion_control.cwnd, self.last_ack
)
f"Congestion window size = {self.congestion_control.cwnd:.1f}, "
f"last ack = {self.last_ack}."
)

if bbr_update:
Expand Down
11 changes: 3 additions & 8 deletions ns/packet/dist_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,9 @@ def run(self):

if self.debug:
print(
"DistPacketGenerator {} sent packet {:d} with size {:d}, "
"flow_id {:d} at time {:.4f}.".format(
self.element_id,
packet.packet_id,
packet.size,
packet.flow_id,
self.env.now,
)
f"DistPacketGenerator {self.element_id} sent packet {packet.packet_id}"
f" with size {packet.size}, "
f"flow_id {packet.flow_id} at time {self.env.now:.4f}."
)

# waits for the next transmission
Expand Down
9 changes: 5 additions & 4 deletions ns/packet/proxy_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,18 @@ def put(self, packet):
self.last_arrival[rec_index] = now

if self.debug:
print("At time {:.2f}, packet {:d} arrived at {}.".format(
now, packet.packet_id, self.element_id))
print(f"At time {now:.2f}, packet {packet.packet_id} "
f"arrived at {self.element_id}.")
if self.rec_waits and len(self.packet_sizes[rec_index]) >= 10:
bytes_received = sum(self.packet_sizes[rec_index][-9:])
time_elapsed = self.env.now - (
self.packet_times[rec_index][-10] +
self.waits[rec_index][-10])
if time_elapsed > 0:
print(
"Average throughput (last 10 packets): {:.2f} bytes/second."
.format(bytes_received / time_elapsed))
f"Average throughput (last 10 packets): "
f"{(bytes_received / time_elapsed):.2f} bytes/second."
)

self.packets_received[rec_index] += 1
self.bytes_received[rec_index] += packet.size
10 changes: 4 additions & 6 deletions ns/packet/sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,17 @@ def put(self, packet):

if self.debug:
print(
"At time {:.2f}, packet {:d} in flow {:d} arrived.".format(
now, packet.packet_id, packet.flow_id
)
f"At time {now:.2f}, packet {packet.packet_id} in "
f"flow {packet.flow_id} arrived."
)
if self.rec_waits and len(self.packet_sizes[rec_index]) >= 10:
bytes_received = sum(self.packet_sizes[rec_index][-9:])
time_elapsed = self.env.now - (
self.packet_times[rec_index][-10] + self.waits[rec_index][-10]
)
print(
"Average throughput (last 10 packets): {:.2f} bytes/second.".format(
float(bytes_received) / time_elapsed
)
f"Average throughput (last 10 packets): "
f"{(float(bytes_received) / time_elapsed):.2f} bytes/second."
)

self.packets_received[rec_index] += 1
Expand Down
9 changes: 4 additions & 5 deletions ns/port/wire.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ def run(self):
self.out.put(packet)

if self.debug:
print("Left wire #{} at {:.3f}: {}".format(
self.wire_id, self.env.now, packet))
print(f"Left wire #{self.wire_id} at "
f"{self.env.now:.3f}: {packet}")
else:
if self.debug:
print("Dropped on wire #{} at {:.3f}: {}".format(
self.wire_id, self.env.now, packet))

print(f"Dropped on wire #{self.wire_id} at "
f"{self.env.now:.3f}: {packet}")
def put(self, packet):
""" Sends a packet to this element. """
self.packets_rec += 1
Expand Down

0 comments on commit 5e797d2

Please sign in to comment.