Skip to content

Instantly share code, notes, and snippets.

View Jasata's full-sized avatar
💭
Creating DTEK0068

Jani Tammi Jasata

💭
Creating DTEK0068
View GitHub Profile
/*
* TEMPer.c - Simple TEMPer USB temperature meter reader
* Adapted from https://gist.github.com/artms/5356eafcd1244c6fabc0f735e5de7096
* Credit: Arturas Moskvinas
*
* Adapted to C by Jani Tammi <jasata@utu.fi>, 2020-10-11
*
*/
#include <stdio.h>
#include <stdint.h>
#!/usr/bin/env python3
#
# Simple command line yes/no prompter.
# 2020-04-24 Jani Tammi <jasata@utu.fi>
#
# default = None | "y" | "n" If and how ENTER is interpreted.
# echo = False | True Will the prompt line be appended with selection.
#
def getch():
@Jasata
Jasata / ros_json_server.py
Created February 22, 2020 19:19
Minimal JSON REST API (ROS 1 access for Jupyter)
#! /usr/bin/env python2
#
# ROS 1 "Kinetic Kame" server
# EOL date at April 2021, but this version uses Python 2.7
#
# Demo code for Jupyter access, need accesspoints and all the
# associated data packet handling.
#
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import json
@Jasata
Jasata / DefaultDict.py
Created December 28, 2019 08:29
Dictionary with .default value
class DefaultDict(dict):
"""Returns DefaultDict.default value for missing key, or raises KeyError if default has not been set."""
def __missing__(self, key):
if getattr(self, 'default', None):
return self.default
raise KeyError(
f"Key '{key}' does not exist and default has not been set!"
)
@Jasata
Jasata / DefaultDotDict.py
Last active December 29, 2019 19:13
Dot-access dictionary with default value
#! /usr/bin/env python3
# 2019-12-28 Jani Tammi
#
# Slightly improved DefaultDotDict, using '*' as default value.
# NOTE: DefaultDotDict.get('foobar') does NOT return value for key '*'.
# You still get to define your own default value.
#
class DefaultDotDict(dict):
@Jasata
Jasata / val2list.py
Created December 21, 2019 09:04
Configparser list-value clean
#! /usr/bin/env python3
# Snippet for cleaning out configuration file list-values (import configparser).
# For reading/processing configuration file values, performance is not an issue.
# The key = value can sometimes look something like:
#
# files = , data.db ,script.py, ,, data.db ,,,
#
# NOTE: Neither of these can deal with quotations (" or ') and thus cannot process
# list items that need to have the separator (,) in them.
#
@Jasata
Jasata / RunningStatistics.py
Created December 17, 2019 14:49
Welford's method for running statistics
#! /usr/bin/env python3
#
# A class to compute the mean, SAMPLE variance, and SAMPLE standard deviation
# of a stream of data.
#
# RunningStatistics.py - 2018, Jani Tammi <jasata@utu.fi>
# 0.1.0 Initial version.
#
# Makes use of a method to calculate running variance
# by B. P. Welford, 1962,
@Jasata
Jasata / with_identity.py
Created December 17, 2019 11:05
Convenient short term user change
#! /usr/bin/env python3
#
# with_identity.py - Jani Tammi <jasata@utu.fi>
#
# Convenient way to assume another user identity for limited work.
# Created for installation scripts that run as root.
#
import os
import pwd
import grp
@Jasata
Jasata / ConfigFile.py
Last active December 17, 2019 14:40
Python class for configuration files (installation scripts run by root)
#! /usr/bin/env python3
#
# ConfigFile.py - Jani Tammi <jasata@utu.fi>
#
# NOTE: You obviously still need the proper privileges.
# You cannot run this as a regular user and set
# owner to be root and expect the .create() to
# actually manage that.
# THIS WAS WRITTEN PRIMARILY FOR INSTALLATION
# SCRIPTS THAT EXECUTE AS ROOT !!