简体   繁体   中英

How to use try except in case of a dictionary with multiple values?

I am using parallel processing to generate a dictionary as the following:

def transform(x):
    result = {'name': x.name, 'result_cap1': NLTK.parser(cap1), 
            'result_cap2': NLTK.parser(cap2)}  

    return result


final_result = tuple(map(transform, mylist))

But the problem is something may go wrong with the NLTK.parser() so for that particular value I want to change it to " " . I was able to come up with something like this:

def transform(x):
    try:
        result = {'name': x.name, 'result_cap1': NLTK.parser(cap1), 
                'result_cap2': NLTK.parser(cap2)}  
    except Exception:
        result = {'name': x.name, 'result_cap1': " ", 
                'result_cap2': " "}

    return result

But the problem is I am not sure if the error was because of cap1 or cap2 , so I am assigning both of them to " " . How to assign " " to the value that cause the error only?

Just do it in two steps, like this:

def transform(x):
    result = {'name': x.name};
    try:
       result['result_cap1'] = NLTK.parser(cap1)
    except:
        result['result_cap1'] = " "
    try:
        result['result_cap2'] = NLTK.parser(cap2)
    except:
        result['result_cap2'] = " "

    return result 

Put your exception handlers around the NLTK.parser() calls separately , by assigning their results to variables first:

try:
    result_cap1 = NLTK.parser(cap1)
except Exception:
    result_cap1 = " "

try:
    result_cap2 = NLTK.parser(cap2)
except Exception:
    result_cap2 = " "

result = {'name': x.name, 'result_cap1': result_cap1, 'result_cap2': result_cap2}

or you can first create the dictionary (with defaults) and assign to the keys:

result = {'name': x.name, 'result_cap1': " ", 'result_cap2': " "}


try:
    result['result_cap1'] = NLTK.parser(cap1)
except Exception:
    pass

try:
    result['result_cap2'] = NLTK.parser(cap2)
except Exception:
    pass

You really want to find more specific exceptions to catch, however; surely a more specific ValueError or TypeError can be caught instead.

You also need to take into account that some NLTK parsers can return a generator from a .parse() call; if you are to re-use the parser for multiple inputs, you probably want to convert the generator object to a list first.

To minimize the amount of repetitious code and follow the software development principle of DRY , I would suggest doing something along these line which utilizes a helper function:

def transform(x):

    def parse(cap):
        """ Nested helper function. """
        try:
            result = NLTK.parser(cap)
        except Exception:
            result = " "
        return result

    return {'name': x.name,
            'result_cap1': parse(cap1),
            'result_cap2': parse(cap2)}

final_result = tuple(map(transform, mylist))

The helper function doesn't have to be nested inside transform() , but doing so make it more obvious it's only being used with that function.

Also note that it's generally a poor programming practice to handle such generic exception in an except clause because it can hide problems you probably wouldn't want ignored, such as a SyntaxError . In other words, it would be better to be more specific and explicitly state the kind to ignore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM