Skip to content

Instantly share code, notes, and snippets.

View JeffCohen's full-sized avatar

Jeff Cohen JeffCohen

View GitHub Profile
# Run this script with:
# curl -sL https://gist.github.com/JeffCohen/60e8d8e3be66d4f167b1249fb1bd451a/raw | bash -E -
cd
echo
echo "Web Development Setup Script for Mac, Version 0.15"
echo
echo "This will take about 25 minutes. Do not turn off your computer until this is complete."
echo "All progress is being recorded to a file named install-log.txt"
# This script is for Windows 10.
# The Windows Subsystem for Linux must be enabled before running this script.
#
# Run this script from a Windows bash session with:
#
# curl -sL https://gist.github.com/JeffCohen/8b81c334c313340d50810a88c0df2bfb/raw | bash -E -
cd
echo
@JeffCohen
JeffCohen / backtrace_silencers.rb
Created July 7, 2017 01:08
Suppress those long stack traces in the server log!
# Be sure to restart your server when you modify this file.
# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
# Rails.backtrace_cleaner.add_silencer { |line| line =~ /lib/ }
# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
# Rails.backtrace_cleaner.remove_silencers!
module ActionDispatch
class ExceptionWrapper
# Python 3
#
# https://docs.python.org/3/howto/urllib2.html
# http://api.open-notify.org/astros.json
# This code works. Run it to verify.
# Then read the challenge at the bottom of this file.
import urllib.request
import json
# (Run this code with Python 3 only.)
#
# This program decodes a secret message by advancing each
# letter by 2 places. Spaces and punctuation should remain
# unchanged.
# Examples:
# "nwrfml" decodes into: "python"
# "ynnjc" decodes into: "apple"
#
@JeffCohen
JeffCohen / sample.rb
Created May 7, 2016 17:17
ActiveRecord::sample
# Example usage:
# Product.sample => returns a Product
# Product.sample(3) => returns an array of 3 products
# Product.sample(3, true).order('title') => allows chaining
class ActiveRecord::Base
# Choose rows at random
def self.sample(n = 1, keep_as_relation = false)
rows = offset(rand(0...(count-n-1))).limit(n)
@JeffCohen
JeffCohen / omdb_practice.ex
Last active April 24, 2016 02:04
Elixir example
defmodule HelloPhoenix.MoviesController do
use HelloPhoenix.Web, :controller
require Logger
def show(conn, %{"id" => query}) do
results = fetch_by_id(query)
# results is a hash that might have a key named "Error"
# in which case we need to try to query by title instead of id
@JeffCohen
JeffCohen / learning-elixir.md
Last active April 14, 2016 18:35
My notes on learning Elixir

Installation

Fairly straightforward, but installing postgres via brew wouldn't work until I followed the instructions in this comment: Homebrew/legacy-homebrew#35240 (comment)

Elixir-to-Ruby Dictionary

Elixir Ruby
mix ruby
hex rubygems
@JeffCohen
JeffCohen / car.py
Created March 7, 2016 14:32
Exercise #23, using a binary tree
class TreeNode:
def __init__(self, content):
self.content = content
self.yes = None
self.no = None
def is_question(self):
return (self.yes != None or self.no != None)
# Prepare the decision tree
@JeffCohen
JeffCohen / scope.py
Created November 14, 2015 18:09
Odd scoping in Python
# I don't understand the output (shown below).
# L is local to f, right? So how are the
# contents of L remembered between invocations?
def f(a, L=[]):
L.append(a)
return L
print(f(1))
print(f(2))