bluerobotics / bluerobotics/ping-python
avoid using print() function
- Linguagem predominante
- Python
- Estrelas
- 60
- Forks
- 39
- Métricas de merge de PRs
- Nenhum PR com merge em 30d
Descrição
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
Guia de contribuição
Nenhum guia de contribuição indexado para este repositório
Direção de pesquisa
Abra brping/device.py e inspecione connect_serial(), incluindo as duas chamadas a print e o caminho de exceção existente. Exercite os casos de dispositivo ausente e de abertura bem-sucedida para confirmar que o primeiro gera uma exceção conforme descrito e que o segundo não escreve mais em stdout.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- python
- Domínio
- backend
- Tipo de issue
- Refatoração
- Dificuldade
- 2/5
- Tempo estimado
- 1-3 horas
- Status de atividade
- Pouca atividade
- Clareza
- Claramente especificada
- Facilidade para iniciantes
- 68/100