avoid using print() function
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 68/100
Research direction
Open brping/device.py and inspect connect_serial(), including the two print calls and the existing exception path. Exercise the missing-device and successful-open cases to confirm that the former raises as described and the latter no longer writes to stdout.
Written by the indexing model from the issue text.
Description
This is just a copy of connect_serial() method from brping/device.py (ver 0.1.5)
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:
raise ValueError("Device name is required")
And the next print is indeed a logging:
logger.info("Opening %s at %d bps", device_name, baudrate)
I've had to use workarounds to prevent these prints from being sent to my app's stdout:
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
- Dominant language
- Python
- Stars
- 60
- Forks
- 39
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from bluerobotics/ping-python
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
bluerobotics/ping-python#74 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 25/100
bluerobotics/ping-python#171 ·
-
set sound speed Open
bluerobotics/ping-python#169 · 2 comments · 1 assignee ·
-
Code after return Open
Difficulty 2/5 1-3 hours Newbie friendliness 48/100
bluerobotics/ping-python#167 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 32/100
bluerobotics/ping-python#159 ·
All issues in bluerobotics/ping-python
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100