Skip to content

Instantly share code, notes, and snippets.

View vstoykov's full-sized avatar
🇧🇬

Venelin Stoykov vstoykov

🇧🇬
View GitHub Profile
@vstoykov
vstoykov / advanced_one_to_one_field.py
Created March 22, 2012 16:04
One To One field that does not rise an Exception
"""
One To One field that does not rise an Exception
In your models you now can write:
class Profile(models.Model):
user = AdvancedOneToOneField(User, related_name="profile")
And using in code like this:
@vstoykov
vstoykov / intcomma.js
Created July 30, 2012 11:43
Pretty print numbers with thousand delimiter
var intcomma = function(value, delimiter) {
/*
Original script found here https://gist.github.com/559757
Added optional argument delimiter to choose witch symbol
to use for thousand delimiter
*/
var origValue = String(value);
if (typeof(delimiter) == 'undefined') delimiter = ',';
var newValue = origValue.replace(/^(-?\d+)(\d{3})/, '$1'+delimiter+'$2');
if (origValue == newValue){
@vstoykov
vstoykov / extra_PS.sh
Last active October 7, 2015 18:27
Add some extra stuffs on BASH prompt
export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)} `if [ $? = 0 ]; then echo "\[\e[32m\]✔"; else echo "\[\e[31m\]✘"; fi` \[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] `__git_ps1 "(%s)"`\n \[\033[01;34m\]\$\[\033[00m\] '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(__git_ps1 " (%s) ")\$ '
fi
@vstoykov
vstoykov / convert_encoding.py
Last active October 10, 2015 07:18
Script that convert text files from one encoding to another.
#!/usr/bin/env python
# coding: utf-8
"""
Program that convert text files from one encoding to another.
By default it is used to convert windows-1251 encoded subtitles
into ISO-8859-5 ecoded because this is encoding for cyrilic
characters in Panasonic Viera TV
Tested on Python 2.7+ and Python 3.2+
@vstoykov
vstoykov / LCD-test.html
Created November 5, 2012 17:30
Simple page that rotate some main colors ('white', 'reg', 'green', 'blue', 'cyan', 'yellow', 'magenta', 'black') to test pixels of the LCD monitor.
<html>
<head>
<style type="text/css">
html, body { width: 100%; height: 100%; overflow: hidden; padding: 0; margin: 0; border: 0;}
.bg-white { background: #FFFFFF; }
.bg-red { background: #FF0000; }
.bg-green { background: #00FF00; }
.bg-blue { background: #0000FF; }
.bg-cyan { background: #00FFFF; }
.bg-yellow { background: #FFFF00; }
@vstoykov
vstoykov / eik_checker.py
Created December 20, 2012 10:31
Method for checking validity of bulgarian EIK/BULSTAT codes.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# eik_checker.py
#
# Copyright 2012 Venelin Stoykov <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
from string import letters
def calc_excel_column_number(string):
"""
Return number representing the column index in MS excel
A is the first column - return 1
B is the second column - return 2
...
Z is the 26th column - return 26
@vstoykov
vstoykov / timeit.py
Last active December 11, 2015 20:18
Simple class for easy measuring time for code execution
from time import time
class TimeIt:
"""
Simple class for easy measuring time for code execution
Usage:
with TimeIt('Simple description of my code'):
do something...
@vstoykov
vstoykov / XUACompatibleMiddleware.py
Created February 19, 2013 20:23
Django Middleware which add a X-UA-Compatible header to the response. This header tells to Internet Explorer to render page with latest possible version or to use chrome frame if it is installed.
class XUACompatibleMiddleware(object):
"""
Add a X-UA-Compatible header to the response
This header tells to Internet Explorer to render page with latest
possible version or to use chrome frame if it is installed.
"""
def process_response(self, request, response):
response['X-UA-Compatible'] = 'IE=edge,chrome=1'
return response
@vstoykov
vstoykov / ProfilinkMiddleware.py
Created June 18, 2013 11:01
Print information about executed SQL queries and time needed for execution
import re
from time import time
from django.core.exceptions import MiddlewareNotUsed
from django.conf import settings
from django.db import connection
PATH_INFO_RE = re.compile(r'^(/favicon\.ico|%s|%s)' % (settings.STATIC_URL, settings.MEDIA_URL))