Reputation: 361
I'm going to clarify.
I'm logging 6 different variables and some of them have errors (what kind of error doesn't matter). When that error occurs, I just want that specific variable to be put to "NA".
Here's an idea of what I mean:
myDict = []
for i in data:
try:
eye = ...
nose = ...
mouth = ...
ear = ...
hair = ...
tongue = ...
myDict.append([eye, nose, mouth, ear, hair, tongue])
except eye:
eye = "NA"
myDict.append([eye, nose, mouth, ear, hair, tongue])
except nose:
nose = "NA"
myDict.append([eye, nose, mouth, ear, hair, tongue])
except mouth:
mouth = "NA"
myDict.append([eye, nose, mouth, ear, hair, tongue])
...
Do I have to do an "except" for every single variable? Is there some way I could just do "except whatever variable has error, assign its value to "NA" and append normally"?
I also don't know what happens if there's an error in more than 1 variable.
The fundamental idea is: "if there's an error for a variable(S), just assign it/them the value "NA" and keep appending.
Upvotes: 0
Views: 842
Reputation: 9379
Here's an example of an approach that will do what you're asking.
First some comments:
myDict
which is actually a python list; I will rename this result
in the code below to avoid confusion.The logic of the code below can be summarized as:
eye = ...
or mouth = ...
in your question) and append the result to a list L
, unless an exception is raised, in which case the try block instead appends "NA" to L
;L
will have a value appended to it for each variable tag;L
to the result.Here is the sample code:
class eye_exception(Exception):
pass
class nose_exception(Exception):
pass
class mouth_exception(Exception):
pass
class ear_exception(Exception):
pass
class hair_exception(Exception):
pass
class tongue_exception(Exception):
pass
def getValueForEye(i):
return "eye_value" + str(i)
def getValueForNose(i):
return "nose_value" + str(i)
def getValueForMouth(i):
if i % 3 == 0:
raise mouth_exception()
return "mouth_value" + str(i)
def getValueForEar(i):
return "ear_value" + str(i)
def getValueForHair(i):
if i % 3 != 0:
raise hair_exception()
return "hair_value" + str(i)
def getValueForTongue(i):
return "tongue_value" + str(i)
data = [1, 2, 3]
result = []
for i in data:
L = []
for key in ['eye', 'nose', 'mouth', 'ear', 'hair', 'tongue']:
try:
match key:
case 'eye':
value = getValueForEye(i)
case 'nose':
value = getValueForNose(i)
case 'mouth':
value = getValueForMouth(i)
case 'ear':
value = getValueForEar(i)
case 'hair':
value = getValueForHair(i)
case 'tongue':
value = getValueForTongue(i)
L.append(value)
except (eye_exception, nose_exception, mouth_exception, ear_exception, hair_exception, tongue_exception):
L.append("NA")
result.append(L)
Sample result
:
['eye_value1', 'nose_value1', 'mouth_value1', 'ear_value1', 'NA', 'tongue_value1']
['eye_value2', 'nose_value2', 'mouth_value2', 'ear_value2', 'NA', 'tongue_value2']
['eye_value3', 'nose_value3', 'NA', 'ear_value3', 'hair_value3', 'tongue_value3']
Alternatively, if you are using a version of python that does not support the match/case construct, or you simply prefer not to use it, you can replace the loop above with this code which uses a dictionary to map from variable tag to function:
funcDict = {
'eye':getValueForEye,
'nose':getValueForNose,
'mouth':getValueForMouth,
'ear':getValueForEar,
'hair':getValueForHair,
'tongue':getValueForTongue
}
for i in data:
L = []
for key, func in funcDict.items():
try:
value = func(i)
L.append(value)
except (eye_exception, nose_exception, mouth_exception, ear_exception, hair_exception, tongue_exception):
L.append("NA")
result.append(L)
Upvotes: 1