Skip to content

Instantly share code, notes, and snippets.

View FergusInLondon's full-sized avatar
😑

Fergus FergusInLondon

😑
View GitHub Profile
@FergusInLondon
FergusInLondon / bearing.py
Last active March 13, 2024 16:24
bearing + angle of elevation calculations
from collections import namedtuple
from math import radians, sin, cos, atan2, sqrt, degrees
# Point is a convenience wrapper to hold the co-ordinates belonging to
# a particular "point" - i.e. the base station location, or the remote
# device location.
#
# Units: lat, long = radians. alt = meters.
Point = namedtuple('Point', ['lat', 'lon', 'alt'])
@FergusInLondon
FergusInLondon / profiling.py
Last active February 5, 2021 06:56
Two Python contextmanagers for performance profiling - <100 line
"""
profiling.py - standard library backed context managers for performance profiling
To use these context managers, ensure that the appropriate environment variable is
set - i.e. 'PROFILING_ENABLED'. The default directory for outputting profiling data
is the *current directory* - i.e `.` - although this too can be overidden via the
environment - specifically `PROFILING_DIRECTORY`.
Due to the quirk in the design of `pathlib`, passing an absolute path to one of the
context managers will override the output directory associated with that trace.
@FergusInLondon
FergusInLondon / gists.js
Created April 30, 2020 15:17
Get github gists for static site generation
async function getGithubGists(username) {
const gists = await require('axios').get(
`https://api.github.com/users/${username}/gists`
)
return gists.data.map(({html_url, description, created_at, files}) => {
return {
url: html_url,
files: Object.keys(files),
description, created_at
@FergusInLondon
FergusInLondon / gitstats.js
Created April 29, 2020 13:01
Get git stats for static site generation.
const run = require('util').promisify(require('child_process').exec)
async function getGitStats() {
const {stdout: refHash } = await run('git rev-parse HEAD')
const {stdout: message } = await run(`git log --format=%B -n 1 ${refHash}`)
const {stdout: branch } = await run('git rev-parse --abbrev-ref HEAD')
return { refHash, message, branch }
}
@FergusInLondon
FergusInLondon / 1.window.js
Last active April 28, 2019 23:38
Processing Sequential Data w/ "*Sliding Windows*"
/**
* A (very over-engineered, yet still impressively scruffy) implementation of a
* Sliding Window (adjacent, overlapping, and buffered.) for the processing of
* sequential data.
*
* Written very hurriedly one evening, to support a blog post.
*
* @param {array} data
* @param {int|Number} width
* @param {array?} accumulator
@FergusInLondon
FergusInLondon / DockerClient.php
Created April 23, 2018 23:41
Example Docker Engine API Client using PHP
<?php
class DockerClient {
/** @param resource */
private $curlClient;
/** @param string */
private $socketPath;
@FergusInLondon
FergusInLondon / DependencyTraverser.js
Last active April 29, 2020 15:57
Dependency Traversal
const _ = require("lodash");
const fixtures = require("./fixtures.js");
/**
* Simple object capable of demonstrating how to build a dependency tree
* from a POM-like structure; using BFS (queue based).
*/
function DependencyTraverser(targetObject = {})
{
/** Queue containing dependencies to process */
@FergusInLondon
FergusInLondon / mpris.py
Last active September 21, 2020 04:36
Retrieve data from a Media Player in Linux, via dbus. (Uses Python)
import dbus
class MediaPlayer:
"""Recieves state from a MediaPlayer using dbus."""
player_properties = False
def __init__(self, player_name):
# Get an instance of the dbus session bus, and retrieve
# a proxy object for accessing the MediaPlayer
@FergusInLondon
FergusInLondon / collection.c
Created May 2, 2017 19:26
Dynamic array/container implementation in C - includes iterator. (WIP / Brain-Dump)
#include <string.h> // memcpy
#include <stdlib.h> //
/**
*
*
*/
typedef struct {
uint8_t object_count, iteration_counter, max_count;
uint8_t *indices_in_use;
@FergusInLondon
FergusInLondon / problems.py
Created May 1, 2017 17:18
Use dbus to parse system problems
# Fergus In London - https://fergus.london <[email protected]>
#
# Uses the org.freedesktop.problems dbus channel to read out reported problems.
#
# Requires pydbus from pip.
# Requires python-gobject and glib installed on your distro.
#
import sys
import argparse
from pydbus import SystemBus