def WordCount2(): from pycompss.api.api import compss_wait_on ''' WordCount Test - Wordcount task receives a PSCO and returns a dictionary. - Reduce task receives a INOUT PSCO (result) where accumulates the partial results. ''' words = [ Words('This is a test'), Words('This is a test'), Words('This is a test'), Words('This is a test') ] for w in words: w.makePersistent() result = Result() result.makePersistent() localResults = [] for w in words: partialResults = wordcountTask(w) reduceTaskPSCOs(result, partialResults) final = compss_wait_on(result) print(final.myd) result = final.get() if result['This'] == 4 and result['is'] == 4 and result[ 'a'] == 4 and result['test'] == 4: print("- Python Wordcount 2 with PSCOs: OK") return True else: print("- Python Wordcount 2 with PSCOs: ERROR") return False
def txn(fit): result = Result.get_by_key_name('best') if result is None: result = Result(key_name='best', fitness=fit) elif result.fitness > fit: result.fitness = fit result.put()
def exists(cls,iid,table): if table == "match": try: Match.get(iid=iid) except Match.DoesNotExist: return False if table == "odd": try: Odd.get(iid=iid) except Odd.DoesNotExist: return False if table == "cat": try: OddCategory.get(idd=iid) except OddCategory.DoesNotExist: return False if table == "result": try: Result.get(iid=iid) except Result.DoesNotExist: return False if table == "user": try: User.get(iid=iid) except User.DoesNotExist: return False return True
def save(request): results = simplejson.loads(request.POST['results']) session_key = request.session.session_key #request.COOKIES[settings.SESSION_COOKIE_NAME] session = Session(key=session_key, global_time=time()) session.save() # Convert user results to Result table row db_results = [] for result in results: db_result = Result( session = session, time = result['time'], selection = result['selection'], ) db_result.image_id = result['id'] db_results.append(db_result) try: # This could be done better with a transaction for db_result in db_results: db_result.save() except Exception as e: print e pass return HttpResponseRedirect('/static/thankyou.html')
def process_result(self, message: Message, user: User, bot: TeleBot, competitor: Competitor, final=False): opponent, opponent_user = get_opponent_and_opponent_user(competitor) if not opponent or not opponent_user: return teardown_challenge( competitor, message, user, bot, 'challenge_confirm_cannot_find_opponent_msg' if not opponent else 'challenge_confirm_cannot_fin_opponents_user_msg') res = user.check_result() if not res: res = Result(player_a=competitor, player_b=opponent, confirmged=False) res.save() user.current_result = res user.save() text = render_result(res, final) bot.send_message( message.chat.id, text, reply_markup=self.__base_keyboard(confirmation_stage=final), parse_mode='html') return RET.OK, None, None, None
def save(request): results = simplejson.loads(request.POST['results']) session_key = request.session.session_key #request.COOKIES[settings.SESSION_COOKIE_NAME] session = Session(key=session_key, global_time=time()) session.save() # Convert user results to Result table row db_results = [] for result in results: db_result = Result( session=session, time=result['time'], selection=result['selection'], ) db_result.image_id = result['id'] db_results.append(db_result) try: # This could be done better with a transaction for db_result in db_results: db_result.save() except Exception as e: print e pass return HttpResponseRedirect('/static/thankyou.html')
def exists(cls, iid, table): if table == "match": try: Match.get(iid=iid) except Match.DoesNotExist: return False if table == "odd": try: Odd.get(iid=iid) except Odd.DoesNotExist: return False if table == "cat": try: OddCategory.get(idd=iid) except OddCategory.DoesNotExist: return False if table == "result": try: Result.get(iid=iid) except Result.DoesNotExist: return False if table == "user": try: User.get(iid=iid) except User.DoesNotExist: return False return True
def send_request(r_type, task_id, method, url, body=None, headers=None): print("Send request") if not headers: headers = {'Content-Type': 'application/json'} if method == "GET": response = requests.get(url, headers=headers) elif method == "POST": response = requests.post(url, body, headers=headers) if r_type == "sync": data = response.json() # Get latest result latest_result = get_latest_result_for_task(task_id) # Create new Result new_result = Result(data=json.dumps(data)) task = Task.objects.get(id=task_id) new_result.task = task new_result.save() # Update Task with Result task.results.append(new_result) task.save() diff_results(new_result.data, latest_result.data, task.notification_type, task.notification_args) elif r_type == "poll": pass return
def db_put(url, results=None, errors=None): db_errs= [] # save the results try: from models import Result if errors: result = Result( url=url, datetime=f"{datetime.utcnow()} (UTC)", errors=errors ) else: result = Result( url=url, datetime=f"{datetime.utcnow()} (UTC)", results=results ) db.session.add(result) db.session.commit() return result.id except Exception as err: db_errs.append( f"Unable to add item to database:{str(err)}" ) return {"errors": db_errs}
def processEnterMarksForm(request, enterMarksForm, gamename): groupid = extractGroupId(gamename) groupExamIds = examIDsinGroup(gamename) userResults = Result.objects.filter(user=request.user.id, exam_id__in=groupExamIds) examname = enterMarksForm.data['exam'] enteredMark = enterMarksForm.data['mark'] if str(examname) == "": return "The Exam field in Enter Marks Form cannot be left empty!" elif str(enteredMark) == "": return "The Mark field in Enter Marks Form cannot be left empty!" try: int(enteredMark) except: return "Your mark must be a whole number from 0 to 100." examid = extractExamIDgivenGroup(examname, gamename) alreadyEnteredExams = [] for result in userResults.values(): alreadyEnteredExams.append(result['exam_id']) if examid in alreadyEnteredExams: return "You have already entered your mark for this exam!" exam = Exam.objects.get(pk=examid) if not (int(enteredMark) >= 0 and int(enteredMark) <= 100): return "The entered mark is invalid! It must be a whole number between 0 and 100." else: newResult = Result(exam=exam, user=request.user, mark=enteredMark) newResult.save() calculateWinner(request.user.id, examid, enteredMark, gamename) return False
def results_upload(request): try: if request.method == "POST": login = request.POST.get('login', '') test_id = request.POST.get('test-id', '') points = request.POST.get('points', '') points_percentage = request.POST.get('points-percentage', '') test = get_object_or_404(Test, id_unq=test_id) if test.password: # results with test with password can be submited only once try: Result.objects.get(user_id_unq=login, test_id_unq=test_id) return HttpResponse("ERROR:Test already submited") except Result.DoesNotExist: pass result = Result(user_id_unq=login, test_id_unq=test_id, points=points, points_percentage=points_percentage) result.save() try: testuser = TestUser.objects.get(user_id_unq=login, test_id_unq=test_id) testuser.test_returned() except TestUser.DoesNotExist: pass results_xml = request.FILES["results_xml"] default_storage.save("results_files/%s/%s"%(result.id, results_xml.name), results_xml) return HttpResponse("OK") else: return HttpResponse("ERROR:Use POST") except: # print 'exception' # print sys.exc_info() # raise return HttpResponse("ERROR: Unhandled exception, contact administrator")
def syncResult(self): self.connection.request("GET", "/sync/result") response = self.connection.getresponse() data = json.loads(response.read()) print response.read() if not data.has_key('up-to-date'): for result in data['name']: odd = Odd.get(iid=result['odd']) Result.create(iid=result['iid'], odd=odd)
def syncResult(self): self.connection.request("GET", "/sync/result") response = self.connection.getresponse() data = json.loads(response.read()) print response.read() if not data.has_key('up-to-date'): for result in data['name']: odd = Odd.get(iid=result['odd']) Result.create(iid=result['iid'],odd=odd)
def save(request): if request.method == 'POST': #Create new Result object and populate it with the data deck_id = request.POST['deck_id'] score = float(request.POST['score']) res = Result(score=score, deck_id=deck_id) #Save it res.save() #Schedule next date #... return HttpResponse('Result saved.')
def classify(self, content): base64_image = content['picture'] identifier = 'not_defined' if content['partId'] == 1 else content[ 'partId'] model = content['model'] save = content['save'] user = content['user'] device = content['device'] image = data_uri_to_cv2_img(base64_image) image_path = '' file_name = '' tf = TFModel(model) prediction = tf.predict(image) h, w = image.shape[:2] if save: image_path = get_picture_path(self.image_save_path, model, identifier) file_name = image_path.split('/')[-1] image[int( h * 0.90 ):, :, :] = 0 # Creates a black stripe at the bottom of the image font = cv2.FONT_HERSHEY_SIMPLEX color = (0, 217, 217) cv2.putText(image, f'{prediction["label"]}', (int(w * 0.01), int(h * 0.95)), font, w / 1000, color, 1) cv2.putText(image, f'{ file_name }', (int(w * 0.01), int(h * 0.99)), font, w / 1000, color, 1) cv2.imwrite(image_path, image) result = Result(user=user, device=device, instance=model, path=image_path, label=prediction['label'], confidence=round(prediction['confidence'], 2)) result.save() return jsonify({ "error": False, "message": "OK", "content": { "label": prediction['label'], "confidence": (round(prediction['confidence'], 2)), "imagePath": image_path, "identifier": identifier } })
def do_train(request, network_id, inputs_dataset_id, targets_dataset_id, batch_size, epochs): network = Network.objects.get(pk=network_id) if request.user == network.user : result = Result() result.action = 'training' result.updated_at = timezone.now() result.save() t = threading.Thread(target=train, args=(network_id, result.id, inputs_dataset_id, targets_dataset_id, int(batch_size), int(epochs))) t.setDaemon(True) t.start() return redirect('/dnn/result/' + str(result.id)) else : raise PermissionDenied
def do_predict(request, network_id, inputs_dataset_id): network = Network.objects.get(pk=network_id) if request.user == network.user : result = Result() result.action = 'prediction' result.updated_at = timezone.now() result.save() t = threading.Thread(target=predict, args=(network_id, result.id, inputs_dataset_id)) t.setDaemon(True) t.start() return redirect('/dnn/result/' + str(result.id)) else : raise PermissionDenied
def setUp(self): self.app = tested_app.test_client() self.main = tested_app.app_context() with tested_app.app_context(): db.init_app(tested_app) # removes cyclic dependency?? db.create_all() db.session.add(Result(year=1994, type="birth", event="Alice")) db.session.add(Result(year=1995, type="death", event="Bob")) db.session.add( Result(year=1996, month=1, type="birth", event="John")) db.session.add( Result(year=1997, month=2, day=1, type="birth", event="Ann")) db.session.commit()
def scraper(): print('About to start the scraper.') base_rows = get_rows(base_scraper_url) base_for_today = base_rows[0] base_datacells = base_for_today.find_all('td') today_url_cell = base_datacells[0].find('a') base_dict['url'] = unescape(base_url + today_url_cell.attrs['href']) base_dict['date'] = parse(today_url_cell.text) todays_rows = get_rows(base_dict.get('url')) for row in todays_rows: datacells = row.find_all('td') doc = { 'url': base_dict.get('url'), 'date': base_dict.get('date'), 'source': datacells[1].text, 'agency': datacells[2].text, 'fsg': datacells[3].text, 'title': datacells[4].text, 'keywords': datacells[5].text, 'url_2': unescape(base_url + datacells[4].find('a').attrs.get('href')), } article = get_article(doc.get('url_2')) if article: try: doc['description'] = get_body(article) result = Result(**doc) db.session.add(result) db.session.commit() except Exception as e: result = Result(**doc) db.session.add(result) db.session.commit() print('Scraper completed execution') return article
def do_body(self, args): """Render the HTML for all the results, and provide enough information to edit an game""" data = { 'url_args': args, } year = self.get_year(args) if year == ALL_YEARS: data['rounds'] = Round.gql('ORDER BY date ASC') else: data['rounds'] = Round.gql('WHERE year = :1 ORDER BY date ASC', year) tpath = os.path.join(DeeWhyPage.TEMPLATE_DIR, 'rounds.html') self.response.out.write(template.render(tpath, data)) data = { 'url_args': args, } # Get the full data for the given round so they can edit it if args.has_key('round'): round_num = args['round'] round_key = Round.get_key(round_num, year) result_key = Result.get_key(round_num, year) if round_key != '': curr_round = Round.get_by_key_name(round_key) if curr_round: data['players'] = [ p.player for p in TeamList.gql('WHERE year = :1', curr_round.date.year) ] data['players'].sort(self.sort_players) data['round'] = curr_round curr_result = Result.get_by_key_name(result_key) if curr_result: data['result'] = curr_result if curr_result.other_goals == 0: curr_result.other_goals = None if curr_result.own_goals == 0: curr_result.own_goals = None #if curr_result.deewhy_forfeit: # curr_result.deewhy_goals = 0 # curr_result.opponent_goals = 5 #elif curr_result.opponent_forfeit: # curr_result.deewhy_goals = 5 # curr_result.opponent_goals = 0 tpath = os.path.join(DeeWhyPage.TEMPLATE_DIR, 'manage_results.html') self.response.out.write(template.render(tpath, data))
def index(_logged_user): user_key = _logged_user.key query = Result.query(Result.user == user_key) results = query.fetch() resp = "---" query = Autor.query() autores = query.fetch() game_keys = [autor.destination for autor in autores] games = ndb.get_multi(game_keys) resps = [] for game in games: for result in results: if game.key == result.game: if result.won_medal is True: resp = "medal" else: resp = "%d / %d" % (result.best_points, result.size) resps.append(resp) resp = "---" jogo_lista = [] for game, result in zip(games, resps): game_dict = game.to_dict() game_dict['result'] = result jogo_lista.append(game_dict) return TemplateResponse({"games": jogo_lista}, 'jogos/home.html')
def index(): errors = [] results = {} if request.method == "POST": # get url that the user has entered try: h = request.form['height'] w = request.form['weight'] height = float(h) weight = float(w) print(str(height) + " " + str(weight)) except: errors.append( "Unable to get Weight & Height. Please make sure it's valid and try again." ) return render_template('index.html', errors=errors) if height and weight: bmi = round((weight / (height * height)) * 703) print "BMI: " + str(bmi) # save the results results = bmi try: from models import Result result = Result(height=height, weight=weight, bmi=bmi) db.session.add(result) db.session.commit() except: errors.append("Unable to add item to database.") return render_template('index.html', errors=errors, results=results)
def loader(start, stop): """ডাটা লোড করার মেইন ফাংশন। start থেকে stop প্যারামিটারের সকল রোল সেভ করবে (start এবং stop রোল সহ।) এবং লগ ইন্টারেক্টিভ শেলে প্রিন্ট করবে, কোন কারনে ছয়বার এরর হলে বা ডাটা লোড করতে না পারলে লুপ থেমে যাবে। ইনপুটঃ শুরু এবং শেষের রোল নং ইন্টেজার। আউটপুটঃ নান""" error_count = 0 for roll in range(start, stop+1): if error_count > 5: break try: payload['Roll'] = roll r = requests.post(result_url,payload,headers=headers) r.encoding = 'utf-8' result = beautify(r) data_row = Result(roll, result) session.add(data_row) session.commit() print('roll {} added to database'.format(roll)) except: error_count +=1 print('one error occured') continue if error_count > 4: print( "data loading abort due to 5 error!!") else: print("Data loaded succesfully")
def __get_row_data(element): columns = element.find_elements_by_xpath("td") return Result(name=columns[0].text, price=int(columns[1].text.replace(",", "")), quantity=columns[2].text, s_name=columns[3].text, where=columns[4].text)
def index(): form = LivechatForm() if request.method == 'POST' and form.validate(): message = form.Message.data gender = form.Gender.data Text_analyser = TextAnalyser(message) cl, pos, neg = Text_analyser.opinion() entity = Text_analyser.entity('templates/entity.html') result = Result(message=message, gender=gender, cl=cl, pos=pos, neg=neg) db.session.add(result) db.session.commit() else: message = None gender = None cl = None pos = None neg = None return render_template('index.html', form=form, message=message, gender=gender, cl=cl, pos=pos, neg=neg)
def index(): errors = [] results = {} r = None # prevents uninitialization error, which happens on Heroku but not my laptop if request.method == 'POST': # get the URL entered try: url = request.form['url'] r = requests.get(url) except: errors.append("Unable to get URL - try again") if r is not None: (raw_counts, stop_removed_count) = count_words_from_html(r) # package results for web display results = sorted(stop_removed_count.items(), key=operator.itemgetter(1), reverse=True)[:10] # store results in the database try: db_result = Result( url=url, result_all=raw_counts, result_no_stop_words=stop_removed_count ) db.session.add(db_result) db.session.commit() except Exception as e: err = "Unable to add results to the database: %s" % e errors.append(err) return render_template('index.html', errors=errors, results=results)
def cnnFetch(): script = Scraper().scrape('cnn') result = Result(source="cnn", date=datetime.date.today(), titles=" ".join(script)) db.session.add(result) db.session.commit()
def index(): errors = [] results = {} if request.method == 'POST': try: url = request.form['url'] r = requests.get(url) except: errors.append('Unable to get URL') return render_template('index.html', errors=errors) if r: raw = BeautifulSoup(r.text, 'html.parser').get_text() nltk.data.path.append('./nltk/data') tokens = nltk.word_tokenize(raw) text = nltk.Text(tokens) nonPunct = re.compile('.*[A-za-z]*.') raw_words = [w for w in text if nonPunct.match(w)] raw_words_count = Counter(raw_words) no_stop_words = [w for w in raw_words if w.lower() not in stops] no_stop_words_count = Counter(no_stop_words) results = sorted(no_stop_words_count.items(), key=operator.itemgetter(1), reverse=True) try: result = Result(url=url, result_all=raw_words_count, results_no_stop_words=no_stop_words_count) db.session.add(result) db.session.commit() except: errors.append('Unable to add item to database') return render_template('index.html', errors=errors, results=results)
def generate_feed_for_query(query): global db logger.debug("generate_feed_for_query()") results = db.get_top_results_for_query(query.id) fg = FeedGenerator() fg.id(f"{settings.BASE_URL}/results/{query.id}") fg.title(f"Results for {query.search}") fg.author({"name": "Reef", "email": "*****@*****.**"}) fg.description("A list of latest results for a search") fg.link(href=settings.BASE_URL) fg.language("en") for result_raw in results: result = Result(json=result_raw) logger.debug( f"adding entry for {result.id}: {result.title}: {result.content}") fe = fg.add_entry() fe.id(f"{settings.BASE_URL}/results/{query.id}/{result.id}") fe.title(result.title) fe.link(href=result.content) if settings.FEED_FORMAT == "rss": return fg.rss_str(pretty=True) # else... return fg.atom_str(pretty=True)
def index(): errors = [] results = {} if request.method == "POST": try: url = request.form['url'] r = requests.get(url) print(r.text) except: errors.append("유효하지 않는 url. 다시 확인부탁") render_template('index.html', errors=errors) if r: print(r) raw = BeautifulSoup(r.text, 'html.parser').get_text() nltk.data.path.append('./nltk_data/') tokens = nltk.word_tokenize(raw) text = nltk.Text(tokens) nonPunct = re.compile('.*[A-Za-z].*') raw_words = [w for w in text if nonPunct.match(w)] raw_word_count = Counter(raw_words) no_stop_words = [w for w in raw_words if w.lower() not in stops] no_stop_words_count = Counter(no_stop_words) results = sorted(no_stop_words_count.items(), key=operator.itemgetter(1), reverse=True) try: result = Result(url=url, result_all=raw_word_count, result_no_stop_words=no_stop_words_count) db.session.add(result) db.session.commit() except: errors.append("db에 데이터를 추가할 수 없음") return render_template('index.html', errors=errors, results=results)
def add_result(): event = request.data['event'] first_name = request.data['first_name'] first_institution = request.data['first_institution'] second_name = request.data['second_name'] second_institution = request.data['second_institution'] third_name = request.data['third_name'] third_institution = request.data['third_institution'] print('event:' + event + 'firstname:' + first_name + 'first_institution:' + first_institution + '-----------------------------------------------') if not session.query(Event).filter_by(id=event).first(): session.close() return {'status': 'BAD REQUEST', 'message': 'EVENT DOES NOT EXIST'} info = Result(event_id=event, first_name=first_name, first_institution=first_institution, \ second_name=second_name, second_institution=second_institution, third_name=third_name, \ third_institution=third_institution) session.add(info) try: session.commit() except: session.rollback() flash(config.UNEXPECTED_ERROR) finally: session.close() return redirect('/dashboard')
def count_and_save_words(url): """Count the times a word shows up and save them.""" errors = [] try: r = requests.get(url) except Exception: errors.append( 'Unable to get URL. Please make sure it is valid and try again.') return {"error": errors} raw = BeautifulSoup(r.text).get_text() nltk.data.path.append('./nltk_data/') # set the path tokens = nltk.word_tokenize(raw) text = nltk.Text(tokens) # remove punctuation, count raw words non_punct = re.compile('.*[A-Za-z].*') raw_words = [w for w in text if non_punct.match(w)] raw_word_count = Counter(raw_words) # stop words no_stop_words = [w for w in raw_words if w.lower() not in stops] no_stop_words_count = Counter(no_stop_words) # save the results try: result = Result(url=url, result_all=raw_word_count, result_no_stop_words=no_stop_words_count) db.session.add(result) db.session.commit() return result.id except Exception: errors.append('Unable to add item to database.') return {"error": errors}
def process(self): raw = BeautifulSoup(self.r.text, 'html.parser').get_text() nltk.data.path.append('./nltk_data/') # set the path tokens = nltk.word_tokenize(raw) text = nltk.Text(tokens) # remove punctuation, count raw words nonPunct = re.compile('.*[A-Za-z].*') raw_words = [w for w in text if nonPunct.match(w)] raw_word_count = Counter(raw_words) #stop words non_stop_words = [w for w in raw_words if w.lower() not in stops] non_stop_words_count = Counter(non_stop_words) self.results = sorted(non_stop_words_count.items(), key=operator.itemgetter(1), reverse=True) # add [:10] to limit to first 10 try: result = Result(url=self.url, result_all=raw_word_count, result_no_stop_words=non_stop_words_count) db.session.add(result) db.session.commit() self.job_id = result.id except Exception as error: print("Unable to add item to database.", error) self.errors.append("Unable to add item to database.") return {"errors": self.errors, "job_id": self.job_id}
def __createDbEntries(self, task, results): """ Create the result and cluster objects :param task: :param results: :return: """ result = Result() result.task = task result.statistics = {} result.save() self.__buildClusters(task, results, result) return result
def get_results(job_key): errors = [] job = Job.fetch(job_key, connection=conn) if job.is_finished: # save the results url, raw_word_count, no_stop_words_count = job.result try: result_record = Result( url=url, result_all=raw_word_count, result_no_stop_words=no_stop_words_count ) db.session.add(result_record) db.session.commit() # return result.id except: errors.append("Unable to add item to database.") return {"error": errors} result = Result.query.filter_by(id=result_record.id).first() results = sorted( result.result_no_stop_words.items(), key=operator.itemgetter(1), reverse=True )[:10] return jsonify(results) else: return "Nay!", 202
def count_and_save_words(url): errors = [] try: r = requests.get(url) except: errors.append( "Unable to get URL. Please make sure it's valid and try again.") raw = BeautifulSoup(r.text, 'html.parser').get_text() nltk.data.path.append('./nltk_data/') tokens = nltk.word_tokenize(raw) text = nltk.Text(tokens) non_punct = re.compile('.*[A-Za-z].*') raw_words = [w for w in text if non_punct.match(w)] raw_words_count = Counter(raw_words) no_stop_words = [w for w in raw_words if w.lower() not in stops] no_stop_words_count = Counter(no_stop_words) try: result = Result(url=url, result_all=raw_words_count, result_no_stop_words=no_stop_words_count) db.session.add(result) db.session.commit() return result.id except: errors.append('Unable to add item to database.') return {"error": errors}
def post_result(db: SessionClass, room_id, user_id, user_hand): with session_manager(db) as session: if session.query(Room).filter(Room.id == room_id).count() == 0: raise HTTPException(status_code=404) room = session.query(Room).filter(Room.id == room_id).first() if user_id == room.host_user_id: result = Result(room_id=room.id, user_id=user_id, stage=room.latest_stage, hand=user_hand) session.add(result) client_users_result = session.query(Result).filter( Result.room_id == room_id, Result.stage == room.latest_stage).all() for client_user_result in client_users_result: if (client_user_result.hand - user_hand) % 3 == 1: client_user_result.is_win = True else: client_user_result.is_win = False session.commit() else: host_results = session.query(Result).filter( Result.user_id == room.host_user_id, Result.room_id == room.id, Result.stage == room.latest_stage) if host_results.count() == 0: result = Result(room_id=room.id, user_id=user_id, stage=room.latest_stage, hand=user_hand) session.add(result) else: host_result = host_results.first() if (user_hand - host_result.hand) % 3 == 1: result = Result(room_id=room.id, user_id=user_id, is_win=True, stage=room.latest_stage, hand=user_hand) session.add(result) else: result = Result(room_id=room.id, user_id=user_id, is_win=False, stage=room.latest_stage, hand=user_hand) session.add(result) return result
def aljFetch(): script = Scraper().scrape('alj') result = Result(source="alj", date=datetime.date.today(), titles=" ".join(script)) db.session.add(result) db.session.commit() return "202"
def nytFetch(): script = Scraper().scrape('nyt') result = Result(source="nyt", date=datetime.date.today(), titles=" ".join(script)) print(script) db.session.add(result) db.session.commit()
def rtl(request): try: input_text = request.POST['input_text'] try: output_text = CSSRtlParser(input_text).parse() result = Result.create(input_text, output_text, True) result.save() return redirect('/result/%s/' % result.pk) # TODO arikg: handle smaller exception except Exception: result = Result.create(input_text, Exception.message, False) result.save() return redirect('/result/%s/' % result.pk) except KeyError: return render(request, 'main/index.html', { 'error_message': "Please fill in the text to rtl", })
def gradeResult(request): render = {} grade = get_object_or_404(Grade, id = request.POST.get('grade')) resultdate = get_object_or_404(ResultDate, id = request.POST.get('resultdate')) render['grade'] = grade.id render['resultdate'] = resultdate.id if grade in request.user.grades.all(): if not request.POST.get('send'): pupils = Pupil.objects.filter(grade = grade) results = [] from math import pow for pupil in pupils: try: result = Result.objects.get(resultdate = resultdate, pupil = pupil, subject = request.user.current_subject) form = ResultForm(prefix = pupil.id, instance = result) except ObjectDoesNotExist: form = ResultForm(prefix = pupil.id) sum = 0 marks = Mark.objects.filter(lesson__date__range = (resultdate.startdate, resultdate.enddate), pupil = pupil, lesson__subject = request.user.current_subject) for mark in marks: if mark.mark: sum += mark.mark if marks.__len__()<>0 and sum<>0: sa = round(float(sum)/float(marks.__len__()), 3) else: sa = 0 results.append({'name': pupil.fi(), 'form': form, 'sa': sa}) render['pupils'] = results return render_to_response('marks/teacher/gradeResult.html', render, context_instance = RequestContext(request)) else: error = 0 for pupil in Pupil.objects.filter(grade = grade): if request.POST.get('%d-mark' % pupil.id): form = ResultForm(request.POST, prefix = pupil.id) if form.is_valid(): try: result = Result.objects.get(pupil = Pupil.objects.get(id = pupil.id), resultdate = resultdate, subject = request.user.current_subject) except ObjectDoesNotExist: result = Result() result.pupil = pupil result.resultdate = resultdate result.mark = form.cleaned_data['mark'] result.subject = request.user.current_subject result.save() else: error = 1 if error == 0: return HttpResponseRedirect('/marks/result/') else: results = [] pupils = Pupil.objects.filter(grade = grade) for pupil in pupils: results.append({'name': pupil.fi(), 'form': ResultForm(request.POST, prefix = pupil.id)}) render['pupils'] = results return render_to_response('marks/teacher/gradeResult.html', render, context_instance = RequestContext(request)) else: return Http404
def questionnaire(request, contestant): characters = People.objects.filter(~Q(name='Nobody')) cont = get_object_or_404(Contestant, name=contestant) if request.method == 'POST': for person in characters: x = TestQuestion(request.POST, prefix=person) if x.is_valid(): test = Result() test.name = cont test.character = person test.killed_by = x.cleaned_data['killed_by'] test.is_survivor = x.cleaned_data['is_survivor'] test.save() print person return HttpResponseRedirect('/done') else: forms = {} for person in characters: x = TestQuestion(initial={'character':person, 'name':cont}, prefix=person) forms[person] = x return render_to_response('brbappl/questions.html', {'forms': forms}, context_instance=RequestContext(request) )
def runMatch(red, blue): (redUser, redPlayer) = red (blueUser, bluePlayer) = blue rname = redUser + '/' + redPlayer bname = blueUser + '/' + bluePlayer logger.info('playing %s vs. %s' % (rname, bname)) makeTraceDir() command = (runtemplate % (ICYPC_JAR, script(redUser, redPlayer), script(blueUser, bluePlayer), TRACES_DIR)).split() logger.info('running: "%s"' % command) p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (output, stderr) = p.communicate(); m = output_pattern.match(output) (winner, loser) = (red, blue) # default if m: logger.info('winner is %s' % m.group(1)) challengerState = 'loses' if m.group(1) == '1': challengerState = 'wins' (winner, loser) = (blue, red) # challenger won log = 'challenger (%s) %s: %s [%s (%s %s)] vs. %s [%s (%s %s)]' % (bname, challengerState, rname, m.group(2), m.group(3), m.group(4), bname, m.group(5), m.group(6), m.group(7)) r = Result(time=datetime.now(), winnerUser=winner[0], winnerPlayer=winner[1], loserUser=loser[0], loserPlayer=loser[1], output=log) r.save() umask(002) # ensure world can read these files rename(TRACES_DIR + '/trace.txt', TRACES_DIR + '/%s.txt' % r.pk) try: f = open('%s/%s-stdout.txt' % (TRACES_DIR, r.pk), 'w'); f.write(output); f.close() f = open('%s/%s-stderr.txt' % (TRACES_DIR, r.pk), 'w'); f.write(stderr); f.close() except: logger.critical('could not write std out/err files in %s--tell a friend' % TRACES_DIR) else: logger.error('no winner found in stdout; probably a script was not executable; stdout and stderr follow') logger.error('===== begin stdout =====\n%s\n===== end stdout' % output) logger.error('===== begin stderr =====\n%s\n===== end stderr' % stderr) return (winner, loser)
def test_update_result(self): result = Result.change_result_attrs(points=1, won_medal=True, game_title=self.game.tit, game=self.game, user_key=self.user.key, duration=0.20) result.put() self.assertEquals(result.best_points, 1) self.assertEquals(result.best_duration, 0.20) self.assertEquals(result.best_date, date.today()) self.assertEquals(result.frequency, 1) result = Result.change_result_attrs(points=2, duration=0.10, won_medal=True, game_title=self.game.tit, game=self.game, user_key=self.user.key) self.assertEquals(result.best_points, 2) self.assertEquals(result.best_duration, 0.10) self.assertEquals(result.best_date, date.today()) self.assertEquals(result.frequency, 2)
def analise(game_id): game = Game.get_by_id(long(game_id)) query = Result.query(Result.game == game.key).order(-Result.best_points).order(-Result.won_medal) results_lista = query.fetch() results = [] for result in results_lista: user = Node.get_by_id(long(result.user.id())) result_dict = result.to_dict() result_dict['user_name'] = user.name results.append(result_dict) return TemplateResponse({"results": results, "game_id": game_id}, template_path="gerenciar/analise.html")
def main(args): """ Runs the pipeline on a given input/output pair and dataset. @params args.dataset: subfolder of data/ args.name: name of the image to process, input and output are stored under data/args.dataset/args.name """ # Setup params params = Parameters() processor = Processor(params) # I/O paths outputDir = os.path.join(OUTPUT_DIR, args.dataset) inputDir = os.path.join(DATA_DIR, args.dataset) # Result struct r = Result() r.dataset = args.dataset r.name = args.name r.dataPath = os.path.join(inputDir,args.name) r.outputPath = os.path.join(outputDir,args.name) r.error = "" # Run r = processor.process(r) print "---------------------------" print "* Processed image %s/%s" % (args.dataset, args.name) print " - time\t%.2f s." % r.computation_time print " - PSNR:\t %5.2f dB" % r.psnr print " - input:\t %5.2f %%" % (r.compression_up*100) print " - output:\t %5.2f %%" % (r.compression_down*100)
def create_game_result(): result = Result.query.filter_by(id=request.form['id']).first() if not result: if request.form['finished'] == '0': result = Result( input_phrase='', user_id=request.form['user_id'], initiating=False, game_id=request.form['game_id'], accuracy=0, finished=False, wpm=0, duration=0, created_at=datetime.datetime.now()) else: result = Result( input_phrase=request.form['input_phrase'], user_id=request.form['user_id'], initiating=True, game_id=request.form['game_id'], accuracy=request.form['accuracy'], finished=True, wpm=request.form['wpm'], duration=request.form['duration'], created_at=datetime.datetime.now()) increment_games_played(request.form['user_id']) db.session.add(result) else: result.finished = True result.input_phrase = request.form['input_phrase'] result.accuracy=request.form['accuracy'] result.wpm=request.form['wpm'] result.duration=request.form['duration'] if not result.initiating: if result.finished: increment_games_played(request.form['user_id']) game = Game.query.filter_by(id=result.game_id).first() game.active = False game.responded_to = datetime.datetime.now() db.session.commit() data = { 'id' : result.id, 'status' : 'success' } resp = jsonify(data) resp.status_code = 200 return resp
def test_create_new_result(self): result = Result.change_result_attrs(points=1, won_medal=True, game_title=self.game.tit, game=self.game, user_key=self.user.key, duration=0.20) result.put() self.assertEquals(result.last_points, 1) self.assertEquals(result.game_title, self.game.tit) self.assertEquals(result.won_medal, True) self.assertEquals(result.game, self.game.key) self.assertEquals(result.last_duration, 0.20) self.assertEquals(result.best_duration, 0.20)
def update_results(session, results): for game in results: try: result = session.query(Result).filter_by( matchday=game.split("_")[0], home_team=game.split("_")[1], away_team=game.split("_")[2] ).first() if not result: result = Result( matchday=game.split("_")[0], home_team=game.split("_")[1], home_score=results[game]["home_score"], away_team=game.split("_")[2], away_score=results[game]["away_score"] ) else: result.home_score = results[game]["home_score"] result.away_score = results[game]["away_score"] session.add(result) session.commit() except: session.rollback()
def get_quremo(a, b): """Memcache or DataStore interaction implementation with aim to avoid server calculations overload""" a_key = a + '&' + b # an unique key for each pair # looking for MemCache value firstly: cached_result = memcache.get(key=a_key) if cached_result is None: # looking for persistent cached value: q = Result.query(Result.a_key == a_key) if q.get(): # the values are there calc_val = tuple(q.fetch(1)[-1].a_value) memcache.add(key=a_key, value=calc_val, time=60) logging.info("Data was restored out from ndb") else: # values are completely new calc_val = quremo(a, b) memcache.add(key=a_key, value=calc_val, time=60) R = Result() R.a_key, R.a_value = a_key, calc_val R.put() logging.info("Data is new and was cached successfully") else: calc_val = cached_result logging.info("Data was retrieved out from MemCache") return calc_val
def submit(request): if not 'id' in request.POST: return HttpResponse('{"Error": "No ID supplied."}', content_type='application/json', status=400) if not 'result_data' in request.POST: return HttpResponse('{"Error": "No result data supplied."}', content_type='application/json', status=400) try: subject = Subject.objects.filter(subject_id=request.POST['id']).all()[0] except IndexError: return HttpResponse('{"Error": "Unknown id."}', content_type='application/json') result_data = json.loads(request.POST['result_data']) try: task = Task.objects.filter(pk=result_data['task_id']).all()[0] except IndexError: return HttpResponse('{"Error": "Unknown task."}', content_type='application/json') result = Result(subject=subject, task=task) result.first_card_flipped = result_data['1'] result.second_card_flipped = result_data['2'] result.third_card_flipped = result_data['3'] result.fourth_card_flipped = result_data['4'] result.time = result_data['time'] result.save() if subject.results.count() >= min(AbstractTask.objects.count(), ConcreteTask.objects.count()): return HttpResponse('{"Finished": "Tasks completed."}', content_type='application/json') if subject.is_abstract_group(): task = AbstractTask.objects.all()[subject.results.count()] else: task = ConcreteTask.objects.all()[subject.results.count()] return HttpResponse(json.dumps({ 'task_id': task.pk, 'description': task.description, 1: { 'card_id': task.first_card }, 2: { 'card_id': task.second_card }, 3: { 'card_id': task.third_card }, 4: { 'card_id': task.fourth_card }, 'story': task.story, 'task_number': min(AbstractTask.objects.count(), ConcreteTask.objects.count()), 'progress': subject.results.count() }), content_type='application/json')
def execute_tester(submission): base_dir = os.path.dirname(__file__) s_cmd = submission.command s_id = submission.id (s_path, s_pkg_name) = os.path.split(submission.package.path) # Move to the submission directory os.chdir(s_path) s_split = s_cmd.split() if len(s_split) == 1: # This is run.sh or run.py so add exec permissions os.chmod( os.path.join(s_path, s_split[0]), stat.S_IEXEC | stat.S_IREAD ) for test in TEST_FILE_NAMES: map_path = os.path.join( base_dir, '../static/maps', test) result_file_name = os.path.join( s_path, test + '_' + RESULT_JSON_FILE_NAME) if not os.path.exists(result_file_name): open(result_file_name, 'w').close() cmd = [ 'python2.7', base_dir + '/../bin/simulator/main.py', '-c', '--map', map_path, '--robot', s_cmd, '--output', result_file_name ] proc = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = proc.communicate() # Log testing process here print s_id, 'stderr', stderr # Log result lines = stdout.splitlines() result = json.load(open(result_file_name)) db_result = Result() db_result.submission_id = s_id db_result.report = json.dumps(result) print s_id, 'report', db_result.report db_result.log = stdout + stderr db_result.save() print '\n\nResult saved.\n\n'
def get(self): """ Gets 50 last results. """ config = Config.get_master() # First, check memcache results = memcache.get('results') if not results: results_raw = Result.query().order(-Result.when).fetch(50) results = [r.to_dict() for r in results_raw] memcache.add('results', results, time=1800) # next_refresh: seconds to next refresh elapsed = datetime.now() - config.last_refresh next_refresh = REFRESH_MINUTES * 60 - elapsed.total_seconds() if not self.authenticated_user: # Hash all nicks for not logged in users for result in results: author = hashlib.sha1(result['author']).hexdigest() result['author'] = author[:10] self.respond_json({ 'results': results, 'next_refresh': int(next_refresh) })
def get_results(self): """ Function: results ----------------- Get the results of a query. """ result = Result() # Get the query results and schema. rows = [row for row in self.cursor] if len(rows) > 0: result.results = rows result.schema = self.get_schema() result.col_names = self.get_column_names() result.col_types = self.get_column_types() # Pretty-printed output. result.output = prettyprint(result.results, self.get_column_names()) return result
def results(request, result_id=None): if result_id: result = get_object_or_404(Result, pk=result_id) # XXX: remove this when it works result.process() result.save() else: action = request.POST.get('action') if action == 'text': # process text form = ValidateTextForm(request.POST) if form.is_valid(): result = Result(input=form.cleaned_data['text']) try: result.process() except LapinError, exception: return render_to_response('validator/error.html', locals(), request) result.save() return HttpResponseRedirect(result.get_absolute_url()) else: return HttpResponseRedirect('/') else:
def match(request): if request.method == "POST": form = QueryForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] interest = form.cleaned_data['interest'] affiliation = form.cleaned_data['affiliation'] try: result = Result.objects.get(stuname=name, stuaffiliation=affiliation, stuinterest=interest) except Result.DoesNotExist: lucene.getVMEnv().attachCurrentThread() student = {} student['name'] = name student['interest'] =interest student['affiliation'] = affiliation algo_id = 1 prof_matcher = matcher() if random.randint(1,1000) & 1: boost = {'interest':1.0, 'processed_aff':2.0} else: boost = {'interest':2.0, 'processed_aff':1.0} algo_id = 2 try: prof_result = prof_matcher.getProfMatch(student, fieldList = ["interest","processed_aff"], boosts=boost) except: messages.error(request, "Sorry, but I can't recognize your query.") return render_to_response('index.html', {'form': form}, context_instance= RequestContext(request)) if not prof_result: messages.error(request, "Can't found " "enough experts to show to you, please adjust " "the query.") return render_to_response('index.html', {'form': form}, context_instance= RequestContext(request)) prof_list = [] for result in prof_result: name = result['name'] interest = result['interest'] print name, interest professor = Professor.objects.get(name__icontains=name.split(' ')[0], interest=interest) prof_list.append(professor.id) print prof_list result = Result(stuinterest=student['interest'], stuname=student['name'], stuaffiliation= student['affiliation'], date=datetime.now(), pos1id=prof_list[0], pos2id=prof_list[1], pos3id=prof_list[2], algoid=Algo.objects.get(pk=algo_id)) result.save() request.session['result_id'] = result.id return HttpResponseRedirect(reverse('kdd_matcher:results')) else: return render_to_response('index.html', {'form': form}, context_instance =RequestContext(request))
def __init__(self): OddCategory.create_table(fail_silently=True) Match.create_table(fail_silently=True) Ticket.create_table(fail_silently=True) Odd.create_table(fail_silently=True) Result.create_table(fail_silently=True)
#also try cursor.callproc(procname, args) for less hardcoding cursor.execute(sql,SearchTerm_id) except MySQLdb.Error, e: print "Error %d: %s" % (e.args[0], e.args[1]) db.commit() # retrieve data from temp table into object # assume re-ranking already done in SQL, so just need to take top 10 rows sql = """SELECT * from t_scoring ORDER BY score desc, status_date desc LIMIT 0,10""" try: cursor.execute(sql) except MySQLdb.Error, e: print "Error %d: %s" % (e.args[0], e.args[1]) r = cursor.fetchall() retval = {} for i in range(cursor.rowcount): retval[i] = Result() retval[i].statusid = r[i][0] retval[i].score = r[i][1] retval[i].text = r[i][2] retval[i].geo_lat = r[i][3] retval[i].geo_lon = r[i][4] retval[i].from_user = r[i][5] retval[i].profile_image_url = r[i][6] retval[i].created_at = r[i][7] retval[i].SearchTermID = r[i][8] # sql = """DROP TABLE IF EXISTS t_scoring""" # cursor.execute(sql) cursor.close() db.close()
def result(request): if request.is_ajax(): if request.method == 'GET': id = request.GET.get('id') idc = request.GET.get('idc') client = request.GET.get('client') tgt_type = request.GET.get('tgt_type') tgt = request.GET.get('tgt','') fun = request.GET.get('fun') arg = request.GET.get('arg','') #user = request.user.username if id: r=Result.objects.get(id=id) result = json.loads(r.result) #result.html默认从数据库中读取 #return JsonResponse(result,safe=False) return HttpResponse(json.dumps(result), content_type="application/json") try: node_slt = node.objects.get(id=int(idc)) #根据机房ID选择对应salt服务端 token_api_id = token_id(node_slt.salt_user, node_slt.salt_passwd, node_slt.salt_url) #sapi = SaltAPI(url=salt_server.url,username=salt_server.username,password=salt_server.password) if re.search('runner',client) or re.search('wheel',client): list = salt_api_token( {'client': client, 'fun': fun, 'arg': arg, 'timeout': 100}, node_slt.salt_url, {"X-Auth-Token": token_api_id}) master_status = list.run() #result=sapi.SaltRun(client=client,fun=fun,arg=arg) else: list = salt_api_token( {'client': client, 'fun': fun, 'tgt':tgt, 'arg': arg, 'expr_form':tgt_type, 'timeout': 100 }, node_slt.salt_url, {"X-Auth-Token": token_api_id}) result = list.CmdRun() #result = sapi.SaltCmd(client=client,tgt=tgt,fun=fun,arg=arg,expr_form=tgt_type) if re.search('async',client): jid = result['return'][0]['jid'] # minions = ','.join(result['return'][0]['minions']) r=Result(client=client,jid=jid,minions=tgt,fun=fun,arg=arg,tgt_type=tgt_type,idc_id=int(idc)) r.save() res=r.jid #异步命令只返回JID,之后JS会调用jid_info else: try: res=result['return'][0]#同步命令直接返回结果 r=Result(client=client,minions=tgt,fun=fun,arg=arg,tgt_type=tgt_type,idc_id=int(idc),result=json.dumps(res)) r.save() # res=model_to_dict(r,exclude='result') #return JsonResponse(res,safe=False) except Exception as error: return HttpResponse(json.dumps(res), content_type="application/json") return HttpResponse(json.dumps(res), content_type="application/json") except Exception as error: #return JsonResponse({'Error':"%s"%error},safe=False) return HttpResponse(json.dumps(error), content_type="application/json") else: idc_list= node.objects.all() result_list = Result.objects.order_by('-id') return render(request, 'SALT/result.html', locals())