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

105

106

107

108

109

110

111

112

113

114

115

# coding: utf-8 

""" 

Collection of low-level tools that faciliate the interface with resource managers. 

 

The preferred way of importing this module is: 

 

import qutils as qu 

""" 

from __future__ import print_function, division, unicode_literals 

 

from monty.string import is_string 

from pymatgen.core.units import Time, Memory 

 

import logging 

logger = logging.getLogger(__name__) 

 

 

def slurm_parse_timestr(s): 

""" 

A slurm time parser. Accepts a string in one the following forms: 

 

# "days-hours", 

# "days-hours:minutes", 

# "days-hours:minutes:seconds". 

# "minutes", 

# "minutes:seconds", 

# "hours:minutes:seconds", 

 

Returns: 

Time in seconds. 

 

Raises: 

`ValueError` if string is not valid. 

""" 

days, hours, minutes, seconds = 0, 0, 0, 0 

if '-' in s: 

# "days-hours", 

# "days-hours:minutes",  

# "days-hours:minutes:seconds".  

days, s = s.split("-") 

days = int(days) 

 

if ':' not in s: 

hours = int(float(s)) 

elif s.count(':') == 1: 

hours, minutes = map(int, s.split(':')) 

elif s.count(':') == 2: 

hours, minutes, seconds = map(int, s.split(':')) 

else: 

raise ValueError("More that 2 ':' in string!") 

 

else: 

# "minutes", 

# "minutes:seconds", 

# "hours:minutes:seconds", 

if ':' not in s: 

minutes = int(float(s)) 

elif s.count(':') == 1: 

minutes, seconds = map(int, s.split(':')) 

elif s.count(':') == 2: 

hours, minutes, seconds = map(int, s.split(':')) 

else: 

raise ValueError("More than 2 ':' in string!") 

 

return Time((days*24 + hours)*3600 + minutes*60 + seconds, "s") 

 

 

def time2slurm(timeval, unit="s"): 

""" 

Convert a number representing a time value in the given unit (Default: seconds) 

to a string following the slurm convention: "days-hours:minutes:seconds". 

 

>>> assert time2slurm(61) == '0-0:1:1' and time2slurm(60*60+1) == '0-1:0:1' 

>>> assert time2slurm(0.5, unit="h") == '0-0:30:0' 

""" 

d, h, m, s = 24*3600, 3600, 60, 1 

 

timeval = Time(timeval, unit).to("s") 

days, hours = divmod(timeval, d) 

hours, minutes = divmod(hours, h) 

minutes, secs = divmod(minutes, m) 

 

return "%d-%d:%d:%d" % (days, hours, minutes, secs) 

 

 

def time2pbspro(timeval, unit="s"): 

""" 

Convert a number representing a time value in the given unit (Default: seconds) 

to a string following the PbsPro convention: "hours:minutes:seconds". 

 

>>> assert time2pbspro(2, unit="d") == '48:0:0'  

""" 

h, m, s = 3600, 60, 1 

 

timeval = Time(timeval, unit).to("s") 

hours, minutes = divmod(timeval, h) 

minutes, secs = divmod(minutes, m) 

 

return "%d:%d:%d" % (hours, minutes, secs) 

 

 

def timelimit_parser(s): 

"""Convert a float or a string into time in seconds.""" 

try: 

return Time(float(s), "s") 

except ValueError: 

return slurm_parse_timestr(s) 

 

 

def any2mb(s): 

"""Convert string or number to memory in megabytes.""" 

if is_string(s): 

return int(Memory.from_string(s).to("Mb")) 

else: 

return int(s)