Skip to content

Instantly share code, notes, and snippets.

@dnozay
dnozay / mixins.py
Created January 8, 2015 08:21
django-rest-framework mixin that caches serialization output with cache key based on DateTimeField(auto_now=True)
# License: MIT
# django-rest-framework mixin
class CachedReprMixin(serializers.ModelSerializer):
'''
mixin for ModelSerializer subclass whose model has a 'modified' field.
assuming the model has a 'modified' field:
modified = models.DateTimeField(auto_now=True)
@dnozay
dnozay / angular-moment.md
Last active August 29, 2015 14:11
angular momentjs directive.

If we detect angular is present, we can expose a directive:

<ANY moment date="scope.myDate" format="'lll'"></ANY>
'use strict';

/**
@dnozay
dnozay / README.md
Created November 23, 2014 06:01
simple distributed web crawler using flask + scrapy + redis

design

Requests are handled by flask, a bunch of urls are inserted in the object store (redis) and arguments are put on the queue (redis again) for workers to consume. More workers would mean more items processed in parallel.

Other possible implementations:

  • multiprocessing module for consuming all cpus.
  • multiprocessing.managers.SyncManager for distributing task to other machines.
@dnozay
dnozay / autoreload.py
Last active August 29, 2015 14:10
Autoreload features (code snippets)
# code snippet from django.utils.autoreload (BSD license)
# https://github.com/django/django/blob/master/django/utils/autoreload.py
def code_changed():
global _mtimes, _win
for filename in gen_filenames():
stat = os.stat(filename)
mtime = stat.st_mtime
if _win:
mtime -= stat.st_ctime
@dnozay
dnozay / triggers.sql
Created November 18, 2014 21:16
mirror bugzilla milesones into a custom field cf_support_milestone.
-- mirror the bugzilla milestones in another picklist
-- developers have their own target milestones (the default picklist for milestones)
-- support engineers have their own support milestones which is a custom field.
-- milestones -> cf_support_milestone
delimiter //
CREATE TRIGGER cf_milestones_insert
AFTER INSERT ON milestones
FOR EACH ROW
@dnozay
dnozay / trigger.sql
Created November 7, 2014 21:26
mysql change value on insert/update
-- example mysql triggers that change column values
-- before they are inserted/updated.
-- use case: when building code that comes from a git repository
-- we want to track the branch information, but some of the jobs
-- building the product use references rather than short names.
-- e.g. refs/remotes/origin/my/branch rather than origin/my/branch.
delimiter //
@dnozay
dnozay / main.py
Created November 1, 2014 23:53
python multi-processing example using initializer function.
#!/usr/bin/env python
# This example shows how to use multiprocessing with an initializer function.
# We take advantage of that to make the workers each have a custom initial
# load. And in particular example, we will make the workers sleep.
# Because we make them sleep different amounts, one of them is going to be
# ready much before the others, and thus we can guess easily which worker
# will do most of the work.
#
# see https://stackoverflow.com/questions/26693797/python-multiprocessing-process-number
[tox]
envlist = py{26,27,33}
[testenv]
commands = python setup.py test
@dnozay
dnozay / .travis.yml
Created October 25, 2014 23:28
Travis config file for simple project using tox.
language: python
python:
- "2.7"
install:
- pip install tox
script:
- tox
@dnozay
dnozay / tests.rb
Last active August 29, 2015 14:08
Jekyll filters to help with hashes.
@context = { 'myhash' => { 'b' => 'B', 'a' => 'A', 'c' => 'C' } }
@template = Liquid::Template.parse("{{ myhash | keys | to_ul }}")
@template.render(@context) # => "<ul><li>b</li><li>a</li><li>c</li></ul>"
@template = Liquid::Template.parse("{{ myhash | keys | sort | to_ul }}")
@template.render(@context) # => "<ul><li>a</li><li>b</li><li>c</li></ul>"