Tries a series of functions and prints their results. `context` is a dictionary mapping names to values; the value will only be tried if it's callable. >>> tryall(dict(j=lambda: True)) j: True ---------------------------------------- results:
(context, prefix=None)
| 1199 | |
| 1200 | |
| 1201 | def tryall(context, prefix=None): |
| 1202 | """ |
| 1203 | Tries a series of functions and prints their results. |
| 1204 | `context` is a dictionary mapping names to values; |
| 1205 | the value will only be tried if it's callable. |
| 1206 | |
| 1207 | >>> tryall(dict(j=lambda: True)) |
| 1208 | j: True |
| 1209 | ---------------------------------------- |
| 1210 | results: |
| 1211 | True: 1 |
| 1212 | |
| 1213 | For example, you might have a file `test/stuff.py` |
| 1214 | with a series of functions testing various things in it. |
| 1215 | At the bottom, have a line: |
| 1216 | |
| 1217 | if __name__ == "__main__": tryall(globals()) |
| 1218 | |
| 1219 | Then you can run `python test/stuff.py` and get the results of |
| 1220 | all the tests. |
| 1221 | """ |
| 1222 | context = context.copy() # vars() would update |
| 1223 | results = {} |
| 1224 | for key, value in iteritems(context): |
| 1225 | if not hasattr(value, "__call__"): |
| 1226 | continue |
| 1227 | if prefix and not key.startswith(prefix): |
| 1228 | continue |
| 1229 | print(key + ":", end=" ") |
| 1230 | try: |
| 1231 | r = value() |
| 1232 | dictincr(results, r) |
| 1233 | print(r) |
| 1234 | except: |
| 1235 | print("ERROR") |
| 1236 | dictincr(results, "ERROR") |
| 1237 | print(" " + "\n ".join(traceback.format_exc().split("\n"))) |
| 1238 | |
| 1239 | print("-" * 40) |
| 1240 | print("results:") |
| 1241 | for key, value in iteritems(results): |
| 1242 | print(" " * 2, str(key) + ":", value) |
| 1243 | |
| 1244 | |
| 1245 | class ThreadedDict(threadlocal): |