Best Python code snippet using localstack_python
yahoo_finance_scraper.py
Source:yahoo_finance_scraper.py  
...78                r = requests.get(f"https://finance.yahoo.com/quote/{symbol}/key-statistics?p={symbol}", headers=headers)79                soup = bs(r.content, "html.parser")80                soup_tracker[symbol] = soup81                for m in self.df.columns:82                    self.df.loc[symbol, m] = self.get_metric_data(soup, m, statistics_page_element)83            except Exception:84                print(f"-- {symbol.upper()} was not found")85                # Since the data86                self.df = self.df.drop(symbol)87        # Ranges for each field88        profitability_range = detailed_metric_list[0:2]89        management_effectiveness_range = detailed_metric_list[2:4]90        income_statement_range = detailed_metric_list[4:12]91        balance_sheet_range = detailed_metric_list[12:18]92        cash_flow_statement_range = detailed_metric_list[18:20]93        stock_price_history_range = detailed_metric_list[20:25]94        share_statistics_range = detailed_metric_list[25:32]95        dividend_and_splits_range = detailed_metric_list[32:42]96        self.profitability = pd.DataFrame(index=self.ticker_list, columns=profitability_range)97        self.management_effectiveness = pd.DataFrame(index=self.ticker_list, columns=management_effectiveness_range)98        self.income_statement = pd.DataFrame(index=self.ticker_list, columns=income_statement_range)99        self.balance_sheet = pd.DataFrame(index=self.ticker_list, columns=balance_sheet_range)100        self.cash_flow_statement = pd.DataFrame(index=self.ticker_list, columns=cash_flow_statement_range)101        self.stock_price_history = pd.DataFrame(index=self.ticker_list, columns=stock_price_history_range)102        self.share_statistics = pd.DataFrame(index=self.ticker_list, columns=share_statistics_range)103        self.dividends_and_split = pd.DataFrame(index=self.ticker_list, columns=dividend_and_splits_range)104        self.statistics_table = pd.DataFrame(index=self.ticker_list, columns=table_metric_list)105        # Fill each dataframe106        # Financials Table107        for symbol in self.profitability.index:108            for m in table_metric_list:109                self.statistics_table.loc[symbol, m] = self.get_metric_data(soup_tracker[symbol], m, statistics_table_element)110        # Profitability section111        for symbol in self.profitability.index:112            for m in detailed_metric_list[0:2]:113                self.profitability.loc[symbol,m] = self.get_metric_data(soup_tracker[symbol], m, statistics_page_element)114        # Management Effectiveness section115        for symbol in self.management_effectiveness.index:116            for m in detailed_metric_list[2:4]:117                self.management_effectiveness.loc[symbol,m] = self.get_metric_data(soup_tracker[symbol], m, statistics_page_element)118        # Income Statement119        for symbol in self.income_statement.index:120            for m in detailed_metric_list[4:12]:121                self.income_statement.loc[symbol,m] = self.get_metric_data(soup_tracker[symbol], m, statistics_page_element)122        # Balance Sheet123        for symbol in self.balance_sheet.index:124            for m in detailed_metric_list[12:18]:125                self.balance_sheet.loc[symbol,m] = self.get_metric_data(soup_tracker[symbol], m, statistics_page_element)126        # Cash flow statement127        for symbol in self.cash_flow_statement.index:128            for m in detailed_metric_list[18:20]:129                self.cash_flow_statement.loc[symbol, m] = self.get_metric_data(soup_tracker[symbol], m, statistics_page_element)130        # Stock price history131        for symbol in self.stock_price_history.index:132            for m in detailed_metric_list[20:25]:133                self.stock_price_history[symbol,m] = self.get_metric_data(soup_tracker[symbol], m, statistics_page_element)134        # Share Statistics135        for symbol in self.share_statistics.index:136            for m in detailed_metric_list[25:32]:137                self.share_statistics.loc[symbol, m] = self.get_metric_data(soup_tracker[symbol], m, statistics_page_element)138        # Dividends & Splits139        for symbol in self.share_statistics.index:140            for m in detailed_metric_list[32:42]:141                self.dividends_and_split.loc[symbol, m] = self.get_metric_data(soup_tracker[symbol], m, statistics_page_element)142    '''143    Function: set_income_statement()144    Description: 145    Returns: DataFrame146    '''147    def set_income_statement(self):148        # All the metrics that will be collected149        table_metric_list = ["Total Revenue", "Cost of Revenue", "Gross Profit",150                             "Operating Expense", "Operating Income", "Net Non Operating Interest Income Expense",151                             "Other Income Expense","Pretax Income", "Tax Provision",152                             "Net Income Common Stockholders", "Diluted NI Available to Com Stockholders", "Basic EPS",153                             "Diluted EPS", "Basic Average Shares", "Diluted Average Shares",154                             "Total Operating Income as Reported", "Total Expenses", "Net Income from Continuing & Discontinued Operation",155                             "Normalized Income", "Interest Income", "Interest Expense",156                             "Net Interest Income", "EBIT", "EBITDA",157                             "Reconciled Cost of Revenue", "Reconciled Depreciation", "Net Income from Continuing Operation Net Minority Interest",158                             "Normalized EBITDA"]159        # Class element for the table160        table_element = "Ta(c) Py(6px) Bxz(bb) BdB Bdc($seperatorColor) Miw(120px) Miw(100px)--pnclg Bgc($lv1BgColor) fi-row:h_Bgc($hoverBgColor) D(tbc)"161        # Assigning the dataframe162        self.income_statement_dataframe = pd.DataFrame(index=self.ticker_list, columns=table_metric_list)163        for symbol in self.income_statement_dataframe.index:164            try:165                r = requests.get(f"https://finance.yahoo.com/quote/{symbol}/financials?p={symbol}", headers=headers)166                soup = bs(r.content, "html.parser")167                for m in self.income_statement_dataframe.columns:168                    self.income_statement_dataframe.loc[symbol, m] = self.get_metric_data(soup, m, table_element)169            except Exception:170                print(f"-- {symbol.upper()} was not found")171                # Since the data172                self.income_statement_dataframe = self.income_statement_dataframe.drop(symbol)173    '''174    Function: set_balance_sheet()175    Description: Scrapes the balance sheet and stores the values in a dataframe.176    Returns: None177    '''178    def set_balance_sheet(self):179        balance_sheet_metrics = ["Total Assets", "Total Liabilities Net Minority Interest", "Total Equity Gross Minority Interest",180                                 "Total Capitalization", "Common Stock Equity", "Net Tangible Assets",181                                 "Working Capital", "Invested Capital", "Tangible Book Value",182                                 "Total Debt", "Net Debt", "Share Issued", "Ordinary Shares Number"]183        # Class element for the table184        table_element = "Ta(c) Py(6px) Bxz(bb) BdB Bdc($seperatorColor) Miw(120px) Miw(100px)--pnclg D(tbc)"185        # Assigning the dataframe186        self.balance_sheet_dataframe= pd.DataFrame(index=self.ticker_list, columns=balance_sheet_metrics)187        for symbol in self.balance_sheet_dataframe.index:188            try:189                r = requests.get(f"https://finance.yahoo.com/quote/{symbol}/balance-sheet?p={symbol}", headers=headers)190                soup = bs(r.content, "html.parser")191                for m in self.balance_sheet_dataframe.columns:192                    self.balance_sheet_dataframe.loc[symbol, m] = self.get_metric_data(soup, m, table_element)193            except Exception:194                print(f"-- {symbol.upper()} was not found")195                # Since the data196                self.balance_sheet_dataframe = self.balance_sheet_dataframe.drop(symbol)197    '''198    Function: set_cash_flow_statement()199    Description: Scrapes the cash flow statement from yahoo.200    Returns: None201    '''202    def set_cash_flow_statement(self):203        cash_flow_metrics = ["Operating Cash Flow", "Investing Cash Flow", "Financing Cash Flow",204                             "End Cash Position", "Income Tax Paid Supplemental Data", "Interest Paid Supplemental Data",205                             "Capital Expenditure", "Issuance of Capital Stock", "Issuance of Debt",206                             "Repayment of Debt", "Repurchase of Capital Stock", "Free Cash Flow"]207        # Class element for the table208        table_element = "Ta(c) Py(6px) Bxz(bb) BdB Bdc($seperatorColor) Miw(120px) Miw(100px)--pnclg D(tbc)"209        # Assigning the dataframe210        self.cash_flow_dataframe = pd.DataFrame(index=self.ticker_list, columns=cash_flow_metrics)211        212        # Fill the dataframe213        for symbol in self.cash_flow_dataframe.index:214            try:215                r = requests.get(f"https://finance.yahoo.com/quote/{symbol}/balance-sheet?p={symbol}", headers=headers)216                soup = bs(r.content, "html.parser")217                for m in self.cash_flow_dataframe.columns:218                    self.cash_flow_dataframe.loc[symbol, m] = self.get_metric_data(soup, m, table_element)219            except Exception:220                print(f"-- {symbol.upper()} was not found")221                # Since the data222                self.cash_flow_dataframe = self.cash_flow_dataframe.drop(symbol)223    '''224    Function: set_price_data()225    Description:226    Returns: 227    '''228    def set_price_data(self):229        230        # Assigning the dataframe231       # self.price_dataframe = pd.DataFrame(index=self.ticker_list)232        dud = self.subtract_dates(4)233        print(f"DUD: {dud}")234        # Fill the dataframe235        for symbol in self.ticker_list:236            r = f"https://query1.finance.yahoo.com/v7/finance/download/{symbol}?period1={self.subtract_dates(5)}&period2={dt.datetime.now().timestamp()}&interval=1d&events=history&includeAdjustedClose=true"237            df = pd.read_csv(r)238            print(f"DF: {df}")239        240        241    '''242    Function: subtract_dates(int)243    Description: Subtracts a specified amount of years by the current date244    Returns: str245    '''246    def subtract_dates(self, years_to_subtract: int):247        new_time = dt.datetime.now() - relativedelta(years=years_to_subtract)248        print(f"New: {int(new_time.timestamp())}")249        return int(new_time.timestamp())250    '''251    Function:252    Description:253    Returns: 254    '''255    '''256    Function:257    Description:258    Returns: 259    '''260    '''261    Function: get_metric_data(BeautifulSoup, str)262    Description: Gets the data for the specific metric passed.263    Returns: Any264    '''265    @staticmethod266    def get_metric_data(soup, metric: str, class_of_element: str):267        data = soup.find(text=metric).find_next(class_=class_of_element).text268        return data269def main():270    ticker = "AAPL"271    yf = YahooFinanceScraper(ticker)272    yf.set_price_data()...draw_plot.py
Source:draw_plot.py  
...158        previous = result[len(result) - 1]159        if metric[2] > previous[2]:160            result.append(metric)161    return result162def get_metric_data(data_dir, baseline_file, name):163    result_files = []164    files = os.listdir(data_dir)165    for file in files:166        if not os.path.isdir(file):167            result_files.append(os.path.join(data_dir, file))168    metric_datas = []169    for result_file in result_files:170        recall, qps = calc_recall(result_file, baseline_file)171        metric_datas.append(("useless", name, recall, qps))172    metric_datas = remove_useless_point(metric_datas)173    print(name, metric_datas)174    return metric_datas175def draw_ip_recipe(image_name):176    linestyle = create_linestyles([ivf_name, hnsw_name,177                                   nra_ivf_name, nra_hnsw_name,178                                   nra_ivf_omp_name, nra_hnsw_omp_name])179    baseline_file = "/home/abner/workspace/MultiVector/cmake-build-debug/test/ip-recipe/baseline.txt"180    ivf_dir = "/home/abner/workspace/MultiVector/cmake-build-debug/test/ip-recipe/ivf"181    hnsw_dir = "/home/abner/workspace/MultiVector/cmake-build-debug/test/ip-recipe/hnsw"182    nra_ivf_dir = "/home/abner/workspace/MultiVector/cmake-build-debug/test/ip-recipe/nra-ivf"183    nra_hnsw_dir = "/home/abner/workspace/MultiVector/cmake-build-debug/test/ip-recipe/nra-hnsw"184    nra_ivf_omp_dir = "/home/abner/workspace/MultiVector/cmake-build-debug/test/ip-recipe/nra-ivf2"185    nra_hnsw_omp_dir = "/home/abner/workspace/MultiVector/cmake-build-debug/test/ip-recipe/nra-hnsw2"186    ivf_data = get_metric_data(ivf_dir, baseline_file, ivf_name)187    hnsw_data = get_metric_data(hnsw_dir, baseline_file, hnsw_name)188    nra_ivf_data = get_metric_data(nra_ivf_dir, baseline_file, nra_ivf_name)189    nra_hnsw_data = get_metric_data(nra_hnsw_dir, baseline_file, nra_hnsw_name)190    nra_omp_ivf_data = get_metric_data(nra_ivf_omp_dir, baseline_file, nra_ivf_omp_name)191    nra_omp_hnsw_data = get_metric_data(nra_hnsw_omp_dir, baseline_file, nra_hnsw_name)192    data = {ivf_name: ivf_data, hnsw_name: hnsw_data,193            nra_ivf_name: nra_ivf_data, nra_hnsw_name: nra_hnsw_data,194            nra_ivf_omp_name: nra_omp_ivf_data,195            nra_hnsw_omp_name: nra_omp_hnsw_data}196    create_plot(all_data=data, raw=False, x_log=False, y_log=True,197                xn='k-nn', yn='qps', fn_out=image_name,198                linestyles=linestyle, batch=False)199def draw_ip_glove(image_name):200    linestyle = create_linestyles([ivf_name, hnsw_name,201                                   nra_ivf_name, nra_hnsw_name,202                                   nra_ivf_omp_name, nra_hnsw_omp_name])203    baseline_file = "/home/abner/workspace/MultiVector/cmake-build-debug/test/ip-glove/baseline.txt"204    ivf_dir = "/home/abner/workspace/MultiVector/cmake-build-debug/test/ip-glove/ivf"205    hnsw_dir = "/home/abner/workspace/MultiVector/cmake-build-debug/test/ip-glove/hnsw"206    nra_ivf_dir = "/home/abner/workspace/MultiVector/cmake-build-debug/test/ip-glove/nra-ivf1"207    nra_hnsw_dir = "/home/abner/workspace/MultiVector/cmake-build-debug/test/ip-glove/nra-hnsw1"208    nra_ivf_omp_dir = "/home/abner/workspace/MultiVector/cmake-build-debug/test/ip-glove/nra-ivf"209    nra_hnsw_omp_dir = "/home/abner/workspace/MultiVector/cmake-build-debug/test/ip-glove/nra-hnsw"210    ivf_data = get_metric_data(ivf_dir, baseline_file, ivf_name)211    hnsw_data = get_metric_data(hnsw_dir, baseline_file, hnsw_name)212    nra_ivf_data = get_metric_data(nra_ivf_dir, baseline_file, nra_ivf_name)213    nra_hnsw_data = get_metric_data(nra_hnsw_dir, baseline_file, nra_hnsw_name)214    nra_omp_ivf_data = get_metric_data(nra_ivf_omp_dir, baseline_file, nra_ivf_omp_name)215    nra_omp_hnsw_data = get_metric_data(nra_hnsw_omp_dir, baseline_file, nra_hnsw_name)216    data = {ivf_name: ivf_data, hnsw_name: hnsw_data,217            nra_ivf_name: nra_ivf_data, nra_hnsw_name: nra_hnsw_data,218            nra_ivf_omp_name: nra_omp_ivf_data,219            nra_hnsw_omp_name: nra_omp_hnsw_data}220    create_plot(all_data=data, raw=False, x_log=False, y_log=True,221                xn='k-nn', yn='qps', fn_out=image_name,222                linestyles=linestyle, batch=False)223def draw_l2(image_name):224    linestyle = create_linestyles([225                                   "NRA4096_4096 IVFFLAT", "NRA4096_4096 HNSW",226                                   "NRA8192_8192 IVFFLAT", "NRA8192_8192 HNSW",227                                   "NRA16384_16384 IVFFLAT", "NRA16384_16384 HNSW"228                                   ])229                                   # "NRA4096_4096 IVFFLAT-BATCH", "NRA4096_4096 HNSW-BATCH"])230    baseline_file = "/root/results/l2/baseline.txt"231    ivf_dir1 = "/root/results/l2/ivfflat/test1"232    hnsw_dir1 = "/root/results/l2/hnsw/test1"233    ivf_dir2 = "/root/results/l2/ivfflat/test2"234    hnsw_dir2 = "/root/results/l2/hnsw/test2"235    ivf_dir3 = "/root/results/l2/ivfflat/test3"236    hnsw_dir3 = "/root/results/l2/hnsw/test3"237    ivf_metric_data1 = get_metric_data(ivf_dir1 , baseline_file, "NRA8192_8192 IVFFLAT")238    hnsw_metric_data1 = get_metric_data(hnsw_dir1 , baseline_file, "NRA8192_8192 HNSW")239    ivf_metric_data2 = get_metric_data(ivf_dir2 , baseline_file, "NRA4096_4096 IVFFLAT")240    hnsw_metric_data2 = get_metric_data(hnsw_dir2 , baseline_file, "NRA4096_4096 HNSW")241    ivf_metric_data3 = get_metric_data(ivf_dir3 , baseline_file, "NRA16384_16384 IVFFLAT")242    hnsw_metric_data3 = get_metric_data(hnsw_dir3 , baseline_file, "NRA16384_16384 HNSW")243   # data = {"L2 NRA IVF-Flat": ivf_metric_data, "L2 NRA HNSW": hnsw_metric_data}244    data = {245            "NRA8192_8192 IVFFLAT": ivf_metric_data1, "NRA8192_8192 HNSW": hnsw_metric_data1,246            "NRA4096_4096 IVFFLAT": ivf_metric_data2, "NRA4096_4096 HNSW": hnsw_metric_data2,247            "NRA16384_16384 IVFFLAT": ivf_metric_data3, "NRA16384_16384 HNSW": hnsw_metric_data3248    }249    create_plot(all_data=data, raw=False, x_log=False, y_log=True,250                xn='k-nn', yn='qps', fn_out=image_name,251                linestyles=linestyle, batch=False)252if __name__ == "__main__":253    ivf_name = "IVF-Flat"254    hnsw_name = "HNSW"255    nra_ivf_name = "NRA IVF-Flat"256    nra_hnsw_name = "NRA HNSW"...server.py
Source:server.py  
...13def set_zero_metrics():14    metrics = [OPEN_CONNECTIONS, REQUESTS_PER_SECOND, CONNECTIONS_PER_SECOND, BANDWIDTH_IN, BANDWIDTH_OUT]15    for metric in metrics:16        metric.set(0)17def get_metric_data(data_list):18    sum = 0.019    for data in data_list:20        try:21            sum = sum + float(data[1])22        except:23            pass24    return sum25def get_metrics():26    # Get start and end time 15 secs interval27    end = datetime.utcnow()28    start = end - timedelta(seconds=15)29    iso_end = end.strftime("%Y-%m-%dT%H%M%S")30    iso_start = start.strftime("%Y-%m-%dT%H%M%S")31    # Build request32    payload['start'] = iso_start33    payload['end'] = iso_end34    r = requests.get(url, headers=headers, params=payload)35    data = r.json().get('metrics').get('time_series')36    if not data:37        set_zero_metrics()38    # Set gauges39    OPEN_CONNECTIONS.set(get_metric_data(data.get('connections_per_second').get('values')))40    REQUESTS_PER_SECOND.set(get_metric_data(data.get('requests_per_second').get('values')))41    CONNECTIONS_PER_SECOND.set(get_metric_data(data.get('connections_per_second').get('values')))42    BANDWIDTH_IN.set(get_metric_data(data.get('bandwidth.in').get('values')))43    BANDWIDTH_OUT.set(get_metric_data(data.get('bandwidth.out').get('values')))44if __name__ == '__main__':45    start_http_server(9110)46    while True:47        time.sleep(5)...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!!
