Skip to content

Commit 147726b

Browse files
author
Darren Dale
committed
converted all units to use refactored classes
1 parent 073aa0a commit 147726b

28 files changed

+461
-622
lines changed

quantities/__init__.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@
5050
__version__ = '0.1(bzr)'
5151

5252

53-
from quantity import Quantity, ProtectedUnitsError
54-
55-
from parser import unit_registry
56-
57-
import units
58-
from units import *
59-
60-
import constants
61-
from constants import *
53+
#from quantity import Quantity, ProtectedUnitsError
54+
from quantity import Quantity
55+
56+
#from parser import unit_registry
57+
#
58+
#import units
59+
#from units import *
60+
#
61+
#import constants
62+
#from constants import *

quantities/dimensionality.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ def _format_units(self, udict):
4444
d = -d
4545
if d != 1: u = u + ('**%s'%d).rstrip('.0')
4646
den.append(u)
47-
res = ' * '.join(num)
47+
res = '*'.join(num)
4848
if len(den):
4949
if not res: res = '1'
50-
res = res + ' / ' + ' '.join(den)
51-
if not res: res = '(dimensionless)'
52-
return res
50+
res = res + '/' + '*'.join(den)
51+
if not res: res = 'dimensionless'
52+
return '(%s)'%res
5353

5454

5555
def __add__(self, other):
@@ -63,6 +63,8 @@ def __mul__(self, other):
6363
for unit, power in other.iteritems():
6464
try:
6565
new[unit] += power
66+
if new[unit] == 0:
67+
new.pop(unit)
6668
except KeyError:
6769
new[unit] = power
6870
return new
@@ -72,6 +74,8 @@ def __div__(self, other):
7274
for unit, power in other.iteritems():
7375
try:
7476
new[unit] -= power
77+
if new[unit] == 0:
78+
new.pop(unit)
7579
except KeyError:
7680
new[unit] = -power
7781
return new
@@ -100,10 +104,6 @@ def __init__(self, dict=None, **kwds):
100104
if len(kwds):
101105
self.__data.update(kwds)
102106

103-
# del self.__dict__['__init__']
104-
105-
# del __init__
106-
107107
def __repr__(self):
108108
return self._format_units(self.__data)
109109

quantities/quantity.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
"""
22
"""
33

4+
import copy
5+
46
import numpy
57

6-
from dimensionality import MutableDimensionality, ImmutableDimensionality
8+
from quantities.dimensionality import BaseDimensionality, \
9+
MutableDimensionality, ImmutableDimensionality
710

811

912
class HasDimensionality(numpy.ndarray):
@@ -37,6 +40,19 @@ def dimensionality(self):
3740
def magnitude(self):
3841
return self.view(type=numpy.ndarray)
3942

43+
# def __array_finalize__(self, obj):
44+
# self._dimensionality = copy.deepcopy(
45+
# getattr(obj, '_dimensionality', None)
46+
# )
47+
#
48+
# def __deepcopy__(self, memo={}):
49+
# dimensionality = copy.deepcopy(self.dimensionality)
50+
# return self.__class__(
51+
# self.view(type=ndarray),
52+
# self.dtype,
53+
# dimensionality
54+
# )
55+
4056
def __cmp__(self, other):
4157
raise
4258

@@ -74,6 +90,13 @@ def __div__(self, other):
7490
magnitude = self.magnitude / other
7591
return Quantity(magnitude, magnitude.dtype, dims)
7692

93+
def __rmul__(self, other):
94+
# TODO: This needs to be properly implemented
95+
return self.__mul__(other)
96+
97+
def __rdiv__(self, other):
98+
return other * self**-1
99+
77100
def __pow__(self, other):
78101
assert isinstance(other, (numpy.ndarray, int, float))
79102
dims = self.dimensionality**other

quantities/units/__init__.py

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

