summaryrefslogtreecommitdiff
path: root/gerbonara/gerber/utils.py
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2022-01-18 01:10:16 +0100
committerjaseg <git@jaseg.de>2022-01-18 01:10:16 +0100
commitd85790bc6d40deb1d52cc5d8a4c178f664635625 (patch)
tree8be76c210974a2dbd1f11c5eecfee015c46f9d83 /gerbonara/gerber/utils.py
parent73a44901c0ef0e94e9465c2f35750ca6f85a4473 (diff)
downloadgerbonara-d85790bc6d40deb1d52cc5d8a4c178f664635625.tar.gz
gerbonara-d85790bc6d40deb1d52cc5d8a4c178f664635625.tar.bz2
gerbonara-d85790bc6d40deb1d52cc5d8a4c178f664635625.zip
Unit code refactor WIP
Diffstat (limited to 'gerbonara/gerber/utils.py')
-rw-r--r--gerbonara/gerber/utils.py22
1 files changed, 13 insertions, 9 deletions
diff --git a/gerbonara/gerber/utils.py b/gerbonara/gerber/utils.py
index 4074c4e..533f0a2 100644
--- a/gerbonara/gerber/utils.py
+++ b/gerbonara/gerber/utils.py
@@ -27,13 +27,13 @@ import os
from math import radians, sin, cos, sqrt, atan2, pi
-class Unit:
+class LengthUnit:
def __init__(self, name, shorthand, this_in_mm):
self.name = name
self.shorthand = shorthand
self.factor = this_in_mm
- def from(self, unit, value):
+ def convert_from(self, unit, value):
if isinstance(unit, str):
unit = units[unit]
@@ -42,26 +42,30 @@ class Unit:
return value * unit.factor / self.factor
- def to(self, unit, value):
+ def convert_to(self, unit, value):
if isinstance(unit, str):
- unit = units[unit]
+ unit = to_unit(unit)
if unit is None:
return value
- return unit.from(self, value)
+ return unit.convert_from(self, value)
+
+ def __call__(self, value, unit):
+ return self.convert_from(unit, value)
def __eq__(self, other):
if isinstance(other, str):
return other.lower() in (self.name, self.shorthand)
else:
- return self == other
+ return id(self) == id(other)
MILLIMETERS_PER_INCH = 25.4
-Inch = Unit('inch', 'in', MILLIMETERS_PER_INCH)
-MM = Unit('millimeter', 'mm', 1)
-units = {'inch': Inch, 'mm': MM}
+Inch = LengthUnit('inch', 'in', MILLIMETERS_PER_INCH)
+MM = LengthUnit('millimeter', 'mm', 1)
+units = {'inch': Inch, 'mm': MM, None: None}
+to_unit = lambda name: units[name]
def decimal_string(value, precision=6, padding=False):