Skip to content

Instantly share code, notes, and snippets.

View imankulov's full-sized avatar

Roman Imankulov imankulov

View GitHub Profile
@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 / 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 / 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 / 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 / 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"
#!/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 / 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.
"""
@imankulov
imankulov / gist:3074650
Created July 9, 2012 06:42
django-celery patch
diff --git a/djcelery/migrations/0002_v25_changes.py b/djcelery/migrations/0002_v25_changes.py
index f3bb729..7013f7f 100644
--- a/djcelery/migrations/0002_v25_changes.py
+++ b/djcelery/migrations/0002_v25_changes.py
@@ -11,6 +11,10 @@ def ignore_exists(fun, *args, **kwargs):
fun(*args, **kwargs)
except DatabaseError, exc:
if "exists" in str(exc):
+ # don't panic, everything is okay, it's just a hack
+ if db.has_ddl_transactions:
@imankulov
imankulov / discount_calculator.py
Created May 28, 2012 03:27
Find out why this script doesn't work (discount calculator)
# -*- coding: utf-8 -*-
"""
Why calculator doesn't work and how to fix it?
I expect to see:
$ python /tmp/discount_calculator.py
950.0
@imankulov
imankulov / nominatim.py
Created May 13, 2012 05:17
Simple wrapper around nominatim geocoding web service
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import httplib2
import urllib
import json
def request(address, email=None, limit=None):
"""
Simple wrapper around nominatim geocoding web service