4-
from quantities.parser import unit_registry
5-
6-
import acceleration
7-
unit_registry.update(acceleration)
8-
from acceleration import *
9-
10-
import angle
11-
unit_registry.update(angle)
12-
from angle import *
13-
14-
import area
15-
unit_registry.update(area)
16-
from area import *
17-
18-
import compound
19-
unit_registry.update(compound)
20-
from compound import *
21-
22-
import dimensionless
23-
unit_registry.update(dimensionless)
24-
from dimensionless import *
25-
26-
import electromagnetism
27-
unit_registry.update(electromagnetism)
28-
from electromagnetism import *
29-
30-
import energy
31-
unit_registry.update(energy)
32-
from energy import *
33-
34-
import force
35-
unit_registry.update(force)
36-
from force import *
37-
38-
import frequency
39-
unit_registry.update(frequency)
40-
from frequency import *
41-
42-
import heat
43-
unit_registry.update(heat)
44-
from heat import *
45-
46-
import information
47-
unit_registry.update(information)
48-
from information import *
49-
50-
import length
51-
unit_registry.update(length)
52-
from length import *
53-
54-
import mass
55-
unit_registry.update(mass)
56-
from mass import *
57-
58-
import power
59-
unit_registry.update(power)
60-
from power import *
61-
62-
import pressure
63-
unit_registry.update(pressure)
64-
from pressure import *
65-
66-
import radiation
67-
unit_registry.update(radiation)
68-
from radiation import *
69-
70-
import substance
71-
unit_registry.update(substance)
72-
from substance import *
73-
74-
import temperature
75-
unit_registry.update(temperature)
76-
from temperature import *
77-
78-
import time
79-
unit_registry.update(time)
80-
from time import *
81-
82-
import velocity
83-
unit_registry.update(velocity)
84-
from velocity import *
85-
86-
import viscosity
87-
unit_registry.update(viscosity)
88-
from viscosity import *
89-
90-
import volume
91-
unit_registry.update(volume)
92-
from volume import *
4+
#from quantities.parser import unit_registry
5+
#
6+
#import acceleration
7+
#unit_registry.update(acceleration)
8+
#from acceleration import *
9+
#
10+
#import angle
11+
#unit_registry.update(angle)
12+
#from angle import *
13+
#
14+
#import area
15+
#unit_registry.update(area)
16+
#from area import *
17+
#
18+
#import compound
19+
#unit_registry.update(compound)
20+
#from compound import *
21+
#
22+
#import dimensionless
23+
#unit_registry.update(dimensionless)
24+
#from dimensionless import *
25+
#
26+
#import electromagnetism
27+
#unit_registry.update(electromagnetism)
28+
#from electromagnetism import *
29+
#
30+
#import energy
31+
#unit_registry.update(energy)
32+
#from energy import *
33+
#
34+
#import force
35+
#unit_registry.update(force)
36+
#from force import *
37+
#
38+
#import frequency
39+
#unit_registry.update(frequency)
40+
#from frequency import *
41+
#
42+
#import heat
43+
#unit_registry.update(heat)
44+
#from heat import *
45+
#
46+
#import information
47+
#unit_registry.update(information)
48+
#from information import *
49+
#
50+
#import length
51+
#unit_registry.update(length)
52+
#from length import *
53+
#
54+
#import mass
55+
#unit_registry.update(mass)
56+
#from mass import *
57+
#
58+
#import power
59+
#unit_registry.update(power)
60+
#from power import *
61+
#
62+
#import pressure
63+
#unit_registry.update(pressure)
64+
#from pressure import *
65+
#
66+
#import radiation
67+
#unit_registry.update(radiation)
68+
#from radiation import *
69+
#
70+
#import substance
71+
#unit_registry.update(substance)
72+
#from substance import *
73+
#
74+
#import temperature
75+
#unit_registry.update(temperature)
76+
#from temperature import *
77+
#
78+
#import time
79+
#unit_registry.update(time)
80+
#from time import *
81+
#
82+
#import velocity
83+
#unit_registry.update(velocity)
84+
#from velocity import *
85+
#
86+
#import viscosity
87+
#unit_registry.update(viscosity)
88+
#from viscosity import *
89+
#
90+
#import volume
91+
#unit_registry.update(volume)
92+
#from volume import *

