diff --git a/parser/src/parser/parser.rs b/parser/src/parser/parser.rs index d0cc3ece..fafb88da 100644 --- a/parser/src/parser/parser.rs +++ b/parser/src/parser/parser.rs @@ -4,7 +4,7 @@ use crate::lexer::lexer::Lexer; use crate::parser::ast::*; use crate::parser::string::{extract_string_inside, is_string}; use crate::token::{Kind, Token, TokenValue}; -use miette::{Result, ErrReport, miette}; +use miette::{Result, ErrReport, miette, bail}; use super::diagnostics; use super::expression::{is_atom, is_iterable}; @@ -179,7 +179,12 @@ impl Parser { self.bump_any(); let range = self.finish_node(node); let line_number = self.get_line_number_of_character_position(range.start); - Err(diagnostics::UnexpectedToken(line_number, kind.to_str(), range).into()) + return Err(miette!( + "Unexpected token {:?} at line {} at position {}", + kind, + line_number, + range.start, + )); } fn get_line_number_of_character_position(&self, pos: usize) -> u32 { @@ -1509,6 +1514,12 @@ impl Parser { fn parse_list(&mut self) -> Result { let node = self.start_node(); self.bump(Kind::LeftBrace); + if self.eat(Kind::RightBrace) { + return Ok(Expression::List(Box::new(List { + node: self.finish_node(node), + elements: vec![], + }))); + } let started_with_star = self.at(Kind::Mul); let first_elm = self.parse_star_named_expression()?; if !started_with_star && self.at(Kind::For) { @@ -1855,9 +1866,7 @@ impl Parser { let expr = self.parse_or_expr()?; node = self.finish_node(node); if !is_iterable(&expr) { - return Err( - diagnostics::UnexpectedToken(0, starred_value_kind.to_str(), node).into(), - ); + self.unepxted_token(node, starred_value_kind, ); } return Ok(Expression::Starred(Box::new(Starred { node: self.finish_node(node), @@ -2119,12 +2128,7 @@ impl Parser { let keyword_arg = match self.parse_keyword_item() { Ok(keyword_arg) => keyword_arg, Err(_) => { - return Err(diagnostics::ExpectToken( - "Keyword argument", - self.cur_kind().to_str(), - self.finish_node(self.start_node()), - ) - .into()); + bail!("Expected keyword argument but found {}, at {:?}", self.cur_kind().to_str(), self.cur_token().start); } }; keyword_args.push(keyword_arg); @@ -2339,12 +2343,7 @@ impl Parser { return Ok(expr); } else { - return Err(diagnostics::UnexpectedToken( - 0, - self.cur_kind().to_str(), - self.finish_node(node), - ) - .into()); + return Err(self.unepxted_token(node, self.cur_kind()).err().unwrap()); } } @@ -2635,21 +2634,11 @@ impl Parser { ComparisonOperator::NotIn } _ => { - return Err(diagnostics::UnexpectedToken( - 0, - self.cur_kind().to_str(), - self.finish_node(node), - ) - .into()) + return Err(self.unepxted_token(node, self.cur_kind()).err().unwrap()); } }, _ => { - return Err(diagnostics::UnexpectedToken( - 0, - self.cur_kind().to_str(), - self.finish_node(node), - ) - .into()) + return Err(self.unepxted_token(node, self.cur_kind()).err().unwrap()); } }; self.bump_any(); @@ -2666,12 +2655,7 @@ impl Parser { Kind::Mod => Ok(BinaryOperator::Mod), Kind::Pow => Ok(BinaryOperator::Pow), Kind::MatrixMul => Ok(BinaryOperator::MatMult), - _ => Err(diagnostics::UnexpectedToken( - 0, - self.cur_kind().to_str(), - self.finish_node(self.start_node()), - ) - .into()), + _ => Err(self.unepxted_token(self.start_node(), self.cur_kind()).err().unwrap()), }; self.bump_any(); op @@ -2836,12 +2820,7 @@ impl Parser { self.expect(Kind::RightBracket)?; } _ => { - return Err(diagnostics::UnexpectedToken( - 0, - "unknown token in fstring", - self.finish_node(self.start_node()), - ) - .into()); + return Err(self.unepxted_token(self.start_node(), self.cur_kind()).err().unwrap()); } } } diff --git a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__complete@input_program.py.snap b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__complete@input_program.py.snap new file mode 100644 index 00000000..51de1a10 --- /dev/null +++ b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__complete@input_program.py.snap @@ -0,0 +1,13966 @@ +--- +source: parser/src/parser/parser.rs +description: "def _handle_ticker_index(symbol):\n ticker_index = symbols_data.get_ticker_index(symbol)\n\n if ticker_index is None:\n market_symbol = get_symbol_info(symbol)\n if market_symbol is not None:\n symbols_data.append_symbol_to_file(market_symbol)\n ticker_index = market_symbol.index\n return ticker_index\n\n\ndef _extract_ticker_client_types_data(ticker_index: str) -> List:\n url = TSE_CLIENT_TYPE_DATA_URL.format(ticker_index)\n with requests_retry_session() as session:\n response = session.get(url, timeout=5)\n data = response.text.split(\";\")\n data = [row.split(\",\") for row in data]\n return data\n\n\ndef _create_financial_index_from_text_response(data):\n data = pd.DataFrame(re.split(r\"\\;\", data))\n columns = [\"date\", \"high\", \"low\", \"open\", \"close\", \"volume\", \"__\"]\n data[columns] = data[0].str.split(\",\", expand=True)\n return data.drop(columns=[\"__\", 0])\n\n\ndef _adjust_data_frame(df, include_jdate):\n df.date = pd.to_datetime(df.date, format=\"%Y%m%d\")\n if include_jdate:\n df[\"jdate\"] = \"\"\n df.jdate = df.date.apply(\n lambda gregorian: jdatetime.date.fromgregorian(date=gregorian)\n )\n\n\ndef _adjust_data_frame_for_fIndex(df, include_jdate):\n df[\"date\"] = pd.to_datetime(df[\"date\"], format=\"%Y%m%d\")\n if include_jdate:\n df[\"jdate\"] = df.date.apply(\n lambda gregorian: jdatetime.date.fromgregorian(date=gregorian)\n )\n\n\ndef download(\n symbols: Union[List, str],\n write_to_csv: bool = False,\n include_jdate: bool = False,\n base_path: str = config.DATA_BASE_PATH,\n adjust: bool = False,\n) -> Dict[str, pd.DataFrame]:\n if symbols == \"all\":\n symbols = symbols_data.all_symbols()\n elif isinstance(symbols, str):\n symbols = [symbols]\n\n df_list = {}\n future_to_symbol = {}\n with futures.ThreadPoolExecutor(max_workers=10) as executor:\n session = requests_retry_session()\n for symbol in symbols:\n if (\n symbol.isnumeric()\n and symbols_data.get_ticker_index(symbol) is None\n ):\n ticker_indexes = [symbol]\n else:\n ticker_index = _handle_ticker_index(symbol)\n if ticker_index is None:\n raise Exception(f\"Cannot find symbol: {symbol}\")\n ticker_indexes = symbols_data.get_ticker_old_index(symbol)\n ticker_indexes.insert(0, ticker_index)\n\n for index in ticker_indexes:\n future = executor.submit(\n download_ticker_daily_record, index, session\n )\n\n future_to_symbol[future] = symbol\n\n for future in futures.as_completed(future_to_symbol):\n symbol = future_to_symbol[future]\n try:\n df: pd.DataFrame = future.result()\n except pd.errors.EmptyDataError as ex:\n logger.error(\n f\"Cannot read daily trade records for symbol: {symbol}\",\n extra={\"Error\": ex},\n )\n continue\n df = df.iloc[::-1].reset_index(drop=True)\n df = df.rename(columns=translations.HISTORY_FIELD_MAPPINGS)\n df = df.drop(columns=[\"\", \"\"])\n _adjust_data_frame(df, include_jdate)\n\n if symbol in df_list:\n df_list[symbol] = (\n pd.concat(\n [df_list[symbol], df], ignore_index=True, sort=False\n )\n .sort_values(\"date\")\n .reset_index(drop=True)\n )\n else:\n df_list[symbol] = df\n\n if adjust:\n df_list[symbol] = adjust_price(df_list[symbol])\n\n if write_to_csv:\n Path(base_path).mkdir(parents=True, exist_ok=True)\n if adjust:\n df_list[symbol].to_csv(\n f\"{base_path}/{symbol}-ت.csv\", index=False\n )\n else:\n df_list[symbol].to_csv(\n f\"{base_path}/{symbol}.csv\", index=False\n )\n\n if len(df_list) != len(symbols):\n print(\"Warning, download did not complete, re-run the code\")\n session.close()\n return df_list\n\n\ndef adjust_price(df: pd.DataFrame) -> pd.DataFrame:\n \"\"\"\n Adjust historical records of stock\n\n There is a capital increase/profit sharing,\n if today \"Final Close Price\" is not equal to next day\n \"Yesterday Final Close Price\" by using this ratio,\n performance adjustment of stocks is achieved\n\n Parameters\n ----------\n df : pd.DataFrame\n DataFrame with historical records.\n\n Returns\n -------\n pd.DataFrame\n DataFrame with adjusted historical records.\n\n Notes\n -----\n DataFrame can not be empty or else it makes runtime error\n Type of DataFrame must be RangeIndex to make proper range of records\n that need to be modified\n\n diff: list\n list of indexs of the day after capital increase/profit sharing\n ratio_list: List\n List of ratios to adjust historical data of stock\n ratio: Float\n ratio = df.loc[i].adjClose / df.loc[i+1].yesterday\n\n Description\n -----------\n # Note: adjustment does not include Tenth and twentieth days\n df.index = range(0,101,1)\n # step is 1\n step = df.index.step\n diff = [10,20]\n ratio_list = [0.5, 0.8]\n df.loc[0:10-step, [open,...]] * ratio[0]\n df.loc[10:20-step, [open,...]] * ratio[1]\n \"\"\"\n if df.empty or not isinstance(df.index, pd.core.indexes.range.RangeIndex):\n return df\n\n new_df = df.copy()\n step = new_df.index.step\n diff = list(new_df.index[new_df.shift(1).adjClose != new_df.yesterday])\n if len(diff) > 0:\n diff.pop(0)\n ratio = 1\n ratio_list = []\n for i in diff[::-1]:\n ratio *= (\n new_df.loc[i, \"yesterday\"] / new_df.shift(1).loc[i, \"adjClose\"]\n )\n ratio_list.insert(0, ratio)\n for i, k in enumerate(diff):\n if i == 0:\n start = new_df.index.start\n else:\n start = diff[i - 1]\n end = diff[i] - step\n new_df.loc[\n start:end,\n [\n \"open\",\n \"high\",\n \"low\",\n \"close\",\n \"adjClose\",\n \"yesterday\",\n ],\n ] = round(\n new_df.loc[\n start:end,\n [\n \"open\",\n \"high\",\n \"low\",\n \"close\",\n \"adjClose\",\n \"yesterday\",\n ],\n ]\n * ratio_list[i]\n )\n\n return new_df\n\n\n@retry(\n retry=retry_if_exception_type(HTTPError),\n wait=wait_random(min=1, max=4),\n before_sleep=before_sleep_log(logger, logging.DEBUG),\n)\ndef download_ticker_daily_record(ticker_index: str, session: Session):\n url = tse_settings.TSE_TICKER_EXPORT_DATA_ADDRESS.format(ticker_index)\n response = session.get(url, timeout=10)\n if 400 <= response.status_code < 500:\n logger.error(\n f\"Cannot read daily trade records from the url: {url}\",\n extra={\"response\": response.text, \"code\": response.status_code},\n )\n\n response.raise_for_status()\n\n data = StringIO(response.text)\n return pd.read_csv(data)\n\n\n@retry(\n retry=retry_if_exception_type(HTTPError),\n wait=wait_random(min=1, max=4),\n before_sleep=before_sleep_log(logger, logging.DEBUG),\n)\ndef download_fIndex_record(fIndex: str, session: Session):\n url = tse_settings.TSE_FINANCIAL_INDEX_EXPORT_DATA_ADDRESS.format(fIndex)\n response = session.get(url, timeout=10)\n if 400 <= response.status_code < 500:\n logger.error(\n f\"Cannot read daily trade records from the url: {url}\",\n extra={\"response\": response.text, \"code\": response.status_code},\n )\n\n response.raise_for_status()\n data = response.text\n if not data or \";\" not in data or \",\" not in data:\n raise ValueError(\n f\"\"\"Invalid response from the url: {url}.\n \\nExpected valid financial index data.\"\"\"\n )\n df = _create_financial_index_from_text_response(data)\n return df\n\n\ndef download_financial_indexes(\n symbols: Union[List, str],\n write_to_csv: bool = False,\n include_jdate: bool = False,\n base_path: str = config.FINANCIAL_INDEX_BASE_PATH,\n) -> Dict[str, pd.DataFrame]:\n if symbols == \"all\":\n symbols = symbols_data.all_financial_index()\n elif isinstance(symbols, str):\n symbols = [symbols]\n\n df_list = {}\n future_to_symbol = {}\n with futures.ThreadPoolExecutor(max_workers=10) as executor:\n session = requests_retry_session()\n for symbol in symbols:\n symbol = persian.replace_persian(symbol)\n if (\n symbol.isnumeric()\n and symbols_data.get_financial_index(symbol) is None\n ):\n financial_index = symbol\n else:\n financial_index = symbols_data.get_financial_index(symbol)\n if financial_index is None:\n raise Exception(f\"Cannot find financial index: {symbol}\")\n\n future = executor.submit(\n download_fIndex_record, financial_index, session\n )\n\n future_to_symbol[future] = symbol\n\n for future in futures.as_completed(future_to_symbol):\n symbol = future_to_symbol[future]\n try:\n df: pd.DataFrame = future.result()\n except pd.errors.EmptyDataError as ex:\n logger.error(\n f\"Cannot read daily trade records for symbol: {symbol}\",\n extra={\"Error\": ex},\n )\n continue\n _adjust_data_frame_for_fIndex(df, include_jdate)\n df_list[symbol] = df\n\n if write_to_csv:\n Path(base_path).mkdir(parents=True, exist_ok=True)\n df_list[symbol].to_csv(\n f\"{base_path}/{symbol}.csv\", index=False\n )\n\n if len(df_list) != len(symbols):\n print(\"Warning, download did not complete, re-run the code\")\n session.close()\n return df_list\n\n\ndef download_client_types_records(\n symbols: Union[List, str],\n write_to_csv: bool = False,\n include_jdate: bool = False,\n base_path: str = config.CLIENT_TYPES_DATA_BASE_PATH,\n):\n if symbols == \"all\":\n symbols = symbols_data.all_symbols()\n elif isinstance(symbols, str):\n symbols = [symbols]\n\n df_list = {}\n future_to_symbol = {}\n with futures.ThreadPoolExecutor(max_workers=10) as executor:\n for symbol in symbols:\n ticker_index = _handle_ticker_index(symbol)\n if ticker_index is None:\n raise Exception(f\"Cannot find symbol: {symbol}\")\n future = executor.submit(\n download_ticker_client_types_record, ticker_index\n )\n future_to_symbol[future] = symbol\n for future in futures.as_completed(future_to_symbol):\n symbol = future_to_symbol[future]\n df: pd.DataFrame = future.result()\n # ignore failures\n if df is None:\n continue\n _adjust_data_frame(df, include_jdate)\n df_list[symbol] = df\n if write_to_csv:\n Path(base_path).mkdir(parents=True, exist_ok=True)\n df.to_csv(f\"{base_path}/{symbol}.csv\")\n\n if len(df_list) != len(symbols):\n print(\n \"\"\"could not download client types for all the symbols make\n sure you have what you need or re-run the function\"\"\"\n )\n return df_list\n\n\n@retry(\n retry=retry_if_exception_type(HTTPError),\n wait=wait_random(min=1, max=4),\n before_sleep=before_sleep_log(logger, logging.DEBUG),\n)\ndef download_ticker_client_types_record(ticker_index: Optional[str]):\n data = _extract_ticker_client_types_data(ticker_index)\n if len(data) == 1:\n logger.warning(\n f\"\"\"Cannot create client types data for ticker{ticker_index}\n from data: {data}\"\"\",\n extra={\"ticker_index\": ticker_index},\n )\n return None\n client_types_data_frame = pd.DataFrame(\n data,\n columns=[\n \"date\",\n \"individual_buy_count\",\n \"corporate_buy_count\",\n \"individual_sell_count\",\n \"corporate_sell_count\",\n \"individual_buy_vol\",\n \"corporate_buy_vol\",\n \"individual_sell_vol\",\n \"corporate_sell_vol\",\n \"individual_buy_value\",\n \"corporate_buy_value\",\n \"individual_sell_value\",\n \"corporate_sell_value\",\n ],\n )\n for i in [\n \"individual_buy_\",\n \"individual_sell_\",\n \"corporate_buy_\",\n \"corporate_sell_\",\n ]:\n client_types_data_frame[f\"{i}mean_price\"] = client_types_data_frame[\n f\"{i}value\"\n ].astype(float) / client_types_data_frame[f\"{i}vol\"].astype(float)\n client_types_data_frame[\n \"individual_ownership_change\"\n ] = client_types_data_frame[\"corporate_sell_vol\"].astype(\n float\n ) - client_types_data_frame[\n \"corporate_buy_vol\"\n ].astype(\n float\n )\n return client_types_data_frame\n\n\ndef get_symbol_id(symbol_name: str):\n url = tse_settings.TSE_SYMBOL_ID_URL.format(symbol_name.strip())\n response = requests_retry_session().get(url, timeout=10)\n try:\n response.raise_for_status()\n except HTTPError:\n raise Exception(\"Sorry, tse server did not respond\")\n\n symbol_full_info = response.text.split(\";\")[0].split(\",\")\n if persian.replace_arabic(symbol_name) == symbol_full_info[0].strip():\n return symbol_full_info[2] # symbol id\n return None\n\n\ndef get_symbol_info(symbol_name: str):\n url = tse_settings.TSE_SYMBOL_ID_URL.format(symbol_name.strip())\n response = requests_retry_session().get(url, timeout=10)\n try:\n response.raise_for_status()\n except HTTPError:\n raise Exception(f\"{symbol_name}: Sorry, tse server did not respond\")\n\n symbols = response.text.split(\";\")\n market_symbol = MarketSymbol(\n code=None,\n symbol=None,\n index=None,\n name=None,\n old=[],\n )\n for symbol_full_info in symbols:\n if symbol_full_info.strip() == \"\":\n continue\n symbol_full_info = symbol_full_info.split(\",\")\n if persian.replace_arabic(symbol_full_info[0]) == symbol_name:\n # if symbol id is active\n if symbol_full_info[7] == \"1\":\n market_symbol.symbol = persian.replace_arabic(\n symbol_full_info[0]\n )\n market_symbol.name = persian.replace_arabic(\n symbol_full_info[1]\n )\n market_symbol.index = symbol_full_info[2] # active symbol id\n else:\n market_symbol.old.append(symbol_full_info[2]) # old symbol id\n\n if market_symbol.index is None:\n return None\n return market_symbol\n\n" +input_file: parser/test_data/inputs/input_program.py +--- +Module { + node: Node { + start: 0, + end: 15273, + }, + body: [ + FunctionDef( + FunctionDef { + node: Node { + start: 0, + end: 15273, + }, + name: "_handle_ticker_index", + args: Arguments { + node: Node { + start: 25, + end: 31, + }, + posonlyargs: [], + args: [ + Arg { + node: Node { + start: 25, + end: 31, + }, + arg: "symbol", + annotation: None, + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: [ + AssignStatement( + Assign { + node: Node { + start: 38, + end: 90, + }, + targets: [ + Name( + Name { + node: Node { + start: 38, + end: 50, + }, + id: "ticker_index", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 82, + end: 90, + }, + func: Attribute( + Attribute { + node: Node { + start: 53, + end: 82, + }, + value: Name( + Name { + node: Node { + start: 53, + end: 65, + }, + id: "symbols_data", + }, + ), + attr: "get_ticker_index", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 83, + end: 89, + }, + id: "symbol", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + IfStatement( + If { + node: Node { + start: 99, + end: 342, + }, + test: Compare( + Compare { + node: Node { + start: 99, + end: 119, + }, + left: Name( + Name { + node: Node { + start: 99, + end: 111, + }, + id: "ticker_index", + }, + ), + ops: [ + Is, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 115, + end: 119, + }, + value: None, + }, + ), + ], + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 129, + end: 168, + }, + targets: [ + Name( + Name { + node: Node { + start: 129, + end: 142, + }, + id: "market_symbol", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 145, + end: 168, + }, + func: Name( + Name { + node: Node { + start: 145, + end: 160, + }, + id: "get_symbol_info", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 161, + end: 167, + }, + id: "symbol", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + IfStatement( + If { + node: Node { + start: 180, + end: 320, + }, + test: Compare( + Compare { + node: Node { + start: 180, + end: 205, + }, + left: Name( + Name { + node: Node { + start: 180, + end: 193, + }, + id: "market_symbol", + }, + ), + ops: [ + IsNot, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 201, + end: 205, + }, + value: None, + }, + ), + ], + }, + ), + body: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 253, + end: 268, + }, + func: Attribute( + Attribute { + node: Node { + start: 219, + end: 253, + }, + value: Name( + Name { + node: Node { + start: 219, + end: 231, + }, + id: "symbols_data", + }, + ), + attr: "append_symbol_to_file", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 254, + end: 267, + }, + id: "market_symbol", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + AssignStatement( + Assign { + node: Node { + start: 281, + end: 315, + }, + targets: [ + Name( + Name { + node: Node { + start: 281, + end: 293, + }, + id: "ticker_index", + }, + ), + ], + value: Attribute( + Attribute { + node: Node { + start: 296, + end: 315, + }, + value: Name( + Name { + node: Node { + start: 296, + end: 309, + }, + id: "market_symbol", + }, + ), + attr: "index", + }, + ), + }, + ), + ], + orelse: [], + }, + ), + Return( + Return { + node: Node { + start: 320, + end: 339, + }, + value: Some( + Name( + Name { + node: Node { + start: 327, + end: 339, + }, + id: "ticker_index", + }, + ), + ), + }, + ), + ], + orelse: [], + }, + ), + FunctionDef( + FunctionDef { + node: Node { + start: 342, + end: 655, + }, + name: "_extract_ticker_client_types_data", + args: Arguments { + node: Node { + start: 380, + end: 397, + }, + posonlyargs: [], + args: [ + Arg { + node: Node { + start: 380, + end: 397, + }, + arg: "ticker_index", + annotation: Some( + Name( + Name { + node: Node { + start: 394, + end: 397, + }, + id: "str", + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: [ + AssignStatement( + Assign { + node: Node { + start: 412, + end: 463, + }, + targets: [ + Name( + Name { + node: Node { + start: 412, + end: 415, + }, + id: "url", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 449, + end: 463, + }, + func: Attribute( + Attribute { + node: Node { + start: 418, + end: 449, + }, + value: Name( + Name { + node: Node { + start: 418, + end: 442, + }, + id: "TSE_CLIENT_TYPE_DATA_URL", + }, + ), + attr: "format", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 450, + end: 462, + }, + id: "ticker_index", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + WithStatement( + With { + node: Node { + start: 468, + end: 561, + }, + items: [ + WithItem { + node: Node { + start: 473, + end: 508, + }, + context_expr: Call( + Call { + node: Node { + start: 473, + end: 497, + }, + func: Name( + Name { + node: Node { + start: 473, + end: 495, + }, + id: "requests_retry_session", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + optional_vars: Some( + Name( + Name { + node: Node { + start: 501, + end: 508, + }, + id: "session", + }, + ), + ), + }, + ], + body: [ + AssignStatement( + Assign { + node: Node { + start: 518, + end: 556, + }, + targets: [ + Name( + Name { + node: Node { + start: 518, + end: 526, + }, + id: "response", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 540, + end: 556, + }, + func: Attribute( + Attribute { + node: Node { + start: 529, + end: 540, + }, + value: Name( + Name { + node: Node { + start: 529, + end: 536, + }, + id: "session", + }, + ), + attr: "get", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 541, + end: 544, + }, + id: "url", + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 546, + end: 555, + }, + arg: Some( + "timeout", + ), + value: Constant( + Constant { + node: Node { + start: 554, + end: 555, + }, + value: Int( + "5", + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 561, + end: 592, + }, + targets: [ + Name( + Name { + node: Node { + start: 561, + end: 565, + }, + id: "data", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 587, + end: 592, + }, + func: Attribute( + Attribute { + node: Node { + start: 568, + end: 587, + }, + value: Attribute( + Attribute { + node: Node { + start: 568, + end: 581, + }, + value: Name( + Name { + node: Node { + start: 568, + end: 576, + }, + id: "response", + }, + ), + attr: "text", + }, + ), + attr: "split", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 588, + end: 591, + }, + value: Str( + ";", + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 597, + end: 636, + }, + targets: [ + Name( + Name { + node: Node { + start: 597, + end: 601, + }, + id: "data", + }, + ), + ], + value: ListComp( + ListComp { + node: Node { + start: 604, + end: 636, + }, + element: Call( + Call { + node: Node { + start: 614, + end: 619, + }, + func: Attribute( + Attribute { + node: Node { + start: 605, + end: 614, + }, + value: Name( + Name { + node: Node { + start: 605, + end: 608, + }, + id: "row", + }, + ), + attr: "split", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 615, + end: 618, + }, + value: Str( + ",", + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + generators: [ + Comprehension { + node: Node { + start: 620, + end: 635, + }, + target: Name( + Name { + node: Node { + start: 624, + end: 627, + }, + id: "row", + }, + ), + iter: Name( + Name { + node: Node { + start: 631, + end: 635, + }, + id: "data", + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Return( + Return { + node: Node { + start: 641, + end: 652, + }, + value: Some( + Name( + Name { + node: Node { + start: 648, + end: 652, + }, + id: "data", + }, + ), + ), + }, + ), + ], + decorator_list: [], + returns: Some( + Name( + Name { + node: Node { + start: 402, + end: 406, + }, + id: "List", + }, + ), + ), + type_comment: None, + }, + ), + FunctionDef( + FunctionDef { + node: Node { + start: 655, + end: 925, + }, + name: "_create_financial_index_from_text_response", + args: Arguments { + node: Node { + start: 702, + end: 706, + }, + posonlyargs: [], + args: [ + Arg { + node: Node { + start: 702, + end: 706, + }, + arg: "data", + annotation: None, + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: [ + AssignStatement( + Assign { + node: Node { + start: 713, + end: 755, + }, + targets: [ + Name( + Name { + node: Node { + start: 713, + end: 717, + }, + id: "data", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 732, + end: 755, + }, + func: Attribute( + Attribute { + node: Node { + start: 720, + end: 732, + }, + value: Name( + Name { + node: Node { + start: 720, + end: 722, + }, + id: "pd", + }, + ), + attr: "DataFrame", + }, + ), + args: [ + Call( + Call { + node: Node { + start: 741, + end: 754, + }, + func: Attribute( + Attribute { + node: Node { + start: 733, + end: 741, + }, + value: Name( + Name { + node: Node { + start: 733, + end: 735, + }, + id: "re", + }, + ), + attr: "split", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 742, + end: 747, + }, + value: Str( + "\\;", + ), + }, + ), + Name( + Name { + node: Node { + start: 749, + end: 753, + }, + id: "data", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 760, + end: 826, + }, + targets: [ + Name( + Name { + node: Node { + start: 760, + end: 767, + }, + id: "columns", + }, + ), + ], + value: List( + List { + node: Node { + start: 770, + end: 826, + }, + elements: [ + Constant( + Constant { + node: Node { + start: 771, + end: 777, + }, + value: Str( + "date", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 779, + end: 785, + }, + value: Str( + "high", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 787, + end: 792, + }, + value: Str( + "low", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 794, + end: 800, + }, + value: Str( + "open", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 802, + end: 809, + }, + value: Str( + "close", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 811, + end: 819, + }, + value: Str( + "volume", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 821, + end: 825, + }, + value: Str( + "__", + ), + }, + ), + ], + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 831, + end: 882, + }, + targets: [ + Subscript( + Subscript { + node: Node { + start: 831, + end: 844, + }, + value: Name( + Name { + node: Node { + start: 831, + end: 835, + }, + id: "data", + }, + ), + slice: Name( + Name { + node: Node { + start: 836, + end: 843, + }, + id: "columns", + }, + ), + }, + ), + ], + value: Call( + Call { + node: Node { + start: 864, + end: 882, + }, + func: Attribute( + Attribute { + node: Node { + start: 854, + end: 864, + }, + value: Attribute( + Attribute { + node: Node { + start: 854, + end: 858, + }, + value: Subscript( + Subscript { + node: Node { + start: 847, + end: 854, + }, + value: Name( + Name { + node: Node { + start: 847, + end: 851, + }, + id: "data", + }, + ), + slice: Constant( + Constant { + node: Node { + start: 852, + end: 853, + }, + value: Int( + "0", + ), + }, + ), + }, + ), + attr: "str", + }, + ), + attr: "split", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 865, + end: 868, + }, + value: Str( + ",", + ), + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 870, + end: 881, + }, + arg: Some( + "expand", + ), + value: Constant( + Constant { + node: Node { + start: 877, + end: 881, + }, + value: Bool( + true, + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + ), + Return( + Return { + node: Node { + start: 887, + end: 922, + }, + value: Some( + Call( + Call { + node: Node { + start: 903, + end: 922, + }, + func: Attribute( + Attribute { + node: Node { + start: 894, + end: 903, + }, + value: Name( + Name { + node: Node { + start: 894, + end: 898, + }, + id: "data", + }, + ), + attr: "drop", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 904, + end: 921, + }, + arg: Some( + "columns", + ), + value: List( + List { + node: Node { + start: 912, + end: 921, + }, + elements: [ + Constant( + Constant { + node: Node { + start: 913, + end: 917, + }, + value: Str( + "__", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 919, + end: 920, + }, + value: Int( + "0", + ), + }, + ), + ], + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + ), + }, + ), + ], + decorator_list: [], + returns: None, + type_comment: None, + }, + ), + FunctionDef( + FunctionDef { + node: Node { + start: 925, + end: 15273, + }, + name: "_adjust_data_frame", + args: Arguments { + node: Node { + start: 948, + end: 965, + }, + posonlyargs: [], + args: [ + Arg { + node: Node { + start: 948, + end: 950, + }, + arg: "df", + annotation: None, + }, + Arg { + node: Node { + start: 952, + end: 965, + }, + arg: "include_jdate", + annotation: None, + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: [ + AssignStatement( + Assign { + node: Node { + start: 972, + end: 1022, + }, + targets: [ + Attribute( + Attribute { + node: Node { + start: 972, + end: 979, + }, + value: Name( + Name { + node: Node { + start: 972, + end: 974, + }, + id: "df", + }, + ), + attr: "date", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 996, + end: 1022, + }, + func: Attribute( + Attribute { + node: Node { + start: 982, + end: 996, + }, + value: Name( + Name { + node: Node { + start: 982, + end: 984, + }, + id: "pd", + }, + ), + attr: "to_datetime", + }, + ), + args: [ + Attribute( + Attribute { + node: Node { + start: 997, + end: 1004, + }, + value: Name( + Name { + node: Node { + start: 997, + end: 999, + }, + id: "df", + }, + ), + attr: "date", + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 1006, + end: 1021, + }, + arg: Some( + "format", + ), + value: Constant( + Constant { + node: Node { + start: 1013, + end: 1021, + }, + value: Str( + "%Y%m%d", + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + ), + IfStatement( + If { + node: Node { + start: 1030, + end: 1191, + }, + test: Name( + Name { + node: Node { + start: 1030, + end: 1043, + }, + id: "include_jdate", + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 1053, + end: 1069, + }, + targets: [ + Subscript( + Subscript { + node: Node { + start: 1053, + end: 1064, + }, + value: Name( + Name { + node: Node { + start: 1053, + end: 1055, + }, + id: "df", + }, + ), + slice: Constant( + Constant { + node: Node { + start: 1056, + end: 1063, + }, + value: Str( + "jdate", + ), + }, + ), + }, + ), + ], + value: Constant( + Constant { + node: Node { + start: 1067, + end: 1069, + }, + value: Str( + "", + ), + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 1078, + end: 1188, + }, + targets: [ + Attribute( + Attribute { + node: Node { + start: 1078, + end: 1086, + }, + value: Name( + Name { + node: Node { + start: 1078, + end: 1080, + }, + id: "df", + }, + ), + attr: "jdate", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 1102, + end: 1188, + }, + func: Attribute( + Attribute { + node: Node { + start: 1089, + end: 1102, + }, + value: Attribute( + Attribute { + node: Node { + start: 1089, + end: 1096, + }, + value: Name( + Name { + node: Node { + start: 1089, + end: 1091, + }, + id: "df", + }, + ), + attr: "date", + }, + ), + attr: "apply", + }, + ), + args: [ + Lambda( + Lambda { + node: Node { + start: 1116, + end: 1178, + }, + args: Arguments { + node: Node { + start: 1123, + end: 1132, + }, + posonlyargs: [], + args: [ + Arg { + node: Node { + start: 1123, + end: 1132, + }, + arg: "gregorian", + annotation: None, + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: Call( + Call { + node: Node { + start: 1162, + end: 1178, + }, + func: Attribute( + Attribute { + node: Node { + start: 1134, + end: 1162, + }, + value: Attribute( + Attribute { + node: Node { + start: 1134, + end: 1148, + }, + value: Name( + Name { + node: Node { + start: 1134, + end: 1143, + }, + id: "jdatetime", + }, + ), + attr: "date", + }, + ), + attr: "fromgregorian", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 1163, + end: 1177, + }, + arg: Some( + "date", + ), + value: Name( + Name { + node: Node { + start: 1168, + end: 1177, + }, + id: "gregorian", + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + FunctionDef( + FunctionDef { + node: Node { + start: 1191, + end: 15273, + }, + name: "_adjust_data_frame_for_fIndex", + args: Arguments { + node: Node { + start: 1225, + end: 1242, + }, + posonlyargs: [], + args: [ + Arg { + node: Node { + start: 1225, + end: 1227, + }, + arg: "df", + annotation: None, + }, + Arg { + node: Node { + start: 1229, + end: 1242, + }, + arg: "include_jdate", + annotation: None, + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: [ + AssignStatement( + Assign { + node: Node { + start: 1249, + end: 1305, + }, + targets: [ + Subscript( + Subscript { + node: Node { + start: 1249, + end: 1259, + }, + value: Name( + Name { + node: Node { + start: 1249, + end: 1251, + }, + id: "df", + }, + ), + slice: Constant( + Constant { + node: Node { + start: 1252, + end: 1258, + }, + value: Str( + "date", + ), + }, + ), + }, + ), + ], + value: Call( + Call { + node: Node { + start: 1276, + end: 1305, + }, + func: Attribute( + Attribute { + node: Node { + start: 1262, + end: 1276, + }, + value: Name( + Name { + node: Node { + start: 1262, + end: 1264, + }, + id: "pd", + }, + ), + attr: "to_datetime", + }, + ), + args: [ + Subscript( + Subscript { + node: Node { + start: 1277, + end: 1287, + }, + value: Name( + Name { + node: Node { + start: 1277, + end: 1279, + }, + id: "df", + }, + ), + slice: Constant( + Constant { + node: Node { + start: 1280, + end: 1286, + }, + value: Str( + "date", + ), + }, + ), + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 1289, + end: 1304, + }, + arg: Some( + "format", + ), + value: Constant( + Constant { + node: Node { + start: 1296, + end: 1304, + }, + value: Str( + "%Y%m%d", + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + ), + IfStatement( + If { + node: Node { + start: 1313, + end: 1452, + }, + test: Name( + Name { + node: Node { + start: 1313, + end: 1326, + }, + id: "include_jdate", + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 1336, + end: 1449, + }, + targets: [ + Subscript( + Subscript { + node: Node { + start: 1336, + end: 1347, + }, + value: Name( + Name { + node: Node { + start: 1336, + end: 1338, + }, + id: "df", + }, + ), + slice: Constant( + Constant { + node: Node { + start: 1339, + end: 1346, + }, + value: Str( + "jdate", + ), + }, + ), + }, + ), + ], + value: Call( + Call { + node: Node { + start: 1363, + end: 1449, + }, + func: Attribute( + Attribute { + node: Node { + start: 1350, + end: 1363, + }, + value: Attribute( + Attribute { + node: Node { + start: 1350, + end: 1357, + }, + value: Name( + Name { + node: Node { + start: 1350, + end: 1352, + }, + id: "df", + }, + ), + attr: "date", + }, + ), + attr: "apply", + }, + ), + args: [ + Lambda( + Lambda { + node: Node { + start: 1377, + end: 1439, + }, + args: Arguments { + node: Node { + start: 1384, + end: 1393, + }, + posonlyargs: [], + args: [ + Arg { + node: Node { + start: 1384, + end: 1393, + }, + arg: "gregorian", + annotation: None, + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: Call( + Call { + node: Node { + start: 1423, + end: 1439, + }, + func: Attribute( + Attribute { + node: Node { + start: 1395, + end: 1423, + }, + value: Attribute( + Attribute { + node: Node { + start: 1395, + end: 1409, + }, + value: Name( + Name { + node: Node { + start: 1395, + end: 1404, + }, + id: "jdatetime", + }, + ), + attr: "date", + }, + ), + attr: "fromgregorian", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 1424, + end: 1438, + }, + arg: Some( + "date", + ), + value: Name( + Name { + node: Node { + start: 1429, + end: 1438, + }, + id: "gregorian", + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + FunctionDef( + FunctionDef { + node: Node { + start: 1452, + end: 15273, + }, + name: "download", + args: Arguments { + node: Node { + start: 1470, + end: 1631, + }, + posonlyargs: [], + args: [ + Arg { + node: Node { + start: 1470, + end: 1495, + }, + arg: "symbols", + annotation: Some( + Subscript( + Subscript { + node: Node { + start: 1479, + end: 1495, + }, + value: Name( + Name { + node: Node { + start: 1479, + end: 1484, + }, + id: "Union", + }, + ), + slice: Tuple( + Tuple { + node: Node { + start: 1485, + end: 1495, + }, + elements: [ + Name( + Name { + node: Node { + start: 1485, + end: 1489, + }, + id: "List", + }, + ), + Name( + Name { + node: Node { + start: 1491, + end: 1494, + }, + id: "str", + }, + ), + ], + }, + ), + }, + ), + ), + }, + Arg { + node: Node { + start: 1501, + end: 1527, + }, + arg: "write_to_csv", + annotation: Some( + Name( + Name { + node: Node { + start: 1515, + end: 1519, + }, + id: "bool", + }, + ), + ), + }, + Arg { + node: Node { + start: 1533, + end: 1560, + }, + arg: "include_jdate", + annotation: Some( + Name( + Name { + node: Node { + start: 1548, + end: 1552, + }, + id: "bool", + }, + ), + ), + }, + Arg { + node: Node { + start: 1566, + end: 1604, + }, + arg: "base_path", + annotation: Some( + Name( + Name { + node: Node { + start: 1577, + end: 1580, + }, + id: "str", + }, + ), + ), + }, + Arg { + node: Node { + start: 1610, + end: 1630, + }, + arg: "adjust", + annotation: Some( + Name( + Name { + node: Node { + start: 1618, + end: 1622, + }, + id: "bool", + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [ + Constant( + Constant { + node: Node { + start: 1522, + end: 1527, + }, + value: Bool( + false, + ), + }, + ), + Constant( + Constant { + node: Node { + start: 1555, + end: 1560, + }, + value: Bool( + false, + ), + }, + ), + Attribute( + Attribute { + node: Node { + start: 1583, + end: 1604, + }, + value: Name( + Name { + node: Node { + start: 1583, + end: 1589, + }, + id: "config", + }, + ), + attr: "DATA_BASE_PATH", + }, + ), + Constant( + Constant { + node: Node { + start: 1625, + end: 1630, + }, + value: Bool( + false, + ), + }, + ), + ], + }, + body: [ + IfStatement( + If { + node: Node { + start: 1669, + end: 1800, + }, + test: Compare( + Compare { + node: Node { + start: 1669, + end: 1685, + }, + left: Name( + Name { + node: Node { + start: 1669, + end: 1676, + }, + id: "symbols", + }, + ), + ops: [ + Eq, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 1680, + end: 1685, + }, + value: Str( + "all", + ), + }, + ), + ], + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 1695, + end: 1731, + }, + targets: [ + Name( + Name { + node: Node { + start: 1695, + end: 1702, + }, + id: "symbols", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 1729, + end: 1731, + }, + func: Attribute( + Attribute { + node: Node { + start: 1705, + end: 1729, + }, + value: Name( + Name { + node: Node { + start: 1705, + end: 1717, + }, + id: "symbols_data", + }, + ), + attr: "all_symbols", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ], + orelse: [ + IfStatement( + If { + node: Node { + start: 1736, + end: 1800, + }, + test: Call( + Call { + node: Node { + start: 1741, + end: 1765, + }, + func: Name( + Name { + node: Node { + start: 1741, + end: 1751, + }, + id: "isinstance", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 1752, + end: 1759, + }, + id: "symbols", + }, + ), + Name( + Name { + node: Node { + start: 1761, + end: 1764, + }, + id: "str", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 1775, + end: 1794, + }, + targets: [ + Name( + Name { + node: Node { + start: 1775, + end: 1782, + }, + id: "symbols", + }, + ), + ], + value: List( + List { + node: Node { + start: 1785, + end: 1794, + }, + elements: [ + Name( + Name { + node: Node { + start: 1786, + end: 1793, + }, + id: "symbols", + }, + ), + ], + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 1800, + end: 1812, + }, + targets: [ + Name( + Name { + node: Node { + start: 1800, + end: 1807, + }, + id: "df_list", + }, + ), + ], + value: Dict( + Dict { + node: Node { + start: 1810, + end: 1812, + }, + keys: [], + values: [], + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 1817, + end: 1838, + }, + targets: [ + Name( + Name { + node: Node { + start: 1817, + end: 1833, + }, + id: "future_to_symbol", + }, + ), + ], + value: Dict( + Dict { + node: Node { + start: 1836, + end: 1838, + }, + keys: [], + values: [], + }, + ), + }, + ), + WithStatement( + With { + node: Node { + start: 1843, + end: 15273, + }, + items: [ + WithItem { + node: Node { + start: 1848, + end: 1902, + }, + context_expr: Call( + Call { + node: Node { + start: 1874, + end: 1890, + }, + func: Attribute( + Attribute { + node: Node { + start: 1848, + end: 1874, + }, + value: Name( + Name { + node: Node { + start: 1848, + end: 1855, + }, + id: "futures", + }, + ), + attr: "ThreadPoolExecutor", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 1875, + end: 1889, + }, + arg: Some( + "max_workers", + ), + value: Constant( + Constant { + node: Node { + start: 1887, + end: 1889, + }, + value: Int( + "10", + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + optional_vars: Some( + Name( + Name { + node: Node { + start: 1894, + end: 1902, + }, + id: "executor", + }, + ), + ), + }, + ], + body: [ + AssignStatement( + Assign { + node: Node { + start: 1912, + end: 1946, + }, + targets: [ + Name( + Name { + node: Node { + start: 1912, + end: 1919, + }, + id: "session", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 1922, + end: 1946, + }, + func: Name( + Name { + node: Node { + start: 1922, + end: 1944, + }, + id: "requests_retry_session", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ForStatement( + For { + node: Node { + start: 1955, + end: 15273, + }, + target: Name( + Name { + node: Node { + start: 1959, + end: 1965, + }, + id: "symbol", + }, + ), + iter: Name( + Name { + node: Node { + start: 1969, + end: 1976, + }, + id: "symbols", + }, + ), + body: [ + IfStatement( + If { + node: Node { + start: 1993, + end: 2484, + }, + test: BoolOp( + BoolOperation { + node: Node { + start: 2011, + end: 2095, + }, + op: And, + values: [ + Call( + Call { + node: Node { + start: 2027, + end: 2029, + }, + func: Attribute( + Attribute { + node: Node { + start: 2011, + end: 2027, + }, + value: Name( + Name { + node: Node { + start: 2011, + end: 2017, + }, + id: "symbol", + }, + ), + attr: "isnumeric", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + Compare( + Compare { + node: Node { + start: 2050, + end: 2095, + }, + left: Call( + Call { + node: Node { + start: 2079, + end: 2087, + }, + func: Attribute( + Attribute { + node: Node { + start: 2050, + end: 2079, + }, + value: Name( + Name { + node: Node { + start: 2050, + end: 2062, + }, + id: "symbols_data", + }, + ), + attr: "get_ticker_index", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 2080, + end: 2086, + }, + id: "symbol", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ops: [ + Is, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 2091, + end: 2095, + }, + value: None, + }, + ), + ], + }, + ), + ], + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 2127, + end: 2152, + }, + targets: [ + Name( + Name { + node: Node { + start: 2127, + end: 2141, + }, + id: "ticker_indexes", + }, + ), + ], + value: List( + List { + node: Node { + start: 2144, + end: 2152, + }, + elements: [ + Name( + Name { + node: Node { + start: 2145, + end: 2151, + }, + id: "symbol", + }, + ), + ], + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ForStatement( + For { + node: Node { + start: 2484, + end: 2698, + }, + target: Name( + Name { + node: Node { + start: 2488, + end: 2493, + }, + id: "index", + }, + ), + iter: Name( + Name { + node: Node { + start: 2497, + end: 2511, + }, + id: "ticker_indexes", + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 2529, + end: 2637, + }, + targets: [ + Name( + Name { + node: Node { + start: 2529, + end: 2535, + }, + id: "future", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 2553, + end: 2637, + }, + func: Attribute( + Attribute { + node: Node { + start: 2538, + end: 2553, + }, + value: Name( + Name { + node: Node { + start: 2538, + end: 2546, + }, + id: "executor", + }, + ), + attr: "submit", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 2575, + end: 2603, + }, + id: "download_ticker_daily_record", + }, + ), + Name( + Name { + node: Node { + start: 2605, + end: 2610, + }, + id: "index", + }, + ), + Name( + Name { + node: Node { + start: 2612, + end: 2619, + }, + id: "session", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 2655, + end: 2688, + }, + targets: [ + Subscript( + Subscript { + node: Node { + start: 2655, + end: 2679, + }, + value: Name( + Name { + node: Node { + start: 2655, + end: 2671, + }, + id: "future_to_symbol", + }, + ), + slice: Name( + Name { + node: Node { + start: 2672, + end: 2678, + }, + id: "future", + }, + ), + }, + ), + ], + value: Name( + Name { + node: Node { + start: 2682, + end: 2688, + }, + id: "symbol", + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ForStatement( + For { + node: Node { + start: 2698, + end: 15273, + }, + target: Name( + Name { + node: Node { + start: 2702, + end: 2708, + }, + id: "future", + }, + ), + iter: Call( + Call { + node: Node { + start: 2732, + end: 2750, + }, + func: Attribute( + Attribute { + node: Node { + start: 2712, + end: 2732, + }, + value: Name( + Name { + node: Node { + start: 2712, + end: 2719, + }, + id: "futures", + }, + ), + attr: "as_completed", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 2733, + end: 2749, + }, + id: "future_to_symbol", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 2764, + end: 2797, + }, + targets: [ + Name( + Name { + node: Node { + start: 2764, + end: 2770, + }, + id: "symbol", + }, + ), + ], + value: Subscript( + Subscript { + node: Node { + start: 2773, + end: 2797, + }, + value: Name( + Name { + node: Node { + start: 2773, + end: 2789, + }, + id: "future_to_symbol", + }, + ), + slice: Name( + Name { + node: Node { + start: 2790, + end: 2796, + }, + id: "future", + }, + ), + }, + ), + }, + ), + TryStatement( + Try { + node: Node { + start: 2810, + end: 3120, + }, + body: [ + AnnAssignStatement( + AnnAssign { + node: Node { + start: 2831, + end: 2865, + }, + target: Name( + Name { + node: Node { + start: 2831, + end: 2833, + }, + id: "df", + }, + ), + annotation: Attribute( + Attribute { + node: Node { + start: 2835, + end: 2847, + }, + value: Name( + Name { + node: Node { + start: 2835, + end: 2837, + }, + id: "pd", + }, + ), + attr: "DataFrame", + }, + ), + value: Some( + Call( + Call { + node: Node { + start: 2863, + end: 2865, + }, + func: Attribute( + Attribute { + node: Node { + start: 2850, + end: 2863, + }, + value: Name( + Name { + node: Node { + start: 2850, + end: 2856, + }, + id: "future", + }, + ), + attr: "result", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + simple: true, + }, + ), + ], + handlers: [ + ExceptHandler { + node: Node { + start: 2878, + end: 3120, + }, + typ: Some( + Attribute( + Attribute { + node: Node { + start: 2885, + end: 2909, + }, + value: Attribute( + Attribute { + node: Node { + start: 2885, + end: 2894, + }, + value: Name( + Name { + node: Node { + start: 2885, + end: 2887, + }, + id: "pd", + }, + ), + attr: "errors", + }, + ), + attr: "EmptyDataError", + }, + ), + ), + name: Some( + "ex", + ), + body: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 2945, + end: 3082, + }, + func: Attribute( + Attribute { + node: Node { + start: 2933, + end: 2945, + }, + value: Name( + Name { + node: Node { + start: 2933, + end: 2939, + }, + id: "logger", + }, + ), + attr: "error", + }, + ), + args: [ + JoinedStr( + JoinedStr { + node: Node { + start: 2967, + end: 3022, + }, + values: [ + Constant( + Constant { + node: Node { + start: 3013, + end: 0, + }, + value: Str( + "Cannot read daily trade records for symbol: ", + ), + }, + ), + Name( + Name { + node: Node { + start: 3014, + end: 3020, + }, + id: "symbol", + }, + ), + ], + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 3044, + end: 3063, + }, + arg: Some( + "extra", + ), + value: Dict( + Dict { + node: Node { + start: 3050, + end: 3063, + }, + keys: [ + Constant( + Constant { + node: Node { + start: 3051, + end: 3058, + }, + value: Str( + "Error", + ), + }, + ), + ], + values: [ + Name( + Name { + node: Node { + start: 3060, + end: 3062, + }, + id: "ex", + }, + ), + ], + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + ), + Continue( + Continue { + node: Node { + start: 3099, + end: 3107, + }, + }, + ), + ], + }, + ], + orelse: [], + finalbody: [], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 3120, + end: 3161, + }, + targets: [ + Name( + Name { + node: Node { + start: 3120, + end: 3122, + }, + id: "df", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 3150, + end: 3161, + }, + func: Attribute( + Attribute { + node: Node { + start: 3138, + end: 3150, + }, + value: Subscript( + Subscript { + node: Node { + start: 3132, + end: 3138, + }, + value: Attribute( + Attribute { + node: Node { + start: 3125, + end: 3132, + }, + value: Name( + Name { + node: Node { + start: 3125, + end: 3127, + }, + id: "df", + }, + ), + attr: "iloc", + }, + ), + slice: Slice( + Slice { + node: Node { + start: 3133, + end: 3137, + }, + lower: None, + upper: Some( + UnaryOp( + UnaryOperation { + node: Node { + start: 3135, + end: 3137, + }, + op: USub, + operand: Constant( + Constant { + node: Node { + start: 3136, + end: 3137, + }, + value: Int( + "1", + ), + }, + ), + }, + ), + ), + step: None, + }, + ), + }, + ), + attr: "reset_index", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 3151, + end: 3160, + }, + arg: Some( + "drop", + ), + value: Constant( + Constant { + node: Node { + start: 3156, + end: 3160, + }, + value: Bool( + true, + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 3174, + end: 3233, + }, + targets: [ + Name( + Name { + node: Node { + start: 3174, + end: 3176, + }, + id: "df", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 3188, + end: 3233, + }, + func: Attribute( + Attribute { + node: Node { + start: 3179, + end: 3188, + }, + value: Name( + Name { + node: Node { + start: 3179, + end: 3181, + }, + id: "df", + }, + ), + attr: "rename", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 3189, + end: 3232, + }, + arg: Some( + "columns", + ), + value: Attribute( + Attribute { + node: Node { + start: 3197, + end: 3232, + }, + value: Name( + Name { + node: Node { + start: 3197, + end: 3209, + }, + id: "translations", + }, + ), + attr: "HISTORY_FIELD_MAPPINGS", + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 3246, + end: 3289, + }, + targets: [ + Name( + Name { + node: Node { + start: 3246, + end: 3248, + }, + id: "df", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 3258, + end: 3289, + }, + func: Attribute( + Attribute { + node: Node { + start: 3251, + end: 3258, + }, + value: Name( + Name { + node: Node { + start: 3251, + end: 3253, + }, + id: "df", + }, + ), + attr: "drop", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 3259, + end: 3288, + }, + arg: Some( + "columns", + ), + value: List( + List { + node: Node { + start: 3267, + end: 3288, + }, + elements: [ + Constant( + Constant { + node: Node { + start: 3268, + end: 3275, + }, + value: Str( + "", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 3277, + end: 3287, + }, + value: Str( + "", + ), + }, + ), + ], + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ExpressionStatement( + Call( + Call { + node: Node { + start: 3302, + end: 3339, + }, + func: Name( + Name { + node: Node { + start: 3302, + end: 3320, + }, + id: "_adjust_data_frame", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 3321, + end: 3323, + }, + id: "df", + }, + ), + Name( + Name { + node: Node { + start: 3325, + end: 3338, + }, + id: "include_jdate", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + IfStatement( + If { + node: Node { + start: 3356, + end: 3712, + }, + test: Compare( + Compare { + node: Node { + start: 3356, + end: 3373, + }, + left: Name( + Name { + node: Node { + start: 3356, + end: 3362, + }, + id: "symbol", + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + Name { + node: Node { + start: 3366, + end: 3373, + }, + id: "df_list", + }, + ), + ], + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 3391, + end: 3643, + }, + targets: [ + Subscript( + Subscript { + node: Node { + start: 3391, + end: 3406, + }, + value: Name( + Name { + node: Node { + start: 3391, + end: 3398, + }, + id: "df_list", + }, + ), + slice: Name( + Name { + node: Node { + start: 3399, + end: 3405, + }, + id: "symbol", + }, + ), + }, + ), + ], + value: Call( + Call { + node: Node { + start: 3614, + end: 3625, + }, + func: Attribute( + Attribute { + node: Node { + start: 3602, + end: 3614, + }, + value: Call( + Call { + node: Node { + start: 3573, + end: 3581, + }, + func: Attribute( + Attribute { + node: Node { + start: 3561, + end: 3573, + }, + value: Call( + Call { + node: Node { + start: 3440, + end: 3540, + }, + func: Attribute( + Attribute { + node: Node { + start: 3431, + end: 3440, + }, + value: Name( + Name { + node: Node { + start: 3431, + end: 3433, + }, + id: "pd", + }, + ), + attr: "concat", + }, + ), + args: [ + List( + List { + node: Node { + start: 3466, + end: 3487, + }, + elements: [ + Subscript( + Subscript { + node: Node { + start: 3467, + end: 3482, + }, + value: Name( + Name { + node: Node { + start: 3467, + end: 3474, + }, + id: "df_list", + }, + ), + slice: Name( + Name { + node: Node { + start: 3475, + end: 3481, + }, + id: "symbol", + }, + ), + }, + ), + Name( + Name { + node: Node { + start: 3484, + end: 3486, + }, + id: "df", + }, + ), + ], + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 3489, + end: 3506, + }, + arg: Some( + "ignore_index", + ), + value: Constant( + Constant { + node: Node { + start: 3502, + end: 3506, + }, + value: Bool( + true, + ), + }, + ), + }, + Keyword { + node: Node { + start: 3508, + end: 3518, + }, + arg: Some( + "sort", + ), + value: Constant( + Constant { + node: Node { + start: 3513, + end: 3518, + }, + value: Bool( + false, + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + attr: "sort_values", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 3574, + end: 3580, + }, + value: Str( + "date", + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + attr: "reset_index", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 3615, + end: 3624, + }, + arg: Some( + "drop", + ), + value: Constant( + Constant { + node: Node { + start: 3620, + end: 3624, + }, + value: Bool( + true, + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + IfStatement( + If { + node: Node { + start: 3715, + end: 3800, + }, + test: Name( + Name { + node: Node { + start: 3715, + end: 3721, + }, + id: "adjust", + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 3739, + end: 3786, + }, + targets: [ + Subscript( + Subscript { + node: Node { + start: 3739, + end: 3754, + }, + value: Name( + Name { + node: Node { + start: 3739, + end: 3746, + }, + id: "df_list", + }, + ), + slice: Name( + Name { + node: Node { + start: 3747, + end: 3753, + }, + id: "symbol", + }, + ), + }, + ), + ], + value: Call( + Call { + node: Node { + start: 3757, + end: 3786, + }, + func: Name( + Name { + node: Node { + start: 3757, + end: 3769, + }, + id: "adjust_price", + }, + ), + args: [ + Subscript( + Subscript { + node: Node { + start: 3770, + end: 3785, + }, + value: Name( + Name { + node: Node { + start: 3770, + end: 3777, + }, + id: "df_list", + }, + ), + slice: Name( + Name { + node: Node { + start: 3778, + end: 3784, + }, + id: "symbol", + }, + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + IfStatement( + If { + node: Node { + start: 3803, + end: 4346, + }, + test: Name( + Name { + node: Node { + start: 3803, + end: 3815, + }, + id: "write_to_csv", + }, + ), + body: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 3854, + end: 3883, + }, + func: Attribute( + Attribute { + node: Node { + start: 3848, + end: 3854, + }, + value: Call( + Call { + node: Node { + start: 3833, + end: 3848, + }, + func: Name( + Name { + node: Node { + start: 3833, + end: 3837, + }, + id: "Path", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 3838, + end: 3847, + }, + id: "base_path", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + attr: "mkdir", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 3855, + end: 3867, + }, + arg: Some( + "parents", + ), + value: Constant( + Constant { + node: Node { + start: 3863, + end: 3867, + }, + value: Bool( + true, + ), + }, + ), + }, + Keyword { + node: Node { + start: 3869, + end: 3882, + }, + arg: Some( + "exist_ok", + ), + value: Constant( + Constant { + node: Node { + start: 3878, + end: 3882, + }, + value: Bool( + true, + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + ), + IfStatement( + If { + node: Node { + start: 3903, + end: 4203, + }, + test: Name( + Name { + node: Node { + start: 3903, + end: 3909, + }, + id: "adjust", + }, + ), + body: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 3953, + end: 4044, + }, + func: Attribute( + Attribute { + node: Node { + start: 3946, + end: 3953, + }, + value: Subscript( + Subscript { + node: Node { + start: 3931, + end: 3946, + }, + value: Name( + Name { + node: Node { + start: 3931, + end: 3938, + }, + id: "df_list", + }, + ), + slice: Name( + Name { + node: Node { + start: 3939, + end: 3945, + }, + id: "symbol", + }, + ), + }, + ), + attr: "to_csv", + }, + ), + args: [ + JoinedStr( + JoinedStr { + node: Node { + start: 3979, + end: 4009, + }, + values: [ + Name( + Name { + node: Node { + start: 3982, + end: 3991, + }, + id: "base_path", + }, + ), + Constant( + Constant { + node: Node { + start: 3993, + end: 0, + }, + value: Str( + "/", + ), + }, + ), + Name( + Name { + node: Node { + start: 3994, + end: 4000, + }, + id: "symbol", + }, + ), + Constant( + Constant { + node: Node { + start: 4008, + end: 0, + }, + value: Str( + "-ت.csv", + ), + }, + ), + ], + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 4011, + end: 4022, + }, + arg: Some( + "index", + ), + value: Constant( + Constant { + node: Node { + start: 4017, + end: 4022, + }, + value: Bool( + false, + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + ), + ], + orelse: [], + }, + ), + IfStatement( + If { + node: Node { + start: 4206, + end: 4309, + }, + test: Compare( + Compare { + node: Node { + start: 4206, + end: 4234, + }, + left: Call( + Call { + node: Node { + start: 4206, + end: 4218, + }, + func: Name( + Name { + node: Node { + start: 4206, + end: 4209, + }, + id: "len", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 4210, + end: 4217, + }, + id: "df_list", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ops: [ + NotEq, + ], + comparators: [ + Call( + Call { + node: Node { + start: 4222, + end: 4234, + }, + func: Name( + Name { + node: Node { + start: 4222, + end: 4225, + }, + id: "len", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 4226, + end: 4233, + }, + id: "symbols", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ], + }, + ), + body: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 4244, + end: 4304, + }, + func: Name( + Name { + node: Node { + start: 4244, + end: 4249, + }, + id: "print", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 4250, + end: 4303, + }, + value: Str( + "Warning, download did not complete, re-run the code", + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + ], + orelse: [], + }, + ), + ExpressionStatement( + Call( + Call { + node: Node { + start: 4322, + end: 4324, + }, + func: Attribute( + Attribute { + node: Node { + start: 4309, + end: 4322, + }, + value: Name( + Name { + node: Node { + start: 4309, + end: 4316, + }, + id: "session", + }, + ), + attr: "close", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + Return( + Return { + node: Node { + start: 4329, + end: 4343, + }, + value: Some( + Name( + Name { + node: Node { + start: 4336, + end: 4343, + }, + id: "df_list", + }, + ), + ), + }, + ), + ], + orelse: [], + }, + ), + FunctionDef( + FunctionDef { + node: Node { + start: 4346, + end: 6805, + }, + name: "adjust_price", + args: Arguments { + node: Node { + start: 4363, + end: 4379, + }, + posonlyargs: [], + args: [ + Arg { + node: Node { + start: 4363, + end: 4379, + }, + arg: "df", + annotation: Some( + Attribute( + Attribute { + node: Node { + start: 4367, + end: 4379, + }, + value: Name( + Name { + node: Node { + start: 4367, + end: 4369, + }, + id: "pd", + }, + ), + attr: "DataFrame", + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: [ + ExpressionStatement( + Constant( + Constant { + node: Node { + start: 4402, + end: 5588, + }, + value: Str( + "\n Adjust historical records of stock\n\n There is a capital increase/profit sharing,\n if today \"Final Close Price\" is not equal to next day\n \"Yesterday Final Close Price\" by using this ratio,\n performance adjustment of stocks is achieved\n\n Parameters\n ----------\n df : pd.DataFrame\n DataFrame with historical records.\n\n Returns\n -------\n pd.DataFrame\n DataFrame with adjusted historical records.\n\n Notes\n -----\n DataFrame can not be empty or else it makes runtime error\n Type of DataFrame must be RangeIndex to make proper range of records\n that need to be modified\n\n diff: list\n list of indexs of the day after capital increase/profit sharing\n ratio_list: List\n List of ratios to adjust historical data of stock\n ratio: Float\n ratio = df.loc[i].adjClose / df.loc[i+1].yesterday\n\n Description\n -----------\n # Note: adjustment does not include Tenth and twentieth days\n df.index = range(0,101,1)\n # step is 1\n step = df.index.step\n diff = [10,20]\n ratio_list = [0.5, 0.8]\n df.loc[0:10-step, [open,...]] * ratio[0]\n df.loc[10:20-step, [open,...]] * ratio[1]\n ", + ), + }, + ), + ), + IfStatement( + If { + node: Node { + start: 5596, + end: 5691, + }, + test: BoolOp( + BoolOperation { + node: Node { + start: 5596, + end: 5666, + }, + op: Or, + values: [ + Attribute( + Attribute { + node: Node { + start: 5596, + end: 5604, + }, + value: Name( + Name { + node: Node { + start: 5596, + end: 5598, + }, + id: "df", + }, + ), + attr: "empty", + }, + ), + UnaryOp( + UnaryOperation { + node: Node { + start: 5608, + end: 5666, + }, + op: Not, + operand: Call( + Call { + node: Node { + start: 5612, + end: 5666, + }, + func: Name( + Name { + node: Node { + start: 5612, + end: 5622, + }, + id: "isinstance", + }, + ), + args: [ + Attribute( + Attribute { + node: Node { + start: 5623, + end: 5631, + }, + value: Name( + Name { + node: Node { + start: 5623, + end: 5625, + }, + id: "df", + }, + ), + attr: "index", + }, + ), + Attribute( + Attribute { + node: Node { + start: 5633, + end: 5665, + }, + value: Attribute( + Attribute { + node: Node { + start: 5633, + end: 5654, + }, + value: Attribute( + Attribute { + node: Node { + start: 5633, + end: 5648, + }, + value: Attribute( + Attribute { + node: Node { + start: 5633, + end: 5640, + }, + value: Name( + Name { + node: Node { + start: 5633, + end: 5635, + }, + id: "pd", + }, + ), + attr: "core", + }, + ), + attr: "indexes", + }, + ), + attr: "range", + }, + ), + attr: "RangeIndex", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ], + }, + ), + body: [ + Return( + Return { + node: Node { + start: 5676, + end: 5685, + }, + value: Some( + Name( + Name { + node: Node { + start: 5683, + end: 5685, + }, + id: "df", + }, + ), + ), + }, + ), + ], + orelse: [], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 5691, + end: 5709, + }, + targets: [ + Name( + Name { + node: Node { + start: 5691, + end: 5697, + }, + id: "new_df", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 5707, + end: 5709, + }, + func: Attribute( + Attribute { + node: Node { + start: 5700, + end: 5707, + }, + value: Name( + Name { + node: Node { + start: 5700, + end: 5702, + }, + id: "df", + }, + ), + attr: "copy", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 5714, + end: 5738, + }, + targets: [ + Name( + Name { + node: Node { + start: 5714, + end: 5718, + }, + id: "step", + }, + ), + ], + value: Attribute( + Attribute { + node: Node { + start: 5721, + end: 5738, + }, + value: Attribute( + Attribute { + node: Node { + start: 5721, + end: 5733, + }, + value: Name( + Name { + node: Node { + start: 5721, + end: 5727, + }, + id: "new_df", + }, + ), + attr: "index", + }, + ), + attr: "step", + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 5743, + end: 5814, + }, + targets: [ + Name( + Name { + node: Node { + start: 5743, + end: 5747, + }, + id: "diff", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 5750, + end: 5814, + }, + func: Name( + Name { + node: Node { + start: 5750, + end: 5754, + }, + id: "list", + }, + ), + args: [ + Subscript( + Subscript { + node: Node { + start: 5767, + end: 5813, + }, + value: Attribute( + Attribute { + node: Node { + start: 5755, + end: 5767, + }, + value: Name( + Name { + node: Node { + start: 5755, + end: 5761, + }, + id: "new_df", + }, + ), + attr: "index", + }, + ), + slice: Compare( + Compare { + node: Node { + start: 5768, + end: 5812, + }, + left: Attribute( + Attribute { + node: Node { + start: 5783, + end: 5792, + }, + value: Call( + Call { + node: Node { + start: 5780, + end: 5783, + }, + func: Attribute( + Attribute { + node: Node { + start: 5768, + end: 5780, + }, + value: Name( + Name { + node: Node { + start: 5768, + end: 5774, + }, + id: "new_df", + }, + ), + attr: "shift", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 5781, + end: 5782, + }, + value: Int( + "1", + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + attr: "adjClose", + }, + ), + ops: [ + NotEq, + ], + comparators: [ + Attribute( + Attribute { + node: Node { + start: 5796, + end: 5812, + }, + value: Name( + Name { + node: Node { + start: 5796, + end: 5802, + }, + id: "new_df", + }, + ), + attr: "yesterday", + }, + ), + ], + }, + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + IfStatement( + If { + node: Node { + start: 5822, + end: 5861, + }, + test: Compare( + Compare { + node: Node { + start: 5822, + end: 5835, + }, + left: Call( + Call { + node: Node { + start: 5822, + end: 5831, + }, + func: Name( + Name { + node: Node { + start: 5822, + end: 5825, + }, + id: "len", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 5826, + end: 5830, + }, + id: "diff", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 5834, + end: 5835, + }, + value: Int( + "0", + ), + }, + ), + ], + }, + ), + body: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 5853, + end: 5856, + }, + func: Attribute( + Attribute { + node: Node { + start: 5845, + end: 5853, + }, + value: Name( + Name { + node: Node { + start: 5845, + end: 5849, + }, + id: "diff", + }, + ), + attr: "pop", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 5854, + end: 5855, + }, + value: Int( + "0", + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + ], + orelse: [], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 5861, + end: 5870, + }, + targets: [ + Name( + Name { + node: Node { + start: 5861, + end: 5866, + }, + id: "ratio", + }, + ), + ], + value: Constant( + Constant { + node: Node { + start: 5869, + end: 5870, + }, + value: Int( + "1", + ), + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 5875, + end: 5890, + }, + targets: [ + Name( + Name { + node: Node { + start: 5875, + end: 5885, + }, + id: "ratio_list", + }, + ), + ], + value: List( + List { + node: Node { + start: 5888, + end: 5890, + }, + elements: [], + }, + ), + }, + ), + ForStatement( + For { + node: Node { + start: 5895, + end: 6061, + }, + target: Name( + Name { + node: Node { + start: 5899, + end: 5900, + }, + id: "i", + }, + ), + iter: Subscript( + Subscript { + node: Node { + start: 5904, + end: 5914, + }, + value: Name( + Name { + node: Node { + start: 5904, + end: 5908, + }, + id: "diff", + }, + ), + slice: Slice( + Slice { + node: Node { + start: 5909, + end: 5913, + }, + lower: None, + upper: Some( + UnaryOp( + UnaryOperation { + node: Node { + start: 5911, + end: 5913, + }, + op: USub, + operand: Constant( + Constant { + node: Node { + start: 5912, + end: 5913, + }, + value: Int( + "1", + ), + }, + ), + }, + ), + ), + step: None, + }, + ), + }, + ), + body: [ + AugAssignStatement( + AugAssign { + node: Node { + start: 5924, + end: 6020, + }, + target: Name( + Name { + node: Node { + start: 5924, + end: 5929, + }, + id: "ratio", + }, + ), + op: Mult, + value: BinOp( + BinOp { + node: Node { + start: 5947, + end: 6010, + }, + op: Div, + left: Subscript( + Subscript { + node: Node { + start: 5957, + end: 5973, + }, + value: Attribute( + Attribute { + node: Node { + start: 5947, + end: 5957, + }, + value: Name( + Name { + node: Node { + start: 5947, + end: 5953, + }, + id: "new_df", + }, + ), + attr: "loc", + }, + ), + slice: Tuple( + Tuple { + node: Node { + start: 5958, + end: 5973, + }, + elements: [ + Name( + Name { + node: Node { + start: 5958, + end: 5959, + }, + id: "i", + }, + ), + Constant( + Constant { + node: Node { + start: 5961, + end: 5972, + }, + value: Str( + "yesterday", + ), + }, + ), + ], + }, + ), + }, + ), + right: Subscript( + Subscript { + node: Node { + start: 5995, + end: 6010, + }, + value: Attribute( + Attribute { + node: Node { + start: 5991, + end: 5995, + }, + value: Call( + Call { + node: Node { + start: 5988, + end: 5991, + }, + func: Attribute( + Attribute { + node: Node { + start: 5976, + end: 5988, + }, + value: Name( + Name { + node: Node { + start: 5976, + end: 5982, + }, + id: "new_df", + }, + ), + attr: "shift", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 5989, + end: 5990, + }, + value: Int( + "1", + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + attr: "loc", + }, + ), + slice: Tuple( + Tuple { + node: Node { + start: 5996, + end: 6010, + }, + elements: [ + Name( + Name { + node: Node { + start: 5996, + end: 5997, + }, + id: "i", + }, + ), + Constant( + Constant { + node: Node { + start: 5999, + end: 6009, + }, + value: Str( + "adjClose", + ), + }, + ), + ], + }, + ), + }, + ), + }, + ), + }, + ), + ExpressionStatement( + Call( + Call { + node: Node { + start: 6046, + end: 6056, + }, + func: Attribute( + Attribute { + node: Node { + start: 6029, + end: 6046, + }, + value: Name( + Name { + node: Node { + start: 6029, + end: 6039, + }, + id: "ratio_list", + }, + ), + attr: "insert", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 6047, + end: 6048, + }, + value: Int( + "0", + ), + }, + ), + Name( + Name { + node: Node { + start: 6050, + end: 6055, + }, + id: "ratio", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + ], + orelse: [], + }, + ), + ForStatement( + For { + node: Node { + start: 6061, + end: 6789, + }, + target: Tuple( + Tuple { + node: Node { + start: 6065, + end: 6069, + }, + elements: [ + Name( + Name { + node: Node { + start: 6065, + end: 6066, + }, + id: "i", + }, + ), + Name( + Name { + node: Node { + start: 6068, + end: 6069, + }, + id: "k", + }, + ), + ], + }, + ), + iter: Call( + Call { + node: Node { + start: 6073, + end: 6088, + }, + func: Name( + Name { + node: Node { + start: 6073, + end: 6082, + }, + id: "enumerate", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 6083, + end: 6087, + }, + id: "diff", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + body: [ + IfStatement( + If { + node: Node { + start: 6101, + end: 6202, + }, + test: Compare( + Compare { + node: Node { + start: 6101, + end: 6107, + }, + left: Name( + Name { + node: Node { + start: 6101, + end: 6102, + }, + id: "i", + }, + ), + ops: [ + Eq, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 6106, + end: 6107, + }, + value: Int( + "0", + ), + }, + ), + ], + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 6121, + end: 6147, + }, + targets: [ + Name( + Name { + node: Node { + start: 6121, + end: 6126, + }, + id: "start", + }, + ), + ], + value: Attribute( + Attribute { + node: Node { + start: 6129, + end: 6147, + }, + value: Attribute( + Attribute { + node: Node { + start: 6129, + end: 6141, + }, + value: Name( + Name { + node: Node { + start: 6129, + end: 6135, + }, + id: "new_df", + }, + ), + attr: "index", + }, + ), + attr: "start", + }, + ), + }, + ), + ], + orelse: [], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 6202, + end: 6222, + }, + targets: [ + Name( + Name { + node: Node { + start: 6202, + end: 6205, + }, + id: "end", + }, + ), + ], + value: BinOp( + BinOp { + node: Node { + start: 6208, + end: 6222, + }, + op: Sub, + left: Subscript( + Subscript { + node: Node { + start: 6208, + end: 6215, + }, + value: Name( + Name { + node: Node { + start: 6208, + end: 6212, + }, + id: "diff", + }, + ), + slice: Name( + Name { + node: Node { + start: 6213, + end: 6214, + }, + id: "i", + }, + ), + }, + ), + right: Name( + Name { + node: Node { + start: 6218, + end: 6222, + }, + id: "step", + }, + ), + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 6231, + end: 6783, + }, + targets: [ + Subscript( + Subscript { + node: Node { + start: 6241, + end: 6457, + }, + value: Attribute( + Attribute { + node: Node { + start: 6231, + end: 6241, + }, + value: Name( + Name { + node: Node { + start: 6231, + end: 6237, + }, + id: "new_df", + }, + ), + attr: "loc", + }, + ), + slice: Tuple( + Tuple { + node: Node { + start: 6255, + end: 6457, + }, + elements: [ + Slice( + Slice { + node: Node { + start: 6260, + end: 6264, + }, + lower: Some( + Name( + Name { + node: Node { + start: 6255, + end: 6260, + }, + id: "start", + }, + ), + ), + upper: Some( + Name( + Name { + node: Node { + start: 6261, + end: 6264, + }, + id: "end", + }, + ), + ), + step: None, + }, + ), + List( + List { + node: Node { + start: 6278, + end: 6446, + }, + elements: [ + Constant( + Constant { + node: Node { + start: 6296, + end: 6302, + }, + value: Str( + "open", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 6320, + end: 6326, + }, + value: Str( + "high", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 6344, + end: 6349, + }, + value: Str( + "low", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 6367, + end: 6374, + }, + value: Str( + "close", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 6392, + end: 6402, + }, + value: Str( + "adjClose", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 6420, + end: 6431, + }, + value: Str( + "yesterday", + ), + }, + ), + ], + }, + ), + ], + }, + ), + }, + ), + ], + value: Call( + Call { + node: Node { + start: 6460, + end: 6783, + }, + func: Name( + Name { + node: Node { + start: 6460, + end: 6465, + }, + id: "round", + }, + ), + args: [ + BinOp( + BinOp { + node: Node { + start: 6479, + end: 6773, + }, + op: Mult, + left: Subscript( + Subscript { + node: Node { + start: 6489, + end: 6745, + }, + value: Attribute( + Attribute { + node: Node { + start: 6479, + end: 6489, + }, + value: Name( + Name { + node: Node { + start: 6479, + end: 6485, + }, + id: "new_df", + }, + ), + attr: "loc", + }, + ), + slice: Tuple( + Tuple { + node: Node { + start: 6507, + end: 6745, + }, + elements: [ + Slice( + Slice { + node: Node { + start: 6512, + end: 6516, + }, + lower: Some( + Name( + Name { + node: Node { + start: 6507, + end: 6512, + }, + id: "start", + }, + ), + ), + upper: Some( + Name( + Name { + node: Node { + start: 6513, + end: 6516, + }, + id: "end", + }, + ), + ), + step: None, + }, + ), + List( + List { + node: Node { + start: 6534, + end: 6730, + }, + elements: [ + Constant( + Constant { + node: Node { + start: 6556, + end: 6562, + }, + value: Str( + "open", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 6584, + end: 6590, + }, + value: Str( + "high", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 6612, + end: 6617, + }, + value: Str( + "low", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 6639, + end: 6646, + }, + value: Str( + "close", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 6668, + end: 6678, + }, + value: Str( + "adjClose", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 6700, + end: 6711, + }, + value: Str( + "yesterday", + ), + }, + ), + ], + }, + ), + ], + }, + ), + }, + ), + right: Subscript( + Subscript { + node: Node { + start: 6760, + end: 6773, + }, + value: Name( + Name { + node: Node { + start: 6760, + end: 6770, + }, + id: "ratio_list", + }, + ), + slice: Name( + Name { + node: Node { + start: 6771, + end: 6772, + }, + id: "i", + }, + ), + }, + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + Return( + Return { + node: Node { + start: 6789, + end: 6802, + }, + value: Some( + Name( + Name { + node: Node { + start: 6796, + end: 6802, + }, + id: "new_df", + }, + ), + ), + }, + ), + ], + decorator_list: [], + returns: Some( + Attribute( + Attribute { + node: Node { + start: 4384, + end: 4396, + }, + value: Name( + Name { + node: Node { + start: 4384, + end: 4386, + }, + id: "pd", + }, + ), + attr: "DataFrame", + }, + ), + ), + type_comment: None, + }, + ), + FunctionDef( + FunctionDef { + node: Node { + start: 6955, + end: 7464, + }, + name: "download_ticker_daily_record", + args: Arguments { + node: Node { + start: 6988, + end: 7023, + }, + posonlyargs: [], + args: [ + Arg { + node: Node { + start: 6988, + end: 7005, + }, + arg: "ticker_index", + annotation: Some( + Name( + Name { + node: Node { + start: 7002, + end: 7005, + }, + id: "str", + }, + ), + ), + }, + Arg { + node: Node { + start: 7007, + end: 7023, + }, + arg: "session", + annotation: Some( + Name( + Name { + node: Node { + start: 7016, + end: 7023, + }, + id: "Session", + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: [ + AssignStatement( + Assign { + node: Node { + start: 7030, + end: 7100, + }, + targets: [ + Name( + Name { + node: Node { + start: 7030, + end: 7033, + }, + id: "url", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 7086, + end: 7100, + }, + func: Attribute( + Attribute { + node: Node { + start: 7036, + end: 7086, + }, + value: Attribute( + Attribute { + node: Node { + start: 7036, + end: 7079, + }, + value: Name( + Name { + node: Node { + start: 7036, + end: 7048, + }, + id: "tse_settings", + }, + ), + attr: "TSE_TICKER_EXPORT_DATA_ADDRESS", + }, + ), + attr: "format", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 7087, + end: 7099, + }, + id: "ticker_index", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 7105, + end: 7144, + }, + targets: [ + Name( + Name { + node: Node { + start: 7105, + end: 7113, + }, + id: "response", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 7127, + end: 7144, + }, + func: Attribute( + Attribute { + node: Node { + start: 7116, + end: 7127, + }, + value: Name( + Name { + node: Node { + start: 7116, + end: 7123, + }, + id: "session", + }, + ), + attr: "get", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 7128, + end: 7131, + }, + id: "url", + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 7133, + end: 7143, + }, + arg: Some( + "timeout", + ), + value: Constant( + Constant { + node: Node { + start: 7141, + end: 7143, + }, + value: Int( + "10", + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + ), + IfStatement( + If { + node: Node { + start: 7152, + end: 7369, + }, + test: Compare( + Compare { + node: Node { + start: 7152, + end: 7185, + }, + left: Constant( + Constant { + node: Node { + start: 7152, + end: 7155, + }, + value: Int( + "400", + ), + }, + ), + ops: [ + LtE, + Lt, + ], + comparators: [ + Attribute( + Attribute { + node: Node { + start: 7159, + end: 7179, + }, + value: Name( + Name { + node: Node { + start: 7159, + end: 7167, + }, + id: "response", + }, + ), + attr: "status_code", + }, + ), + Constant( + Constant { + node: Node { + start: 7182, + end: 7185, + }, + value: Int( + "500", + ), + }, + ), + ], + }, + ), + body: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 7207, + end: 7363, + }, + func: Attribute( + Attribute { + node: Node { + start: 7195, + end: 7207, + }, + value: Name( + Name { + node: Node { + start: 7195, + end: 7201, + }, + id: "logger", + }, + ), + attr: "error", + }, + ), + args: [ + JoinedStr( + JoinedStr { + node: Node { + start: 7221, + end: 7275, + }, + values: [ + Constant( + Constant { + node: Node { + start: 7269, + end: 0, + }, + value: Str( + "Cannot read daily trade records from the url: ", + ), + }, + ), + Name( + Name { + node: Node { + start: 7270, + end: 7273, + }, + id: "url", + }, + ), + ], + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 7289, + end: 7352, + }, + arg: Some( + "extra", + ), + value: Dict( + Dict { + node: Node { + start: 7295, + end: 7352, + }, + keys: [ + Constant( + Constant { + node: Node { + start: 7296, + end: 7306, + }, + value: Str( + "response", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 7323, + end: 7329, + }, + value: Str( + "code", + ), + }, + ), + ], + values: [ + Attribute( + Attribute { + node: Node { + start: 7308, + end: 7321, + }, + value: Name( + Name { + node: Node { + start: 7308, + end: 7316, + }, + id: "response", + }, + ), + attr: "text", + }, + ), + Attribute( + Attribute { + node: Node { + start: 7331, + end: 7351, + }, + value: Name( + Name { + node: Node { + start: 7331, + end: 7339, + }, + id: "response", + }, + ), + attr: "status_code", + }, + ), + ], + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + ), + ], + orelse: [], + }, + ), + ExpressionStatement( + Call( + Call { + node: Node { + start: 7394, + end: 7396, + }, + func: Attribute( + Attribute { + node: Node { + start: 7369, + end: 7394, + }, + value: Name( + Name { + node: Node { + start: 7369, + end: 7377, + }, + id: "response", + }, + ), + attr: "raise_for_status", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + AssignStatement( + Assign { + node: Node { + start: 7402, + end: 7432, + }, + targets: [ + Name( + Name { + node: Node { + start: 7402, + end: 7406, + }, + id: "data", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 7409, + end: 7432, + }, + func: Name( + Name { + node: Node { + start: 7409, + end: 7417, + }, + id: "StringIO", + }, + ), + args: [ + Attribute( + Attribute { + node: Node { + start: 7418, + end: 7431, + }, + value: Name( + Name { + node: Node { + start: 7418, + end: 7426, + }, + id: "response", + }, + ), + attr: "text", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + Return( + Return { + node: Node { + start: 7437, + end: 7461, + }, + value: Some( + Call( + Call { + node: Node { + start: 7455, + end: 7461, + }, + func: Attribute( + Attribute { + node: Node { + start: 7444, + end: 7455, + }, + value: Name( + Name { + node: Node { + start: 7444, + end: 7446, + }, + id: "pd", + }, + ), + attr: "read_csv", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 7456, + end: 7460, + }, + id: "data", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + }, + ), + ], + decorator_list: [ + Call( + Call { + node: Node { + start: 6806, + end: 6954, + }, + func: Name( + Name { + node: Node { + start: 6806, + end: 6811, + }, + id: "retry", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 6817, + end: 6857, + }, + arg: Some( + "retry", + ), + value: Call( + Call { + node: Node { + start: 6823, + end: 6857, + }, + func: Name( + Name { + node: Node { + start: 6823, + end: 6846, + }, + id: "retry_if_exception_type", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 6847, + end: 6856, + }, + id: "HTTPError", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + Keyword { + node: Node { + start: 6863, + end: 6893, + }, + arg: Some( + "wait", + ), + value: Call( + Call { + node: Node { + start: 6868, + end: 6893, + }, + func: Name( + Name { + node: Node { + start: 6868, + end: 6879, + }, + id: "wait_random", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 6880, + end: 6885, + }, + arg: Some( + "min", + ), + value: Constant( + Constant { + node: Node { + start: 6884, + end: 6885, + }, + value: Int( + "1", + ), + }, + ), + }, + Keyword { + node: Node { + start: 6887, + end: 6892, + }, + arg: Some( + "max", + ), + value: Constant( + Constant { + node: Node { + start: 6891, + end: 6892, + }, + value: Int( + "4", + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + Keyword { + node: Node { + start: 6899, + end: 6951, + }, + arg: Some( + "before_sleep", + ), + value: Call( + Call { + node: Node { + start: 6912, + end: 6951, + }, + func: Name( + Name { + node: Node { + start: 6912, + end: 6928, + }, + id: "before_sleep_log", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 6929, + end: 6935, + }, + id: "logger", + }, + ), + Attribute( + Attribute { + node: Node { + start: 6937, + end: 6950, + }, + value: Name( + Name { + node: Node { + start: 6937, + end: 6944, + }, + id: "logging", + }, + ), + attr: "DEBUG", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + ], + returns: None, + type_comment: None, + }, + ), + FunctionDef( + FunctionDef { + node: Node { + start: 7614, + end: 8358, + }, + name: "download_fIndex_record", + args: Arguments { + node: Node { + start: 7641, + end: 7670, + }, + posonlyargs: [], + args: [ + Arg { + node: Node { + start: 7641, + end: 7652, + }, + arg: "fIndex", + annotation: Some( + Name( + Name { + node: Node { + start: 7649, + end: 7652, + }, + id: "str", + }, + ), + ), + }, + Arg { + node: Node { + start: 7654, + end: 7670, + }, + arg: "session", + annotation: Some( + Name( + Name { + node: Node { + start: 7663, + end: 7670, + }, + id: "Session", + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: [ + AssignStatement( + Assign { + node: Node { + start: 7677, + end: 7750, + }, + targets: [ + Name( + Name { + node: Node { + start: 7677, + end: 7680, + }, + id: "url", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 7742, + end: 7750, + }, + func: Attribute( + Attribute { + node: Node { + start: 7683, + end: 7742, + }, + value: Attribute( + Attribute { + node: Node { + start: 7683, + end: 7735, + }, + value: Name( + Name { + node: Node { + start: 7683, + end: 7695, + }, + id: "tse_settings", + }, + ), + attr: "TSE_FINANCIAL_INDEX_EXPORT_DATA_ADDRESS", + }, + ), + attr: "format", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 7743, + end: 7749, + }, + id: "fIndex", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 7755, + end: 7794, + }, + targets: [ + Name( + Name { + node: Node { + start: 7755, + end: 7763, + }, + id: "response", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 7777, + end: 7794, + }, + func: Attribute( + Attribute { + node: Node { + start: 7766, + end: 7777, + }, + value: Name( + Name { + node: Node { + start: 7766, + end: 7773, + }, + id: "session", + }, + ), + attr: "get", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 7778, + end: 7781, + }, + id: "url", + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 7783, + end: 7793, + }, + arg: Some( + "timeout", + ), + value: Constant( + Constant { + node: Node { + start: 7791, + end: 7793, + }, + value: Int( + "10", + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + ), + IfStatement( + If { + node: Node { + start: 7802, + end: 8019, + }, + test: Compare( + Compare { + node: Node { + start: 7802, + end: 7835, + }, + left: Constant( + Constant { + node: Node { + start: 7802, + end: 7805, + }, + value: Int( + "400", + ), + }, + ), + ops: [ + LtE, + Lt, + ], + comparators: [ + Attribute( + Attribute { + node: Node { + start: 7809, + end: 7829, + }, + value: Name( + Name { + node: Node { + start: 7809, + end: 7817, + }, + id: "response", + }, + ), + attr: "status_code", + }, + ), + Constant( + Constant { + node: Node { + start: 7832, + end: 7835, + }, + value: Int( + "500", + ), + }, + ), + ], + }, + ), + body: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 7857, + end: 8013, + }, + func: Attribute( + Attribute { + node: Node { + start: 7845, + end: 7857, + }, + value: Name( + Name { + node: Node { + start: 7845, + end: 7851, + }, + id: "logger", + }, + ), + attr: "error", + }, + ), + args: [ + JoinedStr( + JoinedStr { + node: Node { + start: 7871, + end: 7925, + }, + values: [ + Constant( + Constant { + node: Node { + start: 7919, + end: 0, + }, + value: Str( + "Cannot read daily trade records from the url: ", + ), + }, + ), + Name( + Name { + node: Node { + start: 7920, + end: 7923, + }, + id: "url", + }, + ), + ], + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 7939, + end: 8002, + }, + arg: Some( + "extra", + ), + value: Dict( + Dict { + node: Node { + start: 7945, + end: 8002, + }, + keys: [ + Constant( + Constant { + node: Node { + start: 7946, + end: 7956, + }, + value: Str( + "response", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 7973, + end: 7979, + }, + value: Str( + "code", + ), + }, + ), + ], + values: [ + Attribute( + Attribute { + node: Node { + start: 7958, + end: 7971, + }, + value: Name( + Name { + node: Node { + start: 7958, + end: 7966, + }, + id: "response", + }, + ), + attr: "text", + }, + ), + Attribute( + Attribute { + node: Node { + start: 7981, + end: 8001, + }, + value: Name( + Name { + node: Node { + start: 7981, + end: 7989, + }, + id: "response", + }, + ), + attr: "status_code", + }, + ), + ], + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + ), + ], + orelse: [], + }, + ), + ExpressionStatement( + Call( + Call { + node: Node { + start: 8044, + end: 8046, + }, + func: Attribute( + Attribute { + node: Node { + start: 8019, + end: 8044, + }, + value: Name( + Name { + node: Node { + start: 8019, + end: 8027, + }, + id: "response", + }, + ), + attr: "raise_for_status", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + AssignStatement( + Assign { + node: Node { + start: 8051, + end: 8071, + }, + targets: [ + Name( + Name { + node: Node { + start: 8051, + end: 8055, + }, + id: "data", + }, + ), + ], + value: Attribute( + Attribute { + node: Node { + start: 8058, + end: 8071, + }, + value: Name( + Name { + node: Node { + start: 8058, + end: 8066, + }, + id: "response", + }, + ), + attr: "text", + }, + ), + }, + ), + IfStatement( + If { + node: Node { + start: 8079, + end: 8288, + }, + test: BoolOp( + BoolOperation { + node: Node { + start: 8079, + end: 8125, + }, + op: Or, + values: [ + UnaryOp( + UnaryOperation { + node: Node { + start: 8079, + end: 8087, + }, + op: Not, + operand: Name( + Name { + node: Node { + start: 8083, + end: 8087, + }, + id: "data", + }, + ), + }, + ), + BoolOp( + BoolOperation { + node: Node { + start: 8091, + end: 8125, + }, + op: Or, + values: [ + Compare( + Compare { + node: Node { + start: 8091, + end: 8106, + }, + left: Constant( + Constant { + node: Node { + start: 8091, + end: 8094, + }, + value: Str( + ";", + ), + }, + ), + ops: [ + NotIn, + ], + comparators: [ + Name( + Name { + node: Node { + start: 8102, + end: 8106, + }, + id: "data", + }, + ), + ], + }, + ), + Compare( + Compare { + node: Node { + start: 8110, + end: 8125, + }, + left: Constant( + Constant { + node: Node { + start: 8110, + end: 8113, + }, + value: Str( + ",", + ), + }, + ), + ops: [ + NotIn, + ], + comparators: [ + Name( + Name { + node: Node { + start: 8121, + end: 8125, + }, + id: "data", + }, + ), + ], + }, + ), + ], + }, + ), + ], + }, + ), + body: [ + Raise( + Raise { + node: Node { + start: 8135, + end: 8283, + }, + exc: Some( + Call( + Call { + node: Node { + start: 8141, + end: 8283, + }, + func: Name( + Name { + node: Node { + start: 8141, + end: 8151, + }, + id: "ValueError", + }, + ), + args: [ + JoinedStr( + JoinedStr { + node: Node { + start: 8165, + end: 8273, + }, + values: [ + Constant( + Constant { + node: Node { + start: 8200, + end: 0, + }, + value: Str( + "Invalid response from the url: ", + ), + }, + ), + Name( + Name { + node: Node { + start: 8201, + end: 8204, + }, + id: "url", + }, + ), + Constant( + Constant { + node: Node { + start: 8270, + end: 0, + }, + value: Str( + ".\n \\nExpected valid financial index data.", + ), + }, + ), + ], + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + cause: None, + }, + ), + ], + orelse: [], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 8288, + end: 8341, + }, + targets: [ + Name( + Name { + node: Node { + start: 8288, + end: 8290, + }, + id: "df", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 8293, + end: 8341, + }, + func: Name( + Name { + node: Node { + start: 8293, + end: 8335, + }, + id: "_create_financial_index_from_text_response", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 8336, + end: 8340, + }, + id: "data", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + Return( + Return { + node: Node { + start: 8346, + end: 8355, + }, + value: Some( + Name( + Name { + node: Node { + start: 8353, + end: 8355, + }, + id: "df", + }, + ), + ), + }, + ), + ], + decorator_list: [ + Call( + Call { + node: Node { + start: 7465, + end: 7613, + }, + func: Name( + Name { + node: Node { + start: 7465, + end: 7470, + }, + id: "retry", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 7476, + end: 7516, + }, + arg: Some( + "retry", + ), + value: Call( + Call { + node: Node { + start: 7482, + end: 7516, + }, + func: Name( + Name { + node: Node { + start: 7482, + end: 7505, + }, + id: "retry_if_exception_type", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 7506, + end: 7515, + }, + id: "HTTPError", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + Keyword { + node: Node { + start: 7522, + end: 7552, + }, + arg: Some( + "wait", + ), + value: Call( + Call { + node: Node { + start: 7527, + end: 7552, + }, + func: Name( + Name { + node: Node { + start: 7527, + end: 7538, + }, + id: "wait_random", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 7539, + end: 7544, + }, + arg: Some( + "min", + ), + value: Constant( + Constant { + node: Node { + start: 7543, + end: 7544, + }, + value: Int( + "1", + ), + }, + ), + }, + Keyword { + node: Node { + start: 7546, + end: 7551, + }, + arg: Some( + "max", + ), + value: Constant( + Constant { + node: Node { + start: 7550, + end: 7551, + }, + value: Int( + "4", + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + Keyword { + node: Node { + start: 7558, + end: 7610, + }, + arg: Some( + "before_sleep", + ), + value: Call( + Call { + node: Node { + start: 7571, + end: 7610, + }, + func: Name( + Name { + node: Node { + start: 7571, + end: 7587, + }, + id: "before_sleep_log", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 7588, + end: 7594, + }, + id: "logger", + }, + ), + Attribute( + Attribute { + node: Node { + start: 7596, + end: 7609, + }, + value: Name( + Name { + node: Node { + start: 7596, + end: 7603, + }, + id: "logging", + }, + ), + attr: "DEBUG", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + ], + returns: None, + type_comment: None, + }, + ), + FunctionDef( + FunctionDef { + node: Node { + start: 8358, + end: 15273, + }, + name: "download_financial_indexes", + args: Arguments { + node: Node { + start: 8394, + end: 8540, + }, + posonlyargs: [], + args: [ + Arg { + node: Node { + start: 8394, + end: 8419, + }, + arg: "symbols", + annotation: Some( + Subscript( + Subscript { + node: Node { + start: 8403, + end: 8419, + }, + value: Name( + Name { + node: Node { + start: 8403, + end: 8408, + }, + id: "Union", + }, + ), + slice: Tuple( + Tuple { + node: Node { + start: 8409, + end: 8419, + }, + elements: [ + Name( + Name { + node: Node { + start: 8409, + end: 8413, + }, + id: "List", + }, + ), + Name( + Name { + node: Node { + start: 8415, + end: 8418, + }, + id: "str", + }, + ), + ], + }, + ), + }, + ), + ), + }, + Arg { + node: Node { + start: 8425, + end: 8451, + }, + arg: "write_to_csv", + annotation: Some( + Name( + Name { + node: Node { + start: 8439, + end: 8443, + }, + id: "bool", + }, + ), + ), + }, + Arg { + node: Node { + start: 8457, + end: 8484, + }, + arg: "include_jdate", + annotation: Some( + Name( + Name { + node: Node { + start: 8472, + end: 8476, + }, + id: "bool", + }, + ), + ), + }, + Arg { + node: Node { + start: 8490, + end: 8539, + }, + arg: "base_path", + annotation: Some( + Name( + Name { + node: Node { + start: 8501, + end: 8504, + }, + id: "str", + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [ + Constant( + Constant { + node: Node { + start: 8446, + end: 8451, + }, + value: Bool( + false, + ), + }, + ), + Constant( + Constant { + node: Node { + start: 8479, + end: 8484, + }, + value: Bool( + false, + ), + }, + ), + Attribute( + Attribute { + node: Node { + start: 8507, + end: 8539, + }, + value: Name( + Name { + node: Node { + start: 8507, + end: 8513, + }, + id: "config", + }, + ), + attr: "FINANCIAL_INDEX_BASE_PATH", + }, + ), + ], + }, + body: [ + IfStatement( + If { + node: Node { + start: 8578, + end: 8717, + }, + test: Compare( + Compare { + node: Node { + start: 8578, + end: 8594, + }, + left: Name( + Name { + node: Node { + start: 8578, + end: 8585, + }, + id: "symbols", + }, + ), + ops: [ + Eq, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 8589, + end: 8594, + }, + value: Str( + "all", + ), + }, + ), + ], + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 8604, + end: 8648, + }, + targets: [ + Name( + Name { + node: Node { + start: 8604, + end: 8611, + }, + id: "symbols", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 8646, + end: 8648, + }, + func: Attribute( + Attribute { + node: Node { + start: 8614, + end: 8646, + }, + value: Name( + Name { + node: Node { + start: 8614, + end: 8626, + }, + id: "symbols_data", + }, + ), + attr: "all_financial_index", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ], + orelse: [ + IfStatement( + If { + node: Node { + start: 8653, + end: 8717, + }, + test: Call( + Call { + node: Node { + start: 8658, + end: 8682, + }, + func: Name( + Name { + node: Node { + start: 8658, + end: 8668, + }, + id: "isinstance", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 8669, + end: 8676, + }, + id: "symbols", + }, + ), + Name( + Name { + node: Node { + start: 8678, + end: 8681, + }, + id: "str", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 8692, + end: 8711, + }, + targets: [ + Name( + Name { + node: Node { + start: 8692, + end: 8699, + }, + id: "symbols", + }, + ), + ], + value: List( + List { + node: Node { + start: 8702, + end: 8711, + }, + elements: [ + Name( + Name { + node: Node { + start: 8703, + end: 8710, + }, + id: "symbols", + }, + ), + ], + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 8717, + end: 8729, + }, + targets: [ + Name( + Name { + node: Node { + start: 8717, + end: 8724, + }, + id: "df_list", + }, + ), + ], + value: Dict( + Dict { + node: Node { + start: 8727, + end: 8729, + }, + keys: [], + values: [], + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 8734, + end: 8755, + }, + targets: [ + Name( + Name { + node: Node { + start: 8734, + end: 8750, + }, + id: "future_to_symbol", + }, + ), + ], + value: Dict( + Dict { + node: Node { + start: 8753, + end: 8755, + }, + keys: [], + values: [], + }, + ), + }, + ), + WithStatement( + With { + node: Node { + start: 8760, + end: 15273, + }, + items: [ + WithItem { + node: Node { + start: 8765, + end: 8819, + }, + context_expr: Call( + Call { + node: Node { + start: 8791, + end: 8807, + }, + func: Attribute( + Attribute { + node: Node { + start: 8765, + end: 8791, + }, + value: Name( + Name { + node: Node { + start: 8765, + end: 8772, + }, + id: "futures", + }, + ), + attr: "ThreadPoolExecutor", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 8792, + end: 8806, + }, + arg: Some( + "max_workers", + ), + value: Constant( + Constant { + node: Node { + start: 8804, + end: 8806, + }, + value: Int( + "10", + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + optional_vars: Some( + Name( + Name { + node: Node { + start: 8811, + end: 8819, + }, + id: "executor", + }, + ), + ), + }, + ], + body: [ + AssignStatement( + Assign { + node: Node { + start: 8829, + end: 8863, + }, + targets: [ + Name( + Name { + node: Node { + start: 8829, + end: 8836, + }, + id: "session", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 8839, + end: 8863, + }, + func: Name( + Name { + node: Node { + start: 8839, + end: 8861, + }, + id: "requests_retry_session", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ForStatement( + For { + node: Node { + start: 8872, + end: 9506, + }, + target: Name( + Name { + node: Node { + start: 8876, + end: 8882, + }, + id: "symbol", + }, + ), + iter: Name( + Name { + node: Node { + start: 8886, + end: 8893, + }, + id: "symbols", + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 8907, + end: 8947, + }, + targets: [ + Name( + Name { + node: Node { + start: 8907, + end: 8913, + }, + id: "symbol", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 8939, + end: 8947, + }, + func: Attribute( + Attribute { + node: Node { + start: 8916, + end: 8939, + }, + value: Name( + Name { + node: Node { + start: 8916, + end: 8923, + }, + id: "persian", + }, + ), + attr: "replace_persian", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 8940, + end: 8946, + }, + id: "symbol", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + IfStatement( + If { + node: Node { + start: 8963, + end: 9230, + }, + test: BoolOp( + BoolOperation { + node: Node { + start: 8981, + end: 9068, + }, + op: And, + values: [ + Call( + Call { + node: Node { + start: 8997, + end: 8999, + }, + func: Attribute( + Attribute { + node: Node { + start: 8981, + end: 8997, + }, + value: Name( + Name { + node: Node { + start: 8981, + end: 8987, + }, + id: "symbol", + }, + ), + attr: "isnumeric", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + Compare( + Compare { + node: Node { + start: 9020, + end: 9068, + }, + left: Call( + Call { + node: Node { + start: 9052, + end: 9060, + }, + func: Attribute( + Attribute { + node: Node { + start: 9020, + end: 9052, + }, + value: Name( + Name { + node: Node { + start: 9020, + end: 9032, + }, + id: "symbols_data", + }, + ), + attr: "get_financial_index", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 9053, + end: 9059, + }, + id: "symbol", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ops: [ + Is, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 9064, + end: 9068, + }, + value: None, + }, + ), + ], + }, + ), + ], + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 9100, + end: 9124, + }, + targets: [ + Name( + Name { + node: Node { + start: 9100, + end: 9115, + }, + id: "financial_index", + }, + ), + ], + value: Name( + Name { + node: Node { + start: 9118, + end: 9124, + }, + id: "symbol", + }, + ), + }, + ), + ], + orelse: [], + }, + ), + IfStatement( + If { + node: Node { + start: 9233, + end: 9345, + }, + test: Compare( + Compare { + node: Node { + start: 9233, + end: 9256, + }, + left: Name( + Name { + node: Node { + start: 9233, + end: 9248, + }, + id: "financial_index", + }, + ), + ops: [ + Is, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 9252, + end: 9256, + }, + value: None, + }, + ), + ], + }, + ), + body: [ + Raise( + Raise { + node: Node { + start: 9274, + end: 9331, + }, + exc: Some( + Call( + Call { + node: Node { + start: 9280, + end: 9331, + }, + func: Name( + Name { + node: Node { + start: 9280, + end: 9289, + }, + id: "Exception", + }, + ), + args: [ + JoinedStr( + JoinedStr { + node: Node { + start: 9290, + end: 9330, + }, + values: [ + Constant( + Constant { + node: Node { + start: 9321, + end: 0, + }, + value: Str( + "Cannot find financial index: ", + ), + }, + ), + Name( + Name { + node: Node { + start: 9322, + end: 9328, + }, + id: "symbol", + }, + ), + ], + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + cause: None, + }, + ), + ], + orelse: [], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 9345, + end: 9449, + }, + targets: [ + Name( + Name { + node: Node { + start: 9345, + end: 9351, + }, + id: "future", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 9369, + end: 9449, + }, + func: Attribute( + Attribute { + node: Node { + start: 9354, + end: 9369, + }, + value: Name( + Name { + node: Node { + start: 9354, + end: 9362, + }, + id: "executor", + }, + ), + attr: "submit", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 9387, + end: 9409, + }, + id: "download_fIndex_record", + }, + ), + Name( + Name { + node: Node { + start: 9411, + end: 9426, + }, + id: "financial_index", + }, + ), + Name( + Name { + node: Node { + start: 9428, + end: 9435, + }, + id: "session", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 9463, + end: 9496, + }, + targets: [ + Subscript( + Subscript { + node: Node { + start: 9463, + end: 9487, + }, + value: Name( + Name { + node: Node { + start: 9463, + end: 9479, + }, + id: "future_to_symbol", + }, + ), + slice: Name( + Name { + node: Node { + start: 9480, + end: 9486, + }, + id: "future", + }, + ), + }, + ), + ], + value: Name( + Name { + node: Node { + start: 9490, + end: 9496, + }, + id: "symbol", + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ForStatement( + For { + node: Node { + start: 9506, + end: 10374, + }, + target: Name( + Name { + node: Node { + start: 9510, + end: 9516, + }, + id: "future", + }, + ), + iter: Call( + Call { + node: Node { + start: 9540, + end: 9558, + }, + func: Attribute( + Attribute { + node: Node { + start: 9520, + end: 9540, + }, + value: Name( + Name { + node: Node { + start: 9520, + end: 9527, + }, + id: "futures", + }, + ), + attr: "as_completed", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 9541, + end: 9557, + }, + id: "future_to_symbol", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 9572, + end: 9605, + }, + targets: [ + Name( + Name { + node: Node { + start: 9572, + end: 9578, + }, + id: "symbol", + }, + ), + ], + value: Subscript( + Subscript { + node: Node { + start: 9581, + end: 9605, + }, + value: Name( + Name { + node: Node { + start: 9581, + end: 9597, + }, + id: "future_to_symbol", + }, + ), + slice: Name( + Name { + node: Node { + start: 9598, + end: 9604, + }, + id: "future", + }, + ), + }, + ), + }, + ), + TryStatement( + Try { + node: Node { + start: 9618, + end: 9928, + }, + body: [ + AnnAssignStatement( + AnnAssign { + node: Node { + start: 9639, + end: 9673, + }, + target: Name( + Name { + node: Node { + start: 9639, + end: 9641, + }, + id: "df", + }, + ), + annotation: Attribute( + Attribute { + node: Node { + start: 9643, + end: 9655, + }, + value: Name( + Name { + node: Node { + start: 9643, + end: 9645, + }, + id: "pd", + }, + ), + attr: "DataFrame", + }, + ), + value: Some( + Call( + Call { + node: Node { + start: 9671, + end: 9673, + }, + func: Attribute( + Attribute { + node: Node { + start: 9658, + end: 9671, + }, + value: Name( + Name { + node: Node { + start: 9658, + end: 9664, + }, + id: "future", + }, + ), + attr: "result", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + simple: true, + }, + ), + ], + handlers: [ + ExceptHandler { + node: Node { + start: 9686, + end: 9928, + }, + typ: Some( + Attribute( + Attribute { + node: Node { + start: 9693, + end: 9717, + }, + value: Attribute( + Attribute { + node: Node { + start: 9693, + end: 9702, + }, + value: Name( + Name { + node: Node { + start: 9693, + end: 9695, + }, + id: "pd", + }, + ), + attr: "errors", + }, + ), + attr: "EmptyDataError", + }, + ), + ), + name: Some( + "ex", + ), + body: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 9753, + end: 9890, + }, + func: Attribute( + Attribute { + node: Node { + start: 9741, + end: 9753, + }, + value: Name( + Name { + node: Node { + start: 9741, + end: 9747, + }, + id: "logger", + }, + ), + attr: "error", + }, + ), + args: [ + JoinedStr( + JoinedStr { + node: Node { + start: 9775, + end: 9830, + }, + values: [ + Constant( + Constant { + node: Node { + start: 9821, + end: 0, + }, + value: Str( + "Cannot read daily trade records for symbol: ", + ), + }, + ), + Name( + Name { + node: Node { + start: 9822, + end: 9828, + }, + id: "symbol", + }, + ), + ], + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 9852, + end: 9871, + }, + arg: Some( + "extra", + ), + value: Dict( + Dict { + node: Node { + start: 9858, + end: 9871, + }, + keys: [ + Constant( + Constant { + node: Node { + start: 9859, + end: 9866, + }, + value: Str( + "Error", + ), + }, + ), + ], + values: [ + Name( + Name { + node: Node { + start: 9868, + end: 9870, + }, + id: "ex", + }, + ), + ], + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + ), + Continue( + Continue { + node: Node { + start: 9907, + end: 9915, + }, + }, + ), + ], + }, + ], + orelse: [], + finalbody: [], + }, + ), + ExpressionStatement( + Call( + Call { + node: Node { + start: 9928, + end: 9976, + }, + func: Name( + Name { + node: Node { + start: 9928, + end: 9957, + }, + id: "_adjust_data_frame_for_fIndex", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 9958, + end: 9960, + }, + id: "df", + }, + ), + Name( + Name { + node: Node { + start: 9962, + end: 9975, + }, + id: "include_jdate", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + AssignStatement( + Assign { + node: Node { + start: 9989, + end: 10009, + }, + targets: [ + Subscript( + Subscript { + node: Node { + start: 9989, + end: 10004, + }, + value: Name( + Name { + node: Node { + start: 9989, + end: 9996, + }, + id: "df_list", + }, + ), + slice: Name( + Name { + node: Node { + start: 9997, + end: 10003, + }, + id: "symbol", + }, + ), + }, + ), + ], + value: Name( + Name { + node: Node { + start: 10007, + end: 10009, + }, + id: "df", + }, + ), + }, + ), + IfStatement( + If { + node: Node { + start: 10026, + end: 10231, + }, + test: Name( + Name { + node: Node { + start: 10026, + end: 10038, + }, + id: "write_to_csv", + }, + ), + body: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 10077, + end: 10106, + }, + func: Attribute( + Attribute { + node: Node { + start: 10071, + end: 10077, + }, + value: Call( + Call { + node: Node { + start: 10056, + end: 10071, + }, + func: Name( + Name { + node: Node { + start: 10056, + end: 10060, + }, + id: "Path", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 10061, + end: 10070, + }, + id: "base_path", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + attr: "mkdir", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 10078, + end: 10090, + }, + arg: Some( + "parents", + ), + value: Constant( + Constant { + node: Node { + start: 10086, + end: 10090, + }, + value: Bool( + true, + ), + }, + ), + }, + Keyword { + node: Node { + start: 10092, + end: 10105, + }, + arg: Some( + "exist_ok", + ), + value: Constant( + Constant { + node: Node { + start: 10101, + end: 10105, + }, + value: Bool( + true, + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + ), + ExpressionStatement( + Call( + Call { + node: Node { + start: 10145, + end: 10225, + }, + func: Attribute( + Attribute { + node: Node { + start: 10138, + end: 10145, + }, + value: Subscript( + Subscript { + node: Node { + start: 10123, + end: 10138, + }, + value: Name( + Name { + node: Node { + start: 10123, + end: 10130, + }, + id: "df_list", + }, + ), + slice: Name( + Name { + node: Node { + start: 10131, + end: 10137, + }, + id: "symbol", + }, + ), + }, + ), + attr: "to_csv", + }, + ), + args: [ + JoinedStr( + JoinedStr { + node: Node { + start: 10167, + end: 10194, + }, + values: [ + Name( + Name { + node: Node { + start: 10170, + end: 10179, + }, + id: "base_path", + }, + ), + Constant( + Constant { + node: Node { + start: 10181, + end: 0, + }, + value: Str( + "/", + ), + }, + ), + Name( + Name { + node: Node { + start: 10182, + end: 10188, + }, + id: "symbol", + }, + ), + Constant( + Constant { + node: Node { + start: 10193, + end: 0, + }, + value: Str( + ".csv", + ), + }, + ), + ], + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 10196, + end: 10207, + }, + arg: Some( + "index", + ), + value: Constant( + Constant { + node: Node { + start: 10202, + end: 10207, + }, + value: Bool( + false, + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + ), + ], + orelse: [], + }, + ), + IfStatement( + If { + node: Node { + start: 10234, + end: 10337, + }, + test: Compare( + Compare { + node: Node { + start: 10234, + end: 10262, + }, + left: Call( + Call { + node: Node { + start: 10234, + end: 10246, + }, + func: Name( + Name { + node: Node { + start: 10234, + end: 10237, + }, + id: "len", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 10238, + end: 10245, + }, + id: "df_list", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ops: [ + NotEq, + ], + comparators: [ + Call( + Call { + node: Node { + start: 10250, + end: 10262, + }, + func: Name( + Name { + node: Node { + start: 10250, + end: 10253, + }, + id: "len", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 10254, + end: 10261, + }, + id: "symbols", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ], + }, + ), + body: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 10272, + end: 10332, + }, + func: Name( + Name { + node: Node { + start: 10272, + end: 10277, + }, + id: "print", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 10278, + end: 10331, + }, + value: Str( + "Warning, download did not complete, re-run the code", + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + ], + orelse: [], + }, + ), + ExpressionStatement( + Call( + Call { + node: Node { + start: 10350, + end: 10352, + }, + func: Attribute( + Attribute { + node: Node { + start: 10337, + end: 10350, + }, + value: Name( + Name { + node: Node { + start: 10337, + end: 10344, + }, + id: "session", + }, + ), + attr: "close", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + Return( + Return { + node: Node { + start: 10357, + end: 10371, + }, + value: Some( + Name( + Name { + node: Node { + start: 10364, + end: 10371, + }, + id: "df_list", + }, + ), + ), + }, + ), + ], + orelse: [], + }, + ), + FunctionDef( + FunctionDef { + node: Node { + start: 10374, + end: 15273, + }, + name: "download_client_types_records", + args: Arguments { + node: Node { + start: 10413, + end: 10561, + }, + posonlyargs: [], + args: [ + Arg { + node: Node { + start: 10413, + end: 10438, + }, + arg: "symbols", + annotation: Some( + Subscript( + Subscript { + node: Node { + start: 10422, + end: 10438, + }, + value: Name( + Name { + node: Node { + start: 10422, + end: 10427, + }, + id: "Union", + }, + ), + slice: Tuple( + Tuple { + node: Node { + start: 10428, + end: 10438, + }, + elements: [ + Name( + Name { + node: Node { + start: 10428, + end: 10432, + }, + id: "List", + }, + ), + Name( + Name { + node: Node { + start: 10434, + end: 10437, + }, + id: "str", + }, + ), + ], + }, + ), + }, + ), + ), + }, + Arg { + node: Node { + start: 10444, + end: 10470, + }, + arg: "write_to_csv", + annotation: Some( + Name( + Name { + node: Node { + start: 10458, + end: 10462, + }, + id: "bool", + }, + ), + ), + }, + Arg { + node: Node { + start: 10476, + end: 10503, + }, + arg: "include_jdate", + annotation: Some( + Name( + Name { + node: Node { + start: 10491, + end: 10495, + }, + id: "bool", + }, + ), + ), + }, + Arg { + node: Node { + start: 10509, + end: 10560, + }, + arg: "base_path", + annotation: Some( + Name( + Name { + node: Node { + start: 10520, + end: 10523, + }, + id: "str", + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [ + Constant( + Constant { + node: Node { + start: 10465, + end: 10470, + }, + value: Bool( + false, + ), + }, + ), + Constant( + Constant { + node: Node { + start: 10498, + end: 10503, + }, + value: Bool( + false, + ), + }, + ), + Attribute( + Attribute { + node: Node { + start: 10526, + end: 10560, + }, + value: Name( + Name { + node: Node { + start: 10526, + end: 10532, + }, + id: "config", + }, + ), + attr: "CLIENT_TYPES_DATA_BASE_PATH", + }, + ), + ], + }, + body: [ + IfStatement( + If { + node: Node { + start: 10572, + end: 10703, + }, + test: Compare( + Compare { + node: Node { + start: 10572, + end: 10588, + }, + left: Name( + Name { + node: Node { + start: 10572, + end: 10579, + }, + id: "symbols", + }, + ), + ops: [ + Eq, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 10583, + end: 10588, + }, + value: Str( + "all", + ), + }, + ), + ], + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 10598, + end: 10634, + }, + targets: [ + Name( + Name { + node: Node { + start: 10598, + end: 10605, + }, + id: "symbols", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 10632, + end: 10634, + }, + func: Attribute( + Attribute { + node: Node { + start: 10608, + end: 10632, + }, + value: Name( + Name { + node: Node { + start: 10608, + end: 10620, + }, + id: "symbols_data", + }, + ), + attr: "all_symbols", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ], + orelse: [ + IfStatement( + If { + node: Node { + start: 10639, + end: 10703, + }, + test: Call( + Call { + node: Node { + start: 10644, + end: 10668, + }, + func: Name( + Name { + node: Node { + start: 10644, + end: 10654, + }, + id: "isinstance", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 10655, + end: 10662, + }, + id: "symbols", + }, + ), + Name( + Name { + node: Node { + start: 10664, + end: 10667, + }, + id: "str", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 10678, + end: 10697, + }, + targets: [ + Name( + Name { + node: Node { + start: 10678, + end: 10685, + }, + id: "symbols", + }, + ), + ], + value: List( + List { + node: Node { + start: 10688, + end: 10697, + }, + elements: [ + Name( + Name { + node: Node { + start: 10689, + end: 10696, + }, + id: "symbols", + }, + ), + ], + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 10703, + end: 10715, + }, + targets: [ + Name( + Name { + node: Node { + start: 10703, + end: 10710, + }, + id: "df_list", + }, + ), + ], + value: Dict( + Dict { + node: Node { + start: 10713, + end: 10715, + }, + keys: [], + values: [], + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 10720, + end: 10741, + }, + targets: [ + Name( + Name { + node: Node { + start: 10720, + end: 10736, + }, + id: "future_to_symbol", + }, + ), + ], + value: Dict( + Dict { + node: Node { + start: 10739, + end: 10741, + }, + keys: [], + values: [], + }, + ), + }, + ), + WithStatement( + With { + node: Node { + start: 10746, + end: 15273, + }, + items: [ + WithItem { + node: Node { + start: 10751, + end: 10805, + }, + context_expr: Call( + Call { + node: Node { + start: 10777, + end: 10793, + }, + func: Attribute( + Attribute { + node: Node { + start: 10751, + end: 10777, + }, + value: Name( + Name { + node: Node { + start: 10751, + end: 10758, + }, + id: "futures", + }, + ), + attr: "ThreadPoolExecutor", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 10778, + end: 10792, + }, + arg: Some( + "max_workers", + ), + value: Constant( + Constant { + node: Node { + start: 10790, + end: 10792, + }, + value: Int( + "10", + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + optional_vars: Some( + Name( + Name { + node: Node { + start: 10797, + end: 10805, + }, + id: "executor", + }, + ), + ), + }, + ], + body: [ + ForStatement( + For { + node: Node { + start: 10815, + end: 11168, + }, + target: Name( + Name { + node: Node { + start: 10819, + end: 10825, + }, + id: "symbol", + }, + ), + iter: Name( + Name { + node: Node { + start: 10829, + end: 10836, + }, + id: "symbols", + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 10850, + end: 10893, + }, + targets: [ + Name( + Name { + node: Node { + start: 10850, + end: 10862, + }, + id: "ticker_index", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 10865, + end: 10893, + }, + func: Name( + Name { + node: Node { + start: 10865, + end: 10885, + }, + id: "_handle_ticker_index", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 10886, + end: 10892, + }, + id: "symbol", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + IfStatement( + If { + node: Node { + start: 10909, + end: 11008, + }, + test: Compare( + Compare { + node: Node { + start: 10909, + end: 10929, + }, + left: Name( + Name { + node: Node { + start: 10909, + end: 10921, + }, + id: "ticker_index", + }, + ), + ops: [ + Is, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 10925, + end: 10929, + }, + value: None, + }, + ), + ], + }, + ), + body: [ + Raise( + Raise { + node: Node { + start: 10947, + end: 10995, + }, + exc: Some( + Call( + Call { + node: Node { + start: 10953, + end: 10995, + }, + func: Name( + Name { + node: Node { + start: 10953, + end: 10962, + }, + id: "Exception", + }, + ), + args: [ + JoinedStr( + JoinedStr { + node: Node { + start: 10963, + end: 10994, + }, + values: [ + Constant( + Constant { + node: Node { + start: 10985, + end: 0, + }, + value: Str( + "Cannot find symbol: ", + ), + }, + ), + Name( + Name { + node: Node { + start: 10986, + end: 10992, + }, + id: "symbol", + }, + ), + ], + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + cause: None, + }, + ), + ], + orelse: [], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 11008, + end: 11113, + }, + targets: [ + Name( + Name { + node: Node { + start: 11008, + end: 11014, + }, + id: "future", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 11032, + end: 11113, + }, + func: Attribute( + Attribute { + node: Node { + start: 11017, + end: 11032, + }, + value: Name( + Name { + node: Node { + start: 11017, + end: 11025, + }, + id: "executor", + }, + ), + attr: "submit", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 11050, + end: 11085, + }, + id: "download_ticker_client_types_record", + }, + ), + Name( + Name { + node: Node { + start: 11087, + end: 11099, + }, + id: "ticker_index", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 11126, + end: 11159, + }, + targets: [ + Subscript( + Subscript { + node: Node { + start: 11126, + end: 11150, + }, + value: Name( + Name { + node: Node { + start: 11126, + end: 11142, + }, + id: "future_to_symbol", + }, + ), + slice: Name( + Name { + node: Node { + start: 11143, + end: 11149, + }, + id: "future", + }, + ), + }, + ), + ], + value: Name( + Name { + node: Node { + start: 11153, + end: 11159, + }, + id: "symbol", + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ForStatement( + For { + node: Node { + start: 11168, + end: 11849, + }, + target: Name( + Name { + node: Node { + start: 11172, + end: 11178, + }, + id: "future", + }, + ), + iter: Call( + Call { + node: Node { + start: 11202, + end: 11220, + }, + func: Attribute( + Attribute { + node: Node { + start: 11182, + end: 11202, + }, + value: Name( + Name { + node: Node { + start: 11182, + end: 11189, + }, + id: "futures", + }, + ), + attr: "as_completed", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 11203, + end: 11219, + }, + id: "future_to_symbol", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 11234, + end: 11267, + }, + targets: [ + Name( + Name { + node: Node { + start: 11234, + end: 11240, + }, + id: "symbol", + }, + ), + ], + value: Subscript( + Subscript { + node: Node { + start: 11243, + end: 11267, + }, + value: Name( + Name { + node: Node { + start: 11243, + end: 11259, + }, + id: "future_to_symbol", + }, + ), + slice: Name( + Name { + node: Node { + start: 11260, + end: 11266, + }, + id: "future", + }, + ), + }, + ), + }, + ), + AnnAssignStatement( + AnnAssign { + node: Node { + start: 11280, + end: 11314, + }, + target: Name( + Name { + node: Node { + start: 11280, + end: 11282, + }, + id: "df", + }, + ), + annotation: Attribute( + Attribute { + node: Node { + start: 11284, + end: 11296, + }, + value: Name( + Name { + node: Node { + start: 11284, + end: 11286, + }, + id: "pd", + }, + ), + attr: "DataFrame", + }, + ), + value: Some( + Call( + Call { + node: Node { + start: 11312, + end: 11314, + }, + func: Attribute( + Attribute { + node: Node { + start: 11299, + end: 11312, + }, + value: Name( + Name { + node: Node { + start: 11299, + end: 11305, + }, + id: "future", + }, + ), + attr: "result", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + simple: true, + }, + ), + IfStatement( + If { + node: Node { + start: 11360, + end: 11409, + }, + test: Compare( + Compare { + node: Node { + start: 11360, + end: 11370, + }, + left: Name( + Name { + node: Node { + start: 11360, + end: 11362, + }, + id: "df", + }, + ), + ops: [ + Is, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 11366, + end: 11370, + }, + value: None, + }, + ), + ], + }, + ), + body: [ + Continue( + Continue { + node: Node { + start: 11388, + end: 11396, + }, + }, + ), + ], + orelse: [], + }, + ), + ExpressionStatement( + Call( + Call { + node: Node { + start: 11409, + end: 11446, + }, + func: Name( + Name { + node: Node { + start: 11409, + end: 11427, + }, + id: "_adjust_data_frame", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 11428, + end: 11430, + }, + id: "df", + }, + ), + Name( + Name { + node: Node { + start: 11432, + end: 11445, + }, + id: "include_jdate", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + AssignStatement( + Assign { + node: Node { + start: 11459, + end: 11479, + }, + targets: [ + Subscript( + Subscript { + node: Node { + start: 11459, + end: 11474, + }, + value: Name( + Name { + node: Node { + start: 11459, + end: 11466, + }, + id: "df_list", + }, + ), + slice: Name( + Name { + node: Node { + start: 11467, + end: 11473, + }, + id: "symbol", + }, + ), + }, + ), + ], + value: Name( + Name { + node: Node { + start: 11477, + end: 11479, + }, + id: "df", + }, + ), + }, + ), + IfStatement( + If { + node: Node { + start: 11495, + end: 11636, + }, + test: Name( + Name { + node: Node { + start: 11495, + end: 11507, + }, + id: "write_to_csv", + }, + ), + body: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 11546, + end: 11575, + }, + func: Attribute( + Attribute { + node: Node { + start: 11540, + end: 11546, + }, + value: Call( + Call { + node: Node { + start: 11525, + end: 11540, + }, + func: Name( + Name { + node: Node { + start: 11525, + end: 11529, + }, + id: "Path", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 11530, + end: 11539, + }, + id: "base_path", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + attr: "mkdir", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 11547, + end: 11559, + }, + arg: Some( + "parents", + ), + value: Constant( + Constant { + node: Node { + start: 11555, + end: 11559, + }, + value: Bool( + true, + ), + }, + ), + }, + Keyword { + node: Node { + start: 11561, + end: 11574, + }, + arg: Some( + "exist_ok", + ), + value: Constant( + Constant { + node: Node { + start: 11570, + end: 11574, + }, + value: Bool( + true, + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + ), + ExpressionStatement( + Call( + Call { + node: Node { + start: 11601, + end: 11630, + }, + func: Attribute( + Attribute { + node: Node { + start: 11592, + end: 11601, + }, + value: Name( + Name { + node: Node { + start: 11592, + end: 11594, + }, + id: "df", + }, + ), + attr: "to_csv", + }, + ), + args: [ + JoinedStr( + JoinedStr { + node: Node { + start: 11602, + end: 11629, + }, + values: [ + Name( + Name { + node: Node { + start: 11605, + end: 11614, + }, + id: "base_path", + }, + ), + Constant( + Constant { + node: Node { + start: 11616, + end: 0, + }, + value: Str( + "/", + ), + }, + ), + Name( + Name { + node: Node { + start: 11617, + end: 11623, + }, + id: "symbol", + }, + ), + Constant( + Constant { + node: Node { + start: 11628, + end: 0, + }, + value: Str( + ".csv", + ), + }, + ), + ], + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + ], + orelse: [], + }, + ), + IfStatement( + If { + node: Node { + start: 11639, + end: 11832, + }, + test: Compare( + Compare { + node: Node { + start: 11639, + end: 11667, + }, + left: Call( + Call { + node: Node { + start: 11639, + end: 11651, + }, + func: Name( + Name { + node: Node { + start: 11639, + end: 11642, + }, + id: "len", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 11643, + end: 11650, + }, + id: "df_list", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ops: [ + NotEq, + ], + comparators: [ + Call( + Call { + node: Node { + start: 11655, + end: 11667, + }, + func: Name( + Name { + node: Node { + start: 11655, + end: 11658, + }, + id: "len", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 11659, + end: 11666, + }, + id: "symbols", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ], + }, + ), + body: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 11677, + end: 11827, + }, + func: Name( + Name { + node: Node { + start: 11677, + end: 11682, + }, + id: "print", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 11696, + end: 11817, + }, + value: Str( + "could not download client types for all the symbols make\n sure you have what you need or re-run the function", + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + ], + orelse: [], + }, + ), + Return( + Return { + node: Node { + start: 11832, + end: 11846, + }, + value: Some( + Name( + Name { + node: Node { + start: 11839, + end: 11846, + }, + id: "df_list", + }, + ), + ), + }, + ), + ], + orelse: [], + }, + ), + FunctionDef( + FunctionDef { + node: Node { + start: 11999, + end: 13481, + }, + name: "download_ticker_client_types_record", + args: Arguments { + node: Node { + start: 12039, + end: 12066, + }, + posonlyargs: [], + args: [ + Arg { + node: Node { + start: 12039, + end: 12066, + }, + arg: "ticker_index", + annotation: Some( + Subscript( + Subscript { + node: Node { + start: 12053, + end: 12066, + }, + value: Name( + Name { + node: Node { + start: 12053, + end: 12061, + }, + id: "Optional", + }, + ), + slice: Name( + Name { + node: Node { + start: 12062, + end: 12065, + }, + id: "str", + }, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: [ + AssignStatement( + Assign { + node: Node { + start: 12073, + end: 12127, + }, + targets: [ + Name( + Name { + node: Node { + start: 12073, + end: 12077, + }, + id: "data", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 12080, + end: 12127, + }, + func: Name( + Name { + node: Node { + start: 12080, + end: 12113, + }, + id: "_extract_ticker_client_types_data", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 12114, + end: 12126, + }, + id: "ticker_index", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + IfStatement( + If { + node: Node { + start: 12135, + end: 12367, + }, + test: Compare( + Compare { + node: Node { + start: 12135, + end: 12149, + }, + left: Call( + Call { + node: Node { + start: 12135, + end: 12144, + }, + func: Name( + Name { + node: Node { + start: 12135, + end: 12138, + }, + id: "len", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 12139, + end: 12143, + }, + id: "data", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ops: [ + Eq, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 12148, + end: 12149, + }, + value: Int( + "1", + ), + }, + ), + ], + }, + ), + body: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 12173, + end: 12342, + }, + func: Attribute( + Attribute { + node: Node { + start: 12159, + end: 12173, + }, + value: Name( + Name { + node: Node { + start: 12159, + end: 12165, + }, + id: "logger", + }, + ), + attr: "warning", + }, + ), + args: [ + JoinedStr( + JoinedStr { + node: Node { + start: 12187, + end: 12281, + }, + values: [ + Constant( + Constant { + node: Node { + start: 12233, + end: 0, + }, + value: Str( + "Cannot create client types data for ticker", + ), + }, + ), + Name( + Name { + node: Node { + start: 12234, + end: 12246, + }, + id: "ticker_index", + }, + ), + Constant( + Constant { + node: Node { + start: 12272, + end: 0, + }, + value: Str( + "\n from data: ", + ), + }, + ), + Name( + Name { + node: Node { + start: 12273, + end: 12277, + }, + id: "data", + }, + ), + ], + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 12295, + end: 12331, + }, + arg: Some( + "extra", + ), + value: Dict( + Dict { + node: Node { + start: 12301, + end: 12331, + }, + keys: [ + Constant( + Constant { + node: Node { + start: 12302, + end: 12316, + }, + value: Str( + "ticker_index", + ), + }, + ), + ], + values: [ + Name( + Name { + node: Node { + start: 12318, + end: 12330, + }, + id: "ticker_index", + }, + ), + ], + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + ), + Return( + Return { + node: Node { + start: 12351, + end: 12362, + }, + value: Some( + Constant( + Constant { + node: Node { + start: 12358, + end: 12362, + }, + value: None, + }, + ), + ), + }, + ), + ], + orelse: [], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 12367, + end: 12899, + }, + targets: [ + Name( + Name { + node: Node { + start: 12367, + end: 12390, + }, + id: "client_types_data_frame", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 12405, + end: 12899, + }, + func: Attribute( + Attribute { + node: Node { + start: 12393, + end: 12405, + }, + value: Name( + Name { + node: Node { + start: 12393, + end: 12395, + }, + id: "pd", + }, + ), + attr: "DataFrame", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 12415, + end: 12419, + }, + id: "data", + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 12429, + end: 12892, + }, + arg: Some( + "columns", + ), + value: List( + List { + node: Node { + start: 12437, + end: 12892, + }, + elements: [ + Constant( + Constant { + node: Node { + start: 12451, + end: 12457, + }, + value: Str( + "date", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 12471, + end: 12493, + }, + value: Str( + "individual_buy_count", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 12507, + end: 12528, + }, + value: Str( + "corporate_buy_count", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 12542, + end: 12565, + }, + value: Str( + "individual_sell_count", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 12579, + end: 12601, + }, + value: Str( + "corporate_sell_count", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 12615, + end: 12635, + }, + value: Str( + "individual_buy_vol", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 12649, + end: 12668, + }, + value: Str( + "corporate_buy_vol", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 12682, + end: 12703, + }, + value: Str( + "individual_sell_vol", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 12717, + end: 12737, + }, + value: Str( + "corporate_sell_vol", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 12751, + end: 12773, + }, + value: Str( + "individual_buy_value", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 12787, + end: 12808, + }, + value: Str( + "corporate_buy_value", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 12822, + end: 12845, + }, + value: Str( + "individual_sell_value", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 12859, + end: 12881, + }, + value: Str( + "corporate_sell_value", + ), + }, + ), + ], + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ForStatement( + For { + node: Node { + start: 12904, + end: 13210, + }, + target: Name( + Name { + node: Node { + start: 12908, + end: 12909, + }, + id: "i", + }, + ), + iter: List( + List { + node: Node { + start: 12913, + end: 13028, + }, + elements: [ + Constant( + Constant { + node: Node { + start: 12923, + end: 12940, + }, + value: Str( + "individual_buy_", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 12950, + end: 12968, + }, + value: Str( + "individual_sell_", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 12978, + end: 12994, + }, + value: Str( + "corporate_buy_", + ), + }, + ), + Constant( + Constant { + node: Node { + start: 13004, + end: 13021, + }, + value: Str( + "corporate_sell_", + ), + }, + ), + ], + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 13038, + end: 13205, + }, + targets: [ + Subscript( + Subscript { + node: Node { + start: 13038, + end: 13079, + }, + value: Name( + Name { + node: Node { + start: 13038, + end: 13061, + }, + id: "client_types_data_frame", + }, + ), + slice: JoinedStr( + JoinedStr { + node: Node { + start: 13062, + end: 13078, + }, + values: [ + Name( + Name { + node: Node { + start: 13065, + end: 13066, + }, + id: "i", + }, + ), + Constant( + Constant { + node: Node { + start: 13077, + end: 0, + }, + value: Str( + "mean_price", + ), + }, + ), + ], + }, + ), + }, + ), + ], + value: BinOp( + BinOp { + node: Node { + start: 13082, + end: 13205, + }, + op: Div, + left: Call( + Call { + node: Node { + start: 13147, + end: 13154, + }, + func: Attribute( + Attribute { + node: Node { + start: 13140, + end: 13147, + }, + value: Subscript( + Subscript { + node: Node { + start: 13082, + end: 13140, + }, + value: Name( + Name { + node: Node { + start: 13082, + end: 13105, + }, + id: "client_types_data_frame", + }, + ), + slice: JoinedStr( + JoinedStr { + node: Node { + start: 13119, + end: 13130, + }, + values: [ + Name( + Name { + node: Node { + start: 13122, + end: 13123, + }, + id: "i", + }, + ), + Constant( + Constant { + node: Node { + start: 13129, + end: 0, + }, + value: Str( + "value", + ), + }, + ), + ], + }, + ), + }, + ), + attr: "astype", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 13148, + end: 13153, + }, + id: "float", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + right: Call( + Call { + node: Node { + start: 13198, + end: 13205, + }, + func: Attribute( + Attribute { + node: Node { + start: 13191, + end: 13198, + }, + value: Subscript( + Subscript { + node: Node { + start: 13157, + end: 13191, + }, + value: Name( + Name { + node: Node { + start: 13157, + end: 13180, + }, + id: "client_types_data_frame", + }, + ), + slice: JoinedStr( + JoinedStr { + node: Node { + start: 13181, + end: 13190, + }, + values: [ + Name( + Name { + node: Node { + start: 13184, + end: 13185, + }, + id: "i", + }, + ), + Constant( + Constant { + node: Node { + start: 13189, + end: 0, + }, + value: Str( + "vol", + ), + }, + ), + ], + }, + ), + }, + ), + attr: "astype", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 13199, + end: 13204, + }, + id: "float", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + }, + ), + ], + orelse: [], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 13210, + end: 13443, + }, + targets: [ + Subscript( + Subscript { + node: Node { + start: 13210, + end: 13278, + }, + value: Name( + Name { + node: Node { + start: 13210, + end: 13233, + }, + id: "client_types_data_frame", + }, + ), + slice: Constant( + Constant { + node: Node { + start: 13243, + end: 13272, + }, + value: Str( + "individual_ownership_change", + ), + }, + ), + }, + ), + ], + value: BinOp( + BinOp { + node: Node { + start: 13281, + end: 13443, + }, + op: Sub, + left: Call( + Call { + node: Node { + start: 13333, + end: 13354, + }, + func: Attribute( + Attribute { + node: Node { + start: 13326, + end: 13333, + }, + value: Subscript( + Subscript { + node: Node { + start: 13281, + end: 13326, + }, + value: Name( + Name { + node: Node { + start: 13281, + end: 13304, + }, + id: "client_types_data_frame", + }, + ), + slice: Constant( + Constant { + node: Node { + start: 13305, + end: 13325, + }, + value: Str( + "corporate_sell_vol", + ), + }, + ), + }, + ), + attr: "astype", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 13343, + end: 13348, + }, + id: "float", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + right: Call( + Call { + node: Node { + start: 13422, + end: 13443, + }, + func: Attribute( + Attribute { + node: Node { + start: 13415, + end: 13422, + }, + value: Subscript( + Subscript { + node: Node { + start: 13357, + end: 13415, + }, + value: Name( + Name { + node: Node { + start: 13357, + end: 13380, + }, + id: "client_types_data_frame", + }, + ), + slice: Constant( + Constant { + node: Node { + start: 13390, + end: 13409, + }, + value: Str( + "corporate_buy_vol", + ), + }, + ), + }, + ), + attr: "astype", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 13432, + end: 13437, + }, + id: "float", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + }, + ), + Return( + Return { + node: Node { + start: 13448, + end: 13478, + }, + value: Some( + Name( + Name { + node: Node { + start: 13455, + end: 13478, + }, + id: "client_types_data_frame", + }, + ), + ), + }, + ), + ], + decorator_list: [ + Call( + Call { + node: Node { + start: 11850, + end: 11998, + }, + func: Name( + Name { + node: Node { + start: 11850, + end: 11855, + }, + id: "retry", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 11861, + end: 11901, + }, + arg: Some( + "retry", + ), + value: Call( + Call { + node: Node { + start: 11867, + end: 11901, + }, + func: Name( + Name { + node: Node { + start: 11867, + end: 11890, + }, + id: "retry_if_exception_type", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 11891, + end: 11900, + }, + id: "HTTPError", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + Keyword { + node: Node { + start: 11907, + end: 11937, + }, + arg: Some( + "wait", + ), + value: Call( + Call { + node: Node { + start: 11912, + end: 11937, + }, + func: Name( + Name { + node: Node { + start: 11912, + end: 11923, + }, + id: "wait_random", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 11924, + end: 11929, + }, + arg: Some( + "min", + ), + value: Constant( + Constant { + node: Node { + start: 11928, + end: 11929, + }, + value: Int( + "1", + ), + }, + ), + }, + Keyword { + node: Node { + start: 11931, + end: 11936, + }, + arg: Some( + "max", + ), + value: Constant( + Constant { + node: Node { + start: 11935, + end: 11936, + }, + value: Int( + "4", + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + Keyword { + node: Node { + start: 11943, + end: 11995, + }, + arg: Some( + "before_sleep", + ), + value: Call( + Call { + node: Node { + start: 11956, + end: 11995, + }, + func: Name( + Name { + node: Node { + start: 11956, + end: 11972, + }, + id: "before_sleep_log", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 11973, + end: 11979, + }, + id: "logger", + }, + ), + Attribute( + Attribute { + node: Node { + start: 11981, + end: 11994, + }, + value: Name( + Name { + node: Node { + start: 11981, + end: 11988, + }, + id: "logging", + }, + ), + attr: "DEBUG", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + ], + returns: None, + type_comment: None, + }, + ), + FunctionDef( + FunctionDef { + node: Node { + start: 13481, + end: 13980, + }, + name: "get_symbol_id", + args: Arguments { + node: Node { + start: 13499, + end: 13515, + }, + posonlyargs: [], + args: [ + Arg { + node: Node { + start: 13499, + end: 13515, + }, + arg: "symbol_name", + annotation: Some( + Name( + Name { + node: Node { + start: 13512, + end: 13515, + }, + id: "str", + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: [ + AssignStatement( + Assign { + node: Node { + start: 13522, + end: 13586, + }, + targets: [ + Name( + Name { + node: Node { + start: 13522, + end: 13525, + }, + id: "url", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 13565, + end: 13586, + }, + func: Attribute( + Attribute { + node: Node { + start: 13528, + end: 13565, + }, + value: Attribute( + Attribute { + node: Node { + start: 13528, + end: 13558, + }, + value: Name( + Name { + node: Node { + start: 13528, + end: 13540, + }, + id: "tse_settings", + }, + ), + attr: "TSE_SYMBOL_ID_URL", + }, + ), + attr: "format", + }, + ), + args: [ + Call( + Call { + node: Node { + start: 13583, + end: 13585, + }, + func: Attribute( + Attribute { + node: Node { + start: 13566, + end: 13583, + }, + value: Name( + Name { + node: Node { + start: 13566, + end: 13577, + }, + id: "symbol_name", + }, + ), + attr: "strip", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 13591, + end: 13647, + }, + targets: [ + Name( + Name { + node: Node { + start: 13591, + end: 13599, + }, + id: "response", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 13630, + end: 13647, + }, + func: Attribute( + Attribute { + node: Node { + start: 13626, + end: 13630, + }, + value: Call( + Call { + node: Node { + start: 13602, + end: 13626, + }, + func: Name( + Name { + node: Node { + start: 13602, + end: 13624, + }, + id: "requests_retry_session", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + attr: "get", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 13631, + end: 13634, + }, + id: "url", + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 13636, + end: 13646, + }, + arg: Some( + "timeout", + ), + value: Constant( + Constant { + node: Node { + start: 13644, + end: 13646, + }, + value: Int( + "10", + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + ), + TryStatement( + Try { + node: Node { + start: 13652, + end: 13781, + }, + body: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 13690, + end: 13692, + }, + func: Attribute( + Attribute { + node: Node { + start: 13665, + end: 13690, + }, + value: Name( + Name { + node: Node { + start: 13665, + end: 13673, + }, + id: "response", + }, + ), + attr: "raise_for_status", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + ], + handlers: [ + ExceptHandler { + node: Node { + start: 13697, + end: 13781, + }, + typ: Some( + Name( + Name { + node: Node { + start: 13704, + end: 13713, + }, + id: "HTTPError", + }, + ), + ), + name: None, + body: [ + Raise( + Raise { + node: Node { + start: 13723, + end: 13775, + }, + exc: Some( + Call( + Call { + node: Node { + start: 13729, + end: 13775, + }, + func: Name( + Name { + node: Node { + start: 13729, + end: 13738, + }, + id: "Exception", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 13739, + end: 13774, + }, + value: Str( + "Sorry, tse server did not respond", + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + cause: None, + }, + ), + ], + }, + ], + orelse: [], + finalbody: [], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 13781, + end: 13838, + }, + targets: [ + Name( + Name { + node: Node { + start: 13781, + end: 13797, + }, + id: "symbol_full_info", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 13833, + end: 13838, + }, + func: Attribute( + Attribute { + node: Node { + start: 13827, + end: 13833, + }, + value: Subscript( + Subscript { + node: Node { + start: 13824, + end: 13827, + }, + value: Call( + Call { + node: Node { + start: 13819, + end: 13824, + }, + func: Attribute( + Attribute { + node: Node { + start: 13800, + end: 13819, + }, + value: Attribute( + Attribute { + node: Node { + start: 13800, + end: 13813, + }, + value: Name( + Name { + node: Node { + start: 13800, + end: 13808, + }, + id: "response", + }, + ), + attr: "text", + }, + ), + attr: "split", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 13820, + end: 13823, + }, + value: Str( + ";", + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + slice: Constant( + Constant { + node: Node { + start: 13825, + end: 13826, + }, + value: Int( + "0", + ), + }, + ), + }, + ), + attr: "split", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 13834, + end: 13837, + }, + value: Str( + ",", + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + IfStatement( + If { + node: Node { + start: 13846, + end: 13966, + }, + test: Compare( + Compare { + node: Node { + start: 13846, + end: 13912, + }, + left: Call( + Call { + node: Node { + start: 13868, + end: 13881, + }, + func: Attribute( + Attribute { + node: Node { + start: 13846, + end: 13868, + }, + value: Name( + Name { + node: Node { + start: 13846, + end: 13853, + }, + id: "persian", + }, + ), + attr: "replace_arabic", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 13869, + end: 13880, + }, + id: "symbol_name", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ops: [ + Eq, + ], + comparators: [ + Call( + Call { + node: Node { + start: 13910, + end: 13912, + }, + func: Attribute( + Attribute { + node: Node { + start: 13904, + end: 13910, + }, + value: Subscript( + Subscript { + node: Node { + start: 13885, + end: 13904, + }, + value: Name( + Name { + node: Node { + start: 13885, + end: 13901, + }, + id: "symbol_full_info", + }, + ), + slice: Constant( + Constant { + node: Node { + start: 13902, + end: 13903, + }, + value: Int( + "0", + ), + }, + ), + }, + ), + attr: "strip", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ], + }, + ), + body: [ + Return( + Return { + node: Node { + start: 13922, + end: 13948, + }, + value: Some( + Subscript( + Subscript { + node: Node { + start: 13929, + end: 13948, + }, + value: Name( + Name { + node: Node { + start: 13929, + end: 13945, + }, + id: "symbol_full_info", + }, + ), + slice: Constant( + Constant { + node: Node { + start: 13946, + end: 13947, + }, + value: Int( + "2", + ), + }, + ), + }, + ), + ), + }, + ), + ], + orelse: [], + }, + ), + Return( + Return { + node: Node { + start: 13966, + end: 13977, + }, + value: Some( + Constant( + Constant { + node: Node { + start: 13973, + end: 13977, + }, + value: None, + }, + ), + ), + }, + ), + ], + decorator_list: [], + returns: None, + type_comment: None, + }, + ), + FunctionDef( + FunctionDef { + node: Node { + start: 13980, + end: 15273, + }, + name: "get_symbol_info", + args: Arguments { + node: Node { + start: 14000, + end: 14016, + }, + posonlyargs: [], + args: [ + Arg { + node: Node { + start: 14000, + end: 14016, + }, + arg: "symbol_name", + annotation: Some( + Name( + Name { + node: Node { + start: 14013, + end: 14016, + }, + id: "str", + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: [ + AssignStatement( + Assign { + node: Node { + start: 14023, + end: 14087, + }, + targets: [ + Name( + Name { + node: Node { + start: 14023, + end: 14026, + }, + id: "url", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 14066, + end: 14087, + }, + func: Attribute( + Attribute { + node: Node { + start: 14029, + end: 14066, + }, + value: Attribute( + Attribute { + node: Node { + start: 14029, + end: 14059, + }, + value: Name( + Name { + node: Node { + start: 14029, + end: 14041, + }, + id: "tse_settings", + }, + ), + attr: "TSE_SYMBOL_ID_URL", + }, + ), + attr: "format", + }, + ), + args: [ + Call( + Call { + node: Node { + start: 14084, + end: 14086, + }, + func: Attribute( + Attribute { + node: Node { + start: 14067, + end: 14084, + }, + value: Name( + Name { + node: Node { + start: 14067, + end: 14078, + }, + id: "symbol_name", + }, + ), + attr: "strip", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 14092, + end: 14148, + }, + targets: [ + Name( + Name { + node: Node { + start: 14092, + end: 14100, + }, + id: "response", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 14131, + end: 14148, + }, + func: Attribute( + Attribute { + node: Node { + start: 14127, + end: 14131, + }, + value: Call( + Call { + node: Node { + start: 14103, + end: 14127, + }, + func: Name( + Name { + node: Node { + start: 14103, + end: 14125, + }, + id: "requests_retry_session", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + attr: "get", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 14132, + end: 14135, + }, + id: "url", + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 14137, + end: 14147, + }, + arg: Some( + "timeout", + ), + value: Constant( + Constant { + node: Node { + start: 14145, + end: 14147, + }, + value: Int( + "10", + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + ), + TryStatement( + Try { + node: Node { + start: 14153, + end: 14298, + }, + body: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 14191, + end: 14193, + }, + func: Attribute( + Attribute { + node: Node { + start: 14166, + end: 14191, + }, + value: Name( + Name { + node: Node { + start: 14166, + end: 14174, + }, + id: "response", + }, + ), + attr: "raise_for_status", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + ], + handlers: [ + ExceptHandler { + node: Node { + start: 14198, + end: 14298, + }, + typ: Some( + Name( + Name { + node: Node { + start: 14205, + end: 14214, + }, + id: "HTTPError", + }, + ), + ), + name: None, + body: [ + Raise( + Raise { + node: Node { + start: 14224, + end: 14292, + }, + exc: Some( + Call( + Call { + node: Node { + start: 14230, + end: 14292, + }, + func: Name( + Name { + node: Node { + start: 14230, + end: 14239, + }, + id: "Exception", + }, + ), + args: [ + JoinedStr( + JoinedStr { + node: Node { + start: 14240, + end: 14291, + }, + values: [ + Name( + Name { + node: Node { + start: 14243, + end: 14254, + }, + id: "symbol_name", + }, + ), + Constant( + Constant { + node: Node { + start: 14290, + end: 0, + }, + value: Str( + ": Sorry, tse server did not respond", + ), + }, + ), + ], + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + cause: None, + }, + ), + ], + }, + ], + orelse: [], + finalbody: [], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 14298, + end: 14332, + }, + targets: [ + Name( + Name { + node: Node { + start: 14298, + end: 14305, + }, + id: "symbols", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 14327, + end: 14332, + }, + func: Attribute( + Attribute { + node: Node { + start: 14308, + end: 14327, + }, + value: Attribute( + Attribute { + node: Node { + start: 14308, + end: 14321, + }, + value: Name( + Name { + node: Node { + start: 14308, + end: 14316, + }, + id: "response", + }, + ), + attr: "text", + }, + ), + attr: "split", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 14328, + end: 14331, + }, + value: Str( + ";", + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 14337, + end: 14467, + }, + targets: [ + Name( + Name { + node: Node { + start: 14337, + end: 14350, + }, + id: "market_symbol", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 14353, + end: 14467, + }, + func: Name( + Name { + node: Node { + start: 14353, + end: 14365, + }, + id: "MarketSymbol", + }, + ), + args: [], + keywords: [ + Keyword { + node: Node { + start: 14375, + end: 14384, + }, + arg: Some( + "code", + ), + value: Constant( + Constant { + node: Node { + start: 14380, + end: 14384, + }, + value: None, + }, + ), + }, + Keyword { + node: Node { + start: 14394, + end: 14405, + }, + arg: Some( + "symbol", + ), + value: Constant( + Constant { + node: Node { + start: 14401, + end: 14405, + }, + value: None, + }, + ), + }, + Keyword { + node: Node { + start: 14415, + end: 14425, + }, + arg: Some( + "index", + ), + value: Constant( + Constant { + node: Node { + start: 14421, + end: 14425, + }, + value: None, + }, + ), + }, + Keyword { + node: Node { + start: 14435, + end: 14444, + }, + arg: Some( + "name", + ), + value: Constant( + Constant { + node: Node { + start: 14440, + end: 14444, + }, + value: None, + }, + ), + }, + Keyword { + node: Node { + start: 14454, + end: 14460, + }, + arg: Some( + "old", + ), + value: List( + List { + node: Node { + start: 14458, + end: 14460, + }, + elements: [], + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ForStatement( + For { + node: Node { + start: 14472, + end: 15273, + }, + target: Name( + Name { + node: Node { + start: 14476, + end: 14492, + }, + id: "symbol_full_info", + }, + ), + iter: Name( + Name { + node: Node { + start: 14496, + end: 14503, + }, + id: "symbols", + }, + ), + body: [ + IfStatement( + If { + node: Node { + start: 14516, + end: 14577, + }, + test: Compare( + Compare { + node: Node { + start: 14516, + end: 14546, + }, + left: Call( + Call { + node: Node { + start: 14538, + end: 14540, + }, + func: Attribute( + Attribute { + node: Node { + start: 14516, + end: 14538, + }, + value: Name( + Name { + node: Node { + start: 14516, + end: 14532, + }, + id: "symbol_full_info", + }, + ), + attr: "strip", + }, + ), + args: [], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ops: [ + Eq, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 14544, + end: 14546, + }, + value: Str( + "", + ), + }, + ), + ], + }, + ), + body: [ + Continue( + Continue { + node: Node { + start: 14560, + end: 14568, + }, + }, + ), + ], + orelse: [], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 14577, + end: 14623, + }, + targets: [ + Name( + Name { + node: Node { + start: 14577, + end: 14593, + }, + id: "symbol_full_info", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 14618, + end: 14623, + }, + func: Attribute( + Attribute { + node: Node { + start: 14596, + end: 14618, + }, + value: Name( + Name { + node: Node { + start: 14596, + end: 14612, + }, + id: "symbol_full_info", + }, + ), + attr: "split", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 14619, + end: 14622, + }, + value: Str( + ",", + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + IfStatement( + If { + node: Node { + start: 14635, + end: 15273, + }, + test: Compare( + Compare { + node: Node { + start: 14635, + end: 14693, + }, + left: Call( + Call { + node: Node { + start: 14657, + end: 14678, + }, + func: Attribute( + Attribute { + node: Node { + start: 14635, + end: 14657, + }, + value: Name( + Name { + node: Node { + start: 14635, + end: 14642, + }, + id: "persian", + }, + ), + attr: "replace_arabic", + }, + ), + args: [ + Subscript( + Subscript { + node: Node { + start: 14658, + end: 14677, + }, + value: Name( + Name { + node: Node { + start: 14658, + end: 14674, + }, + id: "symbol_full_info", + }, + ), + slice: Constant( + Constant { + node: Node { + start: 14675, + end: 14676, + }, + value: Int( + "0", + ), + }, + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ops: [ + Eq, + ], + comparators: [ + Name( + Name { + node: Node { + start: 14682, + end: 14693, + }, + id: "symbol_name", + }, + ), + ], + }, + ), + body: [ + IfStatement( + If { + node: Node { + start: 14747, + end: 15195, + }, + test: Compare( + Compare { + node: Node { + start: 14747, + end: 14773, + }, + left: Subscript( + Subscript { + node: Node { + start: 14747, + end: 14766, + }, + value: Name( + Name { + node: Node { + start: 14747, + end: 14763, + }, + id: "symbol_full_info", + }, + ), + slice: Constant( + Constant { + node: Node { + start: 14764, + end: 14765, + }, + value: Int( + "7", + ), + }, + ), + }, + ), + ops: [ + Eq, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 14770, + end: 14773, + }, + value: Str( + "1", + ), + }, + ), + ], + }, + ), + body: [ + AssignStatement( + Assign { + node: Node { + start: 14791, + end: 14895, + }, + targets: [ + Attribute( + Attribute { + node: Node { + start: 14791, + end: 14811, + }, + value: Name( + Name { + node: Node { + start: 14791, + end: 14804, + }, + id: "market_symbol", + }, + ), + attr: "symbol", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 14836, + end: 14895, + }, + func: Attribute( + Attribute { + node: Node { + start: 14814, + end: 14836, + }, + value: Name( + Name { + node: Node { + start: 14814, + end: 14821, + }, + id: "persian", + }, + ), + attr: "replace_arabic", + }, + ), + args: [ + Subscript( + Subscript { + node: Node { + start: 14858, + end: 14877, + }, + value: Name( + Name { + node: Node { + start: 14858, + end: 14874, + }, + id: "symbol_full_info", + }, + ), + slice: Constant( + Constant { + node: Node { + start: 14875, + end: 14876, + }, + value: Int( + "0", + ), + }, + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 14912, + end: 15014, + }, + targets: [ + Attribute( + Attribute { + node: Node { + start: 14912, + end: 14930, + }, + value: Name( + Name { + node: Node { + start: 14912, + end: 14925, + }, + id: "market_symbol", + }, + ), + attr: "name", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 14955, + end: 15014, + }, + func: Attribute( + Attribute { + node: Node { + start: 14933, + end: 14955, + }, + value: Name( + Name { + node: Node { + start: 14933, + end: 14940, + }, + id: "persian", + }, + ), + attr: "replace_arabic", + }, + ), + args: [ + Subscript( + Subscript { + node: Node { + start: 14977, + end: 14996, + }, + value: Name( + Name { + node: Node { + start: 14977, + end: 14993, + }, + id: "symbol_full_info", + }, + ), + slice: Constant( + Constant { + node: Node { + start: 14994, + end: 14995, + }, + value: Int( + "1", + ), + }, + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + AssignStatement( + Assign { + node: Node { + start: 15031, + end: 15072, + }, + targets: [ + Attribute( + Attribute { + node: Node { + start: 15031, + end: 15050, + }, + value: Name( + Name { + node: Node { + start: 15031, + end: 15044, + }, + id: "market_symbol", + }, + ), + attr: "index", + }, + ), + ], + value: Subscript( + Subscript { + node: Node { + start: 15053, + end: 15072, + }, + value: Name( + Name { + node: Node { + start: 15053, + end: 15069, + }, + id: "symbol_full_info", + }, + ), + slice: Constant( + Constant { + node: Node { + start: 15070, + end: 15071, + }, + value: Int( + "2", + ), + }, + ), + }, + ), + }, + ), + ], + orelse: [], + }, + ), + IfStatement( + If { + node: Node { + start: 15198, + end: 15251, + }, + test: Compare( + Compare { + node: Node { + start: 15198, + end: 15225, + }, + left: Attribute( + Attribute { + node: Node { + start: 15198, + end: 15217, + }, + value: Name( + Name { + node: Node { + start: 15198, + end: 15211, + }, + id: "market_symbol", + }, + ), + attr: "index", + }, + ), + ops: [ + Is, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 15221, + end: 15225, + }, + value: None, + }, + ), + ], + }, + ), + body: [ + Return( + Return { + node: Node { + start: 15235, + end: 15246, + }, + value: Some( + Constant( + Constant { + node: Node { + start: 15242, + end: 15246, + }, + value: None, + }, + ), + ), + }, + ), + ], + orelse: [], + }, + ), + Return( + Return { + node: Node { + start: 15251, + end: 15271, + }, + value: Some( + Name( + Name { + node: Node { + start: 15258, + end: 15271, + }, + id: "market_symbol", + }, + ), + ), + }, + ), + ], + orelse: [], + }, + ), + ], + orelse: [], + }, + ), + ], + decorator_list: [], + returns: None, + type_comment: None, + }, + ), + ], + }, + ), + ], + decorator_list: [], + returns: None, + type_comment: None, + }, + ), + ], + }, + ), + ], + decorator_list: [], + returns: Some( + Subscript( + Subscript { + node: Node { + start: 8546, + end: 8569, + }, + value: Name( + Name { + node: Node { + start: 8546, + end: 8550, + }, + id: "Dict", + }, + ), + slice: Tuple( + Tuple { + node: Node { + start: 8551, + end: 8569, + }, + elements: [ + Name( + Name { + node: Node { + start: 8551, + end: 8554, + }, + id: "str", + }, + ), + Attribute( + Attribute { + node: Node { + start: 8556, + end: 8568, + }, + value: Name( + Name { + node: Node { + start: 8556, + end: 8558, + }, + id: "pd", + }, + ), + attr: "DataFrame", + }, + ), + ], + }, + ), + }, + ), + ), + type_comment: None, + }, + ), + ], + orelse: [], + }, + ), + ], + orelse: [], + }, + ), + ], + }, + ), + ], + decorator_list: [], + returns: Some( + Subscript( + Subscript { + node: Node { + start: 1637, + end: 1660, + }, + value: Name( + Name { + node: Node { + start: 1637, + end: 1641, + }, + id: "Dict", + }, + ), + slice: Tuple( + Tuple { + node: Node { + start: 1642, + end: 1660, + }, + elements: [ + Name( + Name { + node: Node { + start: 1642, + end: 1645, + }, + id: "str", + }, + ), + Attribute( + Attribute { + node: Node { + start: 1647, + end: 1659, + }, + value: Name( + Name { + node: Node { + start: 1647, + end: 1649, + }, + id: "pd", + }, + ), + attr: "DataFrame", + }, + ), + ], + }, + ), + }, + ), + ), + type_comment: None, + }, + ), + ], + decorator_list: [], + returns: None, + type_comment: None, + }, + ), + ], + decorator_list: [], + returns: None, + type_comment: None, + }, + ), + ], + decorator_list: [], + returns: None, + type_comment: None, + }, + ), + ], +} diff --git a/parser/test_data/inputs/input_program.py b/parser/test_data/inputs/input_program.py new file mode 100644 index 00000000..18499c6b --- /dev/null +++ b/parser/test_data/inputs/input_program.py @@ -0,0 +1,467 @@ +def _handle_ticker_index(symbol): + ticker_index = symbols_data.get_ticker_index(symbol) + + if ticker_index is None: + market_symbol = get_symbol_info(symbol) + if market_symbol is not None: + symbols_data.append_symbol_to_file(market_symbol) + ticker_index = market_symbol.index + return ticker_index + + +def _extract_ticker_client_types_data(ticker_index: str) -> List: + url = TSE_CLIENT_TYPE_DATA_URL.format(ticker_index) + with requests_retry_session() as session: + response = session.get(url, timeout=5) + data = response.text.split(";") + data = [row.split(",") for row in data] + return data + + +def _create_financial_index_from_text_response(data): + data = pd.DataFrame(re.split(r"\;", data)) + columns = ["date", "high", "low", "open", "close", "volume", "__"] + data[columns] = data[0].str.split(",", expand=True) + return data.drop(columns=["__", 0]) + + +def _adjust_data_frame(df, include_jdate): + df.date = pd.to_datetime(df.date, format="%Y%m%d") + if include_jdate: + df["jdate"] = "" + df.jdate = df.date.apply( + lambda gregorian: jdatetime.date.fromgregorian(date=gregorian) + ) + + +def _adjust_data_frame_for_fIndex(df, include_jdate): + df["date"] = pd.to_datetime(df["date"], format="%Y%m%d") + if include_jdate: + df["jdate"] = df.date.apply( + lambda gregorian: jdatetime.date.fromgregorian(date=gregorian) + ) + + +def download( + symbols: Union[List, str], + write_to_csv: bool = False, + include_jdate: bool = False, + base_path: str = config.DATA_BASE_PATH, + adjust: bool = False, +) -> Dict[str, pd.DataFrame]: + if symbols == "all": + symbols = symbols_data.all_symbols() + elif isinstance(symbols, str): + symbols = [symbols] + + df_list = {} + future_to_symbol = {} + with futures.ThreadPoolExecutor(max_workers=10) as executor: + session = requests_retry_session() + for symbol in symbols: + if ( + symbol.isnumeric() + and symbols_data.get_ticker_index(symbol) is None + ): + ticker_indexes = [symbol] + else: + ticker_index = _handle_ticker_index(symbol) + if ticker_index is None: + raise Exception(f"Cannot find symbol: {symbol}") + ticker_indexes = symbols_data.get_ticker_old_index(symbol) + ticker_indexes.insert(0, ticker_index) + + for index in ticker_indexes: + future = executor.submit( + download_ticker_daily_record, index, session + ) + + future_to_symbol[future] = symbol + + for future in futures.as_completed(future_to_symbol): + symbol = future_to_symbol[future] + try: + df: pd.DataFrame = future.result() + except pd.errors.EmptyDataError as ex: + logger.error( + f"Cannot read daily trade records for symbol: {symbol}", + extra={"Error": ex}, + ) + continue + df = df.iloc[::-1].reset_index(drop=True) + df = df.rename(columns=translations.HISTORY_FIELD_MAPPINGS) + df = df.drop(columns=["", ""]) + _adjust_data_frame(df, include_jdate) + + if symbol in df_list: + df_list[symbol] = ( + pd.concat( + [df_list[symbol], df], ignore_index=True, sort=False + ) + .sort_values("date") + .reset_index(drop=True) + ) + else: + df_list[symbol] = df + + if adjust: + df_list[symbol] = adjust_price(df_list[symbol]) + + if write_to_csv: + Path(base_path).mkdir(parents=True, exist_ok=True) + if adjust: + df_list[symbol].to_csv( + f"{base_path}/{symbol}-ت.csv", index=False + ) + else: + df_list[symbol].to_csv( + f"{base_path}/{symbol}.csv", index=False + ) + + if len(df_list) != len(symbols): + print("Warning, download did not complete, re-run the code") + session.close() + return df_list + + +def adjust_price(df: pd.DataFrame) -> pd.DataFrame: + """ + Adjust historical records of stock + + There is a capital increase/profit sharing, + if today "Final Close Price" is not equal to next day + "Yesterday Final Close Price" by using this ratio, + performance adjustment of stocks is achieved + + Parameters + ---------- + df : pd.DataFrame + DataFrame with historical records. + + Returns + ------- + pd.DataFrame + DataFrame with adjusted historical records. + + Notes + ----- + DataFrame can not be empty or else it makes runtime error + Type of DataFrame must be RangeIndex to make proper range of records + that need to be modified + + diff: list + list of indexs of the day after capital increase/profit sharing + ratio_list: List + List of ratios to adjust historical data of stock + ratio: Float + ratio = df.loc[i].adjClose / df.loc[i+1].yesterday + + Description + ----------- + # Note: adjustment does not include Tenth and twentieth days + df.index = range(0,101,1) + # step is 1 + step = df.index.step + diff = [10,20] + ratio_list = [0.5, 0.8] + df.loc[0:10-step, [open,...]] * ratio[0] + df.loc[10:20-step, [open,...]] * ratio[1] + """ + if df.empty or not isinstance(df.index, pd.core.indexes.range.RangeIndex): + return df + + new_df = df.copy() + step = new_df.index.step + diff = list(new_df.index[new_df.shift(1).adjClose != new_df.yesterday]) + if len(diff) > 0: + diff.pop(0) + ratio = 1 + ratio_list = [] + for i in diff[::-1]: + ratio *= ( + new_df.loc[i, "yesterday"] / new_df.shift(1).loc[i, "adjClose"] + ) + ratio_list.insert(0, ratio) + for i, k in enumerate(diff): + if i == 0: + start = new_df.index.start + else: + start = diff[i - 1] + end = diff[i] - step + new_df.loc[ + start:end, + [ + "open", + "high", + "low", + "close", + "adjClose", + "yesterday", + ], + ] = round( + new_df.loc[ + start:end, + [ + "open", + "high", + "low", + "close", + "adjClose", + "yesterday", + ], + ] + * ratio_list[i] + ) + + return new_df + + +@retry( + retry=retry_if_exception_type(HTTPError), + wait=wait_random(min=1, max=4), + before_sleep=before_sleep_log(logger, logging.DEBUG), +) +def download_ticker_daily_record(ticker_index: str, session: Session): + url = tse_settings.TSE_TICKER_EXPORT_DATA_ADDRESS.format(ticker_index) + response = session.get(url, timeout=10) + if 400 <= response.status_code < 500: + logger.error( + f"Cannot read daily trade records from the url: {url}", + extra={"response": response.text, "code": response.status_code}, + ) + + response.raise_for_status() + + data = StringIO(response.text) + return pd.read_csv(data) + + +@retry( + retry=retry_if_exception_type(HTTPError), + wait=wait_random(min=1, max=4), + before_sleep=before_sleep_log(logger, logging.DEBUG), +) +def download_fIndex_record(fIndex: str, session: Session): + url = tse_settings.TSE_FINANCIAL_INDEX_EXPORT_DATA_ADDRESS.format(fIndex) + response = session.get(url, timeout=10) + if 400 <= response.status_code < 500: + logger.error( + f"Cannot read daily trade records from the url: {url}", + extra={"response": response.text, "code": response.status_code}, + ) + + response.raise_for_status() + data = response.text + if not data or ";" not in data or "," not in data: + raise ValueError( + f"""Invalid response from the url: {url}. + \nExpected valid financial index data.""" + ) + df = _create_financial_index_from_text_response(data) + return df + + +def download_financial_indexes( + symbols: Union[List, str], + write_to_csv: bool = False, + include_jdate: bool = False, + base_path: str = config.FINANCIAL_INDEX_BASE_PATH, +) -> Dict[str, pd.DataFrame]: + if symbols == "all": + symbols = symbols_data.all_financial_index() + elif isinstance(symbols, str): + symbols = [symbols] + + df_list = {} + future_to_symbol = {} + with futures.ThreadPoolExecutor(max_workers=10) as executor: + session = requests_retry_session() + for symbol in symbols: + symbol = persian.replace_persian(symbol) + if ( + symbol.isnumeric() + and symbols_data.get_financial_index(symbol) is None + ): + financial_index = symbol + else: + financial_index = symbols_data.get_financial_index(symbol) + if financial_index is None: + raise Exception(f"Cannot find financial index: {symbol}") + + future = executor.submit( + download_fIndex_record, financial_index, session + ) + + future_to_symbol[future] = symbol + + for future in futures.as_completed(future_to_symbol): + symbol = future_to_symbol[future] + try: + df: pd.DataFrame = future.result() + except pd.errors.EmptyDataError as ex: + logger.error( + f"Cannot read daily trade records for symbol: {symbol}", + extra={"Error": ex}, + ) + continue + _adjust_data_frame_for_fIndex(df, include_jdate) + df_list[symbol] = df + + if write_to_csv: + Path(base_path).mkdir(parents=True, exist_ok=True) + df_list[symbol].to_csv( + f"{base_path}/{symbol}.csv", index=False + ) + + if len(df_list) != len(symbols): + print("Warning, download did not complete, re-run the code") + session.close() + return df_list + + +def download_client_types_records( + symbols: Union[List, str], + write_to_csv: bool = False, + include_jdate: bool = False, + base_path: str = config.CLIENT_TYPES_DATA_BASE_PATH, +): + if symbols == "all": + symbols = symbols_data.all_symbols() + elif isinstance(symbols, str): + symbols = [symbols] + + df_list = {} + future_to_symbol = {} + with futures.ThreadPoolExecutor(max_workers=10) as executor: + for symbol in symbols: + ticker_index = _handle_ticker_index(symbol) + if ticker_index is None: + raise Exception(f"Cannot find symbol: {symbol}") + future = executor.submit( + download_ticker_client_types_record, ticker_index + ) + future_to_symbol[future] = symbol + for future in futures.as_completed(future_to_symbol): + symbol = future_to_symbol[future] + df: pd.DataFrame = future.result() + # ignore failures + if df is None: + continue + _adjust_data_frame(df, include_jdate) + df_list[symbol] = df + if write_to_csv: + Path(base_path).mkdir(parents=True, exist_ok=True) + df.to_csv(f"{base_path}/{symbol}.csv") + + if len(df_list) != len(symbols): + print( + """could not download client types for all the symbols make + sure you have what you need or re-run the function""" + ) + return df_list + + +@retry( + retry=retry_if_exception_type(HTTPError), + wait=wait_random(min=1, max=4), + before_sleep=before_sleep_log(logger, logging.DEBUG), +) +def download_ticker_client_types_record(ticker_index: Optional[str]): + data = _extract_ticker_client_types_data(ticker_index) + if len(data) == 1: + logger.warning( + f"""Cannot create client types data for ticker{ticker_index} + from data: {data}""", + extra={"ticker_index": ticker_index}, + ) + return None + client_types_data_frame = pd.DataFrame( + data, + columns=[ + "date", + "individual_buy_count", + "corporate_buy_count", + "individual_sell_count", + "corporate_sell_count", + "individual_buy_vol", + "corporate_buy_vol", + "individual_sell_vol", + "corporate_sell_vol", + "individual_buy_value", + "corporate_buy_value", + "individual_sell_value", + "corporate_sell_value", + ], + ) + for i in [ + "individual_buy_", + "individual_sell_", + "corporate_buy_", + "corporate_sell_", + ]: + client_types_data_frame[f"{i}mean_price"] = client_types_data_frame[ + f"{i}value" + ].astype(float) / client_types_data_frame[f"{i}vol"].astype(float) + client_types_data_frame[ + "individual_ownership_change" + ] = client_types_data_frame["corporate_sell_vol"].astype( + float + ) - client_types_data_frame[ + "corporate_buy_vol" + ].astype( + float + ) + return client_types_data_frame + + +def get_symbol_id(symbol_name: str): + url = tse_settings.TSE_SYMBOL_ID_URL.format(symbol_name.strip()) + response = requests_retry_session().get(url, timeout=10) + try: + response.raise_for_status() + except HTTPError: + raise Exception("Sorry, tse server did not respond") + + symbol_full_info = response.text.split(";")[0].split(",") + if persian.replace_arabic(symbol_name) == symbol_full_info[0].strip(): + return symbol_full_info[2] # symbol id + return None + + +def get_symbol_info(symbol_name: str): + url = tse_settings.TSE_SYMBOL_ID_URL.format(symbol_name.strip()) + response = requests_retry_session().get(url, timeout=10) + try: + response.raise_for_status() + except HTTPError: + raise Exception(f"{symbol_name}: Sorry, tse server did not respond") + + symbols = response.text.split(";") + market_symbol = MarketSymbol( + code=None, + symbol=None, + index=None, + name=None, + old=[], + ) + for symbol_full_info in symbols: + if symbol_full_info.strip() == "": + continue + symbol_full_info = symbol_full_info.split(",") + if persian.replace_arabic(symbol_full_info[0]) == symbol_name: + # if symbol id is active + if symbol_full_info[7] == "1": + market_symbol.symbol = persian.replace_arabic( + symbol_full_info[0] + ) + market_symbol.name = persian.replace_arabic( + symbol_full_info[1] + ) + market_symbol.index = symbol_full_info[2] # active symbol id + else: + market_symbol.old.append(symbol_full_info[2]) # old symbol id + + if market_symbol.index is None: + return None + return market_symbol +