Best Python code snippet using SeleniumBase
chart.py
Source:chart.py
1import harvester2import matplotlib.pyplot as plt3import os4import shutil5import charts6DATABASE_FILE = "C:\\Users\\Has112\\Documents\\db_history\\28-05-2019\\ld-database.db"7DATABASE_VERIFICATION_TEMPLATE = 'database/create_database.sql'8SAVE_CHART = True9SAVE_CHART_DIRECTORY = 'C:\\Users\\Has112\\Documents\\db_history\\28-05-2019\\figures\\' # Should end with '/'10SHOW_CHART = False11TOTAL_DOMAINS = 746091912TRANSPARENT = False13# Connect to Database14dbconnector, crawl_id = harvester.connect(DATABASE_FILE, crawl=False)15if harvester.verify_database(dbconnector, DATABASE_VERIFICATION_TEMPLATE):16 print("Database schema integrity has been verified.")17else:18 print("Error, database schema does not match the provided template.")19 exit(1)20# Override existing output folder if it exists21if not os.path.exists(SAVE_CHART_DIRECTORY):22 os.mkdir(SAVE_CHART_DIRECTORY)23else:24 shutil.rmtree(SAVE_CHART_DIRECTORY)25 os.mkdir(SAVE_CHART_DIRECTORY)26# Plot Seed Count-Time Scatter (with third dimension)27dbconnector.cursor.execute("""28 SELECT Crawl.crawlId, endDate - startDate as elapsed, count(distinct originSeedURI), count(address)29 FROM Crawl, Link30 WHERE Crawl.crawlId = Link.crawlId31 GROUP BY Crawl.crawlId;32 """)33seed_count_time_data = dbconnector.cursor.fetchall()34charts.request_time_scatter.seed_count_time_scatter_3d(seed_count_time_data, 'Seeds and Links Visited vs Time Requirements')35if SHOW_CHART:36 plt.show()37if SAVE_CHART:38 plt.savefig(SAVE_CHART_DIRECTORY + 'seeds_requests_crawl_size_time.png', bbox_inches="tight", dpi=300, transparent = TRANSPARENT)39plt.close()40# Plot Request Count-Time Scatter41dbconnector.cursor.execute("""42 SELECT Crawl.crawlId, endDate - startDate as elapsed, count(address)43 FROM Crawl, Link44 WHERE Crawl.crawlId = Link.crawlId45 GROUP BY Crawl.crawlId;46 """)47seed_count_time_data = dbconnector.cursor.fetchall()48charts.request_time_scatter.request_count_time_scatter(seed_count_time_data, 'Requests Made vs Time Requirements')49if SHOW_CHART:50 plt.show()51if SAVE_CHART:52 plt.savefig(SAVE_CHART_DIRECTORY + 'requests_crawl_size_time.png', bbox_inches="tight", dpi=300, transparent = TRANSPARENT)53plt.close()54# Plot Seed Count-Time Scatter55dbconnector.cursor.execute("""56 SELECT Crawl.crawlId, endDate - startDate as elapsed, count(distinct originSeedURI)57 FROM Crawl, Link58 WHERE Crawl.crawlId = Link.crawlId59 GROUP BY Crawl.crawlId;60 """)61seed_count_time_data = dbconnector.cursor.fetchall()62charts.request_time_scatter.seed_count_time_scatter(seed_count_time_data, 'Crawl Size vs Time Requirements')63if SHOW_CHART:64 plt.show()65if SAVE_CHART:66 plt.savefig(SAVE_CHART_DIRECTORY + 'seeds_crawl_size_time.png', bbox_inches="tight", dpi=300, transparent = TRANSPARENT)67plt.close()68# Plot Progress Pie Chart69dbconnector.cursor.execute("""70 SELECT COUNT(DISTINCT originSeedUri) 71 FROM LINK72 """)73VISITED_DOMAINS = dbconnector.cursor.fetchone()[0]74charts.progress_chart.progress_chart_pie(VISITED_DOMAINS, TOTAL_DOMAINS, "Project Progress")75if SHOW_CHART:76 plt.show()77if SAVE_CHART:78 plt.savefig(SAVE_CHART_DIRECTORY + 'project_progress.png', bbox_inches="tight", dpi=300, transparent = TRANSPARENT)79plt.close()80# Plot Response Format Pie Chart81dbconnector.cursor.execute("""82 SELECT contentFormat, COUNT(contentFormat) 83 FROM LINK84 GROUP BY contentFormat;85 """)86content_format_dict = dict(dbconnector.cursor.fetchall())87charts.file_format_chart.clean_formats(content_format_dict)88charts.file_format_chart.file_format_pie(content_format_dict, 'Response Format Breakdown')89if SHOW_CHART:90 plt.show()91if SAVE_CHART:92 plt.savefig(SAVE_CHART_DIRECTORY + 'content_format_pie.png', bbox_inches="tight", dpi=300, transparent = TRANSPARENT)93plt.close()94# Plot Response Format Bar Chart95charts.file_format_chart.file_format_bar(content_format_dict, 'Response Format Breakdown')96if SHOW_CHART:97 plt.show()98if SAVE_CHART:99 plt.savefig(SAVE_CHART_DIRECTORY + 'content_format_bar.png', bbox_inches="tight", dpi=300, transparent = TRANSPARENT)100plt.close()101# Plot RDF Response Format Pie Chart102dbconnector.cursor.execute("""103 SELECT contentFormat, COUNT(contentFormat) 104 FROM RdfURI105 GROUP BY contentFormat;106 """)107content_format_dict = dict(dbconnector.cursor.fetchall())108charts.file_format_chart.clean_formats(content_format_dict)109charts.file_format_chart.file_format_pie(content_format_dict, 'RDF Format Breakdown')110if SHOW_CHART:111 plt.show()112if SAVE_CHART:113 plt.savefig(SAVE_CHART_DIRECTORY + 'rdf_content_format_pie.png', bbox_inches="tight", dpi=300, transparent = TRANSPARENT)114plt.close()115# Plot RDF Response Format Bar Chart116charts.file_format_chart.file_format_bar(content_format_dict, 'RDF Format Breakdown')117if SHOW_CHART:118 plt.show()119if SAVE_CHART:120 plt.savefig(SAVE_CHART_DIRECTORY + 'rdf_content_format_bar.png', bbox_inches="tight", dpi=300, transparent = TRANSPARENT)121plt.close()122# Plot Site Size Histogram123dbconnector.cursor.execute("""124 SELECT originSeedURI, COUNT(DISTINCT address)125 FROM Link126 GROUP BY originSeedURI127 HAVING COUNT(DISTINCT address) > 1;128 """)129seed_size_data = dbconnector.cursor.fetchall()130charts.size_histogram.plot_size_histogram(seed_size_data, 100, "Domain Size Distribution")131if SHOW_CHART:132 plt.show()133if SAVE_CHART:134 plt.savefig(SAVE_CHART_DIRECTORY + 'domain_size_histogram.png', bbox_inches="tight", dpi=300, transparent = TRANSPARENT)135plt.close()136# Plot RDF Per Site Histogram137dbconnector.cursor.execute("""138 SELECT originSeedURI, COUNT(DISTINCT rdfSeedURI)139 FROM RdfURI140 GROUP BY originSeedURI141 HAVING COUNT(DISTINCT rdfSeedURI) > 1;142 """)143seed_size_data = dbconnector.cursor.fetchall()144charts.size_histogram.plot_size_histogram(seed_size_data, 100, "RDF Domain Size Distribution")145if SHOW_CHART:146 plt.show()147if SAVE_CHART:148 plt.savefig(SAVE_CHART_DIRECTORY + 'rdf_domain_size_histogram.png', bbox_inches="tight", dpi=300, transparent = TRANSPARENT)...
main.py
Source:main.py
...19 dev = io.open(path_dev, "r", encoding="utf-8")20 train = io.open(path_train, "r", encoding="utf-8")21 test = io.open(path_test, "r", encoding="utf-8")22 return [*file_to_trees(dev), *file_to_trees(train), *file_to_trees(test)]23def save_chart(chart, file_name: str = "figure.pdf"):24 chart.figure.savefig(os.path.join(DIST_IMAGES_PATH, file_name))25def freq(items: List[str]) -> pd.DataFrame:26 frequencies = {}27 for i in items:28 count = frequencies.get(i, 0)29 frequencies[i] = count + 130 for i in frequencies:31 frequencies[i] = (frequencies[i], frequencies[i] / len(items))32 values = np.array([v[0] for v in frequencies.values()])33 names = np.array([k for k in frequencies.keys()])34 df = pd.DataFrame(values, index=names).sort_values(by=0, ascending=False)35 df[1] = [df[0][0] / ind for ind in np.arange(1, len(frequencies) + 1)]36 df.columns = ["ÐÑÑиманий ÑозподÑл", "РозподÑл ÐÑпÑа"]37 return df38def sig_sentence(sentence: List[Signature]):39 types = {}40 for sig in sentence:41 amount = types.get(sig.type, 0)42 types[sig.type] = amount + 143 return f"w{types.get(SigType.W, 0)},d{types.get(SigType.D, 0)},n{types.get(SigType.N)}"44def process(treebank: List[ParseTree], lang: str):45 sentences = [[tree.signature for tree in sentence_tree.to_list()] for sentence_tree in treebank]46 signatures = [signature for signatures in sentences for signature in signatures]47 deprel = [sig.node[0] for sig in signatures]48 upos = [sig.node[1] for sig in signatures]49 deprel_upos = [f"{sig.node[0]}+{sig.node[1]}" for sig in signatures]50 type_w = [f"{sig.type},{sig.sentence_id}" for sig in signatures if sig.type == SigType.W]51 type_d = [f"{sig.type},{sig.height},{sig.subtree_type}" for sig in signatures if sig.type == SigType.D]52 type_upos = [f"{sig.type}+{sig.node[1]}" for sig in signatures]53 sent_d_w_n = [sig for sig in map(sig_sentence, sentences)]54 freq_deprel = freq(deprel)55 freq_upos = freq(upos)56 freq_deprel_upos = freq(deprel_upos)57 freq_type_w = freq(type_w)58 freq_type_d = freq(type_d)59 freq_type_upos = freq(type_upos)60 freq_sent_d_w_n = freq(sent_d_w_n)61 chart_deprel = freq_deprel.plot(kind="bar")62 chart_upos = freq_upos.plot(kind="bar")63 chart_deprel_upos = freq_deprel_upos.plot(xticks=[],64 xlabel=f"РозподÑл deprel+upos. ÐÑÑого: {len(deprel_upos)}. УнÑкалÑниÑ
: {len(freq_deprel_upos)}")65 chart_type_w = freq_type_w.plot(xticks=[],66 xlabel=f"РозподÑл ÑÐ¸Ð¿Ñ w. ÐÑÑого: {len(type_w)}. УнÑкалÑниÑ
: {len(freq_type_w)}")67 chart_type_d = freq_type_d.plot(xticks=[],68 xlabel=f"РозподÑл ÑÐ¸Ð¿Ñ d. ÐÑÑого: {len(type_d)}. УнÑкалÑниÑ
: {len(freq_type_d)}")69 chart_type_upos = freq_type_upos.plot(xticks=[], kind="bar",70 xlabel=f"РозподÑл Тип + ЧаÑÑина мови. ÐÑÑого: {len(type_upos)}. УнÑкалÑниÑ
: {len(freq_type_upos)}")71 chart_sent_d_w_n = freq_sent_d_w_n.plot(xticks=[],72 xlabel=f"РозподÑл ÑигнаÑÑÑ ÑеÑеннÑ. ÐÑÑого: {len(sent_d_w_n)}. УнÑкалÑниÑ
: {len(freq_sent_d_w_n)}")73 save_chart(chart_deprel, f"chart_{lang}_deprel.pdf")74 save_chart(chart_upos, f"chart_{lang}_upos.pdf")75 save_chart(chart_deprel_upos, f"chart_{lang}_deprel_upos.pdf")76 save_chart(chart_type_w, f"chart_{lang}_type_w.pdf")77 save_chart(chart_type_d, f"chart_{lang}_type_d.pdf")78 save_chart(chart_type_upos, f"chart_{lang}_type_upos.pdf")79 save_chart(chart_sent_d_w_n, f"chart_{lang}_sent_d_w_n.pdf")80def parse_cli_args():81 args = filter(lambda txt: re.search(r"--\w+=.+", txt), sys.argv[1:])82 keys = lambda txt: txt[2:txt.index('=')]83 vals = lambda txt: txt[txt.index('=') + 1:]84 return dict((keys(v), vals(v)) for v in args)85def print_tree(tree):86 print(tree.metadata.get("text"), "\n")87 for node in tree.to_list():88 print("{:25}{}".format(node.token.get("form"), node.signature))89 print("-" * 100, "\n")90def main():91 uk_trees = get_token_trees(UK_DEV_PATH, UK_TRAIN_PATH, UK_TEST_PATH)92 # es_trees = get_token_trees(ES_DEV_PATH, ES_TRAIN_PATH, ES_TEST_PATH)93 args = parse_cli_args()...
graph_examples.py
Source:graph_examples.py
...15chart_count = 016if save_charts and not os.path.exists(dir_save):17 os.mkdir(dir_save)18 19def save_chart(fig):20 if save_charts:21 global chart_count22 chart_count += 123 plotly.io.write_image(fig, dir_save + str(chart_count) + ".svg")24# Make some fake data25def shoe_to_height(x):26 return x*x27shoe_size = np.random.standard_normal(size=10000)28height = shoe_to_height(shoe_size) + (np.random.rand(shoe_size.shape[0]) - 0.5) * 0.229hat_size = np.random.randint(0,5, size=shoe_size.shape[0])30hair_colour = np.tile(['blonde', 'brunette'], int(shoe_size.shape[0]/2))31is_male = np.random.randint(0, 2, size=shoe_size.shape[0])32data = {33 'shoe_size' : shoe_size,34 'person_height': height,35 'hat_size': hat_size,36 'hair_colour': hair_colour,37 'is_male': is_male38}39# Convert it into a table using pandas40dataset = pandas.DataFrame(data)41# Make example graphs.42def line_1_eq(x):43 return 0.02 * x ** 2 + 0.1 * x + 144def line_2_eq(x):45 return -0.02 * (x - 2) ** 2 + 0.1 * x + 246def line_3_eq(x):47 return -0.04 * (x + 2) ** 2 + 0.2 * x - 248# Plot single function49fig = graphing.line_2D(("line number one", line_1_eq), 50 x_range=[-10,10], 51 label_x="x-axis", 52 label_y="Goodness (cm)",53 show=show_charts)54save_chart(fig)55# Plot multiple functions56fig = graphing.line_2D([ ("line number one", line_1_eq), 57 ("line number two", line_2_eq), 58 ("line number three", line_3_eq)], 59 x_range=[-10,10], 60 label_x="x-axis", 61 label_y="Goodness (cm)",62 legend_title="Line number",63 title="Line plot", show=show_charts)64save_chart(fig)65def surf_eq(x, y):66 return x * 8 + y ** 267fig = graphing.surface(np.array([0,1]), np.linspace(-2,2), surf_eq, axis_title_x="X values", axis_title_y="Y Values", axis_title_z="Z values", show=show_charts)68save_chart(fig)69for n in [10,50,200,1000]:70 fig = graphing.scatter_2D(dataset[:n], title=f"A 2D scatter with {n} points", show=show_charts, trendline=shoe_to_height)71 save_chart(fig)72fig = graphing.scatter_3D(dataset, title="A 3D scatter", show=show_charts)73save_chart(fig)74fig = graphing.box_and_whisker(dataset, title="A simple box and whisker", show=show_charts)75save_chart(fig)76fig = graphing.box_and_whisker(dataset, label_x='hat_size', title="A box and whisker, 1 group level", show=show_charts)77save_chart(fig)78fig = graphing.box_and_whisker(dataset, label_x='hat_size', label_x2='hair_colour', title="A box and whisker, 2 group levels", show=show_charts)79save_chart(fig)80fig = graphing.histogram(dataset, title="A histogram (one variable)", show=show_charts)81save_chart(fig)82fig = graphing.histogram(dataset, label_x="hat_size", label_y="shoe_size", title="A histogram (two variables)", show=show_charts)83save_chart(fig)84fig = graphing.histogram(dataset, label_colour="hat_size", title="A stacked histogram", show=show_charts)85save_chart(fig)86fig = graphing.histogram(dataset, title="A histogram (one variable + boxplot)", include_boxplot=True, show=show_charts)87save_chart(fig)88fig = graphing.histogram(dataset, label_colour="hat_size", title="A stacked histogram (+ boxplot)", include_boxplot=True, show=show_charts)89save_chart(fig)90fig = graphing.multiple_histogram(dataset, label_x="hat_size", label_y="is_male", histfunc='avg', label_group="hair_colour", title="A histogram (two variables)", show=show_charts)91save_chart(fig)92fig = graphing.multiple_histogram(dataset, label_x="hat_size", label_group="hair_colour", title="A histogram (two variables)", show=show_charts)...
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!!