Skip to content

Instantly share code, notes, and snippets.

View dancrew32's full-sized avatar
🕶️
🕶

Dan Masquelier dancrew32

🕶️
🕶
View GitHub Profile
@benhoyt
benhoyt / markov.py
Created November 11, 2023 15:45
Generate text from an input using a simple Markov chain generator
import collections, random, sys, textwrap
# Build possibles table indexed by pair of prefix words (w1, w2)
w1 = w2 = ''
possibles = collections.defaultdict(list)
for line in sys.stdin:
for word in line.split():
possibles[w1, w2].append(word)
w1, w2 = w2, word
@joshuacerbito
joshuacerbito / useScroll.js
Last active January 8, 2024 13:44
Custom React hook for listening to scroll events
/**
* useScroll React custom hook
* Usage:
* const { scrollX, scrollY, scrollDirection } = useScroll();
*/
import { useState, useEffect } from "react";
export function useScroll() {
const [lastScrollTop, setLastScrollTop] = useState(0);
@david-hosier
david-hosier / WebViewWithActivityIndicator.swift
Last active October 31, 2023 04:15
Shows a ViewController that contains a WKWebView and UIActivityIndicatorView. This is a pruned down snippet from working code that may or may not work exactly as-is, but should give the basic idea of how to show/hide an activity indicator while a WKWebView is navigating to a URL. The original project this was adapted from was done in XCode 8, an…
import Foundation
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
var webView: WKWebView!
var activityIndicator: UIActivityIndicatorView!
@IBOutlet var webViewContainer: UIView!
@blixt
blixt / flask_cors.py
Created August 16, 2014 18:24
How to add CORS support to a Flask app in 9 lines of code
def add_cors_headers(response):
response.headers['Access-Control-Allow-Origin'] = '*'
if request.method == 'OPTIONS':
response.headers['Access-Control-Allow-Methods'] = 'DELETE, GET, POST, PUT'
headers = request.headers.get('Access-Control-Request-Headers')
if headers:
response.headers['Access-Control-Allow-Headers'] = headers
return response
app.after_request(add_cors_headers)
@avidal
avidal / gist:b5e2af4e2f618254a0ab
Created July 28, 2014 16:11
amazon url regex
(?:https?://)?(?:[a-zA-Z0-9\-]+\.)?(?:amazon|amzn){1}\.(?P<tld>[a-zA-Z\.]{2,})\/(gp/(?:product|offer-listing|customer-media/product-gallery)/|exec/obidos/tg/detail/-/|o/ASIN/|dp/|(?:[A-Za-z0-9\-]+)/dp/)?(?P<ASIN>[0-9A-Za-z]{10})
I created this a number of years ago for an irc bot (https://github.com/rmmh/skybot) to recognize amazon urls. Keep forgetting to hang onto it.
@dypsilon
dypsilon / frontendDevlopmentBookmarks.md
Last active May 7, 2024 01:27
A badass list of frontend development resources I collected over time.
@reterVision
reterVision / LRU.py
Last active March 1, 2023 02:32
LRU algorithm implemented in Python.
from datetime import datetime
class LRUCacheItem(object):
"""Data structure of items stored in cache"""
def __init__(self, key, item):
self.key = key
self.item = item
self.timestamp = datetime.now()
@builtinnya
builtinnya / colossal-cue-adventure.py
Last active November 12, 2021 08:17
A trivial code for solving the Colossal Cue Adventure.
# A trivial code for solving the Colossal Cue Adventure
# URL: http://adventure.cueup.com/
# Level 1: The VAX's MTH$RANDOM % 36 Roulette
def vax_rng_next(x):
"""Generate the next pseudo-random number."""
a, c, m = 69069, 1, 2**32
return (a * x + c) % m
@ndarville
ndarville / business-models.md
Last active January 13, 2024 17:27
Business models based on the compiled list at http://news.ycombinator.com/item?id=4924647. I find the link very hard to browse, so I made a simple version in Markdown instead.

Business Models

Advertising

Models Examples
Display ads Yahoo!
Search ads Google
@gcaixeta
gcaixeta / gist:3498224
Created August 28, 2012 13:57
how to get the original picture-url from the linkedin gem api
c = LinkedIn::Client.new
c.authorize_from_access authentication.token, authentication.secret
c.search(fields: [{:people => [:picture_url]}]).people.all.first.try(:picture_url)
#or better
c.profile(:fields => ['picture-urls::(original)']).picture_urls.all.first