Skip to content

Instantly share code, notes, and snippets.

@liquidgenius
liquidgenius / postgresql_crop_image.sql
Created August 8, 2020 14:30 — forked from rominf/postgresql_crop_image.sql
PostgreSQL function to crop images (uses plpythonu and python PIL)
CREATE OR REPLACE FUNCTION crop(image bytea, rect box)
RETURNS bytea
LANGUAGE plpythonu
AS $function$
if ('io' in SD) and ('StringIO' in SD) and ('Image' in SD):
io = SD['io']
StringIO = SD['StringIO']
Image = SD['Image']
else:
import io, StringIO
CREATE OR REPLACE FUNCTION public.geocode_google(IN inaddress text, OUT address text, OUT longitude double precision, OUT latitude double precision)
RETURNS record AS
$BODY$
from geopy.geocoders import GoogleV3
geolocator = GoogleV3()
try:
address, (latitude, longitude) = geolocator.geocode(inaddress,timeout=1,exactly_one=True)
return address, longitude, latitude
except:
return None, None, None
@liquidgenius
liquidgenius / psql_text_similaritry_operator.sql
Created August 7, 2020 23:08 — forked from sthum/psql_text_similaritry_operator.sql
Pragmatic solution for calculating the similarity of text / words of database entires in PostgreSQL
--- If you are missing the pypythonu extension, install it first
--- (Ubuntu): sudo apt-get install python-psycopg2
--- Activate plpython
CREATE EXTENSION plpythonu;
--- Create the profiling function
CREATE OR REPLACE FUNCTION similarity(text, text) RETURNS numeric AS $$
import difflib
return difflib.SequenceMatcher(None,args[0], args[1]).ratio()
@liquidgenius
liquidgenius / plpython_returning_record.py
Created August 7, 2020 23:05 — forked from happysundar/plpython_returning_record.py
Example of a PLPythonU method that returns a record
DROP FUNCTION IF EXISTS get_role_to_actor_and_actor_to_role( INOUT BIGINT, OUT JSONB, OUT JSONB );
CREATE OR REPLACE FUNCTION get_role_to_actor_and_actor_to_role(
INOUT program_id BIGINT,
OUT actor_to_role JSONB,
OUT role_to_actor JSONB)
RETURNS RECORD IMMUTABLE
AS $plpython_function$
import json
@liquidgenius
liquidgenius / postgres-cheatsheet.md
Created August 7, 2020 14:20 — forked from Kartones/postgres-cheatsheet.md
PostgreSQL command line cheatsheet

PSQL

Magic words:

psql -U postgres

Some interesting flags (to see all, use -h or --help depending on your psql version):

  • -E: will describe the underlaying queries of the \ commands (cool for learning!)
  • -l: psql will list all databases and then exit (useful if the user you connect with doesn't has a default database, like at AWS RDS)
@liquidgenius
liquidgenius / logging.py
Created February 24, 2020 23:02 — forked from kingspp/logging.py
Python Comprehensive Logging using YAML Configuration
import os
import yaml
import logging.config
import logging
import coloredlogs
def setup_logging(default_path='logging.yaml', default_level=logging.INFO, env_key='LOG_CFG'):
"""
| **@author:** Prathyush SP
| Logging Setup
@liquidgenius
liquidgenius / logging.py
Created February 21, 2020 01:32 — forked from vtemian/logging.py
logging flask
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {},
'formatters': {
'syslog': {
'format': '%(asctime)s ' + HOSTNAME_SHORT +
' %(name)s: %(levelname)s: %(message)s',
'datefmt': '%b %d %H:%M:%S',
}
@liquidgenius
liquidgenius / low_pass_filter.py
Last active September 12, 2022 09:19 — forked from kperry2215/low_pass_filter.py
Implement a low-pass filter to detect anomalies in a time series, and save the filter outputs (True/False) to a new column in the dataframe.
def low_pass_filter_anomaly_detection(df: pd.DataFrame,
column_name: str,
rolling_window: int,
number_of_stdevs_away_from_mean: float):
"""
Implement a low-pass filter to detect anomalies in a time series, and save the filter outputs
(True/False) to a new column in the dataframe.
Arguments:
df: Pandas dataframe
column_name: string. Name of the column that we want to detect anomalies in
@liquidgenius
liquidgenius / install-docker.md
Created July 26, 2019 16:08 — forked from npearce/install-docker.md
Amazon Linux 2 - install docker & docker-compose using 'sudo amazon-linux-extras' command
@liquidgenius
liquidgenius / python-parsing-dates.py
Created May 28, 2019 00:23 — forked from aviafelix/python-parsing-dates.py
Python datetime / dateutil parsing dates example
import datetime
import dateutil.parser
timestring1 = '2007-03-04T21:08:12.127'
timestring2 = '2012-03-04 12:08:12.354'
dt1 = datetime.datetime.strptime(timestring1, '%Y-%m-%dT%H:%M:%S.%f')
dt2 = datetime.datetime.strptime(timestring2, '%Y-%m-%d %H:%M:%S.%f')
dup1 = dateutil.parser.parse(timestring1)
dup2 = dateutil.parser.parse(timestring2)