Best Python code snippet using lemoncheesecake
find_errors.py
Source:find_errors.py
1#!/usr/bin/env python2# -*- coding: utf-8 -*-3"""4Find raw_datasets which are not accepted by the administrator and look5different than other known datasets with the same accepted_formula_id.6(So: Find outliers)7"""8import sys9import logging10logging.basicConfig(level=logging.INFO,11 stream=sys.stdout,12 format='%(asctime)s %(levelname)s: %(message)s')13logger = logging.getLogger(__name__)14# Database stuff15import pymysql16import pymysql.cursors17# Other18import webbrowser19# My modules20from hwrt.handwritten_data import HandwrittenData21from distance_metric import handwritten_data_greedy_matching_distance as dtw22from hwrt import preprocessing23from hwrt import utils24def update_data(cfg, a, unaccept=False):25 mysql = cfg['mysql_online']26 connection_local = pymysql.connect(host=mysql['host'],27 user=mysql['user'],28 passwd=mysql['passwd'],29 db=mysql['db'],30 cursorclass=pymysql.cursors.DictCursor)31 cursor_local = connection_local.cursor()32 if unaccept:33 sql = ("UPDATE `wm_raw_draw_data` "34 "SET `administrator_edit` = now(), "35 "`missing_line` = %i, "36 "`has_hook` = %i, "37 "`has_too_long_line` = %i, "38 "`is_image` = %i, "39 "`other_problem` = %i, "40 "`has_interrupted_line` = %i, "41 "`accepted_formula_id` = NULL "42 "WHERE `wm_raw_draw_data`.`id` =%i "43 "LIMIT 1;") % \44 (int(a.missing_line),45 int(a.has_hook),46 int(a.has_too_long_line),47 int(a.is_image),48 int(a.other_problem),49 int(a.has_interrupted_line),50 a.raw_data_id)51 elif a.istrash:52 sql = ("UPDATE `wm_raw_draw_data` "53 "SET `administrator_edit` = now(), "54 "`missing_line` = %i, "55 "`has_hook` = %i, "56 "`has_too_long_line` = %i, "57 "`is_image` = %i, "58 "`other_problem` = %i, "59 "`has_interrupted_line` = %i, "60 "`accepted_formula_id` = 1 "61 "WHERE `wm_raw_draw_data`.`id` =%i "62 "LIMIT 1;") % \63 (int(a.missing_line),64 int(a.has_hook),65 int(a.has_too_long_line),66 int(a.is_image),67 int(a.other_problem),68 int(a.has_interrupted_line),69 a.raw_data_id)70 else:71 sql = ("UPDATE `wm_raw_draw_data` "72 "SET `administrator_edit` = now(), "73 "`missing_line` = %i, "74 "`has_hook` = %i, "75 "`has_too_long_line` = %i, "76 "`is_image` = %i, "77 "`other_problem` = %i, "78 "`has_interrupted_line` = %i "79 "WHERE `wm_raw_draw_data`.`id` =%i "80 "LIMIT 1;") % \81 (int(a.missing_line),82 int(a.has_hook),83 int(a.has_too_long_line),84 int(a.is_image),85 int(a.other_problem),86 int(a.has_interrupted_line),87 a.raw_data_id)88 cursor_local.execute(sql)89 connection_local.commit()90 cursor_local.close()91 connection_local.close()92 connection_online = pymysql.connect(host=mysql['host'],93 user=mysql['user'],94 passwd=mysql['passwd'],95 db=mysql['db'],96 cursorclass=pymysql.cursors.DictCursor)97 cursor_online = connection_online.cursor()98 cursor_online.execute(sql)99 connection_online.commit()100 cursor_online.close()101 connection_online.close()102class HandwrittenDataM(HandwrittenData):103 def __init__(self,104 raw_data_json,105 formula_id,106 wild_point_count,107 missing_line,108 has_hook,109 has_too_long_line,110 is_image,111 other_problem,112 has_interrupted_line,113 raw_data_id,114 latex):115 HandwrittenData.__init__(self, raw_data_json, formula_id)116 self.wild_point_count = wild_point_count117 self.missing_line = missing_line118 self.has_hook = has_hook119 self.has_too_long_line = has_too_long_line120 self.is_image = is_image121 self.other_problem = other_problem122 self.has_interrupted_line = has_interrupted_line123 self.unaccept = False124 self.ok = False125 self.raw_data_id = raw_data_id126 self.latex = latex127 self.istrash = False128 def show(self):129 import matplotlib.pyplot as plt130 from matplotlib.widgets import CheckButtons131 from matplotlib.widgets import Button132 fig, ax = plt.subplots()133 ax.set_title('Raw data id: %i, LaTeX: %s' % (self.raw_data_id,134 self.latex))135 plt.subplots_adjust(bottom=0.3)136 for line in self.get_pointlist():137 xs, ys = [], []138 for p in line:139 xs.append(p['x'])140 ys.append(p['y'])141 ax.plot(xs, ys, '-o')142 ax.axis((0, 1, 0, 1))143 plt.gca().invert_yaxis()144 ax.set_aspect('equal')145 def handle_checkboxes(label):146 if label == 'other_problem':147 self.other_problem = not self.other_problem148 elif label == 'missing_line':149 self.missing_line = not self.missing_line150 elif label == 'has_hook':151 self.has_hook = not self.has_hook152 elif label == 'has_too_long_line':153 self.has_too_long_line = not self.has_too_long_line154 elif label == 'is_image':155 self.is_image = not self.is_image156 elif label == 'has_interrupted_line':157 self.has_interrupted_line = not self.has_interrupted_line158 plt.draw()159 checkpos = plt.axes([0.01, 0.05, 0.45, 0.2])160 check = CheckButtons(checkpos,161 ('other_problem',162 'has_interrupted_line',163 'missing_line',164 'has_hook',165 'has_too_long_line',166 'is_image'),167 (self.other_problem,168 self.has_interrupted_line,169 self.missing_line,170 self.has_hook,171 self.has_too_long_line,172 self.is_image))173 check.on_clicked(handle_checkboxes)174 def ok_function(a):175 self.ok = True176 plt.close()177 okpos = plt.axes([0.7, 0.05, 0.1, 0.075])178 ok_button = Button(okpos, 'OK')179 ok_button.on_clicked(ok_function)180 def unaccept_function(a):181 self.unaccept = True182 logger.info("unaccept raw_data_id %i" % self.raw_data_id)183 plt.close()184 unacceptpos = plt.axes([0.81, 0.05, 0.1, 0.075])185 unaccept_button = Button(unacceptpos, 'unaccept')186 unaccept_button.on_clicked(unaccept_function)187 def is_trash_function(a):188 a.istrash = True189 logger.info("trash raw_data_id %i" % self.raw_data_id)190 plt.close()191 is_trash_pos = plt.axes([0.7, 0.2, 0.1, 0.075])192 is_trash_button = Button(is_trash_pos, 'is_trash')193 is_trash_button.on_clicked(is_trash_function)194 # maximise height195 mng = plt.get_current_fig_manager()196 width, height = mng.window.maxsize()197 mng.resize(500, height)198 plt.axis('equal')199 plt.show()200def main(cfg, raw_data_start_id):201 mysql = cfg['mysql_online']202 connection = pymysql.connect(host=mysql['host'],203 user=mysql['user'],204 passwd=mysql['passwd'],205 db=mysql['db'],206 cursorclass=pymysql.cursors.DictCursor)207 cursor = connection.cursor()208 # Get formulas209 logger.info("Get formulas")210 print("get formulas")211 sql = ("SELECT `id`, `formula_in_latex` FROM `wm_formula` "212 "WHERE `id` > %s ORDER BY `id`")213 cursor.execute(sql, (raw_data_start_id, ))214 formulas = cursor.fetchall()215 formulaid2latex = {}216 for el in formulas:217 formulaid2latex[el['id']] = el['formula_in_latex']218 preprocessing_queue = [preprocessing.ScaleAndShift(),219 # preprocessing.Douglas_peucker(EPSILON=0.2),220 # preprocessing.Space_evenly(number=100,221 # kind='cubic')222 ]223 checked_formulas = 0224 checked_raw_data_instances = 0225 for formula_id in formulaid2latex.keys():226 alread_shown_in_browser = False227 if formula_id == 1:228 # This formula id is for trash. No need to look at it.229 continue230 # Get data231 logger.info("Get data for formula_id %i (%s)" %232 (formula_id, formulaid2latex[formula_id]))233 sql = ("SELECT `id`, `data`, `accepted_formula_id`, "234 "`wild_point_count`, `missing_line`, `has_hook`, "235 "`has_too_long_line`, `is_image`, `administrator_edit`, "236 "`other_problem`, `has_interrupted_line` "237 "FROM `wm_raw_draw_data` "238 "WHERE `accepted_formula_id` = %i "239 "ORDER BY `administrator_edit` DESC, "240 "`creation_date` ASC;") % formula_id241 cursor.execute(sql)242 raw_datasets = cursor.fetchall()243 logger.info("Raw datasets: %i" % len(raw_datasets))244 checked_raw_data_instances += len(raw_datasets)245 checked_formulas += 1246 if len(raw_datasets) < 100:247 continue248 As = []249 for i, data in enumerate(raw_datasets):250 if data['data'] == "[]":251 continue252 B = HandwrittenDataM(data['data'],253 data['accepted_formula_id'],254 data['wild_point_count'],255 data['missing_line'],256 data['has_hook'],257 data['has_too_long_line'],258 data['is_image'],259 data['other_problem'],260 data['has_interrupted_line'],261 data['id'],262 formulaid2latex[formula_id])263 B.preprocessing(preprocessing_queue)264 B_pll = B.get_pointlist()265 distance = float('inf')266 for A_pll in As:267 distance = min(distance, dtw(A_pll, B_pll))268 if distance > 100:269 if data['administrator_edit'] is not None:270 As.append(B.get_pointlist())271 else:272 if not alread_shown_in_browser:273 alread_shown_in_browser = True274 webbrowser.open("http://www.martin-thoma.de/"275 "write-math/view/?"276 "raw_data_id=%i" % data['id'], 2)277 B.show()278 if B.ok:279 As.append(B.get_pointlist())280 update_data(cfg, B)281 else:282 update_data(cfg, B, True)283 logger.info("[Status] Checked formulas: %i of %i" %284 (checked_formulas, len(formulaid2latex)))285 logger.info("[Status] Checked raw_data_instances: %i" %286 checked_raw_data_instances)287 logger.info("done")288if __name__ == '__main__':289 from argparse import ArgumentParser290 parser = ArgumentParser(description=__doc__)291 # Add more options if you like292 parser.add_argument("-i", dest="i",293 help="at which raw_data_id should it start?",294 metavar="RAW_DATA_ID")295 args = parser.parse_args()296 cfg = utils.get_database_configuration()...
app.py
Source:app.py
...73 """74 if not self.has_controller(name):75 raise KeyError("No controller registered under: {}".format(name))76 self.controllers.pop(name)77 def has_hook(self, name):78 return name in self._hooks.keys()79 def create_hook(self, name):80 """Create a hook to which functions can be registered"""81 if self.has_hook(name):82 raise KeyError("Hook %s already exists" % name)83 self._hooks[name] = []84 def register_hook(self, name, f):85 """Register a function to a hook86 - Use lambda to register a parametrized function87 - Functions should return a boolean to report their success88 """89 if not self.has_hook(name):90 self.create_hook(name)91 self._hooks[name].append(f)92 return self93 def remove_hook(self, name, f):94 """Remove the function registration from a hook"""95 if self.has_hook(name):96 hooks = self._hooks[name]97 if f in hooks:98 hooks.remove(f)99 return self100 def empty_hook(self, name):101 """Remove all function registrations from a hook"""102 if self.has_hook(name):103 self._hooks[name] = []104 return self105 def process_hook(self, name):106 """Run all functions registered to the hook107 :return: Whether all functions ran successful108 :rtype: bool109 """110 hooks = []111 if self.has_hook(name):112 hooks = self._hooks[name]113 result = True114 for f in hooks:115 result = f() and result116 return result117 def prepare(self):118 self._prepare()119 self._configured = True120 return self121 def _prepare(self):122 """Configures the application and loads at least one controller"""123 raise NotImplementedError124 def run(self):125 """Start the application"""...
course.py
Source:course.py
1"""2Runs checks for whether a course is setup properly in github.3Checks branchs, repo, hooks, team, etc4"""5from will.plugin import WillPlugin6from will.decorators import respond_to7from plugins.git.base import GithubBaseMixIn8class CoursePlugin(WillPlugin, GithubBaseMixIn):9 DEPLOYMENT_TEAMS = ['mitx-content-deployment', 'mitx-studio-export', ]10 STAGING_HOST = 'gr-rp'11 PROD_HOST = 'prod-gr-rp'12 INATOR_HOST = 'gr-inator'13 def check_hook(self, use_ghe, owner, repo, host):14 """15 Checks that the git hook is in place for the16 hostname specified and is the form type17 """18 has_hook = False19 correct_type = False20 base_dns = '.mitx.mit.edu'21 hooks, err = self.get_all(22 use_ghe,23 'repos/{}/{}/hooks'.format(owner, repo)24 )25 if not hooks:26 return (False, False)27 for hook in hooks:28 url = hook['config'].get('url', None)29 if url:30 if '{0}{1}'.format(host, base_dns) in url:31 has_hook = True32 if 'form' == hook['config']['content_type'].lower():33 correct_type = True34 break35 return (has_hook, correct_type)36 def add_check_row(self, is_good, item):37 """38 Makes a tr with a checkmark or x mark39 """40 if is_good:41 status = '✓'42 else:43 status = '✘'44 return '<tr><td>{0}</td><td>{1}</td></tr>'.format(status, item)45 @respond_to('(github)? course check for '46 '(?P<owner>[\w\-_\d]+)/(?P<repo>[\d\w\-_]+)')47 def course_check(self, message, owner, repo):48 """49 github: github course check for <owner>/<repo>50 """51 # Find the repo first52 url = 'repos/{0}/{1}'.format(owner, repo)53 is_ghe = True54 repo_dict, err = self.get_all(True, url)55 if not repo_dict:56 repo_dict, err = self.get_all(False, url)57 is_ghe = False58 if not repo_dict:59 self.reply(60 message,61 "All my searching for nothing, that repo doesn't exist for me."62 )63 return64 # Make start of reply table65 table = [('<table><thead><tr>'66 '<td>Status</td>'67 '<td>Item</td>'68 '</tr></thead><tbody>'), ]69 # Check privacy70 table.append(self.add_check_row(71 repo_dict['private'], 'Repo is private'72 ))73 # Find out if it is studio74 if 'studio' in url.lower():75 table.append(self.add_check_row(76 True, 'Studio course and has github repo'77 ))78 else:79 table.append(self.add_check_row(80 True, 'XML based course'81 ))82 # Check that it has live branch83 branch, err = self.get_all(84 is_ghe,85 'repos/{0}/{1}/branches/live'.format(owner, repo)86 )87 has_live = False88 if branch:89 has_live = True90 table.append(self.add_check_row(has_live, 'Has live branch'))91 # Check hook to staging92 has_hook, type_check = self.check_hook(93 is_ghe, owner, repo, self.STAGING_HOST94 )95 table.append(self.add_check_row(96 has_hook, 'Has hook to staging.mitx.mit.edu'97 ))98 table.append(self.add_check_row(99 type_check, 'Correct hook type (form) for staging'100 ))101 # Check hook to inator102 has_hook, type_check = self.check_hook(103 is_ghe, owner, repo, self.INATOR_HOST104 )105 table.append(self.add_check_row(106 has_hook, 'Has hook to my beloved lms-inator.mitx.mit.edu'107 ))108 table.append(self.add_check_row(109 type_check, 'Correct hook type (form) for inator'110 ))111 # All repos need to have hook to prod112 has_hook, type_check = self.check_hook(113 is_ghe, owner, repo, self.PROD_HOST114 )115 table.append(self.add_check_row(116 has_hook, 'Has hook to lms.mitx.mit.edu'117 ))118 table.append(self.add_check_row(119 type_check, 'Correct hook type (form) for lms'120 ))121 # Get the teams to check for deploy122 team_list, err = self.get_all(123 is_ghe,124 'repos/{0}/{1}/teams'.format(owner, repo)125 )126 has_deploy_team = False127 html_team_list = ['<ul>', ]128 if team_list:129 for team in team_list:130 html_team_list.append('<li>{}</li>'.format(team['name']))131 if team['name'].lower() in self.DEPLOYMENT_TEAMS:132 has_deploy_team = True133 html_team_list.append('</ul>')134 table.append(self.add_check_row(135 has_deploy_team,136 'Has deployment team'137 ))138 table.append('</tbody></table>')139 self.reply(140 message,141 ('<b>{0}-inator checklist:</b><br />{1}<br />'142 '<b>Teams:</b><br />{2}').format(143 repo,144 ''.join(table),145 ''.join(html_team_list)146 ),147 html=True...
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!!