|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# Copyright (c) 2015 Ralph Hempel |
| 3 | +# |
| 4 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +# of this software and associated documentation files (the "Software"), to deal |
| 6 | +# in the Software without restriction, including without limitation the rights |
| 7 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +# copies of the Software, and to permit persons to whom the Software is |
| 9 | +# furnished to do so, subject to the following conditions: |
| 10 | +# |
| 11 | +# The above copyright notice and this permission notice shall be included in |
| 12 | +# all copies or substantial portions of the Software. |
| 13 | +# |
| 14 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +# THE SOFTWARE. |
| 21 | +# ----------------------------------------------------------------------------- |
| 22 | + |
| 23 | + |
| 24 | +import sys |
| 25 | + |
| 26 | +if sys.version_info < (3,4): |
| 27 | + raise SystemError('Must be using Python 3.4 or higher') |
| 28 | + |
| 29 | +CENTIMETER_MM = 10 |
| 30 | +DECIMETER_MM = 100 |
| 31 | +METER_MM = 1000 |
| 32 | +INCH_MM = 25.4 |
| 33 | +FOOT_MM = 304.8 |
| 34 | +YARD_MM = 914.4 |
| 35 | +STUD_MM = 8 |
| 36 | + |
| 37 | + |
| 38 | +class DistanceValue(object): |
| 39 | + """ |
| 40 | + A base class for other unit types. Don't use this directly; instead, see |
| 41 | + :class:`DistanceMillimeters`, :class:`DistanceCentimeters`, :class:`DistanceDecimeters`, :class:`DistanceMeters`, |
| 42 | + :class:`DistanceInches`, :class:`DistanceFeet`, :class:`DistanceYards` and :class:`DistanceStuds`. |
| 43 | + """ |
| 44 | + |
| 45 | + # This allows us to sort lists of DistanceValue objects |
| 46 | + def __lt__(self, other): |
| 47 | + return self.mm < other.mm |
| 48 | + |
| 49 | + def __rmul__(self, other): |
| 50 | + return self.__mul__(other) |
| 51 | + |
| 52 | + |
| 53 | +class DistanceMillimeters(DistanceValue): |
| 54 | + """ |
| 55 | + Distance in millimeters |
| 56 | + """ |
| 57 | + |
| 58 | + def __init__(self, millimeters): |
| 59 | + self.millimeters = millimeters |
| 60 | + |
| 61 | + def __str__(self): |
| 62 | + return str(self.millimeters) + "mm" |
| 63 | + |
| 64 | + def __mul__(self, other): |
| 65 | + assert isinstance(other, (float, int)), "{} can only be multiplied by an int or float".format(self) |
| 66 | + return DistanceMillimeters(self.millimeters * other) |
| 67 | + |
| 68 | + @property |
| 69 | + def mm(self): |
| 70 | + return self.millimeters |
| 71 | + |
| 72 | + |
| 73 | +class DistanceCentimeters(DistanceValue): |
| 74 | + """ |
| 75 | + Distance in centimeters |
| 76 | + """ |
| 77 | + |
| 78 | + def __init__(self, centimeters): |
| 79 | + self.centimeters = centimeters |
| 80 | + |
| 81 | + def __str__(self): |
| 82 | + return str(self.centimeters) + "cm" |
| 83 | + |
| 84 | + def __mul__(self, other): |
| 85 | + assert isinstance(other, (float, int)), "{} can only be multiplied by an int or float".format(self) |
| 86 | + return DistanceCentimeters(self.centimeters * other) |
| 87 | + |
| 88 | + @property |
| 89 | + def mm(self): |
| 90 | + return self.centimeters * CENTIMETER_MM |
| 91 | + |
| 92 | + |
| 93 | +class DistanceDecimeters(DistanceValue): |
| 94 | + """ |
| 95 | + Distance in decimeters |
| 96 | + """ |
| 97 | + |
| 98 | + def __init__(self, decimeters): |
| 99 | + self.decimeters = decimeters |
| 100 | + |
| 101 | + def __str__(self): |
| 102 | + return str(self.decimeters) + "dm" |
| 103 | + |
| 104 | + def __mul__(self, other): |
| 105 | + assert isinstance(other, (float, int)), "{} can only be multiplied by an int or float".format(self) |
| 106 | + return DistanceDecimeters(self.decimeters * other) |
| 107 | + |
| 108 | + @property |
| 109 | + def mm(self): |
| 110 | + return self.decimeters * DECIMETER_MM |
| 111 | + |
| 112 | + |
| 113 | +class DistanceMeters(DistanceValue): |
| 114 | + """ |
| 115 | + Distance in meters |
| 116 | + """ |
| 117 | + |
| 118 | + def __init__(self, meters): |
| 119 | + self.meters = meters |
| 120 | + |
| 121 | + def __str__(self): |
| 122 | + return str(self.meters) + "m" |
| 123 | + |
| 124 | + def __mul__(self, other): |
| 125 | + assert isinstance(other, (float, int)), "{} can only be multiplied by an int or float".format(self) |
| 126 | + return DistanceMeters(self.meters * other) |
| 127 | + |
| 128 | + @property |
| 129 | + def mm(self): |
| 130 | + return self.meters * METER_MM |
| 131 | + |
| 132 | + |
| 133 | +class DistanceInches(DistanceValue): |
| 134 | + """ |
| 135 | + Distance in inches |
| 136 | + """ |
| 137 | + |
| 138 | + def __init__(self, inches): |
| 139 | + self.inches = inches |
| 140 | + |
| 141 | + def __str__(self): |
| 142 | + return str(self.inches) + "in" |
| 143 | + |
| 144 | + def __mul__(self, other): |
| 145 | + assert isinstance(other, (float, int)), "{} can only be multiplied by an int or float".format(self) |
| 146 | + return DistanceInches(self.inches * other) |
| 147 | + |
| 148 | + @property |
| 149 | + def mm(self): |
| 150 | + return self.inches * INCH_MM |
| 151 | + |
| 152 | + |
| 153 | +class DistanceFeet(DistanceValue): |
| 154 | + """ |
| 155 | + Distance in feet |
| 156 | + """ |
| 157 | + |
| 158 | + def __init__(self, feet): |
| 159 | + self.feet = feet |
| 160 | + |
| 161 | + def __str__(self): |
| 162 | + return str(self.feet) + "ft" |
| 163 | + |
| 164 | + def __mul__(self, other): |
| 165 | + assert isinstance(other, (float, int)), "{} can only be multiplied by an int or float".format(self) |
| 166 | + return DistanceFeet(self.feet * other) |
| 167 | + |
| 168 | + @property |
| 169 | + def mm(self): |
| 170 | + return self.feet * FOOT_MM |
| 171 | + |
| 172 | + |
| 173 | +class DistanceYards(DistanceValue): |
| 174 | + """ |
| 175 | + Distance in yards |
| 176 | + """ |
| 177 | + |
| 178 | + def __init__(self, yards): |
| 179 | + self.yards = yards |
| 180 | + |
| 181 | + def __str__(self): |
| 182 | + return str(self.yards) + "yd" |
| 183 | + |
| 184 | + def __mul__(self, other): |
| 185 | + assert isinstance(other, (float, int)), "{} can only be multiplied by an int or float".format(self) |
| 186 | + return DistanceYards(self.yards * other) |
| 187 | + |
| 188 | + @property |
| 189 | + def mm(self): |
| 190 | + return self.yards * YARD_MM |
| 191 | + |
| 192 | + |
| 193 | +class DistanceStuds(DistanceValue): |
| 194 | + """ |
| 195 | + Distance in studs |
| 196 | + """ |
| 197 | + |
| 198 | + def __init__(self, studs): |
| 199 | + self.studs = studs |
| 200 | + |
| 201 | + def __str__(self): |
| 202 | + return str(self.studs) + "stud" |
| 203 | + |
| 204 | + def __mul__(self, other): |
| 205 | + assert isinstance(other, (float, int)), "{} can only be multiplied by an int or float".format(self) |
| 206 | + return DistanceStuds(self.studs * other) |
| 207 | + |
| 208 | + @property |
| 209 | + def mm(self): |
| 210 | + return self.studs * STUD_MM |
0 commit comments