Skip to content

Instantly share code, notes, and snippets.

View jmichalicek's full-sized avatar

Justin Michalicek jmichalicek

View GitHub Profile
import logging
import time
from contextlib import ContextDecorator
class log_execution_time(ContextDecorator):
"""
A context processor and decorator for logging timings.
logger = logging.getLogger(__name__)
@log_execution_time('my_func', logger, logging.DEBUG)
@jmichalicek
jmichalicek / django_order_by_list.py
Created June 17, 2019 12:16
Django ORM select and order based on pre-ordered list of ids
# I can never remember that this is a thing, but it is occasionally useful for me
ids = [5, 2, 3, 1, 4]
order = Case(*[When(id=id, then=pos) for pos, id in enumerate(ids)])
queryset = MyModel.objects.filter(id__in=ids).order_by(order)
@jmichalicek
jmichalicek / example.py
Last active September 26, 2018 14:02
Simple example of what Python's Graphene library does
"""
This is an example of how Python's Graphene library behaves. This can result in `self` not being what you expect, particularly
when using graphene-django and `self` on your graphene resolver object is the underlying django model
"""
class A:
def foo(self):
print("This is the foo() method on an object of class", type(self))
class B:
pass
@jmichalicek
jmichalicek / truthy_or_falsey_examples.md
Last active September 1, 2017 02:10
Simple truthy vs falsey examples in a few languages

Python

Python 3.6.1 (default, Apr 12 2017, 14:04:19)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> if 0:
...     print("0 is truthy")
... else:
...     print("0 is falsey")
@jmichalicek
jmichalicek / mousemon.py
Last active July 21, 2017 13:33
Tap sleeping/backlight powered down raspberry pi touchscreen even in console to wake it
"""
Raspberry pi touchscreens get blanked and then later the backlight turned off.
When in X of some sort, taps on the screen can turn it back on. When in a console
taps on the touchscreen are not seen and the display stays asleep.
This script polls mouse inputs in /dev/input for any action on the raspberry pi touchscreen
if there is action, update the file which controls the backlight power to
turn the backlight back on.
This should be tweaked more to not be constantly writing to the bl_power file, possibly monitor
@jmichalicek
jmichalicek / influxdb_data_copy.txt
Created June 12, 2017 17:20
Copy data from one measurement to a new measurement name in influxdb
# from https://github.com/influxdata/influxdb/issues/4155#issuecomment-268719194
SELECT * INTO new_name FROM old_name
DROP MEASUREMENT old_name
@jmichalicek
jmichalicek / Child.ex
Last active September 10, 2016 02:41
Form for a model and related child models in Phoenix 1.2 and Ecto 2.0.
defmodule MyApp.Child do
use MyApp.Web, :model
schema "child" do
field :name, :string
field :description, :string
belongs_to :event, MyApp.Parent
timestamps
end
@jmichalicek
jmichalicek / PrimaryKeyInObjectOutRelatedField.py
Created February 23, 2016 18:19
Django Rest Framework RelatedField allows a PK for input but returns a fully serialized model in response
class PrimaryKeyInObjectOutRelatedField(relations.PrimaryKeyRelatedField):
"""
Django Rest Framework RelatedField which takes the primary key as input to allow setting relations,
but takes an optional `output_serializer_class` parameter, which if specified, will be used to
serialize the data in responses.
Usage:
class MyModelSerializer(serializers.ModelSerializer):
related_model = PrimaryKeyInObjectOutRelatedField(
queryset=MyOtherModel.objects.all(), output_serializer_class=MyOtherModelSerializer)
@jmichalicek
jmichalicek / 114_easy.py
Last active December 10, 2015 01:18
reddit dailyprogrammer exercises will live here
"""
The basic solution to http://www.reddit.com/r/dailyprogrammer/comments/149kec/1242012_challenge_114_easy_word_ladder_steps/
in Python. Neither of the bonuses at the moment
"""
from sys import argv
testword = argv[1]
# wordlist from http://pastebin.com/zY4Xt7iB
for word in open('114_wordlist.txt', 'r'):
@jmichalicek
jmichalicek / handlebars_ajax_templates.js
Created October 27, 2012 00:54
Handlebars.js ajax templates example from Djukebox
$(document).ready(function() {
// lots of other stuff in the full code
var compiled_templates = new Object();
var ALBUMLIST = 'album-list.js'; // real code has several more of these. It's the filename of a handlebars.js template
function getTemplateAjax(file) {
// downloads a handlebars.js template via ajax and sticks it into the cache object after compiling
var template; // this could really go down in the success function
return $.ajax({