Advertisement
Guest User

Untitled

a guest
Dec 20th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. import json
  2. import sys
  3.  
  4. tstyle = 'border="1"'
  5.  
  6. def table_header(cols):
  7.     def th(col):
  8.         if col == "":
  9.             col = '(empty)'
  10.         return "<th>{}</th>".format(col.replace(" ", "-"))
  11.     main_part = "".join(th(col) for col in cols)
  12.     return "<tr><th>test</th>{}</tr>".format(main_part)
  13.  
  14. def correctness_result(test, cols):
  15.     if not test:
  16.         return ""
  17.     results = test["results"]
  18.     def td(col):
  19.         ans = results[col]
  20.         color = "red"
  21.         if ans == "<OK>":
  22.             color = "green"
  23.         ans = ans.replace("<", "&lt;").replace(">", "&gt;").replace(" ", "-")
  24.         if ans == "":
  25.             ans = '(empty)'
  26.         return "<td style=\"color:{}\">{}</td>".format(color, ans)
  27.     preface = "<td><a href=\"{}\">{}</a></td>".format(test["url"], test["name"])
  28.     main_line = "".join(td(col) for col in cols)
  29.     return "<tr>{}{}</tr>".format(preface, main_line)
  30.  
  31. def output_correctness(all_tests):
  32.     cols = list(key for key in all_tests[0]["results"].keys() if key != "dummy")
  33.     header = table_header(cols)
  34.     body = "\n    ".join(correctness_result(test, cols) for test in all_tests)
  35.     return "<table {}><thead>\n{}\n</thead><tbody>\n{}\n</tbody></table>\n\n".format(tstyle, header, body)
  36.    
  37. def performance_result(test, cols):
  38.     if not test:
  39.         return ""
  40.     results = test["results"]
  41.     def td(col):
  42.         if col == "dummy":
  43.             return ""
  44.         ans = results[col]
  45.         return "<td>{:.9f}</td>".format(ans)
  46.     preface = "<td><a href=\"{}\">{}</a></td>".format(test["url"], test["name"])
  47.     main_line = "".join(td(col) for col in cols)
  48.     return "<tr>{}{}</tr>".format(preface, main_line)
  49.  
  50. def output_performance(all_tests):
  51.     cols = list(key for key in all_tests[0]["results"].keys() if key != "dummy")
  52.     header = table_header(cols)
  53.     body = "\n    ".join(performance_result(test, cols) for test in all_tests)
  54.     return "<table {}><thead>\n{}\n</thead><tbody>\n{}\n</tbody></table>\n\n".format(tstyle, header, body)
  55.  
  56. def main():
  57.     results = json.load(sys.stdin)
  58.     print(output_correctness(results["correctness"]))
  59.     print(output_performance(results["timing"]))
  60.  
  61. if __name__ == "__main__":
  62.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement