Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

""" This module contains functions and variables to control hiPhive's logging 

 

* `logger` - the module logger 

* set_config - function to control level and logfile 

""" 

 

import logging 

import sys 

from timeit import default_timer as timer 

 

# This is the root logger of hiPhive 

logger = logging.getLogger('hiphive') 

 

# Will process all levels of INFO or higher 

logger.setLevel(logging.INFO) 

 

# If you know what you are doing you may set this to True 

logger.propagate = False 

 

# The hiPhive logger will collect events from childs and the default behaviour 

# is to print it directly to stdout 

ch = logging.StreamHandler(sys.stdout) 

logger.addHandler(ch) 

 

continuous_logging = False 

 

 

# TODO: use Context management protocol instead 

class Progress: 

""" Progress bar like functionality. """ 

 

def __init__(self, tot=None, mode='frac', estimate_remaining=True): 

33 ↛ 34line 33 didn't jump to line 34, because the condition on line 33 was never true if tot is None: 

self._tot = '?' 

assert not estimate_remaining 

else: 

self._tot = tot 

self._progress = 0 

self._estimate_remaining = estimate_remaining 

self._last_update = 0 

self._start = timer() 

 

def tick(self): 

self._progress += 1 

delta = timer() - self._last_update 

46 ↛ 47line 46 didn't jump to line 47, because the condition on line 46 was never true if continuous_logging and delta > 2: 

self._last_update = timer() 

print('\r' + ' ' * 70 + '\r', end='', flush=True) 

print('{}/{}={:.3%}'.format(self._progress, 

self._tot, self._progress/self._tot), end='', flush=True) 

if self._estimate_remaining and self._tot != '?': 

remaining_time = (self._tot - self._progress) * ( 

timer() - self._start) / self._progress 

print(' time remaining: {:.3}'.format(remaining_time), end='', 

flush=True) 

 

def close(self): 

58 ↛ 59line 58 didn't jump to line 59, because the condition on line 58 was never true if continuous_logging: 

print('\r' + ' ' * 70 + '\r', end='', flush=True) 

s = timer() - self._start 

d, remainder = divmod(s, 60*60*24) 

h, remainder = divmod(remainder, 60*60) 

m, s = divmod(remainder, 60) 

logger.info('Done in {}d {}h {}m {:.3}s' 

.format(int(d), int(h), int(m), s)) 

 

 

class ProgressManager: 

def __init__(self, *args, **kwargs): 

self.bar = Progress(*args, **kwargs) 

 

def __enter__(self): 

return self.bar 

 

def __exit__(self, *args): 

self.bar.close() 

 

 

def set_config(filename=None, level=None, continuous=None): 

""" 

Alters the way logging is handled. 

 

Parameters 

---------- 

filename : str 

name of file the log should be sent to 

level : int 

verbosity level; see `Python logging library 

<https://docs.python.org/3/library/logging.html>`_ for details 

continuous : bool 

if True the progress will be continously updated 

""" 

 

# If a filename is provided a logfile will be created 

95 ↛ 96line 95 didn't jump to line 96, because the condition on line 95 was never true if filename is not None: 

fh = logging.FileHandler(filename) 

logger.addHandler(fh) 

 

99 ↛ 102line 99 didn't jump to line 102, because the condition on line 99 was never false if level is not None: 

logger.setLevel(level) 

 

if continuous is not None: 

global continuous_logging 

continuous_logging = continuous