def search(): if request.method == 'POST': Product_Name = request.form['Product_Name'] print 'productname', Product_Name result = run_query(Product_Name) print result #return render_template('message.html') #return render_template('index.html') return Response(result)
def main(): print(run_query("{ hello }")) query_string = '{ hello(name: "GraphQL") }' print(run_query(query_string)) query_string = '{ menu(numMenus: 2) { recipes(numRecipes: 2){ recipeName ingredients {name quantity} }}}' print(run_query(query_string))
def generate_table(): dbvalue = __BLK_GBB_DB__.format(product_choice) if not dut_choice: return [{}] if not die_choice: return [{}] testtype = str(".*," + test_choice + ",.*") duttype = str(".*," + dut_choice + ",.*") dietype = str(".*," + die_choice.split(',')[0].split('X')[1] + "," + die_choice.split(',')[1].split('Y')[1] + ",.*") query = { "$and": [ { "_id": { "$regex": testtype } }, { "_id": { "$regex": duttype } }, { "_id": { "$regex": dietype } }, ] } query_result = mongo_query.run_query(dbvalue, qual_choice, query) return tuple( dict(zip(row['_id_name'].split(','), row['_id'].split(','))) for row in query_result)
def update_dropdown(product_choice, qual_choice, test_choice, dut_choice, manual_refresh, interval_refresh): try: query = { "$and": [{ "_id": { "$regex": test_choice } }, { "_id": { "$regex": dut_choice } }] } dbvalue = __DIE_DB__.format(product_choice) query_result = mongo_query.run_query(dbvalue, qual_choice, query) for each in query_result: each['_id_pair'] = dict( zip(each['_id_name'].split(','), each['_id'].split(','))) output = set( str('X') + (x['_id_pair']['x']) + str(',Y') + (x['_id_pair']['y']) for x in query_result) return [{'label': i, 'value': i} for i in output] except Exception as e: return []
def update_dropdown(product_choice, qual_choice, test_choice, dut_choice, manual_refresh, interval_refresh): try: # print('dut_choice',dut_choice) # client = MongoClient(host='10.196.155.85', port=27017) query = { "$and": [{ "_id": { "$regex": test_choice } }, { "_id": { "$regex": dut_choice } }] } dbvalue = __DIE_DB__.format(product_choice) query_result = mongo_query.run_query(dbvalue, qual_choice, query) for each in query_result: each['_id_pair'] = dict( zip(each['_id_name'].split(','), each['_id'].split(','))) # print(BLKList.head()) #import pdb; pdb.set_trace() # print(lstDD) output = set( str('X') + (x['_id_pair']['x']) + str(',Y') + (x['_id_pair']['y']) for x in query_result) # print('OUTPUT',output) return [{'label': i, 'value': i} for i in output] except Exception as e: return []
def listtype(request, status, datatype): graph = 'http://%s/%s' % (status.lower(), datatype) qstr = ''' SELECT DISTINCT ?subject WHERE { GRAPH <%s> { ?subject ?p ?o . } FILTER( ?p != mos:header ) } ORDER BY ?subject ''' % graph results = query.run_query(qstr) type_resultsd = count_by_group(get_counts_by_graph(datatype), split_by_type) itemlist = [] for item in [x.get('subject') for x in results]: itemlist.append({ 'url' : url_with_querystring( reverse('edit', kwargs={'status' : status, 'datatype' : datatype}), ref=item), 'label' : item, }) return render_to_response('main.html', RequestContext(request, { 'title' : 'Listing %s' % status.upper(), 'viewname' : 'Listing', 'status' : 'Status: %s' % status.upper(), 'detail' : 'Datatype: %s' % datatype, 'itemlist' : itemlist, 'read_only' : READ_ONLY, 'count' : 'Records: %s' % type_resultsd.get(split_by_datatype(datatype)), 'newshard' : reverse('newshard', kwargs={'status' : status, 'datatype' : datatype}), }) )
def list(request, status): reportq = ''' SELECT DISTINCT ?g WHERE { GRAPH ?g { ?s ?p ?o } . FILTER( REGEX(str(?g), 'http://%s/') ) . } ''' % (status.lower(), ) results = query.run_query(reportq) itemlist = [] count_results = get_counts_by_graph('http://%s/' % status.lower()) status_resultsd = count_by_group(count_results, split_by_status) for item in results: url = reverse('listtype', kwargs={ 'status' : status, 'datatype' : split_by_localname(item) }) itemlist.append({ 'url' : url, 'label' : '%s' % item.get('g'), 'count' : count_by_group(count_results, lambda x:x.get('g')).get(item.get('g')), }) return render_to_response('lists.html', RequestContext(request, { 'title' : status.upper(), 'viewname' : 'List', 'status' : 'status: %s' % status.upper(), 'itemlist' : sorted(itemlist, key=lambda x:x['label']), 'read_only' : READ_ONLY, 'count' : 'Records: %s' % status_resultsd.get(status), }) )
def addSeason(filename): print("Opening file " + filename) tree = ET.parse('data/' + filename) root = tree.getroot() season = filename.split(".")[0] for child in root: if child.tag != "event_date": continue event_date = child.attrib['date'] event_date = "{}-{}-{}".format(int(event_date[:4]), int(event_date[4:6]), int(event_date[6:8])) for grandchild in child: gc = grandchild.attrib # print(gc['vn'] + " vs " + gc['hn']) if gc['vc'] == "nd": # print("ND is the visitor for " + gc['event_name']) score1 = int(gc['vs']) if gc['vs'] else None score2 = int(gc['hs']) if gc['hs'] else None home = False opponent = gc['hn'] # print("Opponent is " + opponent) else: # print("ND is at home for " + gc['event_name']) score1 = int(gc['hs']) if gc['hs'] else None score2 = int(gc['vs']) if gc['vs'] else None home = True opponent = gc['vn'] # print("Opponent is " + opponent) #if score1 and score2: if score1 != None and score2 != None: if score1 > score2: result = "WIN" elif score1 == score2: result = "TIE" elif score1 < score2: result = "LOSS" else: result = None else: result = None print("home={} opponent={} score1={} score2={} result={} date={} seasonId={}".format(home, opponent, score1, score2, result, date, season_map[season])) variables = { "home": home, "opponent": opponent, "score1": score1, "score2": score2, "result": result, "date": event_date, "seasonId": season_map[season], "seasonYear": int(season) } import json print(run_query(mutation, json.dumps(variables)))
def query_statistic(query, dbvalue, colvalue): # split into two functions query_result = mongo_query.run_query(dbvalue, colvalue, query) #print(query_result) for each in query_result: each['_id_pair'] = dict(zip(each['_id_name'].split(','), each['_id'].split(','))) #each['_id'] = each['_id_pair'] return query_result
def query_histogram(query, dbvalue, colvalue): # split into two functions query_result = mongo_query.run_query(dbvalue, colvalue, query) #print(query_result) for each in query_result: each['_id_pair'] = dict(zip(each['_id_name'].split(','), each['_id'].split(','))) #each['_id'] = each['_id_pair'] return tuple(dict( value=tuple(zip(*sorted((int(x), int(y)) for x, y in record.items() if x not in ('_id', '_id_name', '_id_pair', '_id_type')))), _id_pair=record['_id_pair'])for record in query_result)
def get_counts_by_graph(graphurl=''): qstr = ''' SELECT ?g (COUNT(DISTINCT ?s) as ?count) WHERE { GRAPH ?g { ?s ?p ?o } . FILTER( REGEX(str(?g), '%s') ) . } GROUP by ?g ORDER by ?g ''' % graphurl results = query.run_query(qstr) return results
def test_select_w_filter_w_limit(): data = [ {"title": "a", "ratings": 5}, {"title": "c", "ratings": 1}, {"title": "b", "ratings": 2}, ] query = [["SCAN", ["movies"]], ["FILTER", ["ratings", "GT", "1"]], ["LIMIT", [1]]] returned_items = run_query(query, data) assert returned_items == [ {"title": "a", "ratings": 5}, ]
def test_select_w_limit_less_items(): data = [ {"title": "a", "genres": "cartoon"}, ] query = [ ["SCAN", ["movies"]], ["LIMIT", [2]], ] returned_items = run_query(query, data) assert returned_items == [ {"title": "a", "genres": "cartoon"}, ]
def test_sort(): data = [ {"title": "c", "ratings": 1}, {"title": "b", "ratings": 2}, {"title": "a", "ratings": 5}, ] query = [ ["SCAN", ["movies"]], ["SORT", [["ratings", "DESC"]]], ["PROJECTION", ["title"]], ] returned_items = run_query(query, data) assert returned_items == [{"title": "a"}, {"title": "b"}, {"title": "c"}]
def test_projection_w_float(): data = [ {"title": "a", "ratings": 5}, {"title": "c", "ratings": 1}, {"title": "b", "ratings": 2.5}, ] query = [ ["SCAN", ["movies"]], ["FILTER", ["ratings", "GT", "1.4"]], ["PROJECTION", ["title"]], ] returned_items = run_query(query, data) assert returned_items == [ {"title": "a"}, {"title": "b"}, ]
def update_dropdown(product_choice, qual_choice, test_choice, manual_refresh, interval_refresh): try: # client = MongoClient(host='10.196.155.85', port=27017) query = {"_id": {"$regex": test_choice}} dbvalue = __DUT_DB__.format(product_choice) query_result = mongo_query.run_query(dbvalue, qual_choice, query) for each in query_result: each['_id_pair'] = dict( zip(each['_id_name'].split(','), each['_id'].split(','))) output = set(x['_id_pair']['dut'] #+ str('-') #+ x.split(',')[5] + str(',') + x['_id_pair']['pcname'] for x in query_result) return [{'label': i, 'value': i} for i in output] except: return []
def get_shard(shard, status, datatype): qstr = ''' SELECT DISTINCT ?cfname ?unit ?canon_unit WHERE { { # drawing upon stash2cf.ttl as linkage <%s> cf:units ?unit ; cf:name ?cfname . #(metExtra:hasVersion|metExtra:hasPreviousVersion) ?ver . # drawing upon cf-standard-name.ttl as endpoint ?cfname cf:canonical_units ?canon_unit . } UNION { # drawing upon stash2cf.ttl as linkage <%s> a mon:none . #(metExtra:hasVersion|metExtra:hasPreviousVersion) ?ver . BIND( URI(mon:none) as ?cfname ) . } } ''' % (shard, shard) results = query.run_query(qstr) return results
for grandchild in child: gc = grandchild.attrib if gc['vc'] == "nd": opponent = gc['hn'] else: opponent = gc['vn'] if opponent: teams.add(opponent) return teams if __name__ == '__main__': teams = set() if len(sys.argv) > 1: y = sys.argv[1] s = y.split(",") for i in s: teams.update(addTeam(i.strip()+".xml")) else: for filename in sorted(os.listdir("data/")): teams.update(addTeam(filename)) for team in sorted(teams): variables = { "name": team } print(run_query(mutation, json.dumps(variables)))
import xml.etree.ElementTree as ET from query import run_query import os, sys from datetime import date # The GraphQL query (with a few aditional bits included) itself defined as a multi-line string. query = """ mutation createSeason($year: Int!, $games: [SeasongamesGame!]!) { createSeason( year: $year games: $games ) { id } } """ for year in range(1887,2018): variables = { "year": year, "games": [] } import json print(run_query(query, json.dumps(variables)))
def run_crawler(url, inst, user, pwd): # Initialize Webdriver and Input Login Information print("Initializing crawler...") browser = webdriver.PhantomJS() browser.get(url) institution = Select(browser.find_element_by_name("inst")) username = browser.find_element_by_name("instkey") password = browser.find_element_by_name("perskey") submit = browser.find_element_by_xpath("//input[@type='button']") sleep(randint(2, 10)) institution.select_by_value(str(inst)) username.send_keys(str(user)) password.send_keys(str(pwd)) submit.click() print("Login complete.") # Administration Page sleep(randint(2, 10)) administration = browser.find_element_by_xpath( "//input[@value='Administration']") administration.click() print("Step 1 complete.") # Next Page sleep(randint(2, 10)) database = browser.find_element_by_xpath( "//input[@value = 'Database Search']") database.click() print("Step 2 complete.") # Database Page sleep(randint(2, 10)) search = browser.find_element_by_name("sf_aq") submit_query = browser.find_element_by_xpath( "//input[@value='Submit Search']") search.send_keys(query.run_query(query.query_dict)) submit_query.click() print("Input complete.") # Get Xpath get_xpath = browser.page_source get_xpath = BeautifulSoup(get_xpath, 'lxml') get_xpath = get_xpath.find_all( "a", attrs={"href": re.compile("javascript:subViewResult")}) xpath_list = [] for item in get_xpath: xpath_list.append(item.get_text()) xpath_no = xpath_list[xpath_list.index('NEXT>') - 1] print("Found Xpath Key.") # Get CT Evaluation show_results = browser.find_element_by_xpath( '//*[@id="pagedone"]/p[2]/table[1]/tbody/tr[3]/td/center/a[' + xpath_no + ']') show_results.click() html = browser.page_source get_data = BeautifulSoup(html, 'lxml') get_data table = get_data.find("div", attrs={'id': 'pagedone'}) table = table.find("tbody") text_list = [] rows = table.find_all('tr') for row in rows: cols = row.find_all('td') cols = [element.text.strip() for element in cols] text_list.append([element for element in cols if element]) # Get rid of empty values text_list = [item for item in text_list if len(item) > 1] text_list = text_list[2:] text_list = [[word.replace('-', '') for word in item] for item in text_list] text_list = [item for item in text_list if len(item) > 2] headers = [ "Study ID", "SR", "Last Name", "First Name", "MRN", "Intake", "IR", "CT Evaluation", "CR" ] df = pd.DataFrame(text_list, columns=headers) df.drop(["SR", "IR", "CR"], axis=1, inplace=True) df[df == ''] = np.NaN df = df.fillna(method='ffill') df["Intake"] = pd.to_datetime(df["Intake"]) df["CT Evaluation"] = pd.to_datetime(df["CT Evaluation"]) df = df.sort_values(by="CT Evaluation", ascending=False) df.to_csv("update.csv") print("CT Evaluations scraped.") # Download Data sleep(randint(2, 10)) download = browser.find_element_by_xpath("//input[@value='Download Data']") download.click() print("Download complete.") # Close Crawler sleep(randint(2, 10)) if os.path.exists(credentials.path_cwd + "/update.csv"): browser.quit() print("Crawling complete.") return
import xml.etree.ElementTree as ET from query import run_query import os, sys from datetime import date query = """ query { allSeasons { year id } } """ seasons = run_query(query)['data']['allSeasons'] season_map = {} for season in seasons: season_map[str(season['year'])] = season['id'] # The GraphQL query (with a few aditional bits included) itself defined as a multi-line string. mutation = """ mutation createGames ( $home: Boolean $opponent: String $score1: Int $score2: Int $result: RESULT $date: DateTime $seasonId: ID $seasonYear: Int ) {