Skip to content

Instantly share code, notes, and snippets.

View emmanuellyautomated's full-sized avatar

Emmanuel Obi emmanuellyautomated

  • Chicago, IL
View GitHub Profile
@emmanuellyautomated
emmanuellyautomated / flask_stream_generators.py
Created February 5, 2018 15:38
Example of a convenient way to stream generators as JSON in your flask app
import json
from flask import Response, stream_with_context
def render_as_json(generator_creating_function):
""" Decorator to render JSON from generator-creating functions
"""
def wrapper():
@emmanuellyautomated
emmanuellyautomated / trivia.html
Created January 4, 2018 04:21
A simple game of trivia
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Trivia</title>
</head>
<body>
<div id="timer"></div>
@emmanuellyautomated
emmanuellyautomated / hangman.html
Created December 25, 2017 19:27
A reactive game of hangman
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Hangman</title>
</head>
<body>
<div id="scoreboard"></div>
@emmanuellyautomated
emmanuellyautomated / vim.rb
Last active November 24, 2017 19:10
Homebrew formula for installing vim
require 'formula'
class Vim < Formula
homepage 'http://www.vim.org/'
url 'https://vim.googlecode.com/hg/', :revision => '6c318419e331'
version '7.3.515'
def features; %w(tiny small normal big huge) end
def interp; %w(lua mzscheme perl python python3 tcl ruby) end
@emmanuellyautomated
emmanuellyautomated / coroutines.py
Created November 21, 2017 17:14
An example of using Python coroutines to create processing pipelines.
import json
import os
import requests
from copy import copy
# To run try this out, run --> `python coroutines.py`
@emmanuellyautomated
emmanuellyautomated / find_venv_dependency.py
Last active November 10, 2017 22:30
If a virtualenv is activated, this will return the full path of the dependency named.
import os
import sys
def find_venv_dependency(name):
if not hasattr(sys, 'real_prefix'):
return # venv not activated
venv = sys.prefix.split('/')[len(os.getcwd().split('/'))]
for root, dirs, files in os.walk(venv):
@emmanuellyautomated
emmanuellyautomated / merge_dict_arrs.py
Created October 24, 2016 14:05
For dictionaries having arrays as values, this function merges arrays of different dictionaries belonging to the same key. It *does* overwrite the first one though so beware!
def merge_dicts_arrs(d1, d2):
for k,v in d2.items():
if k in d1.keys():
d1[k] = d1[k] + d2[k]
else:
d1.update({k:v})
return d1
@emmanuellyautomated
emmanuellyautomated / search_dictionary_values.py
Last active September 3, 2016 04:39
can determine if a search term is a value in a nested dictionary
def search_dict(d, term, nests=[]):
def last(array):
return array[-1]
'''
breadth-first search a dictionary
'''
presence = None
nests_at_this_level = [d.get(key) for key in d.keys() if type(d.get(key)).__name__ == 'dict']
lists_with_nests = [d.get(key) for key in d.keys()
if type(d.get(key)).__name__ == 'list'
@emmanuellyautomated
emmanuellyautomated / fetch_model_associations.rb
Last active May 9, 2016 14:42
Ruby object that returns all declared associations in your Rails models when given the filepath to your Rails app
class Associator
attr_reader :rails_app_dir, :associations
ASSOCIATION_TYPES = [
"belongs_to",
"has_one",
"has_many",
"has_and_belongs_to_many"
]
@emmanuellyautomated
emmanuellyautomated / prepend_str_to_hash_value.rb
Created April 14, 2016 19:03
Prefix a string to values in hash as per their 'fuzzy-matched' keys
def prepend_str_to_hash_value(hash, subhashes=[], **kwargs)
keys = hash.keys
until keys.count == 0
key = keys.pop
hash[key] = kwargs[:str] + hash[key] if (kwargs[:keys_like] != "" && hash[key] != kwargs[:vals_unlike] && key.to_s.include?(kwargs[:keys_like].to_s) && [String, Fixnum].include?(hash[key].class))
subhashes << hash[key] if hash[key].class == Hash
end
return subhashes.empty? ? true : prepend_str_to_hash_value(subhashes.pop, subhashes, **kwargs)
end