Skip to content

Instantly share code, notes, and snippets.

@danricho
Last active November 21, 2021 23:02
Show Gist options
  • Save danricho/d5c275d372bfbcbfe60972daebe76a12 to your computer and use it in GitHub Desktop.
Save danricho/d5c275d372bfbcbfe60972daebe76a12 to your computer and use it in GitHub Desktop.
import json
phi, theta, psi, lat, long, alt = 1.256588965, 0.234256252, 2.234242324, -27.309271, 153.076155, 2250 # data to send
dataList = [phi, theta, psi, lat, long, alt] # order the data
jsonList = json.dumps( [ round(value,6) for value in dataList ], separators=(',', ':') ) # rounds to 6 decimals and creates json string
# SEND jsonList ACROSS SOCKET HERE (ENCODE, SEND, RECEIVE, DECODE)
dataList = json.loads(jsonList) # convert back to list
phi, theta, psi, lat, long, alt = dataList[0], dataList[1], dataList[2], dataList[3], dataList[4], dataList[5] # back into variables
class dotdict(dict):
"""dot.notation access to dictionary attributes"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
from datetime import datetime
import threading
import traceback
import time
class Killable_Thread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.thread_name = ""
self.dying = False
self.timing = {"loopstart": 0, "loopend": 0}
self.frame_counter = 0
self.refresh_rate = 30
def set_name(self,name):
self.thread_name = name
def kill(self):
print("Thread:", "'"+self.thread_name+"'", "is dying.")
self.dying = True
def run(self):
print("Thread:", "'"+self.thread_name+"'", "started.")
while True:
if self.dying:
exit()
try:
self.loop()
except Exception as e:
print(" " + datetime.now().strftime('%H:%M:%S.%f')[:-3] + " " + self.thread_name + ": Thread encountered an exception!")
print(traceback.format_exc())
def loop(self):
self.timing["loopstart"] = time.perf_counter()
# DO SOMETHING HERE
print("beep")
self.timing["loopend"] = time.perf_counter()
delay = max(((1.000/self.refresh_rate))-((self.timing["loopend"]-self.timing["loopstart"])),0)
time.sleep(delay)
# HOW TO USE IT
thread = Killable_Thread()
thread.set_name("LOOPING THREAD")
thread.start()
while True:
try:
print("boop")
time.sleep(1)
except KeyboardInterrupt:
print("Keyboard interrupt.")
thread.kill()
break
def logPrint(*args, sep=" ", system=None, color=Style.RESET_ALL, **kwargs):
if system:
print(datetime.now().strftime('%H:%M:%S.%f')[:-3] + " : " + color + str(system).ljust(10)[0:10] + Style.RESET_ALL + " : " + sep.join(map(str,args)), **kwargs)
else:
print(datetime.now().strftime('%H:%M:%S.%f')[:-3] + " : " + sep.join(map(str,args)), **kwargs)
def strSinceDateTime(originTime):
dt = datetime.now() - originTime
offset = dt.seconds + (dt.days * 60*60*24)
delta_s = offset % 60
offset = int(offset / 60)
delta_m = offset % 60
offset = int(offset / 60)
delta_h = offset % 24
offset = int(offset / 24)
delta_d = offset % 7
offset = int(offset / 7)
delta_w = offset
if delta_w > 0:
return "%dw %dd" % (delta_w, delta_d)
if delta_d > 0:
return "%dd %dh" % (delta_d, delta_h)
if delta_h > 0:
return "%dh %dm" % (delta_h, delta_m)
if delta_m > 0:
return "%dm %ds" % (delta_m, delta_s)
else:
return "%ds" % delta_s
Useful Python Snippets (Name of Gist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment