"""Static Transaction Type data.""" import csv import pkg_resources CURRENT_CSV_FILE = pkg_resources.resource_filename( 'abacus_common_data', 'data/csv/transaction_types.csv') class TransactionType: """Transaction Type class. Datasource: snowflake's DIM_TRANSACTIONTYPE.""" transaction_type_map = {} def __init__(self, txn_type_code): """Find transaction type by code. Raises KeyError on unknown code.""" TransactionType._load_csv_data() self.transaction_type = TransactionType._get_data(txn_type_code) @staticmethod def parse_csv(file_path): """Build a keyed dict of transaction type data.""" transaction_type_map = {} with open(CURRENT_CSV_FILE, encoding='utf-8') as csv_file: csv_reader = csv.reader(csv_file) next(csv_reader, None) for row in csv_reader: txn_type_id = int(row[0]) txn_type_code = row[1] txn_type_desc = row[2] transaction_type_map[txn_type_code] = { 'txn_type_id': txn_type_id, 'txn_type_code': txn_type_code, 'txn_type_desc': txn_type_desc } return transaction_type_map @classmethod def _get_data(cls, code): """Force a key error if not found.""" return cls.transaction_type_map[code] @classmethod def _load_csv_data(cls): """Initialize the class, loading data in from CSV file.""" if cls.transaction_type_map: return cls.transaction_type_map = TransactionType.parse_csv(CURRENT_CSV_FILE) @property def txn_type_code(self): """Transaction type code.""" return self.transaction_type.get('txn_type_code') @property def txn_type_id(self): """Transaction type id.""" return self.transaction_type.get('txn_type_id') @property def txn_type_desc(self): """Transaction type description.""" return self.transaction_type.get('txn_type_desc') @classmethod def list_all_txn_types(cls): """Return an array of all transaction types.""" TransactionType._load_csv_data() list_of_types = [] for _, transaction_type in cls.transaction_type_map.items(): list_of_types.append(transaction_type) return list_of_types