Best Python code snippet using autotest_python
makeFinancialStatements.py
Source:makeFinancialStatements.py
...36 while nextDate <= endDate:37 quarterlyDates.append(utils.dateToDateString(nextDate))38 nextDate = utils.getEndOfMonth(nextDate + relativedelta(months=3))39 return quarterlyDates40def mergeIncomeStatements(a: IncomeStatement, b: IncomeStatement) -> IncomeStatement:41 return IncomeStatement(42 totalRevenue=a.totalRevenue or b.totalRevenue,43 netIncome=a.netIncome or b.netIncome,44 incomeBeforeTax=a.incomeBeforeTax or b.incomeBeforeTax,45 interestIncome=a.interestIncome or b.interestIncome,46 interestExpense=a.interestExpense or b.interestExpense,47 estimate=a.estimate or b.estimate,48 source=a.source or b.source,49 )50def mergeBalanceSheets(a: BalanceSheet, b: BalanceSheet) -> BalanceSheet:51 return BalanceSheet(52 assets=a.assets or b.assets,53 currentAssets=a.currentAssets or b.currentAssets,54 liabilities=a.liabilities or b.liabilities,55 currentLiabilities=a.currentLiabilities or b.currentLiabilities,56 retainedEarnings=a.retainedEarnings or b.retainedEarnings,57 cash=a.cash or b.cash,58 estimate=a.estimate or b.estimate,59 source=a.source or b.source,60 )61def mergeCashFlowStatements(62 a: CashFlowStatement, b: CashFlowStatement63) -> CashFlowStatement:64 return CashFlowStatement(65 dividendsPaid=a.dividendsPaid or b.dividendsPaid,66 cashFromOperations=a.cashFromOperations or b.cashFromOperations,67 capex=a.capex or b.capex,68 estimate=a.estimate or b.estimate,69 source=a.source or b.source,70 )71def getMergedStatements(72 dates, latestStatements, existingStatements, statementType, factory, merger,73):74 mergedStatements = {}75 for date in dates:76 latestStatement = (77 date in latestStatements and latestStatements[date] or factory78 ) # could be quarterly or yearly79 if statementType == "yearly":80 # reduce the values to 1/4 to represent their quarterly values81 for key in latestStatement:82 if isinstance(latestStatement[key], float):83 latestStatement[key] = latestStatement[key] / 484 else:85 latestStatement[key] = 0.0086 existingStatement = (87 date in existingStatements[statementType]88 and not existingStatements[statementType][date].estimate89 and existingStatements[statementType][date]90 ) or factory # is always quarterly, don't use estimates (we want to extrapolate new values instead)91 mergedStatement = merger(latestStatement, existingStatement)92 mergedStatements[date] = mergedStatement93 return mergedStatements94def isIncomeStatementEmptyOrInvalid(incomeStatement: IncomeStatement) -> bool:95 if incomeStatement == IncomeStatement():96 return True97 return False98def isBalanceSheetEmptyOrInvalid(balanceSheet: BalanceSheet) -> bool:99 if balanceSheet == BalanceSheet():100 return True101 return False102def isCashFlowStatementEmptyOrInvalid(cashFlowStatement: CashFlowStatement) -> bool:103 if cashFlowStatement == CashFlowStatement():104 return True105 return False106def makeFinancialStatements(107 existingStatements: FinancialStatements, latestStatements: AllFinancialStatements,108) -> FinancialStatements:109 # Get a list of dates from the quarterly statements110 quarterlyDates = getQuarterlyDates(existingStatements, latestStatements)111 # if empty return None112 if not quarterlyDates:113 return None114 # Merge statements for date115 mergedQuarterlyIncomeStatements = getMergedStatements(116 quarterlyDates,117 latestStatements.incomeStatements.quarterly,118 existingStatements,119 "incomeStatements",120 IncomeStatement(),121 mergeIncomeStatements,122 )123 # print("QUARTERLY", mergedQuarterlyIncomeStatements)124 mergedYearlyIncomeStatements = getMergedStatements(125 quarterlyDates,126 latestStatements.incomeStatements.yearly,127 existingStatements,128 "incomeStatements",129 IncomeStatement(),130 mergeIncomeStatements,131 )132 # print("YEARLY", mergedQuarterlyIncomeStatements)133 mergedQuarterlyBalanceSheets = getMergedStatements(134 quarterlyDates,135 latestStatements.balanceSheets.quarterly,136 existingStatements,137 "balanceSheets",138 BalanceSheet(),139 mergeBalanceSheets,140 )141 mergedYearlyBalanceSheets = getMergedStatements(142 quarterlyDates,143 latestStatements.balanceSheets.yearly,144 existingStatements,145 "balanceSheets",146 BalanceSheet(),147 mergeBalanceSheets,148 )149 mergedQuarterlyCashFlowStatements = getMergedStatements(150 quarterlyDates,151 latestStatements.cashFlowStatements.quarterly,152 existingStatements,153 "cashFlowStatements",154 CashFlowStatement(),155 mergeCashFlowStatements,156 )157 mergedYearlyCashFlowStatements = getMergedStatements(158 quarterlyDates,159 latestStatements.cashFlowStatements.yearly,160 existingStatements,161 "cashFlowStatements",162 CashFlowStatement(),163 mergeCashFlowStatements,164 )165 incomeStatements = {}166 for date in mergedQuarterlyIncomeStatements:167 quarterlyStatement = mergedQuarterlyIncomeStatements[date]168 # if we have a quarterly statement add it169 if not isIncomeStatementEmptyOrInvalid(quarterlyStatement):170 quarterlyStatement.source = "actual"171 incomeStatements[date] = quarterlyStatement172 # if the quarterlyStatement is empty, we need to try and get the values from the yearlyStatement on this date173 else:174 yearlyStatement = mergedYearlyIncomeStatements[date]175 if yearlyStatement and not isIncomeStatementEmptyOrInvalid(yearlyStatement):176 totalRevenue = yearlyStatement.totalRevenue / 4177 netIncome = yearlyStatement.netIncome / 4178 incomeBeforeTax = yearlyStatement.incomeBeforeTax / 4179 interestIncome = yearlyStatement.interestIncome / 4180 interestExpense = yearlyStatement.interestExpense / 4181 estimatedStatement = IncomeStatement(182 totalRevenue=totalRevenue,183 netIncome=netIncome,184 incomeBeforeTax=incomeBeforeTax,185 interestIncome=interestIncome,186 interestExpense=interestExpense,187 source="yearly",188 estimate=False,189 )190 incomeStatements[date] = estimatedStatement191 else:192 incomeStatements[date] = IncomeStatement()193 # now that we've populated our statements as far as possible194 # we need to try extrapolate empty dates using before and after quarterlyStatements195 for i, date in enumerate(quarterlyDates):196 quarterlyStatement = (197 date in incomeStatements and incomeStatements[date] or IncomeStatement()198 )199 if quarterlyStatement == IncomeStatement():200 # TODO: should we limit it to the last few years?201 totalRevenue = utils.getTrendEstimateForDate(202 incomeStatements, "totalRevenue", IncomeStatement(), date203 )204 netIncome = utils.getTrendEstimateForDate(205 incomeStatements, "netIncome", IncomeStatement(), date206 )207 incomeBeforeTax = utils.getTrendEstimateForDate(208 incomeStatements, "incomeBeforeTax", IncomeStatement(), date209 )210 interestExpense = utils.getTrendEstimateForDate(211 incomeStatements, "interestExpense", IncomeStatement(), date212 )213 interestIncome = utils.getTrendEstimateForDate(214 incomeStatements, "interestIncome", IncomeStatement(), date215 )216 incomeStatement = IncomeStatement(217 totalRevenue=totalRevenue,218 netIncome=netIncome,219 incomeBeforeTax=incomeBeforeTax,220 interestExpense=interestExpense,221 interestIncome=interestIncome,222 source="trend",223 estimate=True,224 )225 incomeStatements[date] = incomeStatement226 balanceSheets = {}227 for date in mergedQuarterlyBalanceSheets:228 quarterlyStatement = mergedQuarterlyBalanceSheets[date]229 # if we have a quarterly statement add it230 if not isBalanceSheetEmptyOrInvalid(quarterlyStatement):231 quarterlyStatement.source = "actual"232 balanceSheets[date] = quarterlyStatement233 # if the quarterlyStatement is empty, we need to try and get the values from the yearlyStatement on this date234 if quarterlyStatement == BalanceSheet():235 yearlyStatement = mergedYearlyBalanceSheets[date]236 if yearlyStatement and not isBalanceSheetEmptyOrInvalid(yearlyStatement):237 # NOTE we don't divide by 4 here238 assets = yearlyStatement.assets239 currentAssets = yearlyStatement.currentAssets240 liabilities = yearlyStatement.liabilities241 currentLiabilities = yearlyStatement.currentLiabilities242 retainedEarnings = yearlyStatement.retainedEarnings243 cash = yearlyStatement.cash244 estimatedStatement = BalanceSheet(245 assets=assets,246 currentAssets=currentAssets,247 liabilities=liabilities,248 currentLiabilities=currentLiabilities,249 retainedEarnings=retainedEarnings,250 cash=cash,251 source="yearly",252 estimate=False,253 )254 balanceSheets[date] = estimatedStatement255 else:256 balanceSheets[date] = BalanceSheet()257 # now that we've populated our statements as far as possible258 # we need to try extrapolate empty dates using before and after quarterlyStatements259 for i, date in enumerate(quarterlyDates):260 quarterlyStatement = (261 date in balanceSheets and balanceSheets[date] or BalanceSheet()262 )263 if quarterlyStatement == BalanceSheet():264 assets = 0 # asset, don't trend265 currentAssets = 0266 liabilities = 0267 retainedEarnings = 0268 currentLiabilities = 0269 cash = 0270 balanceSheet = BalanceSheet(271 assets=assets,272 currentAssets=currentAssets,273 liabilities=liabilities,274 currentLiabilities=currentLiabilities,275 retainedEarnings=retainedEarnings,276 cash=cash,277 source="trend",278 estimate=True,279 )280 balanceSheets[date] = balanceSheet281 cashFlowStatements = {}282 for date in mergedQuarterlyCashFlowStatements:283 quarterlyStatement = mergedQuarterlyCashFlowStatements[date]284 # if we have a quarterly statement add it285 if not isCashFlowStatementEmptyOrInvalid(quarterlyStatement):286 quarterlyStatement.source = "actual"287 cashFlowStatements[date] = quarterlyStatement288 # if the quarterlyStatement is empty, we need to try and get the values from the yearlyStatement on this date289 if quarterlyStatement == CashFlowStatement():290 yearlyStatement = mergedYearlyCashFlowStatements[date]291 if yearlyStatement and not isCashFlowStatementEmptyOrInvalid(292 yearlyStatement293 ):294 dividendsPaid = (295 yearlyStatement.dividendsPaid296 ) # asset, don't divide by 4297 cashFromOperations = yearlyStatement.cashFromOperations298 capex = yearlyStatement.capex299 estimatedStatement = CashFlowStatement(300 dividendsPaid=dividendsPaid,301 cashFromOperations=cashFromOperations,302 capex=capex,303 source="yearly",304 estimate=False,305 )306 cashFlowStatements[date] = estimatedStatement307 else:308 cashFlowStatements[date] = CashFlowStatement()309 # now that we've populated our statements as far as possible310 # we need to try extrapolate empty dates using before and after quarterlyStatements311 for i, date in enumerate(quarterlyDates):312 quarterlyStatement = (313 date in cashFlowStatements314 and cashFlowStatements[date]315 or CashFlowStatement()316 )317 if quarterlyStatement == CashFlowStatement():318 dividendsPaid = 0 # asset, don't trend this319 cashFromOperations = 0320 capex = 0321 cashFlowStatement = CashFlowStatement(322 dividendsPaid=dividendsPaid,323 cashFromOperations=cashFromOperations,324 capex=capex,325 source="trend",326 estimate=True,327 )328 cashFlowStatements[date] = cashFlowStatement329 financialStatements = FinancialStatements(330 incomeStatements=incomeStatements,331 balanceSheets=balanceSheets,332 cashFlowStatements=cashFlowStatements,333 )...
AstNode.py
Source:AstNode.py
...76class StatementsNode(Node):77 def __init__(self, statements):78 self.statements = statements79 def accept(self, visitor):80 return visitor.visitStatements(self)81# declarations82# : declaration*83# ;84class DeclarationsNode(Node):85 def __init__(self, declarationNodes):86 self.declarationNodes = declarationNodes87 def accept(self, visitor):88 return visitor.visitDeclarations(self)89 def accept(self, visitor):90 return visitor.visitStatements(self)91# : location '=' exp ';' # assignStatement92class AssignStatementNode(StatementNode):93 def __init__(self, location, exp):94 self.location = location95 self.exp = exp96 def accept(self, visitor):97 return visitor.visitAssignStatement(self)98# | 'print' exp ';' # printStatement99class PrintStatementNode(StatementNode):100 def __init__(self, exp):101 self.exp = exp102 def accept(self, visitor):103 return visitor.visitPrintStatement(self)104# | 'foreach' '('location ':' exp ')' '{' statements '}' ';' # foreachStatement...
fetchLatestFinancialStatements.py
Source:fetchLatestFinancialStatements.py
...21 except:22 return financialStatements23 financialStatements = utils.falsyToInt(financialStatements)24 return financialStatements25def fetchLatestFinancialStatements(symbol: Symbol) -> YahooQueryFinancialStatements:26 # fetches the latest quarterly and yearly financial statements27 data: YahooQueryTickerData = Ticker(symbol)28 quarterlyIncomeStatements = {}29 quarterlyIncomeStatementsDf = data.income_statement("q")30 if "data unavailable" not in quarterlyIncomeStatementsDf:31 quarterlyIncomeStatements = getFinancialStatementsFromDataFrame(32 quarterlyIncomeStatementsDf33 )34 yearlyIncomeStatements = {}35 yearlyIncomeStatementsDf = data.income_statement()36 if "data unavailable" not in yearlyIncomeStatementsDf:37 yearlyIncomeStatements = getFinancialStatementsFromDataFrame(38 yearlyIncomeStatementsDf39 )40 incomeStatements = YahooQueryIncomeStatements(41 quarterly=quarterlyIncomeStatements, yearly=yearlyIncomeStatements42 )43 quarterlyBalanceSheets = {}44 quarterlyBalanceSheetsDf = data.balance_sheet("q")45 if "data unavailable" not in quarterlyBalanceSheetsDf:46 quarterlyBalanceSheets = getFinancialStatementsFromDataFrame(47 quarterlyBalanceSheetsDf48 )49 yearlyBalanceSheets = {}50 yearlyBalanceSheetsDf = data.balance_sheet()51 if "data unavailable" not in yearlyBalanceSheetsDf:52 yearlyBalanceSheets = getFinancialStatementsFromDataFrame(yearlyBalanceSheetsDf)53 balanceSheets = YahooQueryBalanceSheets(54 quarterly=quarterlyBalanceSheets, yearly=yearlyBalanceSheets55 )56 quarterlyCashFlowStatements = {}57 quarterlyCashFlowStatementsDf = data.cash_flow("q")58 if "data unavailable" not in quarterlyCashFlowStatementsDf:59 quarterlyCashFlowStatements = getFinancialStatementsFromDataFrame(60 quarterlyCashFlowStatementsDf61 )62 yearlyCashFlowStatements = {}63 yearlyCashFlowStatementsDf = data.cash_flow()64 if "data unavailable" not in yearlyCashFlowStatementsDf:65 yearlyCashFlowStatements = getFinancialStatementsFromDataFrame(66 yearlyCashFlowStatementsDf67 )68 cashFlowStatements = YahooQueryCashFlowStatements(69 quarterly=quarterlyCashFlowStatements, yearly=yearlyCashFlowStatements70 )71 financialStatements = YahooQueryFinancialStatements(72 incomeStatements=incomeStatements,73 balanceSheets=balanceSheets,74 cashFlowStatements=cashFlowStatements,75 )...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!