bluerobotics / bluerobotics/ping-python

avoid using print() function

未關閉 適合新手
#141 2 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Python
星號
60
分支
39
PR 合併指標
30 天內沒有已合併 PR

描述

This is just a copy of connect_serial() method from brping/device.py (ver 0.1.5)

```py
def connect_serial(self, device_name: str, baudrate: int =115200):
if device_name is None:
print("Device name is required")
return

try:
print("Opening %s at %d bps" % (device_name, baudrate))

## Serial object for device communication
# write_timeout fixes it getting stuck forever atempting to write to
# /dev/ttyAMA0 on Raspberry Pis, this raises an exception instead.
self.iodev = serial.Serial(device_name, baudrate, write_timeout=1.0)
self.iodev.send_break()
time.sleep(0.001)
self.iodev.write("U".encode("ascii"))

except Exception as exception:
raise Exception("Failed to open the given serial port: {0}".format(exception))
```

The request here is:
Kindly avoid using the `print` function in a library like this. If the device name is required, it should be handled as an exception, not with a `print` and `return`:

```py
raise ValueError("Device name is required")
```

And the next `print` is indeed a logging:

```py
logger.info("Opening %s at %d bps", device_name, baudrate)
```

I've had to use workarounds to prevent these `print`s from being sent to my app's stdout:

```py
def brping_muted_print(*args, **kw):
if len(args) == 1:
args = args[0]
log('brping: %s', args)
# brping module uses raw print statements for logging
# with this trick, we turn them into proper logs
brping.device.print = brping_muted_print
```

Thanks

貢獻指南

這個儲存庫沒有索引到貢獻指南

研究方向

開啟 brping/device.py 並檢查 connect_serial(),包括兩個 print 呼叫和現有的例外路徑。測試裝置遺失和成功開啟這兩種情況,以確認前者會如描述般引發例外,而後者不再寫入 stdout。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend
Issue 類型
重構
難度
2/5
預估耗時
1-3 小時
活躍度
冷清
描述清晰度
描述清楚
新手友好度
68/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。