A bytes-like object is required, not 'str' (not sending to dataOutput) - python-3.x

I have been automating some, and got a problem when migrating from 2.7 to 3.7. The code is as shown
def writeDataToOutput(dataOutput):
global myUartPort
global mySocket
if (m_UseRTT):
try:
mySocket.sendall(dataOutput)
except socket.error:
print('Send failed')
else:
myUartPort.write(dataOutput)
And when running i get this error message
Traceback (most recent call last):
File "monkeytest.py", line 1324, in <module>
executeScript()
File "monkeytest.py", line 1255, in executeScript
if executeCommandLine(line) == 0:
File "monkeytest.py", line 1159, in executeCommandLine
executeCommand(commandPayload, 1, 1)
File "monkeytest.py", line 587, in executeCommand
writeDataToOutput(command + "\r\n")
File "monkeytest.py", line 309, in writeDataToOutput
mySocket.sendall(dataOutput)
TypeError: a bytes-like object is required, not 'str'
I cannot see the wrong in my method, I have tried sendto() without any success.

Related

AttributeError: 'builtin_function_or_method' object has no attribute 'acquire'

In this video: https://www.youtube.com/watch?v=HGOBQPFzWKo, around 4:16:00, it's talked about how to implement a lock for threading issues. But as opposed to the video, I get a debug message when trying to acquire the lock.
This is a part of the code I tried out for myself:
def increase(lock):
global database_value
lock.acquire()
local_copy = database_value
local_copy += 1
time.sleep(0.1)
database_value = local_copy
lock.release()
And this is the debug message:
Exception in thread Thread-32:
Traceback (most recent call last):
File "C:\Users\Gebruiker\miniconda3\lib\threading.py", line 973, in _bootstrap_inner
Exception in thread Thread-33:
Traceback (most recent call last):
File "C:\Users\Gebruiker\miniconda3\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\Users\Gebruiker\miniconda3\lib\threading.py", line 910, in run
self.run()
File "C:\Users\Gebruiker\miniconda3\lib\threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Gebruiker\AppData\Local\Temp\ipykernel_75580\1082204269.py", line 14, in increase
self._target(*self._args, **self._kwargs)
File "C:\Users\Gebruiker\AppData\Local\Temp\ipykernel_75580\1082204269.py", line 14, in increase
AttributeError: 'builtin_function_or_method' object has no attribute 'acquire'
AttributeError: 'builtin_function_or_method' object has no attribute 'acquire'
In the Jupyter Notebook, there seems to be raised an exception when I try to run the thread.
What's going wrong here?
Thanks in advance!
I tried to do what the video did, I expected it to work, but there's something not working in the thread apparently.

Running Faust with kafka crashes with ConsumerStoppedError

