Skip to content

Commit 3780ab3

Browse files
author
Darren Dale
committed
fixed Quantity comparison operators
beginning to add conversions for all units
1 parent 55938dd commit 3780ab3

File tree

12 files changed

+350
-162
lines changed

12 files changed

+350
-162
lines changed

quantities/quantity.py

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,19 +251,67 @@ def __iter__(self):
251251
return QuantityIterator(self)
252252

253253
def __lt__(self, other):
254-
return self.simplified.magnitude < other.simplified.magnitude
254+
try:
255+
ss, os = self.simplified, other.simplified
256+
assert ss.units == os.units
257+
return ss.magnitude < os.magnitude
258+
except AssertionError:
259+
raise TypeError(
260+
'can not compare quantities with units of %s and %s'\
261+
%(ss.units, os.units)
262+
)
255263

256264
def __le__(self, other):
257-
return self.simplified.magnitude <= other.simplified.magnitude
265+
try:
266+
ss, os = self.simplified, other.simplified
267+
assert ss.units == os.units
268+
return ss.magnitude <= os.magnitude
269+
except AssertionError:
270+
raise TypeError(
271+
'can not compare quantities with units of %s and %s'\
272+
%(ss.units, os.units)
273+
)
258274

259275
def __eq__(self, other):
260-
return self.simplified.magnitude == other.simplified.magnitude
276+
try:
277+
ss, os = self.simplified, other.simplified
278+
assert ss.units == os.units
279+
return ss.magnitude == os.magnitude
280+
except AssertionError:
281+
raise TypeError(
282+
'can not compare quantities with units of %s and %s'\
283+
%(ss.units, os.units)
284+
)
261285

262286
def __ne__(self, other):
263-
return self.simplified.magnitude != other.simplified.magnitude
287+
try:
288+
ss, os = self.simplified, other.simplified
289+
assert ss.units == os.units
290+
return ss.magnitude != os.magnitude
291+
except AssertionError:
292+
raise TypeError(
293+
'can not compare quantities with units of %s and %s'\
294+
%(ss.units, os.units)
295+
)
264296

265297
def __gt__(self, other):
266-
return self.simplified.magnitude > other.simplified.magnitude
298+
try:
299+
ss, os = self.simplified, other.simplified
300+
assert ss.units == os.units
301+
return ss.magnitude > os.magnitude
302+
except AssertionError:
303+
raise TypeError(
304+
'can not compare quantities with units of %s and %s'\
305+
%(ss.units, os.units)
306+
)
267307

268308
def __ge__(self, other):
269-
return self.simplified.magnitude >= other.simplified.magnitude
309+
try:
310+
ss, os = self.simplified, other.simplified
311+
assert ss.units == os.units
312+
return ss.magnitude >= os.magnitude
313+
except AssertionError:
314+
raise TypeError(
315+
'can not compare quantities with units of %s and %s'\
316+
%(ss.units, os.units)
317+
)

quantities/units/acceleration.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from quantities.units.time import s
66
from quantities.units.length import m
77

8-
g = gravity = UnitQuantity('g', 9.806650*m/s**2)
9-
force = free_fall = standard_free_fall = gp = dynamic = geopotential = g
8+
g = gravity = force = free_fall = standard_free_fall = gp = dynamic = \
9+
geopotential = \
10+
UnitQuantity('g', 9.806650*m/s**2) # TODO: check
1011

1112
del m, s, UnitQuantity

quantities/units/angle.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
"""
22
"""
33

4+
from numpy import pi
5+
46
from quantities.units.unitquantity import UnitAngle, UnitQuantity
57

