Skip to content

Instantly share code, notes, and snippets.


William Normand Faheetah

View GitHub Profile
Faheetah / struct_parser.exs
Last active Jun 23, 2021
An example of parsing C with Elixir nimble_parsec, specifically Libvirt XDRs
View struct_parser.exs
defmodule StructParser do
import NimbleParsec
@space 0x0020
@tab 0x009
whitespace = utf8_char([@space, @tab, ?\n])
ignore_whitespace =
Faheetah / test_parser.ex
Last active Jun 16, 2021
Parsing a complex key = value structure with optional trailing comments
View test_parser.ex
defmodule Test.Parser do
import NimbleParsec
# iex(9)> Test.Parser.parse """
# ...(9)> FOO = 1 (some foo)
# ...(9)> BAR = 2 (some
# ...(9)> bar)
# ...(9)> FAZ = 3 (some faz)
# ...(9)> """
# {:ok, ["FOO", 1, "somefoo", "BAR", 2, "somebar", "FAZ", 3, "somefaz"], "\n",
Faheetah /
Last active May 1, 2021
Elixir Phoenix TLDR

Generate an App

Requires, install with mix archive.install hex phx_new

mix app

Common flags:

Faheetah /
Created May 1, 2021
Log commands to a .history file for repos

A problem with repositories is sometimes you do not remember what commands you ran to bootstrap the application, or what generators were used. A history file similar to .bash_history would be handy for a per-project basis. The h command above provides a wrapper around this functionality. It should be portable across any system with bash.

Run a command and write out the command to the git project's .history file, this logs $@ to .history

$ h mix project_name
$ h mix deps.get
$ h mix ecto.create
$ cat .history
mix project_name
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)