This is a function of my project (which I cannot share in full) for work which is constantly failing. We're working on a multi-use system for Data Acquisition and motor control using a Raspberry Pi. I set up a large try:/except: system for both debugging and if our users give faulty data somehow.
The function iterates successfully through data provided, setting 0b0
and 0b1
for each text '0' or '1' fed to it. However, once it successfully converts to "true" binary, it fails when iterating through groups of eight to convert them (this is because, for some reason, other attempts with int(x, base)
have thrown the TypeError). The error thrown is not any of the following:
- TypeError
- ArithmeticError
- RuntimeError (which it should fall back to, right?)
- EnvironmentError
- UnboundLocalError
- LookupError
- ValueError
- IndexError
Running the for i in range(0, floor(length / 8), 1):
and subcode also throws an error, but it is not described - it just fails. I have been debugging this singular function for a few days, and this seems to be the last problem, but I honestly have no idea why it is failing. The function, convertStates(data)
, is below.
def convertStates(data):
# Converts binary states (0/1) in any format (due to built-in functions not working) to text.
n = 0
dmn = str(data)
dat = '0b0'
datFirst = True
try:
for i in dmn:
toapp = '0b0' if (i == 0) else '0b1'
if (datFirst):
dat = toapp
datFirst = False
else:
dat.join(toapp)
except:
print("Error: Failed to append binary values to list.")
exit()
try:
length = len(dat)
except:
print("Error acquiring length of binary data")
exit()
try:
for i in (range(0, floor(length / 8), 1)): <<<< SOMETHING HERE
try:
temp = int(dat[i*8:i*8+8],2) # this is perfectly fine
except:
print("Error with byte storage")
try:
a = temp.decode("utf-8")
print(str(a))
except:
print("Error with decoder")
try:
n += chr(a)
except:
print("Error with character converter")
except ValueError:
print("Exception occurred in 'convertStates(data)' function!")
print("Error: Could not convert binary->string due to value mismatch.")
exit()
except TypeError:
print("Exception occurred in 'convertStates(data)' function!")
print("Error: Could not convert binary->string due to value type mismatch.")
exit()
except UnboundLocalError:
print("Exception occurred in 'convertStates(data)' function!")
print("Error: Could not convert binary->string due to unbound variable(s).")
exit()
except IndexError:
print("Exception occurred in 'convertStates(data)' function!")
print("Error: Could not convert binary->string due to an index call for nonexistent value.")
exit()
except ArithmeticError:
print("Exception occurred in 'convertStates(data)' function!")
print("Error: Could not convert binary->string due to math error.")
exit()
except LookupError:
print("Exception occurred in 'convertStates(data)' function!")
print("Error: Could not convert binary->string due to lookup error.")
exit()
except:
print("Exception occurred in 'convertStates(data)' function!")
print("Error: Could not convert binary bytewise to string.")
exit()
return n
As I stated earlier, the try:/except: systems have been put in place to identify the error on my part, and to lay groundwork for end-user problem solving.
I have looked at upwards of ten different posts surrounding this, to no avail. I cannot find the error with this, and I had to set it up this way because int(x, base)
could not convert anything - refer to this post here (Convert string to binary in python), which is one solution I tried to use.
As for the exit()
calls, it's so we don't get an endless loop of twenty exceptions piling atop each other, as this function is one called in part of a loop.
What am I doing wrong, and how do I fix it? Was it some stupid little thing I should've noticed (Python is not my top language), or is it something else entirely? Any help is appreciated. This is just driving me slightly insane - nothing I've looked at can solve it or the problems that caused me to make this.
EDIT: People have said to put the callback w/o the bare except:
, and as I said earlier, it gives nothing. It just fails. No error code, no traceback, nothing. I am being honest here - removed the except:
from test code and from loop. All it does is exit, when it should continue running.