Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View esoergel's full-sized avatar

Ethan Soergel esoergel

View GitHub Profile
@esoergel
esoergel / line_counts.py
Last active March 4, 2019 05:22
Calculate statistics around the line length of a set of matching files
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Usage:
$ find . | grep py$ | python line_counts.py
"""
from __future__ import unicode_literals
import subprocess
import numpy

Beyond 'wrappers' - Using querysets to compartmentalize business logic into the Django ORM

The Django ORM is an invaluable tool for querying your data. However, as a generic tool, it's often not a clean fit. Querying logic ends up being duplicated in multiple places, often with subtle variations. Maybe you have helper functions for commonly used queries, but these can't be combined.

This article describes a technique for extending Django querysets with methods specific to your models. This lets you write business logic in a way that fits in to the Django ORM, like MyModel.objects.filter(...).my_custom_method().first()

Some benefits of encapsulating database queries are:

  • Reusability: This method can be reused elsewhere, preventing the need for duplication.
  • Unification: This query presumably represents something meaningful. By creating and using an abstraction, you avoid multiple implementations of the same concept, which may have subtle differences.
@esoergel
esoergel / puzzle_solver.py
Last active October 4, 2017 21:55
Brute force solution to that twisty snake puzzle thing.
import copy
import datetime
# The length of each segment, in order.
# Note that blocks at an intersection count as part of both segments
puzzle = [
3, 4, 4, 4, 2, 4, 2, 4, 2, 2, 2, 2, 2,
2, 2, 2, 2, 3, 2, 4, 3, 3, 2, 4, 2, 3,
2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 4, 2, 4,
]
@esoergel
esoergel / staticmethod.md
Last active May 24, 2023 12:34
Brief explanation and example of python's `staticmethod` decorator

You ever factor something out of a method and realize you can get rid of the dependency on self (which is really nice to do for clarity and testability)?

In my experience, typically this is then pulled in to a function, but then you have to move the definition all the way outside of the class, which can suck.

To compensate for this, sometimes you'll leave it as a method and just not use self in the body. This is a good use case for the @staticmethod decorator, which I'll explain:

// ==UserScript==
// @name Ignore whitespace button
// @namespace github-ignore-whitespace
// @description Adds a button to github diff views to toggle the "ignore whitespace" option.
// @include https://github.com/*
// @version 1
// @grant none
// ==/UserScript==
function main() {
$("#toc > .btn-group").prepend('<a class="btn btn-sm" href="?w=1">Ignore whitespace</a>');
<?xml version="1.0" encoding="UTF-8" ?>
<h:html xmlns:h="http://www.w3.org/1999/xhtml" xmlns:orx="http://openrosa.org/jr/xforms" xmlns="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://openrosa.org/javarosa" xmlns:vellum="http://commcarehq.org/xforms/vellum">
<h:head>
<h:title>New Commande UX</h:title>
<model>
<instance>
<data xmlns:jrm="http://dev.commcarehq.org/jr/xforms" xmlns="http://openrosa.org/formdesigner/3c2e45b4c8d9ce9df41dfed50a1031255f5ce8b8" uiVersion="1" version="1" name="New Commande UX">
<products ids="" count="" current_index="" vellum:role="Repeat">
<item id="" index="" jr:template="">
<is_required />
{
_index: report_xforms_20150406_1136
_type: report_xform
_id: a54c548fa13642d3bb1127ccab053452
_score: 1
_source: {
location_: [ ]
domain: pact
xmlns: http://dev.commcarehq.org/pact/patientupdate
__retrieved_case_ids: [
@esoergel
esoergel / gist:782725cd6c8956fc9d21
Last active August 29, 2015 14:17
Update everything
function hammer() {
git checkout master
git pull origin master
git submodule update --init --recursive
pip install -r requirements/requirements.txt -r requirements/dev-requirements.txt -r ⤷ requirements/prod-requirements.txt
find . -name '*.pyc' -delete
./manage.py syncdb --migrate
}
@esoergel
esoergel / multiple_inheritance.py
Last active August 29, 2015 14:15
Python multiple inheritance
class A1(object):
def __init__(self):
print "Class A1"
super(A1, self).__init__()
class A2(A1):
def __init__(self):
print "Class A2"
super(A2, self).__init__()
@esoergel
esoergel / github-ignore-whitespace.js
Last active June 17, 2017 01:55
Adds a button to github diffs to append ?w=1 to the diff url, ignoring whitespace.
// ==UserScript==
// @name Ignore whitespace button
// @namespace github-ignore-whitespace
// @description Adds a button to github diff views to toggle the "ignore whitespace" option.
// @include https://github.com/*
// @version 1
// @grant none
// ==/UserScript==
function main() {
$("#toc > .button-group").prepend('<a class="minibutton" href="?w=1">Ignore whitespace</a>');