def __translate(self, keyword: str, target_date: date, arguments: list[str] = None) -> str: parts = keyword.split('$') factor_index = 1 variable_type = VariableTranslator.VariableType.Date translate_market_access: MarketDateAccess = self.__market_access date_parameter: date str_list = [] supported_market = VariableTranslator.__get_supported_market() for p in parts: if p.isdecimal(): factor_index = int(p) continue if p in supported_market: translate_market_access = MarketDateAccess(p) continue if p in self.variable_date_parameter_type: date_parameter = self.__translate_date(translate_market_access=translate_market_access, target_date=target_date, date_parameter=p, factor_index=factor_index) elif p == 'PARAMMASTER': # Retrieve from DB Parameter variable_type == VariableTranslator.VariableType.ParamMaster elif p == 'PARAMDICTIONARY': # return dictionary value based on specified key. Dictionary comes from DB Parameter variable_type == VariableTranslator.VariableType.ParamMasterStringDictionary elif p == 'ARGUMENT': variable_type == VariableTranslator.VariableType.Argument elif p == 'ATTACHMENTFILE': variable_type == VariableTranslator.VariableType.ArgumentFile else: if variable_type == VariableTranslator.VariableType.Date: translate_format = p if translate_format == 'yyyyMMdd' or translate_format == 'yyyymmdd': translate_format = '%Y%m%d' str_list.append(date_parameter.strftime(translate_format)) elif variable_type == VariableTranslator.VariableType.ParamMaster: str_list.append(ParameterAccess.get_string_value(p)) elif variable_type == VariableTranslator.VariableType.ParamMasterStringDictionary: parameter_key = p.split(':') dictionary: dict[str, str] = ParameterAccess.get_json_serialized_value(parameter_key[0], dict[str, str]) str_list.append(dictionary[parameter_key[1]]) elif variable_type == VariableTranslator.VariableType.Argument: str_list.append(arguments[factor_index - 1]) elif variable_type == VariableTranslator.VariableType.ArgumentFile: pass return ''.join(str_list)
def parse_date(self, keyword: str, target_date: date) -> date: parts = keyword.split('$') factor_index = 1 translate_market_access: MarketDateAccess = self.__market_access supported_market = VariableTranslator.__get_supported_market() for p in parts: if p.isdecimal(): factor_index = int(p) continue if p in supported_market: translate_market_access = MarketDateAccess(p) continue if p in self.variable_date_parameter_type: return self.__translate_date(translate_market_access=translate_market_access, target_date=target_date, date_parameter=p, factor_index=factor_index)
def test_get_month_day(self, mock_holiday): market_access = MarketDateAccess("JP") assert market_access.get_business_day_of_beginning_month( date(1984, 5, 2), 2) == date(1984, 5, 2) assert market_access.get_business_day_of_beginning_of_next_month( date(1984, 5, 2), -2) == date(1984, 5, 30) assert market_access.get_business_day_of_beginning_of_next_month( date(1984, 5, 2), 2) == date(1984, 6, 4) assert market_access.get_business_day_of_beginning_of_next_month( date(1984, 5, 1), 2) == date(1984, 6, 4) assert market_access.get_business_day_of_beginning_of_next_month( date(1984, 5, 31), 2) == date(1984, 6, 4)
def test_get_business_day(self, mock_holiday, input_data): market_access = MarketDateAccess("JP") assert input_data.expected_date == market_access.get_business_day( input_data.input_date, input_data.input_offset)
def __translate_date(self, translate_market_access: MarketDateAccess, target_date: date, date_parameter: str, factor_index: int) -> date: if date_parameter == 'TODAY': return target_date elif date_parameter == 'BBOM': # Business Day of Beginning of Month return translate_market_access.get_business_day_of_beginning_month(target_date, factor_index) elif date_parameter == 'NEXTBBOM': # Business Day of Beginning of Next Month return translate_market_access.get_business_day_of_beginning_of_next_month(target_date, factor_index) elif date_parameter == 'BOM': # Beginning of Month beginning_of_month = date(year=target_date.year, month=target_date.month, day=1) return beginning_of_month + timedelta(days=(factor_index - 1)) elif date_parameter == 'BEOM': # Business Day of End Of Month return translate_market_access.get_business_day_of_beginning_of_next_month(target_date, -factor_index) elif date_parameter == 'NEXTBEOM': # Business Day of End of Next Month two_month_after = target_date + relativedelta(months=2) return translate_market_access.get_business_day_of_beginning_month(two_month_after, -factor_index) elif date_parameter == 'PREVBEOM': # Business Day of End of Previous Month bom = date(target_date.year, target_date.month, 1) return translate_market_access.get_business_day(bom, -factor_index) elif date_parameter == 'EOM': # End of Month next_month = target_date + relativedelta(months=1) return date(next_month.year, next_month.month, 1) - timedelta(days=factor_index) elif date_parameter == 'NEXTBUS': # Next Business Day return translate_market_access.get_business_day(target_date, factor_index) elif date_parameter == 'NEXTDAY': # Next Day return target_date + timedelta(days=factor_index) elif date_parameter == 'PREVBUS': # Previous Business Day return translate_market_access.get_business_day(target_date, -factor_index) elif date_parameter == 'PREVDAY': # Previous Day return target_date - timedelta(days=factor_index) elif date_parameter == 'EOY': # End of Year next_year = date(target_date.year + 1, 1, 1) return next_year - timedelta(days=factor_index) elif date_parameter == 'BEOY': # Business Day of End of Year next_year = date(target_date.year + 1, 1, 1) return translate_market_access.get_business_day(next_year, -factor_index) elif date_parameter == 'BBOY': # Business Day of Beginning of Year next_year = date(target_date.year, 1, 1) if translate_market_access.is_business_day(next_year): return translate_market_access.get_business_day(next_year, factor_index - 1) else: return translate_market_access.get_business_day(next_year, factor_index) elif date_parameter == 'BOY': next_year = date(target_date.year, 1, 1) return next_year + timedelta(days=factor_index - 1)
def test_parse_date(self, mock_holiday, mock_variables, input_data): parameter = input_data.parameter market_access = MarketDateAccess('JP') translator = VariableTranslator(market_access) assert input_data.output == translator.parse_date( parameter, input_data.target_date)
def __cmd(application_id: int, task_id: int, parameter: str, register: bool, file: str, start_date: str, end_date: str, market: str): if register: if file == '': # TODO click.echo( 'To register task, need to specify target module using format on import statement or ' 'folder containing target classes/modules') return pass else: task: AbstractBaseTask result: StatusCode if application_id == -1 or task_id == -1: click.echo( 'Need to specify valid task information. Application ID , Task ID' ) return if parameter != '': if file != '': click.echo( 'Need to specify either parameter or file as both contains parameter to use' ) return result = __execute_task(application_id, task_id, parameter) click.echo(str(result)) return elif file != '': if not os.path.exists(file): click.echo('You need to specify valid filename') return with open(file, 'r') as f: parameter = f.read() result = __execute_task(application_id, task_id, parameter) click.echo(str(result)) return elif start_date == '' or len( start_date) != 8 or not start_date.isdigit(): click.echo( 'Need to specify target/start date to execute as yyyymmdd format' ) return else: if end_date == '': result = __execute_task(application_id, task_id, start_date) click.echo(str(result)) return elif len(end_date) != 8 or not end_date.isdigit() or market == '': click.echo( 'Need to specify target/start date to execute as yyyymmdd format' ) click.echo(' And you need to specify market to check holiday') return else: market_access = MarketDateAccess(market=market) target_date = datetime.strptime(start_date, '%Y%m%d') end_date = datetime.strptime(end_date, '%Y%m%d') while target_date <= end_date: click.echo("parameter: " + target_date.strftime('%Y%m%d')) result = __execute_task(application_id, task_id, start_date) if not result.is_success: click.echo(str(result)) return target_date = market_access.get_business_day( target_date, 1)