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

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

# Copyright (C) 2015 Chintalagiri Shashank 

# 

# This file is part of Tendril. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU Affero General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

# GNU Affero General Public License for more details. 

# 

# You should have received a copy of the GNU Affero General Public License 

# along with this program.  If not, see <http://www.gnu.org/licenses/>. 

""" 

This file is part of tendril 

See the COPYING, README, and INSTALL files for more information 

""" 

 

import arrow.arrow 

 

from .unitbase import NumericalUnitBase 

from .unitbase import parse_none 

 

from decimal import Decimal 

from numbers import Number 

import re 

 

 

def parse_frequency(string): 

    rex = re.compile(r'^((?P<number>\d+(.\d+)*)(?P<order>[mkMG])?Hz)$') 

    try: 

        rdict = rex.search(string).groupdict() 

        num = Decimal(rdict['number']) 

        order = rdict['order'] 

        if order == 'm': 

            return num / 1000 

        elif order == 'k': 

            return num * 1000 

        elif order == 'M': 

            return num * 1000000 

        elif order == 'G': 

            return num * 1000000000 

        elif order is None: 

            return num 

    except: 

        raise ValueError 

 

 

class Frequency(NumericalUnitBase): 

    def __init__(self, value): 

        _ostrs = ['mHz', 'Hz', 'kHz', 'MHz', 'GHz'] 

        _dostr = 'Hz' 

        _parse_func = parse_frequency 

        super(Frequency, self).__init__(value, _ostrs, _dostr, _parse_func) 

 

    def __rdiv__(self, other): 

        if isinstance(other, Number): 

            return TimeSpan(Decimal(other) / self._value) 

 

    def __rtruediv__(self, other): 

        return self.__rdiv__(other) 

 

 

class TimeSpan(NumericalUnitBase): 

    def __init__(self, value): 

        _ostrs = None 

        _dostr = None 

        _parse_func = parse_none 

        if isinstance(value, TimeDelta): 

            if value.years != 0 or value.months != 0: 

                raise ValueError( 

                    'TimeDelta instances used to construct TimeSpan instances' 

                    'cannot have non-zero years and months.' 

                ) 

            value = value.microseconds / Decimal('1000000.0') + value.seconds + \ 

                value.minutes * 60 + value.hours * 3600 + \ 

                value.days * 3600 * 24 

        elif not isinstance(value, Number): 

            raise TypeError("Only numerical time spans (in seconds) " 

                            "are supported at this time") 

        super(TimeSpan, self).__init__(value, _ostrs, _dostr, _parse_func) 

 

    def __repr__(self): 

        return repr(self.timedelta) 

 

    @property 

    def timedelta(self): 

        seconds = int(self._value) 

        microseconds = int((self._value-seconds) * 1000000) 

        return TimeDelta(seconds=seconds, microseconds=microseconds) 

 

 

class TimeStamp(arrow.arrow.Arrow): 

    def __sub__(self, other): 

        if isinstance(other, TimeDelta): 

            return self.clone().replace(years=-other.years, 

                                        months=-other.months, 

                                        days=-other.days, 

                                        hours=-other.hours, 

                                        minutes=-other.minutes, 

                                        seconds=-other.seconds, 

                                        microseconds=-other.microseconds) 

        elif isinstance(other, TimeStamp): 

            return TimeDelta(years=self.year-other.year, 

                             months=self.month-other.month, 

                             days=self.day-other.day, 

                             hours=self.hour-other.hour, 

                             minutes=self.minute-other.minute, 

                             seconds=self.second-other.second, 

                             microseconds=self.microsecond-other.microsecond) 

        else: 

            raise NotImplementedError 

 

    def __add__(self, other): 

        if isinstance(other, TimeDelta): 

            return self.clone().replace(years=other.years, 

                                        months=other.months, 

                                        days=other.days, 

                                        hours=other.hours, 

                                        minutes=other.minutes, 

                                        seconds=other.seconds, 

                                        microseconds=other.microseconds) 

        elif isinstance(other, TimeStamp): 

            # return self.clone().replace(years=other.year, 

            #                             months=other.month, 

            #                             days=other.day, 

            #                             hours=other.hour, 

            #                             minutes=other.minute, 

            #                             seconds=other.second, 

            #                             microseconds=other.microsecond) 

            raise ValueError 

        else: 

            raise NotImplementedError("Add not implemented for " + 

                                      repr(self.__class__) + " + " + 

                                      repr(other.__class__)) 

 

 

class TimeDelta(object): 

    def __init__(self, years=0, months=0, days=0, hours=0, minutes=0, 

                 seconds=0, microseconds=0): 

        self.years = years 

        self.months = months 

        self.days = days 

        self.hours = hours 

        self.minutes = minutes 

        self.seconds = seconds 

        self.microseconds = microseconds 

 

    def __sub__(self, other): 

        if isinstance(other, TimeDelta): 

            return TimeDelta( 

                years=self.years - other.years, 

                months=self.months - other.months, 

                days=self.days - other.days, 

                hours=self.hours - other.hours, 

                minutes=self.minutes - other.minutes, 

                seconds=self.seconds - other.seconds, 

                microseconds=self.microseconds - other.microseconds 

            ) 

        else: 

            raise NotImplementedError 

 

    def __add__(self, other): 

        if isinstance(other, TimeDelta): 

            return TimeDelta( 

                years=self.years + other.years, 

                months=self.months + other.months, 

                days=self.days + other.days, 

                hours=self.hours + other.hours, 

                minutes=self.minutes + other.minutes, 

                seconds=self.seconds + other.seconds, 

                microseconds=self.microseconds + other.microseconds 

            ) 

        else: 

            raise NotImplementedError 

 

    def __repr__(self): 

        return ':'.join( 

            [str(self.years), str(self.months), str(self.days), 

             str(self.hours), str(self.minutes), str(self.seconds), 

             str(self.microseconds)] 

        ) 

 

    @property 

    def timespan(self): 

        return TimeSpan(self) 

 

 

timestamp_factory = arrow.ArrowFactory(TimeStamp)