From 4da47be12d36c4f439e9d1ccf3dce1e61bbb02b1 Mon Sep 17 00:00:00 2001 From: Greg Albrecht <gba@onbeep.com> Date: Fri, 20 Sep 2013 12:03:43 -0700 Subject: [PATCH] cleanup --- aprs/__init__.py | 34 +++++++++++++++- aprs/constants.py | 4 +- aprs/decimaldegrees.py | 92 ++++++++++++++++++++++++------------------ aprs/util.py | 16 +++++--- tests/constants.py | 2 +- 5 files changed, 98 insertions(+), 50 deletions(-) diff --git a/aprs/__init__.py b/aprs/__init__.py index 94fd72b..5cde394 100644 --- a/aprs/__init__.py +++ b/aprs/__init__.py @@ -1,11 +1,41 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +# APRS Python Module. + +""" +APRS Python Module. +~~~~ + + +:author: Greg Albrecht W2GMD <gba@onbeep.com> +:copyright: Copyright 2013 OnBeep, Inc. +:license: Apache License, Version 2.0 +:source: <https://github.com/ampledata/aprs> + +""" + +__title__ = 'aprs' +__version__ = '1.0.0' +__build__ = '0x010000' __author__ = 'Greg Albrecht W2GMD <gba@onbeep.com>' +__license__ = 'Apache License, Version 2.0' __copyright__ = 'Copyright 2013 OnBeep, Inc.' -__license__ = 'Apache 2.0' +import logging + from .classes import APRS, APRSKISS -import util + +# Set default logging handler to avoid "No handler found" warnings. +try: # Python 2.7+ + from logging import NullHandler +except ImportError: + class NullHandler(logging.Handler): + """Default logging handler to avoid "No handler found" warnings.""" + def emit(self, record): + """Default logging handler to avoid "No handler found" warnings.""" + pass + +logging.getLogger(__name__).addHandler(NullHandler()) diff --git a/aprs/constants.py b/aprs/constants.py index c5d7bbb..2422014 100644 --- a/aprs/constants.py +++ b/aprs/constants.py @@ -6,8 +6,8 @@ Constants for APRS Module. """ __author__ = 'Greg Albrecht W2GMD <gba@onbeep.com>' +__license__ = 'Apache License, Version 2.0' __copyright__ = 'Copyright 2013 OnBeep, Inc.' -__license__ = 'Apache 2.0' import logging @@ -22,4 +22,4 @@ APRSIS_HTTP_HEADERS = { LOG_LEVEL = logging.DEBUG LOG_FORMAT = ('%(asctime)s %(levelname)s %(name)s.%(funcName)s:%(lineno)d' - ' - %(message)s') \ No newline at end of file + ' - %(message)s') diff --git a/aprs/decimaldegrees.py b/aprs/decimaldegrees.py index 5b1975a..304ed57 100644 --- a/aprs/decimaldegrees.py +++ b/aprs/decimaldegrees.py @@ -1,4 +1,6 @@ +#!/usr/bin/env python # -*- coding: utf-8 -*- + """ PyDecimalDegrees - geographic coordinates conversion utility. @@ -45,9 +47,13 @@ To run doctest units just execut this module script as follows $ python decimaldegrees.py [-v] """ -__revision__ = "$Revision: 1.1 $" + + +__revision__ = '$Revision: 1.1 $' + import decimal as libdecimal + from decimal import Decimal as D @@ -59,39 +65,45 @@ def decimal2dms(decimal_degrees): minutes (2nd element) and seconds (3rd element) will always be positive. Example: - >>> decimal2dms(121.135) - (Decimal('121'), Decimal('8'), Decimal('6.000')) - >>> decimal2dms(-121.135) - (Decimal('-121'), Decimal('8'), Decimal('6.000')) - + + >>> decimal2dms(121.135) + (Decimal('121'), Decimal('8'), Decimal('6.000')) + >>> decimal2dms(-121.135) + (Decimal('-121'), Decimal('8'), Decimal('6.000')) + """ degrees = D(int(decimal_degrees)) - decimal_minutes = libdecimal.getcontext().multiply(\ + decimal_minutes = libdecimal.getcontext().multiply( (D(str(decimal_degrees)) - degrees).copy_abs(), D(60)) minutes = D(int(decimal_minutes)) - seconds = libdecimal.getcontext().multiply(\ + seconds = libdecimal.getcontext().multiply( (decimal_minutes - minutes), D(60)) return (degrees, minutes, seconds) def decimal2dm(decimal_degrees): - """ Converts a floating point number of degrees to the equivalent - number of degrees and minutes, which are returned as a 2-element tuple of decimals. - If 'decimal_degrees' is negative, only degrees (1st element of returned tuple) - will be negative, minutes (2nd element) will always be positive. + """ + Converts a floating point number of degrees to the degress & minutes. + + Returns a 2-element tuple of decimals. + + If 'decimal_degrees' is negative, only degrees (1st element of returned + tuple) will be negative, minutes (2nd element) will always be positive. Example: - >>> decimal2dm(121.135) - (Decimal('121'), Decimal('8.100')) - >>> decimal2dm(-121.135) - (Decimal('-121'), Decimal('8.100')) - - """ + >>> decimal2dm(121.135) + (Decimal('121'), Decimal('8.100')) + >>> decimal2dm(-121.135) + (Decimal('-121'), Decimal('8.100')) + + """ degrees = D(int(decimal_degrees)) - minutes = libdecimal.getcontext().multiply(\ + + minutes = libdecimal.getcontext().multiply( (D(str(decimal_degrees)) - degrees).copy_abs(), D(60)) + return (degrees, minutes) @@ -101,24 +113,25 @@ def dms2decimal(degrees, minutes, seconds): then returned decimal-degrees will also be negative. NOTE: this method returns a decimal.Decimal - + Example: - >>> dms2decimal(121, 8, 6) - Decimal('121.135') - >>> dms2decimal(-121, 8, 6) - Decimal('-121.135') - + + >>> dms2decimal(121, 8, 6) + Decimal('121.135') + >>> dms2decimal(-121, 8, 6) + Decimal('-121.135') + """ - decimal = D(0) deg = D(str(degrees)) min = libdecimal.getcontext().divide(D(str(minutes)), D(60)) sec = libdecimal.getcontext().divide(D(str(seconds)), D(3600)) - if (degrees >= D(0)): + + if degrees >= D(0): decimal = deg + min + sec else: decimal = deg - min - sec - + return libdecimal.getcontext().normalize(decimal) @@ -126,22 +139,23 @@ def dm2decimal(degrees, minutes): """ Converts degrees and minutes to the equivalent number of decimal degrees. If parameter 'degrees' is negative, then returned decimal-degrees will also be negative. - + Example: - >>> dm2decimal(121, 8.1) - Decimal('121.135') - >>> dm2decimal(-121, 8.1) - Decimal('-121.135') - + + >>> dm2decimal(121, 8.1) + Decimal('121.135') + >>> dm2decimal(-121, 8.1) + Decimal('-121.135') + """ return dms2decimal(degrees, minutes, 0) -# Run doctest -def _test(): - import doctest, decimaldegrees +def run_doctest(): + import doctest + import decimaldegrees return doctest.testmod(decimaldegrees) -if __name__ == "__main__": - _test() +if __name__ == '__main__': + run_doctest() diff --git a/aprs/util.py b/aprs/util.py index 9a95097..64451bf 100755 --- a/aprs/util.py +++ b/aprs/util.py @@ -4,14 +4,15 @@ """Utilities for the APRS Python Module.""" __author__ = 'Greg Albrecht W2GMD <gba@onbeep.com>' +__license__ = 'Apache License, Version 2.0' __copyright__ = 'Copyright 2013 OnBeep, Inc.' -__license__ = 'Apache 2.0' import logging import aprs.constants import aprs.decimaldegrees +import aprs.util import kiss.constants @@ -72,6 +73,7 @@ def dec2dm_lng(dec): return ''.join([str(abs_deg), "%.2f" % dm[1], suffix]) +# TODO: Convert doctest to unittest. def decode_aprs_ascii_frame(ascii_frame): """ Breaks an ASCII APRS Frame down to it's constituent parts. @@ -214,7 +216,6 @@ def format_path(start, raw_frame): def encode_callsign(callsign): call_sign = callsign['callsign'] - digi = False ct = (callsign['ssid'] << 1) | 0x60 @@ -233,7 +234,8 @@ def encode_frame(frame): enc_frame = ''.join([ encode_callsign(create_callsign(frame['destination'])), encode_callsign(create_callsign(frame['source'])), - ''.join([encode_callsign(create_callsign(p)) for p in frame['path'].split(',')]) + ''.join([encode_callsign(create_callsign(p)) + for p in frame['path'].split(',')]) ]) return ''.join([ @@ -258,10 +260,12 @@ def decode_frame(raw_frame): # Less than 2 callsigns? if 2 < n < 10: if (ord(raw_frame[raw_slice + 1]) & 0x03 == 0x03 and - ord(raw_frame[raw_slice + 2]) == 0xf0): + ord(raw_frame[raw_slice + 2]) == 0xf0): frame['text'] = raw_frame[raw_slice + 3:] - frame['destination'] = full_callsign(extract_callsign(raw_frame)) - frame['source'] = full_callsign(extract_callsign(raw_frame[7:])) + frame['destination'] = full_callsign( + extract_callsign(raw_frame)) + frame['source'] = full_callsign( + extract_callsign(raw_frame[7:])) frame['path'] = format_path(n, raw_frame) logging.debug('frame=%s', frame) diff --git a/tests/constants.py b/tests/constants.py index 6bae023..d97eb5e 100644 --- a/tests/constants.py +++ b/tests/constants.py @@ -4,8 +4,8 @@ """Constants for APRS Module Tests.""" __author__ = 'Greg Albrecht W2GMD <gba@onbeep.com>' +__license__ = 'Apache License, Version 2.0' __copyright__ = 'Copyright 2013 OnBeep, Inc.' -__license__ = 'Apache 2.0' TEST_FRAMES = 'tests/test_frames.log' -- GitLab