Best Python code snippet using stestr_python
statistics.py
Source:statistics.py
1#!/usr/bin/env python2from __future__ import print_function3import os4BASE_PATH = 'mayan/apps'5def print_views_summary(module_filename):6 with open(module_filename) as file_object:7 print(' module:', module_filename)8 count_class_based_views = 09 count_function_based_views = 010 for line in file_object:11 if line.startswith('class') and 'View' in line:12 count_class_based_views += 113 if line.startswith('def') and 'request' in line:14 count_function_based_views += 115 print(' class based views: {}'.format(count_class_based_views))16 print(' function based views: {}'.format(count_function_based_views))17 return count_class_based_views, count_function_based_views18def print_tests_summary(module_filename):19 with open(module_filename) as file_object:20 print(' module:', module_filename)21 count_tests = 022 for line in file_object:23 if line.startswith(' def test'):24 count_tests += 125 print(' tests: {}'.format(count_tests))26 return count_tests27if __name__ == '__main__':28 count_totals = {29 'Apps': 0,30 'Class based views': 0,31 'Function based views': 0,32 'Class based API views': 0,33 'Function based API views': 0,34 'Tests': 0,35 }36 for app_name in sorted(os.listdir(BASE_PATH)):37 if app_name != '__init__.py':38 count_totals['Apps'] += 139 print('\n\nApp name: {}'.format(app_name))40 app_path = os.path.join(BASE_PATH, app_name)41 print('\n Views')42 try:43 module_filename = os.path.join(app_path, 'views.py')44 count_class_based_views, count_function_based_views = print_views_summary(module_filename=module_filename)45 count_totals['Class based views'] += count_class_based_views46 count_totals['Function based views'] += count_function_based_views47 except IOError:48 # Check for multiple view files inside a view directory49 try:50 module_path = os.path.join(app_path, 'views')51 for module_name in os.listdir(module_path):52 if not module_name.startswith('__init__.py') and not module_name.endswith('.pyc'):53 module_filename = os.path.join(module_path, module_name)54 count_class_based_views, count_function_based_views = print_views_summary(module_filename=module_filename)55 count_totals['Class based views'] += count_class_based_views56 count_totals['Function based views'] += count_function_based_views57 except OSError:58 # No views directory, skip app59 print(' No views')60 print('\n API Views')61 try:62 module_filename = os.path.join(app_path, 'api_views.py')63 count_class_based_views, count_function_based_views = print_views_summary(module_filename=module_filename)64 count_totals['Class based API views'] += count_class_based_views65 count_totals['Function based API views'] += count_function_based_views66 except IOError:67 # No API views directory, skip app68 print(' No API views')69 print('\n Tests')70 module_path = os.path.join(app_path, 'tests')71 try:72 for module_name in os.listdir(module_path):73 if not module_name.startswith('__init__.py') and not module_name.endswith('.pyc'):74 module_filename = os.path.join(module_path, module_name)75 if module_name.startswith('test'):76 count_tests = print_tests_summary(module_filename=module_filename)77 count_totals['Tests'] += count_tests78 except OSError:79 # No tests directory, skip app80 print(' No tests')81 print('-' * 10)82 print('Totals:')83 for key, value in count_totals.items():...
code_statistics.py
Source:code_statistics.py
1#!/usr/bin/env python2import os3BASE_PATH = 'mayan/apps'4def print_views_summary(module_filename):5 with open(file=module_filename) as file_object:6 print(' module:', module_filename)7 count_class_based_views = 08 count_function_based_views = 09 for line in file_object:10 if line.startswith('class') and 'View' in line:11 count_class_based_views += 112 if line.startswith('def') and 'request' in line:13 count_function_based_views += 114 print(' class based views: {}'.format(count_class_based_views))15 print(' function based views: {}'.format(count_function_based_views))16 return count_class_based_views, count_function_based_views17def print_tests_summary(module_filename):18 with open(file=module_filename) as file_object:19 print(' module:', module_filename)20 count_tests = 021 for line in file_object:22 if line.startswith(' def test'):23 count_tests += 124 print(' tests: {}'.format(count_tests))25 return count_tests26if __name__ == '__main__':27 count_totals = {28 'Apps': 0,29 'Class based views': 0,30 'Function based views': 0,31 'Class based API views': 0,32 'Function based API views': 0,33 'Tests': 0,34 }35 for app_name in sorted(os.listdir(BASE_PATH)):36 if app_name != '__init__.py':37 count_totals['Apps'] += 138 print('\n\nApp name: {}'.format(app_name))39 app_path = os.path.join(BASE_PATH, app_name)40 print('\n Views')41 try:42 module_filename = os.path.join(app_path, 'views.py')43 count_class_based_views, count_function_based_views = print_views_summary(module_filename=module_filename)44 count_totals['Class based views'] += count_class_based_views45 count_totals['Function based views'] += count_function_based_views46 except IOError:47 # Check for multiple view files inside a view directory48 try:49 module_path = os.path.join(app_path, 'views')50 for module_name in os.listdir(module_path):51 if not module_name.startswith('__init__.py') and not module_name.endswith('.pyc'):52 module_filename = os.path.join(module_path, module_name)53 count_class_based_views, count_function_based_views = print_views_summary(module_filename=module_filename)54 count_totals['Class based views'] += count_class_based_views55 count_totals['Function based views'] += count_function_based_views56 except OSError:57 # No views directory, skip app58 print(' No views')59 print('\n API Views')60 try:61 module_filename = os.path.join(app_path, 'api_views.py')62 count_class_based_views, count_function_based_views = print_views_summary(module_filename=module_filename)63 count_totals['Class based API views'] += count_class_based_views64 count_totals['Function based API views'] += count_function_based_views65 except IOError:66 # No API views directory, skip app67 print(' No API views')68 print('\n Tests')69 module_path = os.path.join(app_path, 'tests')70 try:71 for module_name in os.listdir(module_path):72 if not module_name.startswith('__init__.py') and not module_name.endswith('.pyc'):73 module_filename = os.path.join(module_path, module_name)74 if module_name.startswith('test'):75 count_tests = print_tests_summary(module_filename=module_filename)76 count_totals['Tests'] += count_tests77 except OSError:78 # No tests directory, skip app79 print(' No tests')80 print('-' * 10)81 print('Totals:')82 for key, value in count_totals.items():...
merge.py
Source:merge.py
1class Merged:2 def __init__(self, id):3 self.id = id4 self.prec10 = 05 self.prec50 = 06 self.prec100 = 07 self.acc10 = 08 self.acc50 = 09 self.acc100 = 010 self.rec = 011 self.pred = 012user_to_merged = {}13def get_acc(top100_file, test_file):14 user_to_tops = {}15 count_tests = {}16 with open(top100_file) as t1f:17 for line in t1f.readlines():18 _, uid, top100 = line.strip().split()19 uid, top100 = int(uid), [int(x) for x in top100.split(',')]20 user_to_tops[uid] = (set(top100[:10]), set(top100[:50]), set(top100))21 with open(test_file) as tf:22 for line in tf.readlines():23 uid, lid, freq = line.strip().split()24 uid, lid, freq = int(uid), int(lid), int(freq)25 if uid not in count_tests:26 count_tests[uid] = [[0, 0], [0, 0], [0, 0]]27 # top 1028 try:29 if lid in user_to_tops[uid][0]:30 count_tests[uid][0][0] += freq31 else:32 count_tests[uid][0][1] += freq33 # top 5034 if lid in user_to_tops[uid][1]:35 count_tests[uid][1][0] += freq36 else:37 count_tests[uid][1][1] += freq38 # top 10039 if lid in user_to_tops[uid][2]:40 count_tests[uid][2][0] += freq41 else:42 count_tests[uid][2][1] += freq43 except Exception:44 pass45 for uid in count_tests.keys():46 try:47 user_to_merged[uid].acc10 = count_tests[uid][0][0]/(count_tests[uid][0][0] + count_tests[uid][0][1])48 user_to_merged[uid].acc50 = count_tests[uid][1][0]/(count_tests[uid][1][0] + count_tests[uid][1][1])49 user_to_merged[uid].acc100 = count_tests[uid][2][0]/(count_tests[uid][2][0] + count_tests[uid][2][1])50 except Exception:51 pass52get_acc('lfbca/result/gow_top_100.txt', 'data/Gowalla_test.txt')53# RECOMENDADOR = ('lfbca', 'lfbca')54# RECOMENDADOR = ('lore', 'LORE')55RECOMENDADOR = ('usg', 'USG')56# RECOMENDADOR = ('geosoca', 'GeoSoCa')57DATASET = ('gow', 'Gowalla')58# DATASET = ('yelp', 'Yelp')59result = RECOMENDADOR[1] + '/rec_' + DATASET[0] + '.out'60previsibility = 'previsibilidade/prev/' + DATASET[0] + '_pred.txt'61top100_file = RECOMENDADOR[1] + '/result/' + DATASET[0] + '_top_100.txt'62test_file = 'data/' + DATASET[1] + '_test.txt'63# precision64with open(result) as result_file:65 lines = result_file.readlines()66 for line in lines[10:]:67 try:68 _, uid, _, prec10, _, rec, _, prec_50, _, prec_100 = line.strip().split(' ')69 uid, prec10, rec, prec_50, prec_100 = int(uid), float(prec10), float(rec), float(prec_50), float(prec_100)70 user_to_merged[uid] = Merged(uid)71 user_to_merged[uid].prec10, user_to_merged[uid].rec = prec10, rec72 user_to_merged[uid].prec50, user_to_merged[uid].prec100 = prec_50, prec_10073 except Exception:74 pass75# accuracy76get_acc(top100_file, test_file)77# previsibility78with open(previsibility) as previsibility_file:79 for line in previsibility_file.readlines():80 try:81 uid, pred = line.strip().split('\t')82 user_to_merged[int(uid)].pred = float(pred)83 except Exception:84 pass85# print('user_id\tprec@10\tprec@50\tprec@100\trec@10\tuser_predictability')86out_str = 'user_id\tprec@10\tprec@50\tprec@100\tacc@10\tacc@50\tacc@100\trec@10\tuser_predictability\n'87keys = [k for k in user_to_merged.keys()]88keys.sort()89for uid in keys:90 merged = user_to_merged[uid]91 # print("{}\t{}\t{}\t{}\t{}\t{}".format(merged.id, merged.prec10, merged.prec50, merged.prec100, merged.rec, merged.pred))92 out_str += f'{merged.id}\t{merged.prec10}\t{merged.prec50}\t{merged.prec100}\t{merged.acc10}\t{merged.acc50}\t{merged.acc100}\t{merged.rec}\t{merged.pred}\n'93outfile = 'merge_results/result/' + RECOMENDADOR[0] + '_' + DATASET[0] + '_2.txt'94with open(outfile, 'w') as of:...
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!!