This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Print function without the newline | |
""" | |
from functools import partial | |
echo = partial(print, end=' ') | |
echo("Hello world!") | |
echo("Hello world!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import namedtuple | |
test_dict = {"a": 0, | |
"b": 1, | |
"c": 2, | |
"d": 3, | |
} | |
test = namedtuple("test", test_dict.keys())(**test_dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Easier to ask forgiveness than permission """ | |
my_dict = {"key_0": 0, | |
"key_1": 10, | |
"key_2": 20, | |
"key_3": 30, | |
"key_4": 40, | |
} | |
NewerOlder