Skip to content

Commit 6addc37

Browse files
committed
pyproject: format code with ruff
Upstream MicroPython switched from black to ruff, so we are following suit. This now includes all non-3rd-party .py files so quite a few files are reformatted that weren't being checked before.
1 parent 7822019 commit 6addc37

30 files changed

+240
-229
lines changed

.github/build-each-commit.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@
5757

5858
# update only required submodules
5959
pybricks.git.submodule("update", "--init", "micropython")
60-
if args.hub in ["cityhub", "movehub", "technichub", "primehub", "essentialhub", "nxt", "ev3"]:
60+
if args.hub in [
61+
"cityhub",
62+
"movehub",
63+
"technichub",
64+
"primehub",
65+
"essentialhub",
66+
"nxt",
67+
"ev3",
68+
]:
6169
pybricks.submodule("micropython").module().git.submodule(
6270
"update", "--init", "lib/micropython-lib"
6371
)
@@ -66,7 +74,9 @@
6674
)
6775
if args.hub == "primehub" or args.hub == "essentialhub":
6876
pybricks.git.submodule("update", "--init", "--checkout", "lib/btstack")
69-
pybricks.git.submodule("update", "--init", "--checkout", "lib/STM32_USB_Device_Library")
77+
pybricks.git.submodule(
78+
"update", "--init", "--checkout", "lib/STM32_USB_Device_Library"
79+
)
7080

7181
# build the firmware
7282
subprocess.check_call(

.github/build-missing-commits.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@
9797
"update", "--init", "lib/stm32lib"
9898
)
9999
pybricks.git.submodule("update", "--init", "--checkout", "lib/btstack")
100-
pybricks.git.submodule("update", "--init", "--checkout", "lib/STM32_USB_Device_Library")
100+
pybricks.git.submodule(
101+
"update", "--init", "--checkout", "lib/STM32_USB_Device_Library"
102+
)
101103

102104
# Make mpy-cross once
103105
print("Building mpy-cross")

