Skip to content

Instantly share code, notes, and snippets.

@kipanshi
kipanshi / gist:1519666
Created December 25, 2011 19:50
OrderedCITestSuiteRunner (if you use TransacionTestCase along with TestCase in Django)
import unittest
from django_jenkins.runner import CITestSuiteRunner
from django.test import TestCase
from django.test.simple import reorder_suite
from django_jenkins import signals
class OrderedCITestSuiteRunner(CITestSuiteRunner):
"""
This particular test suite runner do Django test cases ordering,
putting TransactionTestCase to the end.
@kipanshi
kipanshi / gist:1520747
Created December 26, 2011 08:13
Acessing global namespace
# Assume you have model ``Project`` with 1:M relation to ``Task``
# here ``p`` instance of ``Project``, and it has some amount of tasks
# Following inline is creating global variable ``t<idx>`` for each task (e.g. t1, t2, ...)
[globals().update({'t%s' % (idx + 1): t}) for idx, t in enumerate(p.tasks.all())]
@kipanshi
kipanshi / gist:1672996
Created January 24, 2012 22:04
Reduce + Replace
REPLACE_MAP = (
('project_dir', project_dir),
('user', user)
)
TEMPLATE = '{{ %s }}'
for line in source.readlines():
dest.write(
reduce(
@kipanshi
kipanshi / gist:1951748
Created March 1, 2012 18:07
Pretty Django models updating
def update(instance, **kwargs):
"""Helper to update instance of the model with data from kwargs."""
instance.__class__.objects.filter(pk=instance.pk).update(**kwargs)
@kipanshi
kipanshi / trafaret_tutorial.py
Created March 12, 2012 20:20 — forked from Deepwalker/gist:2023370
Trafaret Tutorial
def trafaret_tutorial():
"""
So, you have some structure from wild. Say this will be some JSON over API.
But you cant change this JSON structure.
"""
sample_data = {
'userNameFirst': 'Adam',
'userNameSecond': 'Smith',
'userPassword': 'supersecretpassword',
'userEmail': 'adam@smith.math.edu',
@kipanshi
kipanshi / gist:2413788
Created April 18, 2012 14:00
Mock datetime.now() for testing
class FakeDatetimeFactory(object):
"""Factory to help testing logic depending on ``datetime.now()``.
"""
@classmethod
def now(self, *args, **kwargs):
class FakeDatetime(datetime):
"""Datetime class with overriden ``now()`` method."""
@kipanshi
kipanshi / gist:2480979
Created April 24, 2012 15:57
My fist MIPS Assembly programm
# 1.s
#
# My first MIPS Assembly program
# This program let you input 2 numbers and output the result of addition
.data
prompt_msg: .asciiz "Enter two numbers to add:\n"
result_msg: .asciiz "Result: "
end_of_line: .asciiz "\n"
@kipanshi
kipanshi / load_dump.sh
Created May 7, 2012 19:07
Django autoloading Postgres sql dump
#!/bin/sh
# This script needs to be run from inside the project folder
# since it needs access to settings.py
#
# DISCLAMER: Use _only_ on local dev installation!
DB_NAME=`python -c "import settings; print settings.DATABASES['default']['NAME']"`
DB_USER=`python -c "import settings; print settings.DATABASES['default']['USER']"`
DB_PASS=`python -c "import settings; print settings.DATABASES['default']['PASSWORD\
']"`
@kipanshi
kipanshi / gist:2759479
Created May 20, 2012 20:47
Overriding settings in Django tests
class SettingsContextManager(object):
"""Allow to change ``settings.*`` in ``with`` context."""
def __init__(self, *settings_tuples):
self.settings = dict(settings_tuples)
self.orig_settings = dict([(key, getattr(settings, key))
for key in self.settings])
def __enter__(self, *args, **kwargs):
[setattr(settings, key, self.settings[key])
for key in self.settings]
@kipanshi
kipanshi / gist:2946670
Created June 18, 2012 03:08
Stub object
class _Stub(object):
"""Class for creating stub object.
For internal use.
"""
def __getattribute__(self, *args, **kwargs):
return self
def next(self):
raise StopIteration