Q:

GPS heading in Python

I am working on a GPS project in Python and need some help. I can't figure out how to get the heading part to work. I want the statement to be true with a heading of e.g. 355 degrees +/- a bit of error.  My problem is getting around 360 degrees. Like
if ((heading – headerror) % 360) <= Gps.heading <= ((heading + headerror) % 360):

if I use a heading of 355 with error of 20 and the gps reads 4 , which are both about north. 
335 <= 4 <= 15

How can I get it to check if it's in a range of 335 to 15 which would really be 335 to 360, 0 to 15?

A:

One simple implementation:
def between(head, low, high):
    """Whether the heading head is between headings low and high."""
    head, low, high = map(lambda x: x % 360, (head, low, high))
    if high >= low:
        return low <= head <= high
    return low <= head < 360 or 0 <= head <= high

First it converts all arguments to headings between 0 and 360, then it determines which situation we're in, whether we can do a simple comparison, or need to account for 0/360. In the latter case, it explicitly checks between the low value and 360 and between 0 and the high value.
In use:
>>> between(4, 355-20, 355+20)
True

You could refactor this to use it like e.g. equal(4, 355, 20) if you wanted.

