Skip to content

Instantly share code, notes, and snippets.

View amontalenti's full-sized avatar

Andrew Montalenti amontalenti

View GitHub Profile
@amontalenti
amontalenti / tree.py
Last active January 20, 2016 12:00
Example of defining a custom data structure for arbitrarily-nestable trees.
class Tree(dict):
"""A Tree is a dict-like object that nests arbitrarily.
It supports both `tree["key"]` and `tree.key` syntax for lookups.
A lookup for a missing key, by either syntax, results in creation
of a new nested tree, e.g.
>>> tree = Tree()
>>> tree.a.b.c = "d"
>>> tree
@amontalenti
amontalenti / delayed.py
Last active December 24, 2015 12:44
When using Python with concurrency, it can sometimes be helpful to delay the execution of functions. This utility function helps with that.
import functools
def delayed(fn):
"""Convert fn into delayed_fn with delayed(fn).
Take a function and convert it into a function that, rather than invoking
the function's call, instead returns a 3-tuple of `(fn, args, kwargs)`.
This can then be later called with the simple `fn(*args, **kwargs)` application
syntax, or the 3-tuple can be passed to other APIs that expect it, like
postgres:
image: postgres:9.4.4
ports:
- "5432:5432"
redis:
image: redis:2.8.21
ports:
- "6379:6379"
elasticsearch:
image: elasticsearch:1.7.2
from __future__ import print_function
import elasticsearch
import collections
import json
def flatten(d, parent_key='', sep='_'):
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
#!/bin/sh
# on Ubuntu 14.04, set a pm-hibernate resume hook
# which is placed in /etc/pm/sleep.d/00_intel_pstate
# it sets up CPU intel pstate appropriately, which by default
# gets ruined by being scaled down to 50% of max CPU without the
# easy ability to change it back; also sets governor to performance
# for good measure, since apparently default is "powersave"
#/bin/bash
# weird one-liner to find top 100 Python imports in a project
# stolen/customized from Chris H. Wiggins @ https://gist.github.com/chrishwiggins/7518300e0d8649d8d791
find . -name '*.py' | grep -v -e '/venv/' -e '(' -e ')' | \
xargs -I % grep import % | sed -e 's/^[ ]*//' | \
grep -e '^from ' -e '^import ' | awk '{print $2}' | \
sed -e 's/^[\.]*//' | cut -d\. -f1 | tr '[A-Z]' '[a-z]' | \
sort -bfd | uniq -c | sort -nr | head -100
@amontalenti
amontalenti / pyqt.md
Created August 29, 2015 19:42
pyqt setup instructions

PyQt App

A PyQt application needs special build instructions.

PySide Setup

To install PySide, you'll need to follow the [PySide installation guide][pyside-install], which will likely installing Qt at the system level along with development headers.

feature area
0 A 32.5
1 A 45.6
2 A 42.1
3 B 1.5
4 B 6.08
5 B 5.1
6 C 5.9
7 C 16.5
8 C 32.5

Lucene Fundamentals

A useful set of Lucene fundamentals that are good for grok'ing Elasticsearch.

Jargon Glossary

  • document: a record; the unit of search; the thing returned as search results
  • field: a typed slot in a document for storing and indexing values
  • index: a collection of documents, typically with the same field mappings or schema
  • corpus: the entire set of documents in an index