Skip to content

Instantly share code, notes, and snippets.

View imankulov's full-sized avatar

Roman Imankulov imankulov

View GitHub Profile
@imankulov
imankulov / shortcut_manager.py
Created July 19, 2012 05:11
Django shortcut manager
# -*- coding: utf-8 -*-
from django.db import models
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
class ShortcutManager(models.Manager):
"""
Manager to get access to instances of your models with just a few keystrokes.
"""
#!/usr/bin/env python
import sys
import subprocess
def grep(fd, magic, chunk_size=1024, alignement=0):
"""
Iteratively yield positions of the magic in a file descriptor
:param fd: open file descriptor (device or a file)
:param magic: substring to find
@imankulov
imankulov / celery_fix_test.sh
Created August 14, 2012 15:43
Simple script to test the fix for broken celery migration
#!/bin/bash
set -eux
db="celery_test"
mysql -e "drop database $db; create database $db default charset utf8"
./manage.py syncdb --noinput;
./manage.py migrate djcelery 0001
# Comment out commands below to test the normal migration flow
mysql -e "drop table $db.celery_taskmeta"
@imankulov
imankulov / client.py
Created September 1, 2012 09:35
Helper class to ease testing Django views
# -*- coding: utf-8 -*-
from django.test.client import Client as BaseClient
from django.core.urlresolvers import reverse
class Client(BaseClient):
"""
I used to be reluctant testing Django views until I wrote this class
@imankulov
imankulov / file_lock.py
Last active December 5, 2017 06:46
This is how we do file locks (python 2.5+)
from __future__ import with_statement
import os
import time
import fcntl
import contextlib
@contextlib.contextmanager
def file_lock(filename, lock_type, timeout=None):
fd = open(filename, 'w')
fctnl_lock_type = getattr(fcntl, lock_type, None)
@imankulov
imankulov / set_vars.sh
Created December 2, 2012 09:33
Bash set_var / unset_var functions
#!/bin/bash
# function `set_var` sets a variable with a possiblity to restore previous
# value back with `unset_var`
#
# Can be used to safely initialize/uninitialize environment in python
# virtualenvwrapper and friends
#
#roman@home:~$ foo=1
#roman@home:~$ set_var foo 2
#roman@home:~$ set_var bar 3
@imankulov
imankulov / parse_pycon_us.py
Created December 11, 2012 10:54
PyCon US Parser
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# requirements: opterator, requests
import csv
import requests
from lxml import html
from opterator import opterate
def get_tree():
@imankulov
imankulov / android2po_plurals.py
Created December 17, 2012 14:54
Get gettext formatted plural forms from CLDR description using android2po (to do the main job) and opterator (for command line parsing)
#!/usr/bin/env python
"""
Requirements:
android2po
opterator
Sample:
$ python android2po_plurals.py pl
@imankulov
imankulov / parse_djangocon_eu.py
Last active December 10, 2015 22:28
DjangoCon Europe voting results parser
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# requirements: requests, lxml
import requests
from lxml import html
def get_tree():
resp = requests.get('http://2013.djangocon.eu/vote/')
tree = html.fromstring(resp.text)
@imankulov
imankulov / test_payments_bare.py
Created March 11, 2013 14:54
py.test fixture creation problem.
# -*- coding: utf-8 -*-
# This is how I'd implement this using only bare pytest functionality
import pytest
from user import add_transaction, delete_transaction, get_user_balance
# Attempt 1.
# According to the documentation at http://pytest.org/latest/fixture.html
# try to start with a simple fixture