6-
turn = circle = UnitAngle('turn')
7-
radian = radians = UnitAngle('radian')
8+
radian = radians = \
9+
UnitAngle('radian')
10+
turn = turns = circle = circles = \
11+
UnitAngle('turn', 2*pi*radian)
812
arcdeg = arcdegs = degree = degrees = angular_degree = angular_degrees = \
9-
UnitAngle('arcdeg')
13+
UnitAngle('arcdeg', pi/180*radian)
1014
arcmin = arcmins = arcminute = arcminutes = angular_minute = \
11-
angular_minutes = UnitAngle('arcmin')
15+
angular_minutes = \
16+
UnitAngle('arcmin', arcdeg/60)
1217
arcsec = arcsecs = arcseconds = arcseconds = angular_second = \
13-
angular_seconds = UnitAngle('arcsec')
14-
grade = grades = UnitAngle('grade')
18+
angular_seconds = \
19+
UnitAngle('arcsec', arcmin/60)
20+
grade = grades = \
21+
UnitAngle('grade', 0.9*arcdeg)
1522

16-
degree_north = degrees_north = degree_N = degrees_N = UnitAngle('degrees_N')
17-
degree_east = degrees_east = degree_E= degrees_E = UnitAngle('degrees_E')
18-
degree_west = degrees_west = degree_W= degrees_W = UnitAngle('degrees_W')
19-
degree_true = degrees_true = degree_T = degrees_T = UnitAngle('degrees_T')
23+
degree_north = degrees_north = degree_N = degrees_N = \
24+
UnitAngle('degrees_N', arcdeg)
25+
degree_east = degrees_east = degree_E= degrees_E = \
26+
UnitAngle('degrees_E', arcdeg)
27+
degree_west = degrees_west = degree_W= degrees_W = \
28+
UnitAngle('degrees_W', arcdeg)
29+
degree_true = degrees_true = degree_T = degrees_T = \
30+
UnitAngle('degrees_T', arcdeg)
2031

21-
sr = steradian = steradians = UnitQuantity('sr')
32+
sr = steradian = steradians = \
33+
UnitQuantity('sr', radian**2)
2234

2335
del UnitQuantity

