Best Python code snippet using SeleniumBase
dutch.py
Source:dutch.py
...133 return self.js_click(*args, **kwargs)134 def js_tekst_bijwerken(self, *args, **kwargs):135 # js_update_text(selector, text)136 return self.js_update_text(*args, **kwargs)137 def js_typ(self, *args, **kwargs):138 # js_type(selector, text)139 return self.js_type(*args, **kwargs)140 def html_inspecteren(self, *args, **kwargs):141 # inspect_html()142 return self.inspect_html(*args, **kwargs)143 def bewaar_screenshot(self, *args, **kwargs):144 # save_screenshot(name)145 return self.save_screenshot(*args, **kwargs)146 def selecteer_bestand(self, *args, **kwargs):147 # choose_file(selector, file_path)148 return self.choose_file(*args, **kwargs)149 def voer_het_script_uit(self, *args, **kwargs):150 # execute_script(script)151 return self.execute_script(*args, **kwargs)...
load_json_into_psql.py
Source:load_json_into_psql.py
1#!/usr/bin/env python2# Adopted from http://blog.endpoint.com/2016/03/loading-json-files-into-postgresql-95.html3 4import os, sys, argparse5import json6 7try:8 import psycopg2 as pg9except:10 print "Install psycopg2"11 exit(123)12 13try:14 import progressbar15except:16 print "Install progressbar2 (by Rick van Hattem (Wolph)!"17 print "$ pip install progressbar2"18 exit(123)19 20def load_json_into_psql(json_path):21 # connect once to setup the 'temp' database22 # IF a 'temp' database already exist, IT WILL BE DELETED!!23 dbconn = pg.connect(user='deanliu', host='localhost', port='5432')24 dbconn.autocommit = True25 cursor = dbconn.cursor()26 cursor.execute('DROP DATABASE IF EXISTS temp;')27 cursor.execute('CREATE DATABASE temp;')28 cursor.close()29 dbconn.close()30 # now connect to our 'temp' database31 dbconn = pg.connect(dbname='temp', user='deanliu', host='localhost', port='5432')32 cursor = dbconn.cursor()33 create_tables(cursor)34 # look for json files in the path35 print 'Searching for json files at path: {}'.format(os.path.abspath(json_path))36 files = [f for f in os.listdir(json_path) if os.path.isfile(os.path.join(json_path, f))]37 json_files = [f for f in files if f.endswith('.json')]38 print 'Found {} files to process'.format(len(json_files))39 # load data40 for file_count, f in enumerate(json_files):41 fname = os.path.join(json_path, f)42 print 'Processing file #{}: {}'.format(file_count+1, fname)43 progress_max = os.path.getsize(fname)44 bar = progressbar.ProgressBar( max_value = progress_max, \45 widgets = [' [', progressbar.Timer(), '] ', \46 progressbar.Bar(), \47 ' (', progressbar.ETA(), ')'] )48 progress = 049 with open(fname) as js_file:50 for js_line in js_file:51 js = json.loads(js_line)52 js_type = js.pop('type')53 if js_type == 'checkin':54 # parse_checkin(cursor, js)55 pass56 elif js_type == 'business':57 parse_business(cursor, js)58 pass59 elif js_type == 'review':60 parse_review(cursor, js)61 pass62 elif js_type == 'tip':63 # parse_tip(cursor, js)64 pass65 elif js_type == 'user':66 parse_user(cursor, js)67 pass68 else: # no js_typ match, FAULT!69 pass70 progress+=len(js_line)71 # print 'progress = ', progress, '/', progress_max72 bar.update(progress)73 bar.finish()74 dbconn.commit() # <--- commit at end of each file. this line saved my butt!75 create_user_reviews_table(cursor)76 dbconn.commit()77 dbconn.close()78 return79def create_tables(cursor):80 # cursor.execute('''CREATE TABLE checkins81 # (id SERIAL PRIMARY KEY, data JSONB NOT NULL);''')82 cursor.execute('''CREATE TABLE businesses83 (biz_id CHAR(22) PRIMARY KEY,84 name VARCHAR(255),85 categories JSONB,86 attributes JSONB,87 hours JSONB,88 data JSONB NOT NULL);''')89 cursor.execute('''CREATE TABLE reviews90 (review_id CHAR(22) PRIMARY KEY,91 biz_id CHAR(22),92 user_id CHAR(22),93 stars NUMERIC,94 rv_txt TEXT,95 rv_date DATE,96 votes JSONB);''')97 # cursor.execute('''CREATE TABLE tips98 # (id SERIAL PRIMARY KEY, data JSONB NOT NULL);''')99 cursor.execute('''CREATE TABLE users100 (user_id CHAR(22) PRIMARY KEY,101 name VARCHAR(255),102 review_count INTEGER,103 friends JSONB,104 data JSONB NOT NULL);''')105 return106def parse_checkin(cursor, js):107 cursor.execute('INSERT INTO checkins(data) VALUES (%s);', [json.dumps(js)] )108# data: [ 'business_id', 'checkin_info']109 return110def parse_business(cursor, js):111 biz_id = js.pop('business_id')112 name = js.pop('name')113 categories = json.dumps(js.pop('categories'))114 attributes = json.dumps(js.pop('attributes'))115 hours = json.dumps(js.pop('hours'))116 data = json.dumps(js)117# data: [ 'full_address', 'city', 'state',118# 'neighborhoods', 'latitude', 'longitude',119# 'review_count', 'stars']120 cursor.execute('INSERT INTO businesses VALUES (%s, %s, %s, %s, %s, %s);', \121 (biz_id, name, categories, attributes, hours, data) )122 return123def parse_review(cursor, js):124 review_id = js.pop('review_id')125 biz_id = js.pop('business_id')126 user_id = js.pop('user_id')127 stars = js.pop('stars')128 rv_txt = js.pop('text')129 rv_date = js.pop('date')130 votes = json.dumps(js.pop('votes'))131# votes: [ 'funny', userful', 'cool' ]132 cursor.execute('''INSERT INTO reviews(review_id, biz_id, user_id, stars, rv_txt, rv_date, votes)133 VALUES (%s, %s, %s, %s, %s, %s, %s);''', \134 (review_id, biz_id, user_id, stars, rv_txt, rv_date, votes) )135 return136def parse_tip(cursor, js):137 cursor.execute('INSERT INTO tips(data) VALUES (%s);', [json.dumps(js)] )138# data: ['business_id', 'user_id', 'date', 'likes', 'text']139 return140def parse_user(cursor, js):141 user_id = js.pop('user_id')142 name = js.pop('name')143 rv_cnt = js.pop('review_count')144 friends = json.dumps(js.pop('friends'))145 data = json.dumps(js)146# data: [ 'elite: [years] ', 'yelping_since', 'fans', 'average_stars',147# 'votes: ['funny', 'userful', 'cool'] ',148# 'compliments: ['profile', 'cute', 'funny', 'plain', writer',149# 'note', 'photos', 'hot', 'cool', 'more'] ' ]150 cursor.execute('INSERT INTO users VALUES (%s, %s, %s, %s, %s);', \151 (user_id, name, rv_cnt, friends, data) )152 return153def create_user_reviews_table(cursor):154 # create new table 'user_reviews' that contains ONLY restaurant reviews155 cursor.execute(''' SELECT r.user_id AS user_id,156 r.biz_id AS business_id,157 r.stars AS stars158 INTO user_reviews159 FROM reviews AS r160 JOIN businesses AS b161 ON r.biz_id = b.biz_id162 WHERE (b.categories @> '["Restaurants"]'::jsonb);163 ''')164 # cursor.execute(''' SELECT r.user_id AS user_id,165 # r.biz_id AS business_id,166 # b.name AS business_name,167 # r.stars AS stars,168 # concat(b.data->>'city',', ', b.data->>'state') AS locale169 # INTO user_reviews170 # FROM reviews AS r171 # JOIN businesses AS b172 # ON r.biz_id = b.biz_id173 # WHERE (b.categories @> '["Restaurants"]'::jsonb);174 # ''')175 # save user_reviews table to file176 fout = open('temp_user_reviews.csv', 'w')177 cursor.copy_expert('COPY user_reviews TO STDOUT WITH CSV HEADER', fout)178 fout.close()179 # save selected business data to file (for item_data in recommender use)180 # fout = open('temp_biz_data.csv', 'w')181 # cursor.copy_expert('''COPY (SELECT biz_id, name, categories, attributes, 182 # FROM businesses183 # ) TO STDOUT WITH CSV HEADER''', fout)184 # fout.close()185 return186# This is the standard boilerplate that calls the main() function.187if __name__ == '__main__':188 ''' '''189 parser = argparse.ArgumentParser(190 description="Load Yelp json data files into a new Postgres SQL database named 'temp'.",)191 parser.add_argument('json_path', type = str, help = 'Path to json data files.')192 args = parser.parse_args()193 json_path = args.json_path...
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!!