How to use __normalize method in avocado

Best Python code snippet using avocado_python

database_api.py

Source:database_api.py Github

copy

Full Screen

...4from requests.exceptions import ConnectionError, Timeout5import json6import re7import pickle8def __normalize(word):9 word = re.sub(r'[^\w\s]', '_', word).replace(" ", "_").lower()10 return word11def server_check(func):12 def wrapper(*args, **kwargs):13 try:14 rtn = func(*args, **kwargs)15 if isinstance(rtn, list) and len(rtn) == 1 and rtn[0] == "":16 return []17 return rtn18 except ValueError:19 return "500 - Internal Error"20 except ConnectionError:21 return "404 - Server is not reachable."22 return wrapper23@server_check24def testConnection(base_url=server_address):25 """26 :param base_url: String; server address with port number.27 :return: Boolean; True if connection is done, false otherwise.28 """29 try:30 get(base_url, timeout=5)31 return True32 except ConnectionError or Timeout:33 return False34@server_check35def addOrganization(token, base_url=server_address, organization=current_organization):36 """37 :param token: String ;JWT User token, superuser token needed.38 :param base_url: String; server address39 :param organization: String; university name40 :return: [] if organization added, NONE otherwise41 """42 organization = __normalize(organization)43 url = base_url + "/organizations"44 return put(url, data={"data": organization},45 headers={"Authorization": "Bearer %s" % token}).json()46@server_check47def addUser(token, id_, name, surname, username, password, email, department, role="student",48 base_url=server_address, organization=current_organization):49 """50 :param token: String; JWT Token51 :param id_: String; user ID (studentID i.e.)52 :param name: String; user's real name53 :param surname: String; user's surname54 :param username: String; username for signing in.55 :param password: String; initial password56 :param email: String; Email of user57 :param department: String; Department of user58 :param role: String; Role of user, can be student, lecturer, admin, superuser59 :param base_url: String; address of server60 :param organization: String; university name61 :return: [] if successfully added, NONE otherwise or already added.62 """63 url = base_url + "/organizations/%s" % __normalize(organization)64 return put(url,data={65 "ID": id_,66 "Name": name,67 "Surname": surname,68 "Role": role,69 "Username": username,70 "Password": password,71 "Email": email,72 "Department": department73 },74 headers = {"Authorization": "Bearer %s" %token}).json()75@server_check76def signIn(username, password, base_url=server_address, organization=current_organization):77 """78 :param username: String; username for sign in79 :param password: String; password for sign in80 :param base_url: String; address of server81 :param organization: String; university name82 :return: List; [String username, String name, String surname, String user id,83 String role, String email, String department, String university, String JWT token]84 """85 url = base_url + "/organizations/%s/%s" % (__normalize(organization), username)86 rtn = get(url, auth=(username, password)).json()87 return rtn88@server_check89def signOut(token, username, base_url=server_address, organization=current_organization):90 """91 :param token: String; JWT token92 :param username: String; username93 :param base_url: String; Server address94 :param organization: String; university name95 :return: NOT IMPLEMENTED, will Deactivate access token.96 """97 url = base_url + "/organizations/%s/%s/out" % (__normalize(organization), username)98 return put(url, headers = {"Authorization": "Bearer %s" %token}).json()99@server_check100def addCourse(token, course_name, course_code, lecturer_users,101 base_url=server_address, organization=current_organization):102 """103 :param token: String; JWT Token104 :param course_name: String; course name105 :param course_code: String; course code106 :param base_url: String; server address107 :param organization: String; university name108 :param lecturer_users: List of Strings; usernames of lecturers of the course109 :return: Course Added if successful.110 """111 course_code = __normalize(course_code)112 course_name = __normalize(course_name)113 organization = __normalize(organization)114 url = base_url + "/organizations/%s/%s" % (organization, course_code)115 return put(url, data={116 "name": course_name,117 "code": course_code,118 "lecturers": pickle.dumps(lecturer_users)},119 headers = {"Authorization": "Bearer %s" %token}120 ).json()121@server_check122# todo: @fatihgulmez123def addLecturerToCourse():124 pass125@server_check126def getCourse(token, course_code, base_url=server_address, organization=current_organization):127 """128 :param token: String; JWT token129 :param course_code: String; course code130 :param base_url: String; server address131 :param organization: String; organization132 :return: JSON; { "Participants": List of lists of all students' data, "Lecturers": List of Strings of Lecturer full Names.133 "Code": String course code, "ID": Course ID, "Name": Course Name134 """135 course_code = __normalize(course_code)136 organization = __normalize(organization)137 url = base_url + "/organizations/%s/%s/get" % (organization, course_code)138 return get(url, data={139 },140 headers = {"Authorization": "Bearer %s" %token}).json()141# todo: one student registration142@server_check143def registerStudent(token, course_code, is_list, students, username,144 base_url=server_address, organization=current_organization):145 organization = __normalize(organization)146 course_code = __normalize(course_code)147 url = base_url + "/organizations/%s/%s/register/%s" % (organization, course_code, is_list)148 if is_list:149 students = open(students)150 return put(url, files={"liste": students}, data={"username": username},151 headers = {"Authorization": "Bearer %s" %token}).json()152 else:153 return put(url, data={"username": username, "liste": pickle.dumps(students)},154 headers={"Authorization": "Bearer %s" % token}).json()155@server_check156def getCourseStudents(token, course_code, base_url=server_address, organization=current_organization):157 """158 :param token: String; JWT Token159 :param course_code: String; course code160 :param base_url: String; server address161 :param organization: String; university name162 :return: List of Lists of students' name, surname, ID, email.163 """164 course_code =__normalize(course_code)165 organization = __normalize(organization)166 url = base_url + "/organizations/%s/%s/register" % (organization, course_code)167 return get(url,168 headers = {"Authorization": "Bearer %s" %token}).json()169@server_check170def getUserCourses(token, username, base_url=server_address, organization=current_organization):171 """172 :param token: String; JWT Token173 :param username: String; username of request174 :param base_url: String; server address175 :param organization: String; university name176 :return: List of lists of course name and course code.177 """178 organization = __normalize(organization)179 url = base_url + "/organizations/%s/%s/courses/role=lecturer" % (organization, username)180 return get(url,181 headers = {"Authorization": "Bearer %s" %token}).json()182@server_check183def changePassword(token, username, password, new_password, isMail=False,184 base_url=server_address, organization=current_organization):185 """186 :param token: String; JWT token187 :param username: String; username of request188 :param password: String; old password189 :param new_password: String; new password or Email190 :param is_mail: Boolean; True if email change function, False if password change function.191 :param base_url: String; server address192 :param organization: String; university name193 :return: String; Mail Changed or Password Change if successful, Not authorized if password is wrong.194 """195 organization = __normalize(organization)196 url = base_url + "/organizations/%s/%s/edit_password" % (organization, username)197 return put(url, data={198 "Password": password,199 "newPassword": new_password,200 "isMail": isMail201 },202 headers = {"Authorization": "Bearer %s" %token}).json()203@server_check204def deleteStudentFromLecture(token, course_code, student_id,205 base_url=server_address, organization=current_organization):206 """207 :param token: String; JWT token208 :param course_code: String; course code209 :param student_id: Integer; student ID210 :param base_url: String; server address211 :param organization: String; university name212 :return: NONE213 """214 organization =__normalize(organization)215 course_code = __normalize(course_code)216 url = base_url + "/organizations/%s/%s/delete_user" % (organization, course_code)217 return delete(url, data={"Student": student_id},218 headers = {"Authorization": "Bearer %s" %token}).json()219@server_check220def createExam(token, course_code, name, time, duration, status="draft",221 base_url=server_address, organization=current_organization):222 """223 :param token: String; JWT Token224 :param course_code: String; course code225 :param name: String; exam name226 :param time: String Timestamp; Start time of exam227 :param duration: Integer; duration of exam in minutes228 :param status: String; can be draft, completed, graded, ready229 :param base_url: String; server address230 :param organization: String; university name231 :return: Integer, ExamID232 """233 organization = __normalize(organization)234 course_code = __normalize(course_code)235 name = __normalize(name)236 url = base_url + "/organizations/%s/%s/exams/add" % (organization, course_code)237 return put(url, data={"name": name, "time": time, "duration": duration, "status": status},238 headers = {"Authorization": "Bearer %s" %token}).json()239@server_check240def getExamsOfLecture(token, course_code, base_url=server_address, organization=current_organization):241 """242 :param token: String; JWT Token243 :param course_code: String; course code244 :param base_url: String; server address245 :param organization: String; university name246 :return: List of Lists of exams data (ID, name, lecture id, timestamp start time, integer duration (min), status)247 """248 organization = __normalize(organization)249 course_code = __normalize(course_code)250 url = base_url + "/organizations/%s/%s/exams/" % (organization, course_code)251 return get(url,252 headers = {"Authorization": "Bearer %s" %token}).json()253@server_check254def getExam(token, course_code, name, base_url=server_address, organization=current_organization):255 """256 :param token: String; JWT token257 :param course_code: String; course code258 :param name: String; exam name259 :param base_url: String; server address260 :param organization: String; university name261 :return: JSON; Name: String exam name, Course: String course code, Questions: JSON(see create exam questions structure),262 Time: String Timestamp starting time, Duration: Integer exam duration in minutes, ID: Integer exam id263 """264 organization = __normalize(organization)265 course_code = __normalize(course_code)266 name = __normalize(name)267 url = base_url + "/organizations/%s/%s/exams/%s" % (organization, course_code, name)268 exam_info = get(url, headers = {"Authorization": "Bearer " + token}).json()269 for question in exam_info["Questions"].values():270 new_dict = {}271 for key, value in question["Test_Cases"].items():272 new_dict[eval(key)] = value273 question["Test_Cases"] = new_dict274 return exam_info275@server_check276def sendAnswers(token, course_code, question_id, username, answer,277 base_url=server_address, organization=current_organization, **kwargs):278 """279 :param token: String; JWT token280 :param course_code: String; course code281 :param question_id: String; question ID282 :param username: String; username of request283 :param answer: String; user's answer284 :param base_url: String; server address285 :param organization: String; university name286 :param kwargs: Dictionary; keys are question_time, compile_number, answer_count287 :return: [] if success, NONE otherwise288 """289 organization = __normalize(organization)290 course_code = __normalize(course_code)291 url = base_url + "/organizations/%s/%s/exams/%s/answers/%s" % (organization, course_code, str(question_id), username)292 return put(url, data={"answers": answer, "question_stats": kwargs}, headers={"Authorization": "Bearer " + token}).json()293@server_check294def deleteExam(token, exam_name, course_code, base_url=server_address, organization=current_organization):295 """296 :param token: String; JWT token297 :param exam_name: String; exam name to be deleted298 :param course_code: String; course code of exam299 :param base_url: String; server address300 :param organization: String; university name301 :return: [] if successful302 """303 organization = __normalize(organization)304 course_code = __normalize(course_code)305 exam_name = __normalize(exam_name)306 url = base_url + "/organizations/%s/%s/exams/%s/delete" % (organization, course_code, exam_name)307 return delete(url,308 headers={"Authorization": "Bearer %s" % token}).json()309@server_check310def uploadProfilePic(token, username, pic, base_url=server_address, organization=current_organization):311 organization = __normalize(organization)312 url = base_url + "/organizations/%s/%s/pic" % (organization, username)313 return put(url, headers={"Authorization": "Bearer " + token}, files = {"pic": open(pic, "rb")}).json()314# @server_check315def getProfilePic(token, username, base_url=server_address, organization=current_organization):316 """317 :param token: String; JWT token318 :param username: String; username of request319 :param base_url: String; server address320 :param organization: String; university321 :return: Done, saves profile picture to SEAS/img/pic_username.png322 """323 organization = __normalize(organization)324 url = base_url + "/organizations/%s/%s/pic" % (organization, username)325 response = get(url, headers={"Authorization": "Bearer " + token})326 try:327 with open("data/img/pic_user_current.png", "wb") as f:328 f.write(response.content)329 except TypeError:330 pass331 return "Done"332@server_check333def grade_answer(token, course_code, question_id, student_user, grade,334 base_url=server_address, organization=current_organization):335 """336 :param token: String; JWT token337 :param course_code: String, course code338 :param question_id: Integer, question id number339 :param student_user: String; username of answer340 :param grade: Integer; grade of answer341 :param base_url: String; Server address342 :param organization: String; university name343 :return: [] if successful.344 """345 organization = __normalize(organization)346 course_code = __normalize(course_code)347 url = base_url + "/organizations/%s/%s/exams/%s/answers/%s/grade" % (organization, course_code, str(question_id), student_user)348 return put(url, headers={"Authorization": "Bearer " + token}, data={"grade": grade}).json()349@server_check350def edit_question(token, course_code, exam_name, question_id, info,351 base_url=server_address, organization=current_organization):352 """353 :param token: String; JWT token354 :param course_code: String, course code355 :param exam_name: String, exam name356 :param question_id: Integer, question id number357 :param info: JSON; question metadata.358 :param base_url: String; Server address359 :param organization: String; university name360 :return: [] if successful.361 """362 organization = __normalize(organization)363 course_code = __normalize(course_code)364 exam_name = __normalize(exam_name)365 url = base_url + "/organizations/%s/%s/exams/%s/%s/edit" % (366 organization, course_code, exam_name, str(question_id))367 return put(url, headers={"Authorization": "Bearer " + token}, data={"data":json.dumps(info)}).json()368@server_check369def add_time_to_exam(token, course_code, exam_name, additional_time,370 base_url=server_address, organization=current_organization):371 """372 :param token:373 :param course_code:374 :param exam_name:375 :param additional_time:376 :param base_url:377 :param organization:378 :return: [] if success379 """380 organization = __normalize(organization)381 course_code = __normalize(course_code)382 exam_name = __normalize(exam_name)383 url = base_url + "/organizations/%s/%s/exams/%s/more_time" % (organization, course_code, exam_name)384 return put(url, headers={"Authorization": "Bearer " + token}, data={"additional_time": additional_time}).json()385@server_check386def change_status_of_exam(token, course_code, exam_name, status,387 base_url=server_address, organization=current_organization):388 """389 :param token:390 :param course_code:391 :param exam_name:392 :param status:393 :param base_url:394 :param organization:395 :return: [] if success396 """397 organization = __normalize(organization)398 course_code = __normalize(course_code)399 exam_name = __normalize(exam_name)400 url = base_url + "/organizations/%s/%s/exams/%s/status" % (organization, course_code, exam_name)401 if status not in ["draft", "finished", "published", "graded", "deactivated", "active"]:402 return "Wrong status."403 return put(url, headers={"Authorization": "Bearer " + token}, data={"status": status}).json()404@server_check405def addQuestionToExam(token, course_code, exam_name, question_info,406 organization=current_organization, base_url=server_address):407 organization = __normalize(organization)408 course_code = __normalize(course_code)409 exam_name = __normalize(exam_name)410 url = base_url + "/organizations/%s/%s/exams/%s/add_question" % (organization, course_code, exam_name)411 return put(url, headers={"Authorization": "Bearer " + token}, data={"data": json.dumps(question_info)}).json()412@server_check413def resetPassword(username, temp_pass=None, new_pass=None,414 organization=current_organization, base_url=server_address):415 organization = __normalize(organization)416 url = base_url + "/organizations/%s/%s/reset_password" % (organization, username)417 if temp_pass is None:418 return get(url).json()419 else:420 return put(url, auth=(temp_pass, new_pass)).json()421@server_check422def getGradesOfExam(token, course_code, exam_name, student_id="ALL",423 organization=current_organization, base_url=server_address):424 organization = __normalize(organization)425 course_code = __normalize(course_code)426 exam_name = __normalize(exam_name)427 if student_id != "ALL":428 try:429 student_id = int(student_id)430 except:431 return "Invalid student ID."432 url = base_url + "/organizations/%s/%s/exams/%s/get_grades/%s" % (organization, course_code, exam_name, student_id)433 return get(url, headers={"Authorization": "Bearer " + token}).json()434@server_check435def getAnswersOfStudent(token, course, exam, student_id,436 organization=current_organization, base_url=server_address):437 # returns a list: [answer_id, question_id, student_id, given_answer, given_grade or None]438 organization = __normalize(organization)439 course = __normalize(course)440 exam = __normalize(exam)441 url = base_url + "/organizations/%s/%s/exams/%s/get_answers/%s" % (organization, course, exam, student_id)442 return get(url, headers={"Authorization": "Bearer " + token}).json()443@server_check444def getLastActivities(token, username, sign_in=False, organization=current_organization, base_url=server_address):445 url = base_url + "/organizations/%s/%s/" % (organization, username)446 if sign_in:447 url += "last_login"448 else:449 url += "last_activities"450 return get(url, headers={"Authorization": "Bearer " + token}).json()451@server_check452def postExamData(token, course, exam, user_id, key_stream, keystroke=None, memory_usage=None, network_download=None,453 network_upload=None, organization=current_organization, base_url=server_address):454 """455 :param token:456 :param course:457 :param exam:458 :param user_id:459 :param key_stream: tuple460 :param keystroke:461 :param memory_usage:462 :param network_download:463 :param network_upload:464 :param organization:465 :param base_url:466 :return:467 """468 organization = __normalize(organization)469 course = __normalize(course)470 exam = __normalize(exam)471 data = {"keystroke": keystroke, "memory_usage": memory_usage, "network_download": network_download,472 "network_upload": network_upload, "key_stream": key_stream}473 url = base_url + "/organizations/%s/%s/exams/%s/data/%s" % (organization, course, exam, user_id)474 return put(url, headers={"Authorization": "Bearer " + token}, data=data).json()475@server_check476def extraMaterials(token, course, exam, question_id, file_, purpose, upload=False,477 organization=current_organization, base_url=server_address):478 purpose = __normalize(purpose)479 if purpose not in ("auto_grade", "reference", "visual_question"):480 return "Wrong purpose!"481 organization = __normalize(organization)482 course = __normalize(course)483 exam = __normalize(exam)484 url = base_url + "/organizations/%s/%s/exams/%s/materials" % (organization, course, exam)485 with open(file_, "rb") as data_f:486 if upload:487 rtn = put(url, files={"file": data_f},488 data={"question_id": question_id, "purpose": purpose},489 headers={"Authorization": "Bearer " + token}).json()490 else:491 rtn = None492 pass493 return rtn494@server_check495def getKeyloggerData(token, course, exam, student_id, organization=current_organization, base_url=server_address):496 organization = __normalize(organization)497 course = __normalize(course)498 exam = __normalize(exam)499 url = base_url + "/organizations/%s/%s/exams/%s/keystrokes" % (organization, course, exam)500 return get(url, data={"student_id": student_id},501 headers={"Authorization": "Bearer " + token}).json()502@server_check503def giveSecondAccessExam(token, course, exam, student_username,504 organization=current_organization, base_url=server_address):505 organization = __normalize(organization)506 course = __normalize(course)507 exam = __normalize(exam)508 url = base_url + "/organizations/%s/%s/exams/%s/exceptional_access" % (organization, course, exam)...