I freshly installed Faust and ran a basic program to send and receive messages over Kafka.I used the sample code mentioned in (Faust example of publishing to a kafka topic) While running the program initially it connects to Kafka(which is also running on my machine). But then while trying to consume Kafka gets disconnected and the app crashes with the below exception
[2020-11-11 07:08:26,623] [76392] [ERROR] [^---Fetcher]: Crashed reason=ConsumerStoppedError()
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mode/services.py", line 779, in _execute_task
await task
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/faust/transport/consumer.py", line 176, in _fetcher
await self._drainer
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/faust/transport/consumer.py", line 1039, in _drain_messages
async for tp, message in ait:
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/faust/transport/consumer.py", line 640, in getmany
records, active_partitions = await self._wait_next_records(timeout)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/faust/transport/consumer.py", line 676, in _wait_next_records
records = await self._getmany(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/faust/transport/consumer.py", line 1269, in _getmany
return await self._thread.getmany(active_partitions, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/faust/transport/drivers/aiokafka.py", line 805, in getmany
return await self.call_thread(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mode/threads.py", line 436, in call_thread
result = await promise
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mode/threads.py", line 383, in _process_enqueued
result = await maybe_async(method(*args, **kwargs))
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mode/utils/futures.py", line 134, in maybe_async
return await res
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/faust/transport/drivers/aiokafka.py", line 822, in _fetch_records
raise ConsumerStoppedError()
On debugging the reason for the consumer getting disconnected I see that in fetcher.py of alokafka consumer is the connection getting closed due to the below exception
Unable to display children:Error resolving variables Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_resolver.py", line 205, in resolve
def resolve(self, dict, key):
KeyError: 'Exception'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_comm.py", line 1227, in do_it
def do_it(self, dbg):
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_vars.py", line 262, in resolve_compound_variable_fields
def resolve_compound_variable_fields(thread_id, frame_id, scope, attrs):
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_vars.py", line 169, in getVariable
def getVariable(thread_id, frame_id, scope, attrs):
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_resolver.py", line 205, in resolve
def resolve(self, dict, key):
AttributeError: 'dict' object has no attribute 'Exception'
The software versions are as given below
Mac OS : 10.15.4
Kafka : 2_12.2.1.1
Aiokafka: 1.1.6
Python : 3.9.0
Faust : 1.10.4
Please help here.
I think your problem is the same mine I was with python 3.9 and I changed to 3.8 now It works.

Python H2O script: TypeErrors at end/closing

The file h2otest.py contains a shortened version of code on https://docs.h2o.ai/h2o/latest-stable/h2o-docs/data-science/glm.html:
import h2o
h2o.init()
from h2o.estimators.glm import H2OGeneralizedLinearEstimator
prostate = h2o.import_file("https://h2o-public-test-data.s3.amazonaws.com/smalldata/prostate/prostate.csv")
prostate['CAPSULE'] = prostate['CAPSULE'].asfactor()
prostate['RACE'] = prostate['RACE'].asfactor()
prostate['DCAPS'] = prostate['DCAPS'].asfactor()
prostate['DPROS'] = prostate['DPROS'].asfactor()
predictors = ["AGE", "RACE", "VOL", "GLEASON"]
response_col = "CAPSULE"
glm_model = H2OGeneralizedLinearEstimator(family="binomial", lambda_= 0, compute_p_values=True)
glm_model.train(predictors, response_col, training_frame= prostate)
print('Finished')
I run the code on the command line in a Conda environment pycharm, where h2o 3.30.1.3 was installed from PyPi. I am using Python 3.8. I get the same error both on my Mac and a Linux server. There is no existing H2O server at start up :
(pycharm) $> python h2otest.py
The code does it's job, but gives error messages at the end:
...
Parse progress: |█████████████████████████████████████████████████████████████████████████████| 100%
glm Model Build progress: |███████████████████████████████████████████████████████████████████| 100%
Finished
Closing connection _sid_a7a8 at exit
H2O session _sid_a7a8 closed.
Exception ignored in: <function ExprNode.__del__ at 0x7f9ee65f83a0>
Traceback (most recent call last):
File "/Users/geiringesandnes/miniconda3/envs/pycharm/lib/python3.8/site-packages/h2o/expr.py", line 204, in __del__
File "/Users/geiringesandnes/miniconda3/envs/pycharm/lib/python3.8/site-packages/h2o/expr.py", line 258, in rapids
TypeError: 'NoneType' object is not callable
Exception ignored in: <function ExprNode.__del__ at 0x7f9ee65f83a0>
Traceback (most recent call last):
File "/Users/geiringesandnes/miniconda3/envs/pycharm/lib/python3.8/site-packages/h2o/expr.py", line 204, in __del__
File "/Users/geiringesandnes/miniconda3/envs/pycharm/lib/python3.8/site-packages/h2o/expr.py", line 258, in rapids
TypeError: 'NoneType' object is not callable
Exception ignored in: <function ExprNode.__del__ at 0x7f9ee65f83a0>
Traceback (most recent call last):
File "/Users/geiringesandnes/miniconda3/envs/pycharm/lib/python3.8/site-packages/h2o/expr.py", line 204, in __del__
File "/Users/geiringesandnes/miniconda3/envs/pycharm/lib/python3.8/site-packages/h2o/expr.py", line 258, in rapids
TypeError: 'NoneType' object is not callable
Exception ignored in: <function ExprNode.__del__ at 0x7f9ee65f83a0>
Traceback (most recent call last):
File "/Users/geiringesandnes/miniconda3/envs/pycharm/lib/python3.8/site-packages/h2o/expr.py", line 204, in __del__
File "/Users/geiringesandnes/miniconda3/envs/pycharm/lib/python3.8/site-packages/h2o/expr.py", line 258, in rapids
TypeError: 'NoneType' object is not callable
How can I get rid of those error messages?

How to solve the error: TypeError: a bytes-like object is required, not 'str' with FLASK-MAIL?

In one of my FLASK projects, I use flask-mail to send emails. I tried to attach a log file to the mail but I receive an error: TypeError: a bytes-like object is required, not 'str'. Here is the complete error:
Exception in thread Thread-37:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/home/Tobin/Documents/PYTHON-PROJECTS/FLASK CHAOS V1/app/email.py", line 7, in send_async_email
mail.send(msg)
File "/home/Tobin/.local/lib/python3.7/site-packages/flask_mail.py", line 492, in send
message.send(connection)
File "/home/Tobin/.local/lib/python3.7/site-packages/flask_mail.py", line 427, in send
connection.send(self)
File "/home/Tobin/.local/lib/python3.7/site-packages/flask_mail.py", line 190, in send
message.as_bytes() if PY3 else message.as_string(),
File "/home/Tobin/.local/lib/python3.7/site-packages/flask_mail.py", line 385, in as_bytes
return self._message().as_bytes()
File "/home/Tobin/.local/lib/python3.7/site-packages/flask_mail.py", line 349, in _message
f = MIMEBase(*attachment.content_type.split('/'))
TypeError: a bytes-like object is required, not 'str'
Here is my code:
from threading import Thread
from flask_mail import Message
from app import app, mail
def send_async_email(app, msg):
with app.app_context():
mail.send(msg)
def send_email(subject, sender, recipients, text_body):
msg = Message(subject, sender=sender, recipients=recipients)
msg.body = text_body
with app.open_resource("../logs/chaos.log") as fp:
msg.attach(b'../logs/chaos.log', fp.read())
Thread(target=send_async_email, args=(app, msg)).start()
I read in a previous post that it was not possible to open a file in binary mode and treat it as str. One solution would be to use a byte object (msg.attach(b'../logs/chaos.log', fp.read())). but that does not seem to solve my problem. apparently there is an essential notion that escapes me, so how to solve this bug?
You are supplying wrong arguments to msg.attach(). See the API for the correct parameters. The first argument should be a filename, second a content type, third the data. So the call should read something like this:
with app.open_resource("../logs/chaos.log") as fp:
msg.attach('chaos.log', 'text/plain', fp.read())

python 2/3 compatibility issue with exception

I wrote the following code that works with python3
try:
json.loads(text)
except json.decoder.JSONDecodeError:
(exception handling)
However, if I use python2, when json.loads throws the exception I get:
File "mycode.py", line xxx, in function
except json.decoder.JSONDecodeError:
AttributeError: 'module' object has no attribute 'JSONDecodeError'
And actually, https://docs.python.org/2/library/json.html doesn't mention any JSONDecodeError exception, while https://docs.python.org/3/library/json.html does.
How can I have the code running both with python 2 and 3?
In Python 2 json.loads raises ValueError:
Python 2.7.9 (default, Sep 17 2016, 20:26:04)
>>> json.loads('#$')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
You can try to use json.decoder.JSONDecodeError. If it fails you will know that you need to catch ValueError:
try:
json_parse_exception = json.decoder.JSONDecodeError
except AttributeError: # Python 2
json_parse_exception = ValueError
Then
try:
json.loads(text)
except json_parse_exception:
(exception handling)
Will work in either case.

Resources