How to use sort_key method in hypothesis

Best Python code snippet using hypothesis

json2html.py

Source:json2html.py Github

copy

Full Screen

1#!/usr/bin/env python2# -*- coding: utf-8 -*-3"""4create HTML from isaw.awol JSON5"""6import argparse7import codecs8import errno9from functools import wraps10import json11import logging12import os13import regex as re 14import sys15import traceback16from bs4 import UnicodeDammit17import langid18import regex as re 19import transliterate20import translitcodec21import unicodedata22from unidecode import unidecode23import dateutil.parser24import dominate25from dominate.tags import *26import pytz27import requests28DEFAULTLOGLEVEL = logging.WARNING29LOCAL = pytz.timezone ("America/Chicago")30RX_PUNCT = re.compile(ur'[\p{P}_\d]+')31DROMEDARY = {32 33}34def arglogger(func):35 """36 decorator to log argument calls to functions37 """38 @wraps(func)39 def inner(*args, **kwargs): 40 logger = logging.getLogger(func.__name__)41 logger.debug("called with arguments: %s, %s" % (args, kwargs))42 return func(*args, **kwargs) 43 return inner 44def dateout(_datetime):45 try:46 mydt = _datetime.astimezone(pytz.utc)47 except ValueError:48 mydt = pytz.utc.localize(_datetime).astimezone(pytz.utc)49 return mydt.strftime('%d %b %Y %H:%M:%S %z').replace(u'+0000', u'UTC')50def un_camel(x):51 """Convert CamelCase strings to space-delimited52 This function is slightly adapated from an example provided by Teh Tris:53 http://stackoverflow.com/posts/19940888/revisions54 """55 final = ''56 try:57 final = DROMEDARY[x]58 except KeyError: 59 for item in x:60 if item.isupper():61 final += " "+item.lower()62 else:63 final += item64 final = final.strip()65 DROMEDARY[x] = final66 return final67def html_out(doc, filepath):68 logger = logging.getLogger(sys._getframe().f_code.co_name)69 content = unicode(doc)70 content = content.encode('utf-8')71 with open(filepath, 'w') as file_html:72 try:73 file_html.write(content)74 except UnicodeDecodeError, e:75 msg = unicode(e) + u' file: {0}'.format(filepath)76 logger.error(msg)77 raise78def list_entry(parent, rp):79 logger = logging.getLogger(sys._getframe().f_code.co_name)80 _li = parent.add(li())81 title_text = unicode(rp['title'])82 try:83 domain_u = unicode(rp['domain'])84 except UnicodeDecodeError:85 logger.error(rp['domain'])86 raise87 try:88 hash_u = unicode(rp['hash'])89 except UnicodeDecodeError:90 logger.error(rp['hash'])91 raise92 try:93 sort_key = unicode(rp['sort_key'])94 except KeyError, UnicodeDecodeError:95 sort_key = 'unsorted'96 content_href = u'/'.join((u'.', domain_u, hash_u)) + u'.html'97 _li += a(title_text, href=content_href, cls=sort_key)98 if 'issn' in rp.keys() or 'isbn' in rp.keys():99 _li += u' ('100 if 'issn' in rp.keys():101 _li += u'issn: {0}'.format(rp['issn'])102 if 'issn' in rp.keys() and 'isbn' in rp.keys():103 _li += u', '104 if 'isbn' in rp.keys():105 _li += u'isbn: {0}'.format(rp['isbn'])106 _li += u')'107 return _li108def index_primary(primary, path_dest):109 logger = logging.getLogger(sys._getframe().f_code.co_name)110 transliterate_languages = transliterate.get_available_language_codes()111 doc = dominate.document(title=u'AWOL Index: Top-Level Resources')112 with doc.head:113 link(rel='stylesheet', type='text/css', href='http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css')114 link(rel='stylesheet', type='text/css', href='http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css')115 link(rel='stylesheet', type='text/css', href='http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,700italic,400,600,700&subset=latin,cyrillic-ext,greek-ext,greek,latin-ext,cyrillic')116 link(rel='stylesheet', type='text/css', href='./index-style.css')117 doc += h1('Index of Top-Level Resources')118 _p = p('part of ', cls='subtitle')119 _p += a('The AWOL Index', href='../index.html')120 doc += _p121 _ul = doc.add(ul())122 for pri in primary:123 rtitle = pri['title']124 sort_key = rtitle.lower()125 if sort_key != unicode(sort_key.encode('ascii', 'ignore')):126 classification = langid.classify(sort_key)127 if classification[1] > 0.9:128 if classification[0] in transliterate_languages:129 sort_key = transliterate.translit(sort_key, classification[0], reversed=True)130 sort_key = unicodedata.normalize('NFKD', sort_key)131 sort_key = codecs.encode(sort_key, 'translit/long')132 sort_key = unidecode(sort_key)133 sort_key = sort_key.encode('ascii', 'ignore')134 if len(sort_key) == 0:135 sort_key = rtitle.lower()136 sort_key = RX_PUNCT.sub(u'', sort_key)137 sort_key = u''.join(sort_key.strip().split()).lower()138 logger.debug(u'sortkey for title "{0}": "{1}"'.format(rtitle, sort_key))139 pri['sort_key'] = sort_key140 for pri in sorted([pri for pri in primary if ' ' not in pri['domain'] and pri['title'] != u'' and pri['sort_key'] != u''], key=lambda k: k['sort_key']):141 _li = list_entry(_ul, pri)142 html_out(doc, os.path.join(path_dest, 'index-top.html'))143def index_keywords(keywords, primary, path_dest):144 logger = logging.getLogger(sys._getframe().f_code.co_name)145 doc = dominate.document(title=u'AWOL Index: Resources by Keywords')146 with doc.head:147 link(rel='stylesheet', type='text/css', href='http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css')148 link(rel='stylesheet', type='text/css', href='http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css')149 link(rel='stylesheet', type='text/css', href='http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,700italic,400,600,700&subset=latin,cyrillic-ext,greek-ext,greek,latin-ext,cyrillic') 150 link(rel='stylesheet', type='text/css', href='./index-style.css')151 doc += h1('Index of Resources by Keywords')152 _p = p('part of ', cls='subtitle')153 _p += a('The AWOL Index', href='../index.html')154 doc += _p155 for kw in sorted(keywords.keys(), key=lambda s: s.lower()):156 these = [pri for pri in keywords[kw] if ' ' not in pri['domain'] and pri['title'] != u'' and pri in primary]157 if len(these) > 0:158 for pri in these:159 try:160 sort_key = pri['sort_key']161 except KeyError:162 rtitle = pri['title']163 sort_key = rtitle.lower()164 if sort_key != unicode(sort_key.encode('ascii', 'ignore')):165 classification = langid.classify(sort_key)166 if classification[1] > 0.9:167 if classification[0] in transliterate_languages:168 sort_key = transliterate.translit(sort_key, classification[0], reversed=True)169 sort_key = unicodedata.normalize('NFKD', sort_key)170 sort_key = codecs.encode(sort_key, 'translit/long')171 sort_key = unidecode(sort_key)172 sort_key = sort_key.encode('ascii', 'ignore')173 if len(sort_key) == 0:174 sort_key = rtitle.lower()175 sort_key = RX_PUNCT.sub(u'', sort_key)176 sort_key = u''.join(sort_key.strip().split()).lower()177 logger.debug(u'sortkey for title "{0}": "{1}"'.format(rtitle, sort_key))178 pri['sort_key'] = sort_key179 _div = doc.add(div(id=kw.lower().replace(u' ', u'-')))180 _div += h2(kw)181 _ul = _div.add(ul())182 for pri in sorted(these, key=lambda k: k['sort_key']):183 _li = list_entry(_ul, pri)184 else:185 logger.warning(u"suppressed keyword with no top resources: {0}".format(kw))186 html_out(doc, os.path.join(path_dest, 'index-keywords.html'))187@arglogger188def main (args):189 """190 main functions191 """192 logger = logging.getLogger(sys._getframe().f_code.co_name)193 path_source = os.path.realpath(args.json[0])194 path_dest = os.path.abspath(args.html[0])195 print('path_dest: {0}'.format(path_dest))196 r = requests.get('https://raw.githubusercontent.com/mattcg/language-subtag-registry/master/data/json/registry.json')197 if r.status_code == 200:198 lang_registry = r.json()199 else:200 # load from file201 pass202 languages = {}203 for lang in lang_registry:204 if lang['Type'] == 'language':205 languages[lang['Subtag']] = lang['Description'][0]206 # statistics and indexes207 quality_primary = 0.0208 quality_subordinate = 0.0209 quantity_primary = 0210 quantity_subordinate = 0211 primary = []212 secondary = []213 domains = []214 keywords = {}215 primary = []216 def grade(ratio):217 if ratio >= 0.9:218 return 'A'219 elif ratio >= 0.8:220 return 'B'221 elif ratio >= 0.7:222 return 'C'223 elif ratio >= 0.6:224 return 'D'225 else:226 return 'F'227 for dir_name, sub_dir_list, file_list in os.walk(path_source):228 this_dir = os.path.basename(dir_name)229 try:230 logger.info(u'converting {0}'.format(this_dir))231 except UnicodeDecodeError:232 try:233 logger.info('converting {0}'.format(this_dir))234 except UnicodeDecodeError:235 try:236 logger.info(u'converting {0}'.format(UnicodeDammit(this_dir).unicode_markup))237 except UnicodeDecodeError:238 logger.warning('this directory name is unspeakable evil')239 for file_name_json in file_list:240 with open(os.path.join(dir_name, file_name_json), 'r') as file_json:241 resource = json.load(file_json)242 this_out = os.path.splitext(file_name_json)[0]243 # stats and grades244 rtitle = resource['title']245 rdesc = resource['description']246 if rdesc is not None:247 rdescwords = sorted(set(RX_PUNCT.sub(u'', rdesc).lower().split()))248 rtitlewords = sorted(set(RX_PUNCT.sub(u'', rtitle).lower().split()))249 if rdescwords != rtitlewords:250 if len(rdescwords) > 6 or resource['volume'] is not None or resource['year'] is not None:251 quality = 1.0252 else:253 quality = 0.5254 else:255 quality = 0.0256 else:257 quality = 0.0258 pkg = {259 'domain': this_dir,260 'hash': this_out,261 'title': rtitle,262 'url': resource['url']263 }264 issn = None265 isbn = None266 try:267 issn = resource['identifiers']['issn']['electronic'][0]268 except KeyError:269 try:270 issn = resource['identifiers']['issn']['generic'][0]271 except KeyError:272 try:273 isbn = resource['identifiers']['isbn']['electronic'][0]274 except KeyError:275 try:276 isbn = resource['identifiers']['isbn']['generic'][0]277 except KeyError:278 pass279 if issn is not None:280 pkg['issn'] = issn281 if isbn is not None:282 pkg['isbn'] = isbn283 if resource['is_part_of'] is not None and len(resource['is_part_of']) > 0:284 quantity_subordinate += 1285 quality_subordinate += quality286 else:287 primary.append(pkg)288 quantity_primary += 1289 quality_primary += quality290 for k in resource['keywords']:291 try:292 kwlist = keywords[k]293 except KeyError:294 kwlist = keywords[k] = []295 kwlist.append(pkg)296 # make html297 doc = dominate.document(title=u'AWOL Index: {0}'.format(resource['title']))298 with doc.head:299 link(rel='stylesheet', type='text/css', href='http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css')300 link(rel='stylesheet', type='text/css', href='http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css')301 link(rel='stylesheet', type='text/css', href='../item-style.css')302 doc += h1(a(resource['title'], href=resource['url'], target="_blank"))303 _p = p('this record is part of ', cls='subtitle')304 _p += a('The AWOL Index', href='../../index.html')305 doc += _p 306 _p = p()307 _p += a('JSON version', href='../../json/{0}/{1}'.format(dir_name.split('/')[-1], file_name_json), target="_blank") 308 doc += _p309 _dl = doc.add(dl())310 for k in sorted(resource.keys()):311 if k != 'title' and k!= 'provenance' and resource[k] is not None:312 field = resource[k]313 if type(field) in [list, dict]:314 if len(field) > 0:315 if k == 'identifiers':316 for ident_type in field.keys():317 if type(field[ident_type]) == dict:318 for ident_subtype in field[ident_type].keys():319 if ident_subtype == 'generic':320 _dl += dt(u'{0}: '.format(ident_type))321 else:322 _dl += dt(u'{0} ({1}): '.format(ident_type, ident_subtype))323 _dl += dd(field[ident_type][ident_subtype])324 elif type(field[ident_type]) == list:325 _dl += dt(u'{0}: '.format(ident_type))326 for v in field[ident_type]:327 _dl += dt(u'{0}: '.format(v))328 else:329 raise ValueError('heckito')330 else:331 _dl += dt(k.replace(u'_', u' '))332 if type(field) == list:333 if k == 'language':334 _dl += dd(languages[resource[k][0]])335 elif k in ['subordinate_resources', 'related_resources']:336 _ul = _dl.add(ul())337 for rr in field:338 _ul += li(a(rr['title_full'], href=rr['url']))339 else:340 _dl += dd(u', '.join(sorted([unicode(thing) for thing in resource[k]])))341 else:342 _ul = ul()343 for kk in sorted(field.keys()):344 if type(field[kk]) in [unicode, str] and field[kk][0:4] == 'http':345 _li = _ul.add(li(u'{0}: '.format(kk)))346 _li += a(field[kk], href=field[kk], target="_blank")347 else:348 _ul += li(u'{0}: {1}'.format(kk, field[kk]))349 _dl += dd(_ul)350 else:351 _dl += dt(k)352 if resource[k][0:4] == 'http':353 _dl += dd(a(resource[k], href=resource[k], target="_blank"))354 else:355 _dl += dd(resource[k])356 if 'provenance' in resource.keys():357 _div = doc.add(div(id='provenance'))358 _div += h2('data provenance')359 _dl = _div.add(dl())360 events = sorted([prov for prov in resource['provenance']], key=lambda k: k['when'])361 for event in events:362 try:363 _dl += dt(dateout(dateutil.parser.parse(event['when'])))364 except ValueError:365 _dl += dt(dateout(LOCAL.localize(dateutil.parser.parse(event['when']))))366 _dd = _dl.add(dd(u'{0}: '.format(un_camel(event['term'].split(u'/')[-1]).replace(u'cites as ', u''))))367 rid = event['resource']368 if rid[0:4] == 'http':369 _dd += (a(rid.split('://')[1], href=rid, target="_blank"))370 else:371 _dd += rid372 try:373 _dd += u' (last updated: {0})'.format(dateout(dateutil.parser.parse(event['resource_date'])))374 except KeyError:375 _dd += u' (last updated not indicated)'376 377 #print (unicode(doc))378 file_name_html = '.'.join((this_out, 'html'))379 out_path = os.path.join(path_dest, this_dir)380 file_path_html = os.path.join(out_path, file_name_html)381 #print file_path_html382 try:383 os.makedirs(out_path)384 except OSError as exc: # Python >2.5385 if exc.errno == errno.EEXIST and os.path.isdir(out_path):386 pass387 else: raise388 with open(file_path_html, 'w') as file_html:389 file_html.write(unicode(doc).encode('utf-8'))390 for ignore_dir in ['.git', '.svn', '.hg']:391 if ignore_dir in sub_dir_list:392 sub_dir_list.remove(ignore_dir)393 index_primary(primary, path_dest)394 index_keywords(keywords, primary, path_dest)395 quantity = quantity_primary + quantity_subordinate396 print "quantity: {0}".format(quantity)397 print " top-level: {0}".format(quantity_primary)398 print " subordinate: {0}".format(quantity_subordinate)399 qratio = (quality_primary + quality_subordinate) / float(quantity)400 qratio_primary = quality_primary / float(quantity_primary)401 qratio_subordinate = quality_subordinate / float(quantity_subordinate)402 print "quality ratio: {0:.2f}".format(qratio)403 print " top-level: {0:.2f}".format(qratio_primary)404 print " subordinate: {0:.2f}".format(qratio_subordinate)405 print "grade: {0}".format(grade(qratio))406 print " top-level: {0}".format(grade(qratio_primary))407 print " subordinate: {0}".format(grade(qratio_subordinate))408if __name__ == "__main__":409 log_level = DEFAULTLOGLEVEL410 log_level_name = logging.getLevelName(log_level)411 logging.basicConfig(level=log_level)412 try:413 parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter)414 parser.add_argument ("-l", "--loglevel", type=str, help="desired logging level (case-insensitive string: DEBUG, INFO, WARNING, ERROR" )415 parser.add_argument ("-v", "--verbose", action="store_true", default=False, help="verbose output (logging level == INFO")416 parser.add_argument ("-vv", "--veryverbose", action="store_true", default=False, help="very verbose output (logging level == DEBUG")417 parser.add_argument('json', type=str, nargs=1, help='json source directory')418 parser.add_argument('html', type=str, nargs=1, help='html output directory')419 args = parser.parse_args()420 if args.loglevel is not None:421 args_log_level = re.sub('\s+', '', args.loglevel.strip().upper())422 try:423 log_level = getattr(logging, args_log_level)424 except AttributeError:425 logging.error("command line option to set log_level failed because '%s' is not a valid level name; using %s" % (args_log_level, log_level_name))426 if args.veryverbose:427 log_level = logging.DEBUG428 elif args.verbose:429 log_level = logging.INFO430 log_level_name = logging.getLevelName(log_level)431 logging.getLogger().setLevel(log_level)432 if log_level != DEFAULTLOGLEVEL:433 logging.warning("logging level changed to %s via command line option" % log_level_name)434 else:435 logging.info("using default logging level: %s" % log_level_name)436 logging.debug("command line: '%s'" % ' '.join(sys.argv))437 main(args)438 sys.exit(0)439 except KeyboardInterrupt, e: # Ctrl-C440 raise e441 except SystemExit, e: # sys.exit()442 raise e443 except Exception, e:444 print "ERROR, UNEXPECTED EXCEPTION"445 print str(e)446 traceback.print_exc()...

