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

avoid using print() function #141

Open
gowhari opened this issue Aug 19, 2024 · 0 comments
Open

avoid using print() function #141

gowhari opened this issue Aug 19, 2024 · 0 comments

Comments

@gowhari
Copy link

gowhari commented Aug 19, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant