Skip to content

Instantly share code, notes, and snippets.

View Faheetah's full-sized avatar

William Normand Faheetah

View GitHub Profile
@Faheetah
Faheetah / test_parser.ex
Last active June 16, 2021 20:08
Parsing a complex key = value structure with optional trailing comments
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
Faheetah / README.md
Last active May 1, 2021 22:23
Elixir Phoenix TLDR

Generate an App

Requires phx.new, install with mix archive.install hex phx_new

mix phx.new app

Common flags:

@Faheetah
Faheetah / brute_force_calculate_stars.exs
Created March 23, 2021 03:47
How many permutations of rows can make 51 stars, max of 25 per row, max of 10 rows, can alternate
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 -> []
end
@Faheetah
Faheetah / improved.py
Created October 18, 2016 20:58
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
Faheetah / decorators.py
Last active November 30, 2018 21:15
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():
print('decorator')
# Will be bar() from the below wrapped call
@Faheetah
Faheetah / rethinkdb.py
Created August 17, 2017 21:16
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', '127.0.0.1')
PORT = env.get('RETHINK_PORT', 28015)
DB = env.get('RETHINK_DB', 'inventory')
GROUP = env.get('RETHINK_GROUP', 'dev')
@Faheetah
Faheetah / ap
Created August 15, 2017 17:40
Ansible helper script
#!/bin/bash
set -e
find_ansible_dir() {
dir=$(readlink -f ${1:-.})
if [ $dir = '/' ]
then
echo "Could not find a playbook in $(pwd)"
@Faheetah
Faheetah / pointless.py
Created October 1, 2016 21:41
Pointless self replicating Python script
contents = '''contents = {0}{0}{0}{1}{0}{0}{0}
contents = contents.format('\{0}',contents)
with open("pointless.py", "w") as f:
f.write(contents)
'''
contents = contents.format('\'',contents)
@Faheetah
Faheetah / macbeth.yml
Last active August 24, 2016 18:23
Poor man's audiobook
- hosts: localhost
tasks:
- uri:
url: http://www.textfiles.com/etext/AUTHORS/SHAKESPEARE/shakespeare-macbeth-46.txt
return_content: true
register: blah
- osx_say:
msg: "{{ blah.content|replace('\n', ' ')|replace('\t',' ') }}"