quantities/units/acceleration.py

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

4-
from quantities.units.unitquantities import compound
4+
from quantities.units.unitquantity import UnitQuantity
55
from quantities.units.time import s
66
from quantities.units.length import m
77

8-
g = force = gravity = free_fall = standard_free_fall = 9.806650 * m/s**2
9-
gp = dynamic = geopotential = gravity
8+
g = gravity = UnitQuantity('g', 9.806650*m/s**2)
9+
force = free_fall = standard_free_fall = gp = dynamic = geopotential = g
1010

11-
g_ = force_ = gravity_ = free_fall_ = standard_free_fall_ = compound('g')
12-
gp_ = dynamic_ = geopotential_ = compound('gp')
13-
14-
del m, s, compound
11+
del m, s, UnitQuantity

quantities/units/angle.py

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

4-
from quantities.units.unitquantities import angle, compound
4+
from quantities.units.unitquantity import UnitAngle, UnitQuantity
55

6-
turn = circle = angle('turn')
7-
radian = radians = angle('radian')
6+
turn = circle = UnitAngle('turn')
7+
radian = radians = UnitAngle('radian')
88
arcdeg = arcdegs = degree = degrees = angular_degree = angular_degrees = \
9-
angle('arcdeg')
9+
UnitAngle('arcdeg')
1010
arcmin = arcmins = arcminute = arcminutes = angular_minute = \
11-
angular_minutes = angle('arcmin')
11+
angular_minutes = UnitAngle('arcmin')
1212
arcsec = arcsecs = arcseconds = arcseconds = angular_second = \
13-
angular_seconds = angle('arcsec')
14-
grade = grades = angle('grade')
13+
angular_seconds = UnitAngle('arcsec')
14+
grade = grades = UnitAngle('grade')
1515

16-
degree_north = degrees_north = degree_N = degrees_N = angle('degrees_N')
17-
degree_east = degrees_east = degree_E= degrees_E = angle('degrees_E')
18-
degree_west = degrees_west = degree_W= degrees_W = angle('degrees_W')
19-
degree_true = degrees_true = degree_T = degrees_T = angle('degrees_T')
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')
2020

21-
sr = steradian = steradians = radian**2
21+
sr = steradian = steradians = UnitQuantity('steradian')
2222

23-
del compound
23+
del UnitQuantity

quantities/units/area.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
"""
22
"""
33

4-
from quantities.units.unitquantities import compound
4+
from quantities.units.unitquantity import UnitQuantity
55
from quantities.units.length import m, rod
66

7-
are = ares = 100 * m**2
8-
b = barn = barns = 1e-28 * m**2
9-
circular_mil = 5.067075e-10 *m**2
10-
darcy = 9.869233e-13 * m**2
11-
hectare = 1e4 * m**2
12-
acre = acres = 160 * rod**2
7+
are = ares = UnitQuantity('are', 100*m**2)
8+
b = barn = barns_ = UnitQuantity('b', 1e-28*m**2)
9+
circular_mil = UnitQuantity('circular_mil', 5.067075e-10*m**2)
10+
darcy = UnitQuantity('darcy', 9.869233e-13*m**2)
11+
hectare = UnitQuantity('hectare', 1e4*m**2)
12+
acre = acres = UnitQuantity('acre', 160*rod**2)
1313

14-
kayser = 1e2/m
14+
kayser = UnitQuantity('kayser', 1e2/m)
1515

16-
are_ = ares_ = compound('are')
17-
b_ = barn_ = barns_ = compound('b')
18-
circular_mil_ = compound('circular_mil')
19-
darcy_ = compound('darcy')
20-
hectare_ = compound('hectare')
21-
acre_ = acres_ = compound('acre')
22-
23-
kayser_ = compound('kayser')
24-
25-
del compound, m, rod
16+
del UnitQuantity, m, rod

0 commit comments

Comments
 (0)