2019-11-18 12:00:08 +02:00
|
|
|
from decimal import Decimal as D
|
2019-11-18 11:36:19 +02:00
|
|
|
|
|
|
|
class Deg:
|
|
|
|
def __str__(self):
|
2019-11-18 13:49:37 +02:00
|
|
|
return "%03d-%02d-%4.2f" % (self.deg, self.mm, self.ss)
|
2019-11-18 11:36:19 +02:00
|
|
|
|
|
|
|
def __init__(self, deg, mm, ss):
|
2019-11-18 12:00:08 +02:00
|
|
|
self.deg = D(deg)
|
|
|
|
self.mm = D(mm)
|
|
|
|
self.ss = D(ss)
|
2019-11-18 11:36:19 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_1(cls, deg):
|
2019-11-18 12:00:08 +02:00
|
|
|
assert isinstance(deg, D)
|
2019-11-18 11:36:19 +02:00
|
|
|
|
|
|
|
pdeg, pmm = divmod(deg, 1)
|
2019-11-18 12:00:08 +02:00
|
|
|
pmm = pmm * D(60)
|
2019-11-18 11:36:19 +02:00
|
|
|
pmm, pss = divmod(pmm, 1)
|
2019-11-18 12:00:08 +02:00
|
|
|
pss = pss * D(60)
|
2019-11-18 11:36:19 +02:00
|
|
|
return cls(pdeg, pmm, pss)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def guess(cls, instr):
|
|
|
|
if '-' in instr:
|
|
|
|
deg, mm, ss = instr.split('-')
|
|
|
|
return Deg(deg, mm, ss)
|
2019-11-18 12:00:08 +02:00
|
|
|
return cls.from_1(D(instr))
|
2019-11-18 11:36:19 +02:00
|
|
|
|
2019-11-18 12:00:08 +02:00
|
|
|
@property
|
2019-11-18 11:36:19 +02:00
|
|
|
def frac(self):
|
|
|
|
return (
|
|
|
|
self.deg +
|
2019-11-18 12:00:08 +02:00
|
|
|
self.mm / D(60) +
|
|
|
|
self.ss / D(3600)).normalize()
|
2019-11-18 11:36:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
class TestDeg(unittest.TestCase):
|
|
|
|
def test_deg(self):
|
2019-11-18 12:00:08 +02:00
|
|
|
w = Deg.guess('125.505')
|
2019-11-18 11:36:19 +02:00
|
|
|
self.assertEqual(125, w.deg)
|
|
|
|
self.assertEqual(30, w.mm)
|
2019-11-18 12:00:08 +02:00
|
|
|
self.assertEqual(18, w.ss)
|
|
|
|
|
|
|
|
def test_guess(self):
|
|
|
|
w = Deg.guess('112-58-25.4')
|
|
|
|
self.assertEqual(112, w.deg)
|
|
|
|
self.assertEqual(58, w.mm)
|
|
|
|
self.assertEqual(D('25.4'), w.ss)
|
|
|
|
frac = "%3.7f" % w.frac
|
|
|
|
self.assertEqual('112.9737222', frac)
|
2019-11-18 11:36:19 +02:00
|
|
|
|
|
|
|
def test_str_lower(self):
|
|
|
|
w = Deg(120, 1, 42.541)
|
|
|
|
self.assertEqual("120-01-42.54", str(w))
|
|
|
|
|
|
|
|
def test_str_upper(self):
|
|
|
|
w = Deg(120, 1, 42.545)
|
|
|
|
self.assertEqual("120-01-42.55", str(w))
|