Skip to content

Instantly share code, notes, and snippets.

@lenolib
lenolib / chunked_graphql_execute.js
Last active August 14, 2022 21:37
Monkey patches graphql execute in order to not block event loop by processing long arrays in chunks in completeListValue
# Related graphql-js issue: https://github.com/graphql/graphql-js/issues/2262
const rewire = require('rewire');
const rewiredExecuteModule = rewire('graphql/execution/execute');
const completeValue = rewiredExecuteModule.__get__('completeValue');
const handleFieldError = rewiredExecuteModule.__get__('handleFieldError');
const { GraphQLError } = require('graphql');
const { addPath, pathToArray } = require('graphql/jsutils/Path');
@lenolib
lenolib / postgres_fts.sql
Created December 16, 2020 09:50
postgresql full text search schema
ALTER TABLE my_table ADD COLUMN tsdoc tsvector;
CREATE INDEX my_table_tsvec_idx ON my_table USING GIN (customer_id, tsdoc);
-- Full text search for datafield specs
CREATE FUNCTION my_table_tsvec_trigger() RETURNS trigger AS $$
BEGIN
NEW.tsdoc :=
setweight(to_tsvector('simple', coalesce(NEW.name,'')), 'A') ||
setweight(to_tsvector('simple', coalesce(NEW.description,'')), 'B');
return NEW;
@lenolib
lenolib / enumerate-notion-items.py
Created December 11, 2019 11:17
Create a unique sequential id for items (tasks) in a notion database / view
import re
from notion.client import NotionClient
def enumerate_notion_items(
view_url, id_col, created_at_col="created_time", token=None, client=None
):
"""
Given that a property with name e.g. "Item ID" exists for a notion dataset,
and that at least one item has a value for that property, this function
@lenolib
lenolib / non-stdlib-imports.sh
Created August 22, 2016 12:07
Find python imports in current directory that are not part of the python standard library
# Find python imports in current directory that are not part of the python standard library
function non-stdlib-imports () {
# Requires isort to be pip-installed
stdlib_mods=`python -c "import isort, sys; sys.stdout.write('|'.join(isort.settings.default['known_standard_library']))"`
non_stdlib_used_mods=`grep "import " $(find . -name "*.py") | cut -f2 -d ' ' | cut -f1 -d'.' | sort | uniq | grep -v -E $stdlib_mods`
grep -nRE "$non_stdlib_used_mods" | grep -e "import.* " -e "from.* "
}
@lenolib
lenolib / graphite_retention_schema_breakdown.py
Created January 26, 2016 08:24
graphite retention schema breakdown
def archive_to_bytes(archive):
def to_seconds(s):
SECONDS_IN_A = {
's': 1,
'm': 1 * 60,
'h': 1 * 60 * 60,
'd': 1 * 60 * 60 * 24,
'y': 1 * 60 * 60 * 24 * 365,
}
from IPython.core.history import HistoryAccessor
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
hist_access = HistoryAccessor()
start_times, n_cmds = zip(*list(hist_access.db.execute('select start, num_cmds from sessions')))
num_cmds = pd.Series(n_cmds, index=start_times)
cmds_hod = num_cmds.groupby(num_cmds.index.hour).agg(sum)
starts = pd.Series(np.ones(len(start_times)), index=start_times)
@lenolib
lenolib / Decomposed printing
Last active December 26, 2015 19:39
Provides a decomposed heirachical overview of objects and their component types.
from __future__ import print_function
from collections import Iterable, Sized
def deprint(obj, depth=5, indent=0, size_limit=10, index=""):
if not depth:
print("".join([" "*indent,"[reached depth limit]"]))
return
type_str = type(obj).__name__
if type(obj).__module__ != '__builtin__':
type_str = ".".join([type(obj).__module__, type_str])