def compare_outputs(test, ref, skip_compare=('png', 'traceback', 'latex', 'prompt_number', 'metadata')): for key in ref: if key not in test: print("missing key: %s != %s" % (list(test.keys()), list(ref.keys()))) return False elif key not in skip_compare and sanitize(test[key]) != sanitize( ref[key]): print("mismatch %s:" % key) exp = ref[key] eff = test[key] if exp[:-1] != '\n': exp += '\n' if eff[:-1] != '\n': eff += '\n' print(''.join( diff(exp.splitlines(1), eff.splitlines(1), fromfile='expected', tofile='effective'))) return False return True
def compare_outputs(ref, test, ignores=[]): '''Check that two lists of outputs are equivalent and report the result.''' cref = [canonical_dict(d, ignores) for d in ref if keep_dict(d)] ctest = [canonical_dict(d, ignores) for d in test if keep_dict(d)] ok = True if len(cref) != len(ctest): print("output length mismatch (expected {}, got {})".format( len(cref), len(ctest))) ok = False # There can be several outputs. For instance wnen the cell both # prints a result (goes to "stdout") and displays an automaton # (goes to "data"). exp = pprint.pformat(cref, width=132) eff = pprint.pformat(ctest, width=132) if exp[:-1] != '\n': exp += '\n' if eff[:-1] != '\n': eff += '\n' if exp == eff: return ok else: print(''.join( diff(exp.splitlines(1), eff.splitlines(1), fromfile='expected', tofile='effective'))) return False
def changecallback(fname, runner): try: pid = int(open('.lastRun', 'rt').read()) except: pass else: kill(pid) print '_' * 79 try: past = open(fname + '~', 'r').read() except: print "[+] No previous file" else: print "[+] You changed..." new = open(fname, 'r').read() lastmtime = mtimeFor(fname + '~') currentmtime = mtimeFor(fname) print ''.join( diff(past.splitlines(1), new.splitlines(1), 'previous', 'current', time.ctime(lastmtime), time.ctime(currentmtime))) print "[+] %s elapsed since last run" % (timeDiff(currentmtime - lastmtime)) p = subprocess.Popen(runner + ' ' + fname) print "[+] %d started" % p.pid shutil.copyfile(fname, fname + '~') open('.lastRun', 'w').write(str(p.pid))
def changecallback(fname,runner): try: pid=int(open('.lastRun','rt').read()) except: pass else: kill(pid) print '_'*79 try: past=open(fname+'~','r').read() except: print "[+] No previous file" else: print "[+] You changed..." new=open(fname,'r').read() lastmtime=mtimeFor(fname+'~') currentmtime=mtimeFor(fname) print ''.join(diff(past.splitlines(1),new.splitlines(1), 'previous','current', time.ctime(lastmtime),time.ctime(currentmtime))) print "[+] %s elapsed since last run"%(timeDiff(currentmtime-lastmtime)) p=subprocess.Popen(runner+' '+fname) print "[+] %d started"%p.pid shutil.copyfile(fname,fname+'~') open('.lastRun','w').write(str(p.pid))
def diff(old_content, new_content, n=3): """similar to difflib.ndiff, but supports limited context lines""" from difflib import ndiff as diff diffdata = tuple(diff(old_content, new_content)) difflines = set() for lineno, line in enumerate(diffdata): if not line.startswith(' '): # Ignore the similar lines. difflines.update(range(lineno - n, lineno + n + 1)) return '\n'.join( line.rstrip('\n') for lineno, line in enumerate(diffdata) if lineno in difflines )
def rst_diff(expected, effective): "Report the difference bw `expected` and `effective`." exp = format(expected) eff = format(effective) if exp[:-1] != '\n': exp += '\n' if eff[:-1] != '\n': eff += '\n' rst_file( "Diff on output", ''.join( diff(exp.splitlines(1), eff.splitlines(1), fromfile='expected', tofile='effective')))
def diff(old_content, new_content, n=3): """similar to difflib.ndiff, but supports limited context lines""" from difflib import ndiff as diff diffdata = tuple(diff(old_content, new_content)) difflines = set() for lineno, line in enumerate(diffdata): if not line.startswith(str(' ')): # Ignore the similar lines. difflines.update(range(lineno - n, lineno + n + 1)) return '\n'.join( line.rstrip('\n') for lineno, line in enumerate(diffdata) if lineno in difflines )
def assert_same_file_content(old_file, new_file): old_content = open(old_file, 'rb').readlines() new_content = open(new_file, 'rb').readlines() diffs = diff(old_content, new_content) if diffs: diffs = f'Results differ:\n--- {old_file}\n+++ {new_file}\n{diffs}' # py.test derps on non-utf8 bytes, so I force text like so: if isinstance(diffs, bytes): diffs = diffs.decode('UTF-8', 'replace') raise AssertionError(diffs) else: from os import unlink unlink(new_file)
def assert_same_file_content(old_file, new_file): old_content = open(old_file, 'rb').readlines() new_content = open(new_file, 'rb').readlines() diffs = diff(old_content, new_content) if diffs: diffs = 'Results differ:\n--- %s\n+++ %s\n%s' % (old_file, new_file, diffs) # py.test derps on non-utf8 bytes, so I force text like so: if isinstance(diffs, bytes): diffs = diffs.decode('UTF-8', 'replace') raise AssertionError(diffs) else: from os import unlink unlink(new_file)
def assert_same_file_content(old_file, new_file): old_content = open(old_file).readlines() new_content = open(new_file).readlines() from difflib import ndiff as diff diffs = '\n'.join( line.rstrip('\n') for line in diff(old_content, new_content) if not line.startswith(' ') # Remove the similar lines. ) if diffs: diffs = 'Results differ:\n--- %s\n+++ %s\n%s' % (old_file, new_file, diffs) raise AssertionError(diffs) else: from os import unlink unlink(new_file)
def compare_outputs(test, ref, skip_compare=('png', 'traceback', 'latex', 'prompt_number', 'metadata')): for key in ref: if key not in test: print("missing key: %s != %s" % (list(test.keys()), list(ref.keys()))) return False elif key not in skip_compare and sanitize(test[key]) != sanitize(ref[key]): print("mismatch %s:" % key) exp = ref[key] eff = test[key] if exp[:-1] != '\n': exp += '\n' if eff[:-1] != '\n': eff += '\n' print(''.join(diff(exp.splitlines(1), eff.splitlines(1), fromfile='expected', tofile='effective'))) return False return True
def compare_outputs(ref, test, ignores=[]): '''Check that two lists of outputs are equivalent and report the result.''' # There can be several outputs. For instance wnen the cell both # prints a result (goes to "stdout") and displays an automaton # (goes to "data"). exp = pprint.pformat([canonical_dict(d, ignores) for d in ref], width=132) eff = pprint.pformat([canonical_dict(d, ignores) for d in test], width=132) if exp[:-1] != '\n': exp += '\n' if eff[:-1] != '\n': eff += '\n' if exp == eff: return True else: print(''.join(diff(exp.splitlines(1), eff.splitlines(1), fromfile='expected', tofile='effective'))) return False
] main_translation = lookup(translation_gls, lookup_word) if main_translation: main_translation_text = main_translation.split('\t', 1)[1] else: main_translation_text = 'Not Found' translation_texts = [[]] * len(meanings) similiarities = [[]] * len(meanings) for i in chosen_meaning_index: for synonym in meanings[i]['synonyms']: line = lookup(translation_gls, synonym) if line: splited_line = line.split('\t', 1)[1] translation_texts[i].append(splited_line) similiarities[i].append( diff(None, main_translation_text, splited_line).ratio()) else: translation_texts[i].append('Not Found\n') similiarities[i].append(0) translation_texts[i] = [ x for _, x in sorted(zip(similiarities[i], translation_texts[i]), reverse=True) ] meanings[i]['synonyms'] = [ x for _, x in sorted(zip(similiarities[i], meanings[i] ['synonyms']), reverse=True) ] similiarities[i] = sorted(similiarities[i], reverse=True) print('\n' + (colored('100%', 'red') + '\t') * bool(main_translation) +
def assert_same_content(old_file, new_content): new_file = old_file+'.test_failure' try: open(new_file,'w').write(new_content) except IOError, e: if e.errno == 2: # No such file. from os import makedirs from os.path import dirname makedirs(dirname(new_file)) open(new_file,'w').write(new_content) else: raise old_content = open(old_file).readlines() new_content = open(new_file).readlines() from difflib import ndiff as diff diffs = ''.join( line for line in diff(old_content, new_content) if not line.startswith(' ') # Remove the similar lines. ) if diffs: diffs = 'Results differ:\n--- %s\n+++ %s\n%s' % (old_file, new_file, diffs) raise AssertionError(diffs) else: from os import unlink unlink(new_file)