Ok, at the risk of being ridiculed for not ‘trying harder’, I have a scenario that I’ve been attempting to adapt to a pythonic switch case statement. I know python has the new match
method in 3.10 but I’m limited to 3.8.10 in my AWS use case. I’ve been reading up on switch cases in other languages and I want to find a pythonic way to convert the following jumbled mess of if/elif/else
statements to a clean switch case. I am wondering what others would do in this scenario
OBJECTIVE: I have a filename that will be passed into this sequence of code and I need to return the first three items (i.e. transaction_recipient_verification
, transaction_account_tokenization
, etc). Occasionally the code will receive a filename containing “field_results” or “issuers” and I need to make sure that the trimmed return string contains the respective case.
import random sampleKeys = [ 'transaction_recipient_notification_status_sent/transaction_recipient_notification_status_sent_2021_10_29_12_02_14.snappy', 'transaction_recipient_payment_status_success/transaction_recipient_payment_status_success_2021_10_29_12_02_14.snappy', 'transaction_recipient_verification_rvdm_failure/transaction_recipient_verification_rvdm_failure_2021_10_29_12_02_14.snappy', 'transaction_recipient_verification_rvdm_failure_field_results/transaction_recipient_verification_rvdm_failure_2021_10_29_12_02_14.snappy', 'transaction_recipient_authentication_status_success/transaction_recipient_authentication_status_success_2021_10_29_12_02_14.snappy', 'transaction_recipient_authentication_status_success_field_results/transaction_recipient_authentication_status_success_2021_10_29_12_02_14.snappy', 'transaction_account_tokenization_success/transaction_account_tokenization_success_2021_10_29_12_02_14.snappy', 'transaction_account_tokenization_success_issuers/transaction_account_tokenization_success_2021_10_29_12_02_14.snappy', 'transaction_recipient_payment_status_terminated/transaction_recipient_payment_status_terminated_2021_10_29_12_02_14.snappy', 'transaction_recipient_verification_rvdm_success/transaction_recipient_verification_rvdm_success_2021_10_29_12_02_14.snappy', 'transaction_recipient_verification_rvdm_success_field_results/transaction_recipient_verification_rvdm_success_2021_10_29_12_02_14.snappy', 'transaction_recipient_notification_status_received/transaction_recipient_notification_status_received_2021_10_29_12_02_14.snappy', 'transaction_recipient_authentication_status_success/transaction_recipient_authentication_status_success_2021_10_29_11_17_45.snappy' ] key = random.choice(sampleKeys) array_data = any(substring in key for substring in ['_issuers', '_field_results']) if not array_data: if 'transaction_recipient_notification' in key: keySubject = 'transaction_recipient_notification' elif 'transaction_recipient_authentication' in key: keySubject = 'transaction_recipient_authentication' elif 'transaction_recipient_verification' in key: keySubject = 'transaction_recipient_verification' elif 'transaction_account_verification' in key: keySubject = 'transaction_account_verification' elif 'transaction_account_tokenization' in key: keySubject = 'transaction_account_tokenization' elif 'transaction_recipient_payment' in key: keySubject = 'transaction_recipient_payment' else: if '_issuers' in key: if 'transaction_recipient_notification' in key: keySubject = 'transaction_recipient_notification_issuers' elif 'transaction_recipient_authentication' in key: keySubject = 'transaction_recipient_authentication_issuers' elif 'transaction_recipient_verification' in key: keySubject = 'transaction_recipient_verification_issuers' elif 'transaction_account_verification' in key: keySubject = 'transaction_account_verification_issuers' elif 'transaction_account_tokenization' in key: keySubject = 'transaction_account_tokenization_issuers' elif 'transaction_recipient_payment' in key: keySubject = 'transaction_recipient_payment_issuers' elif '_field_results' in key: if 'transaction_recipient_notification' in key: keySubject = 'transaction_recipient_notification_field_results' elif 'transaction_recipient_authentication' in key: keySubject = 'transaction_recipient_authentication_field_results' elif 'transaction_recipient_verification' in key: keySubject = 'transaction_recipient_verification_field_results' elif 'transaction_account_verification' in key: keySubject = 'transaction_account_verification_field_results' elif 'transaction_account_tokenization' in key: keySubject = 'transaction_account_tokenization_field_results' elif 'transaction_recipient_payment' in key: keySubject = 'transaction_recipient_payment_field_results' print(f'BEFORE ===> {key}') print(f'AFTER ===> {keySubject}')
Possible Direction:
import re class MainKeyHandleSwitch: ARRAY_OPTIONS = ['_issuers', '_field_results'] def __init__(self,key): self._original_key = key self._array_data = any(substring in key for substring in self.ARRAY_OPTIONS) self._trimmed_dict = self.trimmed_dict() @property def get_trimmed_dict(self): return self._trimmed_dict @property def get_trimmed_key(self): return self.__get_key_subject__() def trimmed_dict(self): trim_dict = dict() trim_dict['case_one'] = re.search('transaction_recipient_notification+', self._original_key) trim_dict['case_two'] = re.search('transaction_recipient_authentication+', self._original_key) trim_dict['case_three'] = re.search('transaction_recipient_verification+', self._original_key) trim_dict['case_four'] = re.search('transaction_account_verification+', self._original_key) trim_dict['case_five'] = re.search('transaction_account_tokenization+', self._original_key) trim_dict['case_six'] = re.search('transaction_recipient_payment+', self._original_key) return trim_dict def __get_key_subject__(self): obj = next(item for item in list(self._trimmed_dict.values()) if item is not None) if not self._array_data: return obj.group(0) else: if '_issuers' in self._original_key: return f'{obj.group(0)}_issuers' elif '_field_results' in self._original_key: return f'{obj.group(0)}_field_results'
And the code to test the class:
import random key = random.choice(sampleKeys) print(f'before ===> {key}') a = MainKeyHandleSwitch(key) trimmed_key = a.get_trimmed_key print(f'after ===> {trimmed_key}')
Advertisement
Answer
I see a lot of repetition in your code, so the first thing I’ll think of would be: “can I use a loop to simply this code?” and the answer is yes!
Since your code repeatedly used the six subjectTypes
and the keySubject
depends on the subject type, creating a list of the six types then use next()
with a generator expression should simplify the over abundance of if
‘s (If there weren’t any correlations, a dictionary would work instead). Also, instead of array_data
, you can use an if-elif-else
clause to prevent an extra block level.
sampleKeys = [...] key = random.choice(sampleKeys) subjectTypes = ['transaction_recipient_notification', 'transaction_recipient_authentication', 'transaction_recipient_verification', 'transaction_account_verification', 'transaction_account_tokenization', 'transaction_recipient_payment'] if '_issuers' in key: keySubject = next(t + '_issuers' for t in subjectTypes if t in key) elif '_field_results' in key: keySubject = next(t + '_field_results' for t in subjectTypes if t in key) else: keySubject = next(t for t in subjectTypes if t in key) print(f'BEFORE ===> {key}') print(f'AFTER ===> {keySubject}')