Skip to content

Instantly share code, notes, and snippets.

View montanalow's full-sized avatar

Montana Low montanalow

  • San Francisco, CA
View GitHub Profile
@montanalow
montanalow / denormalized.sql
Last active February 3, 2021 23:04
Move the full text keywords index to items
SELECT *
FROM products
NATURAL JOIN items
WHERE items.keywords @@ to_tsquery('apple')
AND items.retailer_location_id = 123
@montanalow
montanalow / dot_product.sql
Last active March 3, 2021 18:24
Implementing a dot product in SQL
CREATE FUNCTION dot_product(embedding1 real[], embedding2 real[])
RETURNS real
LANGUAGE plpgsql
IMMUTABLE
AS $$
DECLARE
dot_product real;
BEGIN
SELECT SUM(multiplied.values) INTO dot_product
FROM (SELECT UNNEST(embedding1) * UNNEST(embedding2) AS values) AS multiplied;
@montanalow
montanalow / simple_schema
Last active February 4, 2021 02:16
Products and Items
Products (1TB) - 100x wider than Items
╔══════╦════════════╦═════╦═══════╦═════════════════╗
║ id ║ name ║ upc ║ emoji ║ 100KB more data ║
╠══════╬════════════╬═════╬═══════╬═════════════════╣
║ 1 ║ fuji apple ║ 123 ║ 🍏  ║ ... ║
║ 2 ║ gala apple ║ 456 ║ 🍎  ║ ... ║
║ 3 ║ fuji water ║ 789 ║ 🌊  ║ ... ║
╠══════╩════════════╩═════╩═══════╩═════════════════╣
║ ... 10M more rows ... ║
╚═══════════════════════════════════════════════════╝
@montanalow
montanalow / benchmark.rb
Last active September 13, 2019 02:39
Ruby hash vs instance variable benchmark
require 'benchmark'
$CYCLE = 10_000_000
class HashString
def initialize
@attrs = {}
end
def attr=(value)
# install virtualenv
pip install virtualenv
# create a virtualenv
virtualenv ~/venvs/a_cool_project
# install your thing in its own virtualenv
~/venvs/a_cool_project/bin/pip install a_cool_project
# make the virtualenv installed executable available system wide
# install lore
$ pip install --upgrade lore
# setup dependency managment
$ cd my_app
$ lore init . --bare --python-version=3.6.6 # 2 or 3, whatever
# Ship it!
$ git commit -a -m 'deep freeze dependencies'
$ git push
@montanalow
montanalow / tour.sh
Last active October 14, 2018 17:34
$ cd my_app
$ lore python # launch python in my_app's virtualenv
$ lore pip # my_app's pip
$ lore console # my_app's ipython (installed in virtualenv)
$ lore notebook # my_app's jupyter notebook
$ lore lab # my_app's jupyter lab
$ lore exec # execute anything in my_app's virtualenv/bin/
$ lore env # see what else is in there
$ lore test # you do have unit tests, don't you?
$ git init .
$ git add .
$ git add -f models/my_app.models.product_popularity/Keras/1 # or your preferred fitting number to deploy
$ git commit -m "My first lore app!"
$ pip3 install lore
$ git clone https://github.com/montanalow/my_app.git
$ cd my_app
$ lore install # caching all dependencies locally takes a few minutes the first time
$ lore server &
$ curl "http://localhost:5000/product_popularity.Keras/predict.json?product_name=Banana&department=produce"
$ heroku login
$ heroku create
$ heroku config:set LORE_PROJECT=my_app
$ heroku config:set LORE_ENV=production
$ git push heroku master
$ heroku open
$ curl “`heroku info -s | grep web_url | cut -d= -f2`product_popularity.Keras/predict.json?product_name=Banana&department=produce”