Skip to content

Instantly share code, notes, and snippets.

@antoine-lizee
antoine-lizee / hackathon_test.py
Last active November 16, 2021 15:36
Fetch issues & comments from Topics
import json
import time
import requests
issues_url = 'https://api.github.com/repos/alan-eu/Topics/issues'
comments_url = 'https://api.github.com/repos/alan-eu/Topics/issues/comments'
rate_limit_url = 'https://api.github.com/rate_limit'
headers = {'Authorization': 'token <your_token>', 'Accept': 'application/vnd.github.v3+json'}
-- Get the number of orders for every customer signed in the last year:
WITH customers_with_orders AS MATERIALIZED ( -- [needs "MATERIALIZED" only for PG12+]
SELECT customer_id, signup_date, count(*) n_orders
FROM remote.customers c
JOIN remote.orders o ON c.id = o.customer_id
GROUP BY 1, 2
)
SELECT *
FROM customers_with_orders
WHERE signup_date > current_date - 365
-- Show the distribution of orders from senior customers per month:
WITH senior_customers AS ( -- [This would not be a pb if PG 12+]
SELECT *
FROM remote.customers
WHERE age > 60
)
SELECT date_trunc(paid_date) AS order_month, count(*) AS n_orders
FROM remote.orders o -- brings back to the local server ALL the orders
JOIN senior_customers c ON c.id = o.customer_id -- do a JOIN without the indices from the remote
-- Show the number of paid and delivered orders per country:
WITH orders_from_france AS (
SELECT paid_date, delivered_date
FROM orders
WHERE geo_country(lat, lon) = 'France'
)
, orders_by_paid_date AS (
SELECT paid_date AS date_, count(*) AS n_paid_orders
FROM orders_from_france
)
-- Show the distribution of customers per number of orders in 2020:
WITH n_orders_by_customer AS (
SELECT customer_id, count(*) AS n_orders
FROM orders
WHERE paid_date > '2020-01-01'
GROUP BY 1
)
SELECT n_orders, count(*) AS n_customers
FROM n_orders_by_customer
GROUP BY 1
@antoine-lizee
antoine-lizee / email_provider_domains.txt
Created May 16, 2019 17:53
Email provider domain list
gmail.com
yahoo.com
hotmail.com
aol.com
hotmail.co.uk
hotmail.fr
msn.com
yahoo.fr
wanadoo.fr
orange.fr
@antoine-lizee
antoine-lizee / host_routed_app.py
Last active December 22, 2016 20:31
Implement exception-based catchall host routing with flask & tests
from werkzeug.routing import Rule as BaseRule
class Rule(BaseRule):
except_hosts = []
def match(self, path):
""" Monkey patched version of the default Werkzeug behavior to achieve host matching with catchall.
By default, if host matching is enabled, the match is only done if the host is always provided, which is
@antoine-lizee
antoine-lizee / install_ensurepip.py
Last active July 6, 2016 02:13 — forked from uranusjr/install_ensurepip.py
Script to install ensurepip to Python. Works on MacOSX for Anaconda and classic python installs.
import os
import sys
import io
import tarfile
import urllib.request
import re
ARCHIVE_URL = 'http://d.pr/f/YqS5+'
## Fuction that computes the devaluation factor depending on the
# time interval, based on the target yearly ratio.
decrease <- function(days, yrRatio = 0.5) {
exp(days/365.25 * log(yrRatio))
}
# Test & plot
x <- 0:800;
plot(x, decrease(x), type = "l", yaxp = c(0.25, 1, 3), #xaxp = c(0, 365.25*2, 6),
## claim_queries.R
# Quick R-script to test the claim API built by
# Andrew.
# Setup -------------------------------------------------------------------
# Library
library(httr)
# Constants