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 / deeply_nested_hashes
Last active April 6, 2016 14:05
Two deeply-nested hashes
'{
"foo_code": 404,
"foo_rbody": {
"query": {
"info": {
"acme_no": "444444",
"road_runner": "123"
},
"error": "no_lunch",
"message": "runner problem."
@emmanuellyautomated
emmanuellyautomated / simple_search.rb
Created April 6, 2016 14:06
Retrieves value from hash if present; returns `false` otherwise
def search(house, val, houses=[])
keys = house.keys
until keys.count == 0
key = keys.pop
houses << house[key] if house[key].class == Hash
return house[key] if house[key] == val
end
return houses.empty? ? false : search(houses.pop, val, houses)
end
@emmanuellyautomated
emmanuellyautomated / search_with_debug.rb
Last active April 7, 2016 03:46
search function that returns value and keypath --- has debug print statements
def search(hash, val, stack=[], keyring=[], stash=[])
keys = hash.keys
puts ">" * 50
puts "KEYS: #{keys}"
before = stack.length
puts "." * 20
puts "BEFORE: #{before}"
until keys.length == 0
@emmanuellyautomated
emmanuellyautomated / search.rb
Last active April 6, 2016 22:24
Recursively traverses a hash and returns specified value if present along with keypath
def search(hash, val, stack=[], keyring=[])
stash=[]
keys = hash.keys
before = stack.length
until keys.length == 0
key = keys.pop
if hash[key].class == Hash
keyring << key
stack << hash[key]
@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
@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 / 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 / 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 / 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 / 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`