Skip to content

Instantly share code, notes, and snippets.

💭
I may be slow to respond.

David Cramer dcramer

💭
I may be slow to respond.
Block or report user

Report or block dcramer

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View attach_foreignkey.py
def attach_foreignkey(objects, field, select_related=None):
"""
Shortcut method which handles a pythonic LEFT OUTER JOIN.
``attach_foreignkey(posts, Post.thread)``
"""
field = field.field
qs = field.rel.to.objects.filter(pk__in=distinct(getattr(o, field.column) for o in objects))
if select_related:
qs = qs.select_related(*select_related)
@dcramer
dcramer / track_data.py
Created Dec 6, 2010
Tracking changes on properties in Django
View track_data.py
from django.db.models.signals import post_init
def track_data(*fields):
"""
Tracks property changes on a model instance.
The changed list of properties is refreshed on model initialization
and save.
>>> @track_data('name')
@dcramer
dcramer / cramermath.py
Created Mar 29, 2019 — forked from mattrobenolt/cramermath.py
David Cramer math
View cramermath.py
"""
cramermath
~~~~~~~~~~
Usage:
>>> import cramermath
>>> cramermath.log(10)
0.014728067495500818
"""
@dcramer
dcramer / args.py
Last active Feb 26, 2019
Python Standards (that I would change and enforce if I could)
View args.py
# dont do this
this_function_name(foo, bar
baz)
# do this
cramers_version(
foo, bar, baz)
# allow this
cramers_version(foo, bar,
View gist:3898601
View gist:6abbf037a51bc342f512724c889b2dc0
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCuJUzqmnQCHvvgqJgAxew3qqrJtNQp3Ap1T0Bu4LFSLJ1ysSOFkC0v1eCmmNrLvUf7WYRmDBDC0aH8pxKFjE1CFZqolJFlp469aVaOcsqP3dfERGuWZ2zXokZjZr88CDmja7QVQ1l9gevnftHxy9rgov2mNdRqpJZpNcgct+0JvyFeJKhaCcyX2nYK5G0qe8FDub1azT3E/Ey3g/NvNzbmyqjDdIEU5hJo5J2uDGq26gXrudzJ/iARl7vJrvn160VNIn+tUvsejyc8UuqwIxu/4GVzKRcENJJUOg0GYhkIss7++ZbvvnINxbDi9wCgf8C5IQ4MD6Pp6dlvdihov6Np dcramer@MacBook-Pro-4.local
View base_job.py
class BaseJobResource(Resource):
def dispatch_request(
self, provider: str, owner_name: str, repo_name: str, build_number: int, job_number: int, *args, **kwargs
) -> Response:
queryset = Job.query.join(Build, Build.id == Job.build_id).join(
Repository, Repository.id == Build.repository_id
).filter(
Repository.provider == RepositoryProvider(provider),
Repository.owner_name == owner_name,
Repository.name == repo_name,
View componentToString.js
import ReactDOM from 'react-dom';
const componentToString = function(node) {
let el = document.createElement('div');
ReactDOM.render(node, el);
return el.innerHTML;
};
View gist:550438
class QuerySetDoubleIteration(Exception):
"A QuerySet was iterated over twice, you probably want to list() it."
pass
# "Skinny" here means we use iterator by default, rather than
# ballooning in memory.
class SkinnyManager(Manager):
def get_query_set(self):
return SkinnyQuerySet(self.model, using=self._db)
@dcramer
dcramer / gist:7728286
Created Dec 1, 2013
find oldest file in directory
View gist:7728286
find -type f -printf '%T+ %p\n' | sort -r | head -1
You can’t perform that action at this time.