Skip to content

Instantly share code, notes, and snippets.

@yymm
Created November 19, 2015 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yymm/9affbc96e2fb28b2c7db to your computer and use it in GitHub Desktop.
Save yymm/9affbc96e2fb28b2c7db to your computer and use it in GitHub Desktop.
virtual_matrix_diff.py
# -*- coding: utf-8 -*-
import sys
import math
def getcolor(colorname): # {{{
colors = {
'clear': '\033[0m',
'black': '\033[30m',
'red': '\033[31m',
'green': '\033[32m',
'yellow': '\033[33m',
'blue': '\033[34m',
'purple': '\033[35m',
'cyan': '\033[36m',
'white': '\033[37m'
}
def func(c):
return colors[colorname] + c + colors['clear']
return func
black = getcolor('black')
red = getcolor('red')
green = getcolor('green')
yellow = getcolor('yellow')
blue = getcolor('blue')
purple = getcolor('purple')
cyan = getcolor('cyan')
white = getcolor('white')
def compare(f1, f2):
d1 = None
d1 = None
with open(f1) as f:
d1 = f.readlines()
with open(f2) as f:
d2 = f.readlines()
print(len(d1))
print(len(d2))
if len(d1) != len(d2):
sys.stderr.write("Invalid file size.")
exit(1)
ln = 0 # line number
for (l1, l2) in zip(d1, d2):
if l1.find("*") >= 0 or l1.find("---") >= 0 or \
l2.find("*") >= 0 or l2.find("---") >= 0:
ln += 1
sys.stdout.write(l1)
continue
tokens1 = l1.split()
tokens2 = l2.split()
for (w1, w2) in zip(tokens1, tokens2):
f1 = float(w1)
f2 = float(w2)
d = abs(f1-f2)
if d < 10E-14:
sys.stdout.write(cyan("[o]"))
else:
if d > 10E-8:
sys.stdout.write(yellow("[x]"))
else:
sys.stdout.write(red("% 3d"%int(math.floor(math.log10(d)))))
print("")
ln += 1
if __name__ == '__main__':
argvs = sys.argv
argc = len(argvs)
if argc != 3:
sys.stderr.write("Require two args.")
exit(1)
compare(argvs[1], argvs[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment