Search code examples
pythonconditional-statementstry-catch

Conditional exception handling in Python


I'm processing transactions from a list to make individual data items more easily accessible.

I've set up the below code to process a list of transactions item by item with a try-except block to exclude problem transactions for the time being.

A sample transaction might look like the below (ie, t):

'(GOOGL)  [ST]S (partial) 01/25/2024 02/01/2024 $1,001 - $15,000'
import re as rx  

patterns = {
    "ticker": r"\(([A-Z]+)",            
    "asset_type": r"\[([A-Z]+)",
    "transaction_type": r"\([^)]*\)\s*\[[^\]]*\]\s*([A-Z])\s",
    "amount": r"(\$[-\$\d, ]+)",
}            

for t in transactions:
    try:
        data = {}
        
        data['date'], data['trans_date'] = rx.findall(r'(\d+/\d+/\d+)', t)             
                            
        for key, pattern in patterns.items():
            data[key] = rx.search(pattern, t).group(1)                        
       
        transaction_data.append(data)
    except Exception as e:
        print(f"{data}, {e}")
        continue

The above code parses the transaction into parts (eg, ticker: 'GOOG', asset_type: 'ST', etc), and I've included the try catch to simply omit transactions that cause issues with parsing for the time being.

I'd like to add an exception to the try-catch, whereby if the exception is thrown in attempting to parse t for key equal to 'transaction_type', I can simply assign a default value instead of omitting the entire transaction. Eg, if the exception is thrown for key == 'asset_type', simply set data[key] = 'ST' and move on to the next pattern item.

Is there a way to do this.


Solution

  • Don't do the condition in the exception handling, just use regular if/then logic.

    Replace

            for key, pattern in patterns.items():
                data[key] = rx.search(pattern, t).group(1)                        
    

    with

            for key, pattern in patterns.items():
                match = rx.search(pattern, t)
                if key == 'transaction_type' and not match:
                    data[key] = 'ST' # Use default for missing transaction type
                else:
                    data[key] = match.group(1)