Full Screen

Full Screen

test_utils.py

Source:test_utils.py Github

copy

Full Screen

1from django.test import TestCase2from student.tests.factories import UserFactory, CourseEnrollmentFactory3from django_comment_common.models import Role, Permission4from factories import RoleFactory5import django_comment_client.utils as utils6class DictionaryTestCase(TestCase):7 def test_extract(self):8 d = {'cats': 'meow', 'dogs': 'woof'}9 k = ['cats', 'dogs', 'hamsters']10 expected = {'cats': 'meow', 'dogs': 'woof', 'hamsters': None}11 self.assertEqual(utils.extract(d, k), expected)12 def test_strip_none(self):13 d = {'cats': 'meow', 'dogs': 'woof', 'hamsters': None}14 expected = {'cats': 'meow', 'dogs': 'woof'}15 self.assertEqual(utils.strip_none(d), expected)16 def test_strip_blank(self):17 d = {'cats': 'meow', 'dogs': 'woof', 'hamsters': ' ', 'yetis': ''}18 expected = {'cats': 'meow', 'dogs': 'woof'}19 self.assertEqual(utils.strip_blank(d), expected)20 def test_merge_dict(self):21 d1 = {'cats': 'meow', 'dogs': 'woof'}22 d2 = {'lions': 'roar', 'ducks': 'quack'}23 expected = {'cats': 'meow', 'dogs': 'woof', 'lions': 'roar', 'ducks': 'quack'}24 self.assertEqual(utils.merge_dict(d1, d2), expected)25class CategorySortTestCase(TestCase):26 def setUp(self):27 self.category_map = {28 'entries': {29 u'General': {30 'sort_key': u'General'31 }32 },33 'subcategories': {34 u'Tests': {35 'sort_key': u'Tests',36 'subcategories': {},37 'entries': {38 u'Quizzes': {39 'sort_key': None40 }, u'All': {41 'sort_key': None42 }, u'Final Exam': {43 'sort_key': None44 },45 }46 },47 u'Assignments': {48 'sort_key': u'Assignments',49 'subcategories': {},50 'entries': {51 u'Homework': {52 'sort_key': None53 },54 u'All': {55 'sort_key': None56 },57 }58 }59 }60 }61 def test_alpha_sort_true(self):62 expected_true = {63 'entries': {64 u'General': {65 'sort_key': u'General'66 }67 },68 'children': [u'Assignments', u'General', u'Tests'],69 'subcategories': {70 u'Tests': {71 'sort_key': u'Tests',72 'subcategories': {},73 'children': [u'All', u'Final Exam', u'Quizzes'],74 'entries': {75 u'All': {76 'sort_key': 'All'77 }, u'Final Exam': {78 'sort_key': 'Final Exam'79 }, u'Quizzes': {80 'sort_key': 'Quizzes'81 }82 }83 },84 u'Assignments': {85 'sort_key': u'Assignments',86 'subcategories': {},87 'children': [u'All', u'Homework'],88 'entries': {89 u'Homework': {90 'sort_key': 'Homework'91 },92 u'All': {93 'sort_key': 'All'94 },95 }96 }97 }98 }99 100 utils.sort_map_entries(self.category_map, True)101 self.assertEqual(self.category_map, expected_true)102 def test_alpha_sort_false(self):103 expected_false = {104 'entries': {105 u'General': {106 'sort_key': u'General'107 }108 },109 'children': [u'Assignments', u'General', u'Tests'],110 'subcategories': {111 u'Tests': {112 'sort_key': u'Tests',113 'subcategories': {},114 'children': [u'Quizzes', u'All', u'Final Exam'],115 'entries': {116 u'Quizzes': {117 'sort_key': None118 }, u'All': {119 'sort_key': None120 }, u'Final Exam': {121 'sort_key': None122 },123 }124 },125 u'Assignments': {126 'sort_key': u'Assignments',127 'subcategories': {},128 'children': [u'All', u'Homework'],129 'entries': {130 u'Homework': {131 'sort_key': None132 },133 u'All': {134 'sort_key': None135 },136 }137 }138 }139 }140 141 utils.sort_map_entries(self.category_map, False)142 self.assertEqual(self.category_map, expected_false)143class AccessUtilsTestCase(TestCase):144 def setUp(self):145 self.course_id = 'edX/toy/2012_Fall'146 self.student_role = RoleFactory(name='Student', course_id=self.course_id)147 self.moderator_role = RoleFactory(name='Moderator', course_id=self.course_id)148 self.student1 = UserFactory(username='student', email='student@edx.org')149 self.student1_enrollment = CourseEnrollmentFactory(user=self.student1)150 self.student_role.users.add(self.student1)151 self.student2 = UserFactory(username='student2', email='student2@edx.org')152 self.student2_enrollment = CourseEnrollmentFactory(user=self.student2)153 self.moderator = UserFactory(username='moderator', email='staff@edx.org', is_staff=True)154 self.moderator_enrollment = CourseEnrollmentFactory(user=self.moderator)155 self.moderator_role.users.add(self.moderator)156 def test_get_role_ids(self):157 ret = utils.get_role_ids(self.course_id)158 expected = {u'Moderator': [3], u'Student': [1, 2], 'Staff': [3]}159 self.assertEqual(ret, expected)160 def test_has_forum_access(self):161 ret = utils.has_forum_access('student', self.course_id, 'Student')162 self.assertTrue(ret)163 ret = utils.has_forum_access('not_a_student', self.course_id, 'Student')164 self.assertFalse(ret)165 ret = utils.has_forum_access('student', self.course_id, 'NotARole')...

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 hypothesis 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