Skip to content

Instantly share code, notes, and snippets.

View petrushev's full-sized avatar

Baze Petrushev petrushev

View GitHub Profile
@petrushev
petrushev / decimal_date_json_handlers.py
Last active December 21, 2015 17:29
Serializer + object hook for Decimal and date objects
from json import loads, dumps, JSONEncoder
from decimal import Decimal
import datetime
class Encoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, Decimal):
return '__decimal__' + str(obj)
@petrushev
petrushev / scope_gotcha.py
Created August 15, 2013 09:14
Nested function scope gotcha
def echo_public():
def nested():
print public_
public_ = 5
nested()
echo_public()
@petrushev
petrushev / nested.data.redis
Created August 9, 2013 09:07
example of storing nested data structures in redis
roomrate
========
{
'city': 'Berlin',
'hotel': 'Hilton',
'date': '2013-08-09'
'room_type': 'single',
'occupancy': 2,
'avail_count': 13,
'booked_count': 4,
@petrushev
petrushev / point_in_polygon.py
Created April 23, 2013 23:56
Determine if a point is inside a given polygon or not Polygon is a list of (x,y) points. Uses the 'Ray Casting' algorithm
def is_inside(polygon, point):
"""Determine if a point is inside a given polygon or not
Polygon is a list of (x,y) points.
Uses the 'Ray Casting' algorithm"""
x, y = point
n = len(polygon)
inside = False
@petrushev
petrushev / qpac.py
Created October 28, 2012 14:12
Get missing optional dependencies and see which packages need them
from subprocess import check_output
EMPTY = tuple()
def get_pkgs():
"""Query local packages"""
tmp = check_output(["pacman", "-Qi"])
li = tmp.strip().split("\n\n")
@petrushev
petrushev / mudrecot.py
Created October 13, 2012 16:41
Create custom `mudrecot` meme
# -*- coding: utf-8 -*-
"""Create custom `mudrecot` meme"""
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
font_path = "/usr/share/fonts/TTF/LiberationSans-Bold.ttf"
@petrushev
petrushev / crawl_mp.py
Created October 11, 2012 20:35
Get large thumbnails from imgur album
# Get name and image of mps from sobranie.mk and put them in `PATH`
import requests as rq
from lxml.html import fromstring
PATH = 'target'
base = 'http://www.sobranie.mk/'
start = base+'?ItemID=C5223B907BD9D247A9245C0C30C2E6AE'
def main():
@petrushev
petrushev / matplotlibrc
Created July 25, 2012 12:25 — forked from huyng/matplotlibrc
my default matplotlib settings
### MATPLOTLIBRC FORMAT
# This is a sample matplotlib configuration file - you can find a copy
# of it on your system in
# site-packages/matplotlib/mpl-data/matplotlibrc. If you edit it
# there, please note that it will be overridden in your next install.
# If you want to keep a permanent local copy that will not be
# over-written, place it in HOME/.matplotlib/matplotlibrc (unix/linux
# like systems) and C:\Documents and Settings\yourname\.matplotlib
# (win32 systems).
@petrushev
petrushev / collate_specific.py
Created June 21, 2012 11:59
Sort list of strings by a specific locale
# -*- coding: utf-8 -*-
import locale
default_locale = locale.getlocale(locale.LC_COLLATE)
def sort_strings(strings, locale_=None):
if locale_ is None:
return sorted(strings)
locale.setlocale(locale.LC_COLLATE, locale_)
sorted_strings = sorted(strings, cmp=locale.strcoll)
@petrushev
petrushev / ircwatch.py
Created June 14, 2012 00:36
Irc logger on the command line
import requests
from time import sleep
from simplejson import loads, dumps
from datetime import datetime
BASE = 'http://irc.softver.org.mk/api/_changes'
DEFAULT_PARAMS = {'feed':'longpoll', 'heartbeat':'30000', 'include_docs':'true',
'filter':'log/channel', 'channel':'lugola'}