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

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

# 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 

""" 

 

from decimal import Decimal 

from decimal import InvalidOperation 

 

from unitbase import UnitBase 

from unitbase import NumericalUnitBase 

from unitbase import Percentage 

from unitbase import GainBase 

from unitbase import parse_none 

 

 

def parse_resistance(value): 

    num_val = Decimal(value[:-1]) 

    ostr = value[-1:] 

    if ostr == 'm': 

        return num_val / 1000 

    elif ostr == 'E': 

        return num_val 

    elif ostr == 'K': 

        return num_val * 1000 

    elif ostr == 'M': 

        return num_val * 1000 * 1000 

 

 

def parse_capacitance(value): 

    num_val = Decimal(value[:-2]) 

    ostr = value[-2:] 

    if ostr == 'pF': 

        return num_val / 1000 

    elif ostr == 'nF': 

        return num_val 

    elif ostr == 'uF': 

        return num_val * 1000 

    elif ostr == 'mF': 

        return num_val * 1000 * 1000 

 

 

def parse_voltage(value): 

    value = value.strip() 

 

    try: 

        num_val = Decimal(value[:-1]) 

        ostr = value[-1:] 

    except InvalidOperation: 

        num_val = Decimal(value[:-2]) 

        ostr = value[-2:] 

 

    if ostr == 'V': 

        return num_val 

    elif ostr == 'mV': 

        return num_val / 1000 

    elif ostr == 'uV': 

        return num_val / 1000000 

    elif ostr == 'nV': 

        return num_val / 1000000000 

    elif ostr == 'pV': 

        return num_val / 1000000000000 

    elif ostr == 'kV': 

        return num_val * 1000 

    else: 

        raise ValueError 

 

 

def parse_current(value): 

    value = value.strip() 

    try: 

        num_val = Decimal(value[:-1]) 

        ostr = value[-1:] 

    except InvalidOperation: 

        num_val = Decimal(value[:-2]) 

        ostr = value[-2:] 

 

    if ostr == 'A': 

        return num_val * 1000 

    elif ostr == 'mA': 

        return num_val 

    elif ostr == 'uA': 

        return num_val / 1000 

    elif ostr == 'nA': 

        return num_val / 1000000 

    elif ostr == 'pA': 

        return num_val / 1000000000 

    elif ostr == 'fA': 

        return num_val / 1000000000000 

    else: 

        raise ValueError 

 

 

def parse_dbm(value): 

    num_val = Decimal(value[:-3]) 

    ostr = value[-3:] 

    if ostr == 'dBm': 

        return num_val 

 

 

def parse_hfe(value): 

    num_val = Decimal(value[:-3]) 

    ostr = value[-3:] 

    if ostr == 'HFE': 

        return num_val 

 

 

class Resistance(NumericalUnitBase): 

    def __init__(self, value): 

        _ostrs = ['m', 'E', 'K', 'M'] 

        _dostr = 'E' 

        _parse_func = parse_resistance 

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

 

 

class Capacitance(NumericalUnitBase): 

    def __init__(self, value): 

        _ostrs = ['pF', 'nF', 'uF', 'mF'] 

        _dostr = 'nF' 

        _parse_func = parse_capacitance 

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

 

 

class Voltage(NumericalUnitBase): 

    def __init__(self, value): 

        _ostrs = ['pV', 'nV', 'uV', 'mV', 'V', 'kV'] 

        _dostr = 'V' 

        _parse_func = parse_voltage 

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

 

 

class VoltageAC(Voltage): 

    pass 

 

 

class VoltageDC(Voltage): 

    pass 

 

 

class DiodeVoltageDC(VoltageDC): 

    pass 

 

 

class Current(NumericalUnitBase): 

    def __init__(self, value): 

        _ostrs = ['fA', 'pA', 'nA', 'uA', 'mA', 'A'] 

        _dostr = 'mA' 

        _parse_func = parse_current 

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

 

 

class CurrentAC(Current): 

    pass 

 

 

class CurrentDC(Current): 

    pass 

 

 

def parse_voltage_gain(value): 

    try: 

        return Decimal(value) 

    except InvalidOperation: 

        if value.endswith('V/V'): 

            return Decimal(value[:-3]) 

        elif value.endswith('dB'): 

            v = Decimal(value[:-2]) 

            return 10 ** (v/20) 

        else: 

            raise ValueError( 

                "Unrecognized string for VoltageGain : " + value 

            ) 

 

 

class VoltageGain(GainBase): 

    def __init__(self, value): 

        _gtype = Voltage 

        _ostrs = ['V/V'] 

        _dostr = 'V/V' 

        _parse_func = parse_voltage_gain 

        super(VoltageGain, self).__init__( 

            value, _ostrs, _dostr, _parse_func, _gtype 

        ) 

 

 

class PowerRatio(NumericalUnitBase): 

    def __init__(self, value): 

        _ostrs = ['dBm'] 

        _dostr = 'dBm' 

        _parse_func = parse_dbm 

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

 

    def __repr__(self): 

        return str(self._value) + self._dostr 

 

 

class HFE(UnitBase): 

    def __init__(self, value): 

        _dostr = 'HFE' 

        _parse_func = parse_hfe 

        super(HFE, self).__init__(value, _dostr, _parse_func) 

 

    def __repr__(self): 

        return str(self._value) + self._dostr 

 

 

class Continuity(UnitBase): 

    def __init__(self, value): 

        _dostr = None 

        _parse_func = parse_none 

        super(Continuity, self).__init__(value, _dostr, _parse_func) 

 

    def __repr__(self): 

        return str(self._value) 

 

 

class DutyCycle(Percentage): 

    pass