Skip to content

Instantly share code, notes, and snippets.

Avatar
😑

Fergus FergusInLondon

😑
View GitHub Profile
@FergusInLondon
FergusInLondon / profiling.py
Last active Feb 5, 2021
Two Python contextmanagers for performance profiling - <100 line
View profiling.py
"""
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 Apr 30, 2020
Get github gists for static site generation
View gists.js
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 Apr 29, 2020
Get git stats for static site generation.
View gitstats.js
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 Apr 28, 2019
Processing Sequential Data w/ "*Sliding Windows*"
View 1.window.js
/**
* 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 Apr 23, 2018
Example Docker Engine API Client using PHP
View DockerClient.php
<?php
class DockerClient {
/** @param resource */
private $curlClient;
/** @param string */
private $socketPath;
View DependencyTraverser.js
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 Sep 21, 2020
Retrieve data from a Media Player in Linux, via dbus. (Uses Python)
View mpris.py
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
Dynamic array/container implementation in C - includes iterator. (WIP / Brain-Dump)
View collection.c
#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
Use dbus to parse system problems
View problems.py
# Fergus In London - https://fergus.london <fergus@fergus.london>
#
# 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
@FergusInLondon
FergusInLondon / roller.js
Last active Apr 27, 2017
Simple Rolling Average function
View roller.js
/* Fergus In London - https://fergus.london - <fergus@fergus.london>
*
* Simple little function for calculating rolling averages, both simple and
* weighted. Example can be found on the GitHub Gist, or on my website.
*
* Do as you please, no rights reserved and no warranties provided. :)
*/
/**