Skip to content

Instantly share code, notes, and snippets.

View MattWoodhead's full-sized avatar

MattWoodhead

  • UK
View GitHub Profile
@MattWoodhead
MattWoodhead / nmea_checksum.py
Created February 17, 2018 13:56
Concise Python 3 function for validating an NMEA string using its included checksum
import operator
from functools import reduce
def nmea_checksum(sentence: str):
"""
This function checks the validity of an NMEA string using it's checksum
"""
sentence = sentence.strip("$\n")
nmeadata, checksum = sentence.split("*", 1)
calculated_checksum = reduce(operator.xor, (ord(s) for s in nmeadata), 0)
@MattWoodhead
MattWoodhead / numpy_normalise_angles.py
Created January 14, 2018 18:57
Normalise angles (to +/-180 deg or +/- ) in a numpy array
import numpy as np
def normalise_angles_deg(array):
""" Converts angles to -180 => A <= +180 """
array = np.where(array<-180 , array + 360, array)
return np.where(array>180 , array - 360, array)
def normalise_angles_rad(array):
""" Converts angles to -Pi => A <= +Pi """
array = np.where(array<-np.pi , array + 2*np.Pi, array)
@MattWoodhead
MattWoodhead / file_change_check.py
Created September 19, 2017 19:59
A function to see if a file has changed
import os
FILE = "test.txt"
def file_changed(file):
""" Checks the windows file attributes to see if a file has been updated """
new_change_time = None
last_change_time = None
while new_change_time == last_change_time:
if last_change_time is None:
"""
Generator function for creating a range of floating point numbers
Numpy linspace is probably better unless you cannot have imports!!
"""
def frange(start, stop, step, precision=10):
""" a range generator that accepts floats """
k = start
while k < stop:
@MattWoodhead
MattWoodhead / date_converter.py
Created August 26, 2017 11:09
A date converter function that can handle multiple date inputs types in the iso format and return a datetime.date object
import string
import datetime as dt
def date_converter(date):
""" converts multiple date formats into a datetime object """
if isinstance(date, str):
try:
d = ""
for char in date:
if char in string.punctuation:
@MattWoodhead
MattWoodhead / echo_print.py
Created August 8, 2017 20:15
A simple print definition for reuse removing the automatic newline
"""
Print function without the newline
"""
from functools import partial
echo = partial(print, end=' ')
echo("Hello world!")
echo("Hello world!")
@MattWoodhead
MattWoodhead / Matplotlib_Subplots.py
Created August 7, 2017 18:40
An up to date plotting example using some of the latest methods for simplifying subplots
import numpy as np
import matplotlib.pyplot as plt
# Create data
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
data1 = (t1, np.exp(t1) * np.cos(2*np.pi*t1))
data2 = (t2, np.exp(t2) * np.cos(2*np.pi*t2))
data3 = (t2, np.cos(2*np.pi*t2))
@MattWoodhead
MattWoodhead / Dict_to_NamedTuple.py
Created August 4, 2017 13:03
One-liner to convert a Dictionary to a frozen named tuple
from collections import namedtuple
test_dict = {"a": 0,
"b": 1,
"c": 2,
"d": 3,
}
test = namedtuple("test", test_dict.keys())(**test_dict)
@MattWoodhead
MattWoodhead / iter_class_test.py
Created August 2, 2017 21:15
""" A test class showing how to add iterators to classes """
class TodoList(object):
""" A test class showing how to add iterators to classes """
def __init__(self):
self.tasks = [] # initialise empty list
def __iter__(self):
return iter(self.tasks)
def add_task(self, task):
self.tasks.append(task)
@MattWoodhead
MattWoodhead / python_dict_forgiveness.py
Created July 24, 2017 20:39
Demonstration of Python's built in get() method for dictionaries
""" Easier to ask forgiveness than permission """
my_dict = {"key_0": 0,
"key_1": 10,
"key_2": 20,
"key_3": 30,
"key_4": 40,
}