quantities/units/compound.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,3 @@
22
"""
33

44
from quantities.units.unitquantity import UnitQuantity
5-
6-
7-
pc_per_cc = UnitQuantity('pc/cm^3')
8-
sqm_per_cm = UnitQuantity('m^2/m^3')

quantities/units/electromagnetism.py

Lines changed: 74 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,82 @@
88
from quantities.units.energy import J
99

1010

11-
A = amp = amps = ampere = amperes = UnitCurrent('A')
12-
mA = milliamp = milliamps = UnitCurrent('mA')
13-
uA = microamp = microamps = UnitCurrent('uA')
14-
nA = nanoamp = nanoamps = UnitCurrent('nA')
15-
pA = picoamp = picoamps = UnitCurrent('pA')
16-
abampere = UnitCurrent('abampere')
17-
gilbert = UnitCurrent('gilbert')
18-
statampere = UnitCurrent('statampere')
19-
biot = UnitCurrent('biot')
11+
A = amp = amps = ampere = amperes = \
12+
UnitCurrent('A')
13+
mA = milliamp = milliamps = \
14+
UnitCurrent('mA', A/1000)
15+
uA = microamp = microamps = \
16+
UnitCurrent('uA', mA/1000)
17+
nA = nanoamp = nanoamps = \
18+
UnitCurrent('nA', uA/1000)
19+
pA = picoamp = picoamps = \
20+
UnitCurrent('pA', nA/1000)
21+
abampere = \
22+
UnitCurrent('abampere', 10*A)
23+
gilbert = \
24+
UnitCurrent('gilbert', 7.957747e-1*A) # TODO: check
25+
statampere = \
26+
UnitCurrent('statampere', 3.335640e-10*A) # TODO: check
27+
biot = \
28+
UnitCurrent('biot', 10*A)
2029

21-
cd = candle = candles = candela = candelas = UnitLuminousIntensity('cd')
30+
cd = candle = candles = candela = candelas = \
31+
UnitLuminousIntensity('cd')
2232

23-
C = coulomb = UnitQuantity('C', A*s)
24-
V = volt = UnitQuantity('V', J/s/A)
25-
F = farad = UnitQuantity('F', C/V)
26-
ohm = ohms = UnitQuantity('ohm', V/A)
27-
S = siemens = UnitQuantity('S', A/V)
28-
Wb = weber = webers = UnitQuantity('Wb', V*s)
29-
T = tesla = teslas = UnitQuantity('T', Wb/m**2)
30-
H = henry = henrys = UnitQuantity('H', Wb/A)
31-
abfarad = abfarads = UnitQuantity('abfarad', 1e9*farad)
32-
abhenry = abhenry = UnitQuantity('abhenry', 1e-9*henry)
33-
abmho = abmhos = UnitQuantity('abmho', 1e9*S)
34-
abohm = abohms = UnitQuantity('abohm', 1e-9*ohm)
35-
abvolt = abvolts = UnitQuantity('abvolt', 1e-8*V)
33+
C = coulomb = \
34+
UnitQuantity('C', A*s)
35+
V = volt = \
36+
UnitQuantity('V', J/s/A)
37+
F = farad = \
38+
UnitQuantity('F', C/V)
39+
ohm = ohms = \
40+
UnitQuantity('ohm', V/A)
41+
S = siemens = \
42+
UnitQuantity('S', A/V)
43+
Wb = weber = webers = \
44+
UnitQuantity('Wb', V*s)
45+
T = tesla = teslas = \
46+
UnitQuantity('T', Wb/m**2)
47+
H = henry = henrys = \
48+
UnitQuantity('H', Wb/A)
49+
abfarad = abfarads = \
50+
UnitQuantity('abfarad', 1e9*farad)
51+
abhenry = abhenry = \
52+
UnitQuantity('abhenry', 1e-9*henry)
53+
abmho = abmhos = \
54+
UnitQuantity('abmho', 1e9*S)
55+
abohm = abohms = \
56+
UnitQuantity('abohm', 1e-9*ohm)
57+
abvolt = abvolts = \
58+
UnitQuantity('abvolt', 1e-8*V)
3659
e = UnitQuantity('e', 1.60217733-19*C)
37-
chemical_faraday = chemical_faradays = UnitQuantity('chemical_faraday', 9.64957e4*C)
38-
physical_faraday = physical_faradays = UnitQuantity('physical_faraday', 9.65219e4*C)
39-
faraday = faradays = C12_faraday = C12_faradays = UnitQuantity('faraday', 9.648531e4*C)
40-
gamma = gammas = UnitQuantity('gamma', 1e-9*T)
41-
gauss = UnitQuantity('gauss', 1e-4*T)
42-
maxwell = maxwells = UnitQuantity('maxwell', 1e-8*Wb)
43-
Oe = oersted = oersteds = UnitQuantity('Oe', 7.957747e1*A/m)
44-
statcoulomb = statcoulombs = UnitQuantity('statcoulomb', 3.335640e-10*C)
45-
statfarad = statfarads = UnitQuantity('statfarad', 1.112650e-12*F)
46-
stathenry = stathenrys = UnitQuantity('stathenry', 8.987554e11*H)
47-
statmho = statmhos = UnitQuantity('statmho', 1.112650e-12*S)
48-
statohm = statohms = UnitQuantity('statohm', 8.987554e11*ohm)
49-
statvolt = statvolts = UnitQuantity('statvolt', 2.997925e2*V)
50-
unit_pole = unit_poles = UnitQuantity('unit_pole', 1.256637e-7*Wb)
60+
chemical_faraday = chemical_faradays = \
61+
UnitQuantity('chemical_faraday', 9.64957e4*C)
62+
physical_faraday = physical_faradays = \
63+
UnitQuantity('physical_faraday', 9.65219e4*C)
64+
faraday = faradays = C12_faraday = C12_faradays = \
65+
UnitQuantity('faraday', 9.648531e4*C)
66+
gamma = gammas = \
67+
UnitQuantity('gamma', 1e-9*T)
68+
gauss = \
69+
UnitQuantity('gauss', 1e-4*T)
70+
maxwell = maxwells = \
71+
UnitQuantity('maxwell', 1e-8*Wb)
72+
Oe = oersted = oersteds = \
73+
UnitQuantity('Oe', 7.957747e1*A/m)
74+
statcoulomb = statcoulombs = \
75+
UnitQuantity('statcoulomb', 3.335640e-10*C)
76+
statfarad = statfarads = \
77+
UnitQuantity('statfarad', 1.112650e-12*F)
78+
stathenry = stathenrys = \
79+
UnitQuantity('stathenry', 8.987554e11*H)
80+
statmho = statmhos = \
81+
UnitQuantity('statmho', 1.112650e-12*S)
82+
statohm = statohms = \
83+
UnitQuantity('statohm', 8.987554e11*ohm)
84+
statvolt = statvolts = \
85+
UnitQuantity('statvolt', 2.997925e2*V)
86+
unit_pole = unit_poles = \
87+
UnitQuantity('unit_pole', 1.256637e-7*Wb)
5188

5289
del UnitQuantity, s, m, J

quantities/units/energy.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,41 @@
66
from quantities.units.length import cm, m
77
from quantities.units.time import s, h
88

9-
J = joule = joules = UnitQuantity('J', N*m)
10-
erg = UnitQuantity('erg', dyne*cm)
11-
btu = Btu = BTU = btus = BTUs = IT_Btu = IT_Btus = UnitQuantity('Btu', J*1.05505585262e3)
12-
eV = electron_volt = electron_volts = UnitQuantity('eV', J*1.602177e-19)
13-
meV = UnitQuantity('meV', eV/1000)
14-
keV = UnitQuantity('keV', 1000*eV)
15-
MeV = UnitQuantity('MeV', 1000*keV)
16-
bev = GeV = UnitQuantity('GeV', 1000*MeV)
17-
EC_therm = EC_therms = UnitQuantity('EC_therm', 1.05506e8*J)
18-
thermochemical_calorie = thermochemical_calories = UnitQuantity('thermochemical_calorie', 4.184000*J)
19-
cal = calorie = calories = IT_calorie = IT_calories = UnitQuantity('cal', J*4.1868)
20-
ton_TNT = UnitQuantity('ton_TNT', 4.184e9*J)
21-
thm = therm = therms = US_therm = US_therms = UnitQuantity('thm', 1.054804e8*J)
22-
Wh = watthour = watthours = UnitQuantity('Wh', J/s*h)
23-
kWh = kilowatthour = kilowatthours = UnitQuantity('kWh', 1000*Wh)
24-
MWh = megawatthour = megawatthours = UnitQuantity('MWh', 1000*kWh)
25-
GWh = gigawatthour = gigawatthours = UnitQuantity('GWh', 1000*MWh)
26-
E_h = Hartree_energy = UnitQuantity('E_h', 4.35974394e-18*J)
9+
J = joule = joules = \
10+
UnitQuantity('J', N*m)
11+
erg = \
12+
UnitQuantity('erg', dyne*cm)
13+
btu = Btu = BTU = btus = BTUs = IT_Btu = IT_Btus = \
14+
UnitQuantity('Btu', J*1.05505585262e3)
15+
eV = electron_volt = electron_volts = \
16+
UnitQuantity('eV', J*1.602177e-19)
17+
meV = \
18+
UnitQuantity('meV', eV/1000)
19+
keV = \
20+
UnitQuantity('keV', 1000*eV)
21+
MeV = \
22+
UnitQuantity('MeV', 1000*keV)
23+
bev = GeV = \
24+
UnitQuantity('GeV', 1000*MeV)
25+
EC_therm = EC_therms = \
26+
UnitQuantity('EC_therm', 1.05506e8*J)
27+
thermochemical_calorie = thermochemical_calories = \
28+
UnitQuantity('thermochemical_calorie', 4.184000*J)
29+
cal = calorie = calories = IT_calorie = IT_calories = \
30+
UnitQuantity('cal', J*4.1868)
31+
ton_TNT = \
32+
UnitQuantity('ton_TNT', 4.184e9*J)
33+
thm = therm = therms = US_therm = US_therms = \
34+
UnitQuantity('thm', 1.054804e8*J)
35+
Wh = watthour = watthours = \
36+
UnitQuantity('Wh', J/s*h)
37+
kWh = kilowatthour = kilowatthours = \
38+
UnitQuantity('kWh', 1000*Wh)
39+
MWh = megawatthour = megawatthours = \
40+
UnitQuantity('MWh', 1000*kWh)
41+
GWh = gigawatthour = gigawatthours = \
42+
UnitQuantity('GWh', 1000*MWh)
43+
E_h = Hartree_energy = \
44+
UnitQuantity('E_h', 4.35974394e-18*J)
2745

2846
del UnitQuantity, dyne, N, cm, m, s, h

quantities/units/force.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,25 @@
88
from quantities.units.acceleration import g
99

1010

11-
N = newton = newtons = UnitQuantity('N', kg*m/s**2)
12-
dyne = dynes = UnitQuantity('dyne', gram*cm/s**2)
13-
pond = ponds = UnitQuantity('pond', 9.806650e-3*N)
14-
kgf = force_kilogram = kilogram_force = UnitQuantity('kgf', kg*g)
15-
ozf = force_ounce = ounce_force = UnitQuantity('ozf', 2.780139e-1*N)
16-
lbf = force_pound = pound_force = UnitQuantity('lbf', 4.4482216152605*N)
17-
poundal = poundals = UnitQuantity('poundal', 1.382550e-1*N)
18-
gf = gram_force = force_gram = UnitQuantity('gf', gram*g)
19-
force_ton = ton_force = UnitQuantity('force_ton', 2000*force_pound)
20-
kip = UnitQuantity('kip', 1000*lbf)
11+
N = newton = newtons = \
12+
UnitQuantity('N', kg*m/s**2)
13+
dyne = dynes = \
14+
UnitQuantity('dyne', gram*cm/s**2)
15+
pond = ponds = \
16+
UnitQuantity('pond', 9.806650e-3*N)
17+
kgf = force_kilogram = kilogram_force = \
18+
UnitQuantity('kgf', kg*g)
19+
ozf = force_ounce = ounce_force = \
20+
UnitQuantity('ozf', 2.780139e-1*N)
21+
lbf = force_pound = pound_force = \
22+
UnitQuantity('lbf', 4.4482216152605*N)
23+
poundal = poundals = \
24+
UnitQuantity('poundal', 1.382550e-1*N)
25+
gf = gram_force = force_gram = \
26+
UnitQuantity('gf', gram*g)
27+
force_ton = ton_force = \
28+
UnitQuantity('force_ton', 2000*force_pound)
29+
kip = \
30+
UnitQuantity('kip', 1000*lbf)
2131

2232
del UnitQuantity, gram, kg, cm, m, s, g

quantities/units/frequency.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
from quantities.units.time import s, min
66

77

8-
Hz = hertz = rps = UnitQuantity('Hz', s**-1)
9-
kHz = UnitQuantity('kHz', Hz*1000)
10-
MHz = UnitQuantity('MHz', kHz*1000)
11-
GHz = UnitQuantity('GHz', MHz*1000)
12-
rpm = UnitQuantity('rpm', min**-1)
8+
Hz = hertz = rps = \
9+
UnitQuantity('Hz', s**-1)
10+
kHz = \
11+
UnitQuantity('kHz', Hz*1000)
12+
MHz = \
13+
UnitQuantity('MHz', kHz*1000)
14+
GHz = \
15+
UnitQuantity('GHz', MHz*1000)
16+
rpm = \
17+
UnitQuantity('rpm', min**-1)
1318

1419
del UnitQuantity, s, min

quantities/units/heat.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from quantities.units.power import W
88

99

10-
clo = clos = UnitQuantity('clo', 1.55e-1*K*m**2/W)
10+
clo = clos = \
11+
UnitQuantity('clo', 1.55e-1*K*m**2/W)
1112

1213
del UnitQuantity, K, m, W

0 commit comments

Comments
 (0)