Full Screen

Full Screen

clock.py

Source:clock.py Github

copy

Full Screen

2class Clock:3 MINS_PER_HOUR = 604 HOURS_PER_DAY = 245 def __init__(self, hour: int, minute: int) -> None:6 nhours, nmins = self.__normalize(hour, minute, self.MINS_PER_HOUR)7 _, nhours = self.__normalize(0, nhours, self.HOURS_PER_DAY)8 self.hours = nhours9 self.mins = nmins10 def __repr__(self) -> str:11 return f"{str(self.hours).zfill(2)}:{str(self.mins).zfill(2)}"12 def __eq__(self, other: "Clock") -> bool:13 return str(self) == str(other)14 def __add__(self, minutes: int) -> "Clock":15 self.hours, self.mins = self.__normalize(16 self.hours, self.mins+minutes, self.MINS_PER_HOUR)17 _, self.hours = self.__normalize(0, self.hours, self.HOURS_PER_DAY)18 return self19 def __sub__(self, minutes: int) -> "Clock":20 self.hours, self.mins = self.__normalize(21 self.hours, self.mins-minutes, self.MINS_PER_HOUR)22 _, self.hours = self.__normalize(0, self.hours, self.HOURS_PER_DAY)23 return self24 def __normalize(self, hi: int, lo: int, base: int) -> Tuple[int, int]:25 if lo < 0:26 norm = (-lo-1) // base + 127 hi -= norm28 lo += norm * base29 if lo >= base:30 norm = lo // base31 hi += norm32 lo -= norm * base...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful