Coverage for sacred/sacred/observers/base.py: 58%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

36 statements  

1#!/usr/bin/env python 

2# coding=utf-8 

3 

4__all__ = ("RunObserver", "td_format") 

5 

6 

7class RunObserver: 

8 """Defines the interface for all run observers.""" 

9 

10 priority = 0 

11 

12 def queued_event( 

13 self, ex_info, command, host_info, queue_time, config, meta_info, _id 

14 ): 

15 pass 

16 

17 def started_event( 

18 self, ex_info, command, host_info, start_time, config, meta_info, _id 

19 ): 

20 pass 

21 

22 def heartbeat_event(self, info, captured_out, beat_time, result): 

23 pass 

24 

25 def completed_event(self, stop_time, result): 

26 pass 

27 

28 def interrupted_event(self, interrupt_time, status): 

29 pass 

30 

31 def failed_event(self, fail_time, fail_trace): 

32 pass 

33 

34 def resource_event(self, filename): 

35 pass 

36 

37 def artifact_event(self, name, filename, metadata=None, content_type=None): 

38 pass 

39 

40 def log_metrics(self, metrics_by_name, info): 

41 pass 

42 

43 def join(self): 

44 pass 

45 

46 

47# http://stackoverflow.com/questions/538666/python-format-timedelta-to-string 

48def td_format(td_object): 

49 seconds = int(td_object.total_seconds()) 

50 if seconds == 0: 

51 return "less than a second" 

52 

53 periods = [ 

54 ("year", 60 * 60 * 24 * 365), 

55 ("month", 60 * 60 * 24 * 30), 

56 ("day", 60 * 60 * 24), 

57 ("hour", 60 * 60), 

58 ("minute", 60), 

59 ("second", 1), 

60 ] 

61 

62 strings = [] 

63 for period_name, period_seconds in periods: 

64 if seconds >= period_seconds: 

65 period_value, seconds = divmod(seconds, period_seconds) 

66 if period_value == 1: 

67 strings.append("%s %s" % (period_value, period_name)) 

68 else: 

69 strings.append("%s %ss" % (period_value, period_name)) 

70 

71 return ", ".join(strings)