/usr/share/pyshared/matplotlib/testing/jpl_units/UnitDbl.py is in python-matplotlib 1.1.1~rc1+git20120423-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | #===========================================================================
#
# UnitDbl
#
#===========================================================================
"""UnitDbl module."""
#===========================================================================
# Place all imports after here.
#
#
# Place all imports before here.
#===========================================================================
#===========================================================================
class UnitDbl:
"""Class UnitDbl in development.
"""
#-----------------------------------------------------------------------
# Unit conversion table. Small subset of the full one but enough
# to test the required functions. First field is a scale factor to
# convert the input units to the units of the second field. Only
# units in this table are allowed.
allowed = {
"m" : ( 0.001, "km" ),
"km" : ( 1, "km" ),
"mile" : ( 1.609344, "km" ),
"rad" : ( 1, "rad" ),
"deg" : ( 1.745329251994330e-02, "rad" ),
"sec" : ( 1, "sec" ),
"min" : ( 60.0, "sec" ),
"hour" : ( 3600, "sec" ),
}
_types = {
"km" : "distance",
"rad" : "angle",
"sec" : "time",
}
#-----------------------------------------------------------------------
def __init__( self, value, units ):
"""Create a new UnitDbl object.
Units are internally converted to km, rad, and sec. The only
valid inputs for units are [ m, km, mile, rad, deg, sec, min, hour ].
The field UnitDbl.value will contain the converted value. Use
the convert() method to get a specific type of units back.
= ERROR CONDITIONS
- If the input units are not in the allowed list, an error is thrown.
= INPUT VARIABLES
- value The numeric value of the UnitDbl.
- units The string name of the units the value is in.
"""
self.checkUnits( units )
data = self.allowed[ units ]
self._value = float( value * data[0] )
self._units = data[1]
#-----------------------------------------------------------------------
def convert( self, units ):
"""Convert the UnitDbl to a specific set of units.
= ERROR CONDITIONS
- If the input units are not in the allowed list, an error is thrown.
= INPUT VARIABLES
- units The string name of the units to convert to.
= RETURN VALUE
- Returns the value of the UnitDbl in the requested units as a floating
point number.
"""
if self._units == units:
return self._value
self.checkUnits( units )
data = self.allowed[ units ]
if self._units != data[1]:
msg = "Error trying to convert to different units.\n" \
" Invalid conversion requested.\n" \
" UnitDbl: %s\n" \
" Units: %s\n" % ( str( self ), units )
raise ValueError( msg )
return self._value / data[0]
#-----------------------------------------------------------------------
def __abs__( self ):
"""Return the absolute value of this UnitDbl."""
return UnitDbl( abs( self._value ), self._units )
#-----------------------------------------------------------------------
def __neg__( self ):
"""Return the negative value of this UnitDbl."""
return UnitDbl( -self._value, self._units )
#-----------------------------------------------------------------------
def __nonzero__( self ):
"""Test a UnitDbl for a non-zero value.
= RETURN VALUE
- Returns true if the value is non-zero.
"""
return self._value.__nonzero__()
#-----------------------------------------------------------------------
def __cmp__( self, rhs ):
"""Compare two UnitDbl's.
= ERROR CONDITIONS
- If the input rhs units are not the same as our units,
an error is thrown.
= INPUT VARIABLES
- rhs The UnitDbl to compare against.
= RETURN VALUE
- Returns -1 if self < rhs, 0 if self == rhs, +1 if self > rhs.
"""
self.checkSameUnits( rhs, "compare" )
return cmp( self._value, rhs._value )
#-----------------------------------------------------------------------
def __add__( self, rhs ):
"""Add two UnitDbl's.
= ERROR CONDITIONS
- If the input rhs units are not the same as our units,
an error is thrown.
= INPUT VARIABLES
- rhs The UnitDbl to add.
= RETURN VALUE
- Returns the sum of ourselves and the input UnitDbl.
"""
self.checkSameUnits( rhs, "add" )
return UnitDbl( self._value + rhs._value, self._units )
#-----------------------------------------------------------------------
def __sub__( self, rhs ):
"""Subtract two UnitDbl's.
= ERROR CONDITIONS
- If the input rhs units are not the same as our units,
an error is thrown.
= INPUT VARIABLES
- rhs The UnitDbl to subtract.
= RETURN VALUE
- Returns the difference of ourselves and the input UnitDbl.
"""
self.checkSameUnits( rhs, "subtract" )
return UnitDbl( self._value - rhs._value, self._units )
#-----------------------------------------------------------------------
def __mul__( self, rhs ):
"""Scale a UnitDbl by a value.
= INPUT VARIABLES
- rhs The scalar to multiply by.
= RETURN VALUE
- Returns the scaled UnitDbl.
"""
return UnitDbl( self._value * rhs, self._units )
#-----------------------------------------------------------------------
def __rmul__( self, lhs ):
"""Scale a UnitDbl by a value.
= INPUT VARIABLES
- lhs The scalar to multiply by.
= RETURN VALUE
- Returns the scaled UnitDbl.
"""
return UnitDbl( self._value * lhs, self._units )
#-----------------------------------------------------------------------
def __div__( self, rhs ):
"""Divide a UnitDbl by a value.
= INPUT VARIABLES
- rhs The scalar to divide by.
= RETURN VALUE
- Returns the scaled UnitDbl.
"""
return UnitDbl( self._value / rhs, self._units )
#-----------------------------------------------------------------------
def __str__( self ):
"""Print the UnitDbl."""
return "%g *%s" % ( self._value, self._units )
#-----------------------------------------------------------------------
def __repr__( self ):
"""Print the UnitDbl."""
return "UnitDbl( %g, '%s' )" % ( self._value, self._units )
#-----------------------------------------------------------------------
def type( self ):
"""Return the type of UnitDbl data."""
return self._types[ self._units ]
#-----------------------------------------------------------------------
def range( start, stop, step=None ):
"""Generate a range of UnitDbl objects.
Similar to the Python range() method. Returns the range [
start, stop ) at the requested step. Each element will be a
UnitDbl object.
= INPUT VARIABLES
- start The starting value of the range.
- stop The stop value of the range.
- step Optional step to use. If set to None, then a UnitDbl of
value 1 w/ the units of the start is used.
= RETURN VALUE
- Returns a list contianing the requested UnitDbl values.
"""
if step is None:
step = UnitDbl( 1, start._units )
elems = []
i = 0
while True:
d = start + i * step
if d >= stop:
break
elems.append( d )
i += 1
return elems
range = staticmethod( range )
#-----------------------------------------------------------------------
def checkUnits( self, units ):
"""Check to see if some units are valid.
= ERROR CONDITIONS
- If the input units are not in the allowed list, an error is thrown.
= INPUT VARIABLES
- units The string name of the units to check.
"""
if units not in self.allowed.keys():
msg = "Input units '%s' are not one of the supported types of %s" \
% ( units, str( self.allowed.keys() ) )
raise ValueError( msg )
#-----------------------------------------------------------------------
def checkSameUnits( self, rhs, func ):
"""Check to see if units are the same.
= ERROR CONDITIONS
- If the units of the rhs UnitDbl are not the same as our units,
an error is thrown.
= INPUT VARIABLES
- rhs The UnitDbl to check for the same units
- func The name of the function doing the check.
"""
if self._units != rhs._units:
msg = "Cannot %s units of different types.\n" \
"LHS: %s\n" \
"RHS: %s" % ( func, self._units, rhs._units )
raise ValueError( msg )
#===========================================================================
|