Skip to content

Instantly share code, notes, and snippets.


William Normand Faheetah

View GitHub Profile
Faheetah / brute_force_calculate_stars.exs
Created Mar 23, 2021
How many permutations of rows can make 51 stars, max of 25 per row, max of 10 rows, can alternate
View brute_force_calculate_stars.exs
defmodule FlagStars do
def calculate() do
Enum.reduce(1..25, [], fn first, acc ->
acc ++ Enum.reduce(1..25, [], fn second, acc ->
acc ++ Enum.reduce(1..10, [], fn rows, acc ->
acc ++ cond do
(rows * first) == 51 -> [{first, rows}]
((div(rows, 2) + rem(rows, 2)) * first) + (div(rows, 2) * second) == 51 -> [{first, second, rows}]
true -> []
Faheetah /
Last active Nov 30, 2018
Python Decorator Foo
# I always get confused with decorators because of all the nesting and where all of the arguments go
# Reference of using decoratores with arguments and with classes
## Very Simple Decorator
def foo(f):
# Inner function that must be returned
def decorator():
# Will be bar() from the below wrapped call
Faheetah /
Created Aug 17, 2017
Ansible RethinkDB dynamic inventory module
#!/usr/bin/env python
import json
import rethinkdb as r
from os import environ as env
HOST = env.get('RETHINK_HOST', '')
PORT = env.get('RETHINK_PORT', 28015)
DB = env.get('RETHINK_DB', 'inventory')
GROUP = env.get('RETHINK_GROUP', 'dev')
Faheetah / ap
Created Aug 15, 2017
Ansible helper script
View ap
set -e
find_ansible_dir() {
dir=$(readlink -f ${1:-.})
if [ $dir = '/' ]
echo "Could not find a playbook in $(pwd)"
Faheetah /
Created Oct 18, 2016
Ansible stdout callback module with better output, used by output=method in task name
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import sys
from ansible import constants as C
from ansible.plugins.callback.default import CallbackModule as CallbackModule_default
from ansible.utils.color import colorize, hostcolor
def _color(play):
Faheetah /
Created Oct 1, 2016
Pointless self replicating Python script
contents = '''contents = {0}{0}{0}{1}{0}{0}{0}
contents = contents.format('\{0}',contents)
with open("", "w") as f:
contents = contents.format('\'',contents)
Faheetah / macbeth.yml
Last active Aug 24, 2016
Poor man's audiobook
View macbeth.yml
- hosts: localhost
- uri:
return_content: true
register: blah
- osx_say:
msg: "{{ blah.content|replace('\n', ' ')|replace('\t',' ') }}"
Faheetah / Jenkinsfile.groovy
Last active Apr 9, 2021
Jenkinsfile idiosynchrasies with escaping and quotes
View Jenkinsfile.groovy
node {
echo 'Results included as an inline comment exactly how they are returned as of Jenkins 2.121, with $BUILD_NUMBER = 1'
echo 'No quotes, pipeline command in single quotes'
sh 'echo $BUILD_NUMBER' // 1
echo 'Double quotes are silently dropped'
sh 'echo "$BUILD_NUMBER"' // 1
echo 'Even escaped with a single backslash they are dropped'
sh 'echo \"$BUILD_NUMBER\"' // 1
echo 'Using two backslashes, the quotes are preserved'
sh 'echo \\"$BUILD_NUMBER\\"' // "1"
Faheetah / Dockerfile
Last active Jul 6, 2020
Docker patterns/anti-patterns
View Dockerfile
### Generic Dockerfile demonstrating good practices
### Imports
# Bad-ish, we do not need Ubuntu for this, nor do we want latest if we are using in a build system, predictable is better
FROM ubuntu:latest
# Better, using a small image since our app has no dependency on Ubuntu
FROM alpine:3.3
Faheetah /
Last active Jul 27, 2020
Ansible bash module boilerplate
# Source arguments from Ansible
# These are passed into the module as $1 with a key=value format
# Sourcing this file sets the variables defined in the Ansible module
# Note that variables that are unused in the module are silently ignored
source $1
# Helper function to fail the module with the specified error
# This can accept $@ in printf for the full error