Skip to content

Instantly share code, notes, and snippets.

/**
* 传入轨迹抽样点,获取整条轨迹穿越地区
* 300: 执行时间184.992ms
* 1000: 执行时间587.400ms
* 5000: 执行时间2834.339ms
* 10000:执行时间5526.228ms
*/
CREATE OR REPLACE FUNCTION getAddressFromPoints(points_str text)
RETURNS text AS $$
import json
@davidradernj
davidradernj / py_environ.sql
Created September 9, 2016 16:55
plpython to read environment variable from PostgreSQL
create extension plpythonu;
create type py_environ_type as (name text, value text);
create or replace function py_environ(name varchar DEFAULT NULL)
returns setof py_environ_type
as $$
import os
aev = []
if name is None:
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import zipfile
@colophonemes
colophonemes / create_triggers
Last active February 1, 2025 14:53
Postgres TRIGGER to call NOTIFY with a JSON payload
CREATE TRIGGER person_notify AFTER INSERT OR UPDATE OR DELETE ON income
FOR EACH ROW EXECUTE PROCEDURE notify_trigger(
'id',
'email',
'username'
);
CREATE TRIGGER income_notify AFTER INSERT OR UPDATE OR DELETE ON income
FOR EACH ROW EXECUTE PROCEDURE notify_trigger(
'id',
@sylvainv
sylvainv / pg_record_timestamps.sql
Last active August 8, 2020 15:07
Records timestamps on insert/update helper for PostgreSQL
CREATE OR REPLACE FUNCTION add_timestamps_to_table(_table text, _type text) RETURNS void AS $$
BEGIN
EXECUTE format('ALTER TABLE %I ADD COLUMN IF NOT EXISTS created_at %s', _table, _type);
EXECUTE format('ALTER TABLE %I ADD COLUMN IF NOT EXISTS updated_at %s', _table, _type);
END;
$$ LANGUAGE 'plpgsql';
CREATE OR REPLACE FUNCTION set_timestamps_not_null_on_table(_table text) RETURNS void AS $$
BEGIN
EXECUTE format('ALTER TABLE %I ALTER COLUMN created_at SET NOT NULL', _table);
@EddyBorja
EddyBorja / postgres_timestamp_functions.sql
Last active August 8, 2020 15:00
Convenience methods for some commonly used timestamps in analytics or reporting.
CREATE OR REPLACE FUNCTION day_begin(dt timestamp with time zone DEFAULT CURRENT_TIMESTAMP)
RETURNS timestamp with time zone AS $$
BEGIN
RETURN date_trunc('day', dt);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION day_end(dt timestamp with time zone DEFAULT CURRENT_TIMESTAMP)
RETURNS timestamp with time zone AS $$
BEGIN
@sammchardy
sammchardy / convert-binance-interval-to-milliseconds.py
Last active April 26, 2019 07:51
Convert Binance interval to milliseconds
def interval_to_milliseconds(interval):
"""Convert a Binance interval string to milliseconds
:param interval: Binance interval string 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w
:type interval: str
:return:
None if unit not one of m, h, d or w
None if string not in correct format
int value of interval in milliseconds
@sammchardy
sammchardy / binance_get_historical_klines.py
Last active March 12, 2025 05:53
Get historical Klines from Binance
# uses the date_to_milliseconds and interval_to_milliseconds functions
# https://gist.github.com/sammchardy/3547cfab1faf78e385b3fcb83ad86395
# https://gist.github.com/sammchardy/fcbb2b836d1f694f39bddd569d1c16fe
from binance.client import Client
import time
def get_historical_klines(symbol, interval, start_str, end_str=None):
"""Get Historical Klines from Binance
@cristianc-ty
cristianc-ty / contact_table_triggers.sql
Created February 8, 2018 13:12
contact_table_triggers.sql
create function customer_search_email_insert()
returns trigger
security definer
language plpgsql
as $$
begin
insert into customer_search(customer_id, contact_method, is_stale, term, contact_id)
values(new.customer_id, 'Email', true, new.email_address, new.id);
return new;
end;