.github/download-commit-metadata.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ def main():
6565
)
6666
sizes = {
6767
item["RowKey"]: {
68-
k: v.value if isinstance(v, EntityProperty) else v for k, v in item.items() if k in HUBS
68+
k: v.value if isinstance(v, EntityProperty) else v
69+
for k, v in item.items()
70+
if k in HUBS
6971
}
7072
for item in firmware_size_table.query_entities(
7173
"PartitionKey eq 'size'",

bricks/ev3/make_bootable_image.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020

2121
def make_firmware(uboot_blob, uimage_blob):
22-
2322
if len(uboot_blob) > UBOOT_MAX_SIZE:
2423
print("u-boot file is bigger than expected. Using only the first 256KiB.")
2524
uboot_blob = uboot_blob[:UBOOT_MAX_SIZE]
@@ -30,7 +29,9 @@ def make_firmware(uboot_blob, uimage_blob):
3029
# Gets combined size, rounded to nearest expected size.
3130
combined_size = UIMAGE_OFFSET + len(uimage_blob)
3231
combined_size = (
33-
(combined_size + FIRMWARE_ROUND_SIZE) // FIRMWARE_ROUND_SIZE * FIRMWARE_ROUND_SIZE
32+
(combined_size + FIRMWARE_ROUND_SIZE)
33+
// FIRMWARE_ROUND_SIZE
34+
* FIRMWARE_ROUND_SIZE
3435
)
3536

3637
# Put it all together.
@@ -51,9 +52,11 @@ def make_firmware(uboot_blob, uimage_blob):
5152
parser.add_argument("output", help="The output file")
5253
args = parser.parse_args()
5354

54-
with open(args.uboot, "rb") as uboot_file, open(args.uimage, "rb") as uimage_file, open(
55-
args.output, "wb"
56-
) as output_file:
55+
with (
56+
open(args.uboot, "rb") as uboot_file,
57+
open(args.uimage, "rb") as uimage_file,
58+
open(args.output, "wb") as output_file,
59+
):
5760
combined = make_firmware(uboot_file.read(), uimage_file.read())
5861
output_file.write(combined)
5962
print(f"Created {args.output} with size {len(combined) // 1024} KB.")

bricks/primehub/modules/_imu_calibrate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,21 @@ def wait_for_stationary(side):
3636

3737

3838
def roll_over_axis(axis, new_side):
39-
4039
global bias, bias_count
4140

4241
print("Roll it towards you, without lifting the hub up!")
4342

4443
angle_start = hub.imu.rotation(axis, calibrated=False)
4544
while hub.imu.up(calibrated=False) != new_side or not hub.imu.stationary():
46-
4745
_, _, z = hub.imu.orientation() * axis
4846
if abs(z) > 0.07:
4947
print(hub.imu.orientation() * axis)
5048
raise RuntimeError("Lifted it!")
5149
wait(100)
5250

53-
uncalibrated_90_deg_rotation = abs(hub.imu.rotation(axis, calibrated=False) - angle_start)
51+
uncalibrated_90_deg_rotation = abs(
52+
hub.imu.rotation(axis, calibrated=False) - angle_start
53+
)
5454
if abs(uncalibrated_90_deg_rotation - 90) > 10:
5555
raise RuntimeError("Not 90 deg!")
5656

lib/pbio/cpython/pbio_virtual/physics/simulation.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ def actuate(self, t, u):
5151
u (array): Control signal vector.
5252
"""
5353
self.input_times = concatenate((self.input_times, array([t])))
54-
self.input_values = concatenate((self.input_values, u.reshape(self.m, 1)), axis=1)
54+
self.input_values = concatenate(
55+
(self.input_values, u.reshape(self.m, 1)), axis=1
56+
)
5557

5658
def simulate(self, time_end):
5759
"""Simulates the system until time_end, subject to the ongoing input.
@@ -84,7 +86,6 @@ def simulate(self, time_end):
8486

8587
# Evaluate RK4 integration for all time steps
8688
for i, t in enumerate(times):
87-
8889
# Save the state and output
8990
states[:, i] = state
9091
outputs[:, i] = self.output(t, state)
@@ -144,13 +145,14 @@ def get_output_at_time(self, time, tolerance):
144145
"""
145146
# If time is in the past, interpolate from available results.
146147
if time < self.times[-1]:
147-
148148
# Find first index larger than given time, which always exists.
149149
i = next(i for i, t in enumerate(self.times) if t > time)
150150

151151
# Interpolate output in time
152152
ratio = (time - self.times[i - 1]) / (self.times[i] - self.times[i - 1])
153-
return self.outputs[:, i - 1] + ratio * (self.outputs[:, i] - self.outputs[:, i - 1])
153+
return self.outputs[:, i - 1] + ratio * (
154+
self.outputs[:, i] - self.outputs[:, i - 1]
155+
)
154156

155157
# Return latest available data.
156158
return self.outputs[:, -1]

lib/pbio/cpython/pbio_virtual/platform/robot.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ def millidegrees_abs(self):
116116

117117

118118
class Platform:
119-
120119
# Ports and attached devices.
121120
PORTS = {
122121
PortId.A: IODeviceTypeId.TECHNIC_L_ANGULAR_MOTOR,
@@ -132,7 +131,6 @@ def on_poll(self, *args):
132131
self.clock[-1].tick()
133132

134133
def __init__(self):
135-
136134
# Initialize devices internal to the hub.
137135
self.battery = {-1: VirtualBattery()}
138136
self.button = {-1: VirtualButtons()}
@@ -157,7 +155,6 @@ def __init__(self):
157155
# Initialize motor simulation model.
158156
self.sim_motor[i] = None
159157
if type_id != IODeviceTypeId.NONE:
160-
161158
# Get current time.
162159
initial_time = self.clock[-1].microseconds / 1000000
163160

lib/pbio/cpython/pbio_virtual/platform/turtle.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ def _apply_state(self) -> None:
175175

176176
def _calc_new_output(self, new_timestamp: int) -> Tuple(int, int):
177177
delta = new_timestamp - self._state.timestamp
178-
new_count = self._state.count + self._state.duty_cycle * delta * self.MAX_SPEED / 1000000
178+
new_count = (
179+
self._state.count
180+
+ self._state.duty_cycle * delta * self.MAX_SPEED / 1000000
181+
)
179182
new_rate = self._state.duty_cycle * self.MAX_SPEED
180183

181184
return new_count, new_rate

lib/pbio/doc/control/motor_model.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ def make_model(name, *, V, tau_0, tau_x, w_0, w_x, i_0, i_x, a, Lm, h):
151151
model[L] = Lm
152152

153153
# Substitute parameters into model to get numeric system matrices
154-
exponent_numeric = numpy.array(exponent.subs(model).evalf().tolist()).astype(numpy.float64)
154+
exponent_numeric = numpy.array(exponent.subs(model).evalf().tolist()).astype(
155+
numpy.float64
156+
)
155157

156158
# Get matrix exponential and system matrices
157159
exponential = scipy.linalg.expm(exponent_numeric * h)
@@ -196,7 +198,6 @@ def make_model(name, *, V, tau_0, tau_x, w_0, w_x, i_0, i_x, a, Lm, h):
196198

197199

198200
if __name__ == "__main__":
199-
200201
print(HEADER)
201202

202203
print(

lib/pbio/test/animator/data_parser.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@
3737

3838
# Write the CSS component with frames.
3939
with open("../results/frames.css", "w") as frame_file:
40-
4140
for info in FRAME_INFO:
42-
4341
# CSS rows for each frame.
4442
frames = "".join(
4543
[
46-
f"{i * 100 // (len(angles) - 1)}% {{transform: translate({info.x}px, {info.y}px) rotate( {int(row[info.index])// info.gearing}deg );}}\n"
44+
f"{i * 100 // (len(angles) - 1)}% {{transform: translate({info.x}px, {info.y}px) rotate( {int(row[info.index]) // info.gearing}deg );}}\n"
4745
for i, row in enumerate(angles)
4846
]
4947
)

0 commit comments

Comments
 (0)