Skip to content

Instantly share code, notes, and snippets.

@gcetusic
gcetusic / paramiko_http_proxy.py
Created November 28, 2016 12:22
Paramiko connection through http proxy
def http_proxy_tunnel_connect(proxy, target, timeout=None):
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
sock.connect(proxy)
logger.debug("connected")
cmd_connect = "CONNECT %s:%d HTTP/1.1\r\n\r\n" % target
@gcetusic
gcetusic / validators.py
Last active August 30, 2016 14:40
xsd validator in python
class Validator(object):
error = 'Invalid value: {value}. {message}'
def __init__(self, object_type=None, regex=None, max_length=None):
self.object_type = object_type
self.regex = regex
self.regex_object = re.compile(regex) if regex else None
self.max_length = max_length
def __call__(self, value):
@gcetusic
gcetusic / check_filetype
Created June 20, 2016 15:42
check file magic
import magic
file = open(file.name) # start of the file (probably a better way of doing this...)
filetype = magic.from_buffer(file.read(1024), mime=True)
if not allowed_filetype(filetype):
return io.bad_request('File type not supported.')
file.seek(0, 0)
@gcetusic
gcetusic / caller_name.py
Created March 30, 2016 13:32 — forked from techtonik/caller_name.py
Python - inspect - Get full caller name (package.module.function)
# Public Domain, i.e. feel free to copy/paste
# Considered a hack in Python 2
import inspect
def caller_name(skip=2):
"""Get a name of a caller in the format module.class.method
`skip` specifies how many levels of stack to skip while getting caller
name. skip=1 means "who calls me", skip=2 "who calls my caller" etc.
@gcetusic
gcetusic / random_articles.py
Last active February 24, 2016 13:56
Randomly select queryset elements
def get_random_section_articles(sections, nums, exclude_ids=[]):
"""
Get random site articles
:return: list
"""
random_articles = []
counter = 0
while counter < nums:
for section in sections:
@gcetusic
gcetusic / Django PIL Jpeg support test
Created February 4, 2016 12:20 — forked from philippWassibauer/Django PIL Jpeg support test
A test for checking if PIL has Jpeg support, including instructions to fix it for Ubuntu
import os
import unittest
from os.path import abspath
from PIL import Image
class PhotoTests(unittest.TestCase):
"""Checks JPEG support for your system. You need a folder 'testdata' that has a test.jpg in it
could be improved, but works fine for me at the moment"""
def test_jpg(self):
image_path = os.path.dirname(os.path.abspath(__file__))
@gcetusic
gcetusic / floodfill.py
Created February 1, 2016 12:42
Flood fill algorithm
def fill(x, y, image, color, old_color):
pixel_color = image.get_color(x, y)
if pixel_color == old_color:
image.set_color(color)
fill(x + 1, y, image, color, old_color)
fill(x, y + 1, image, color, old_color)
fill(x - 1, y + 1, image, color, old_color)
fill(x, y - 1, image, color, old_color)
return
@gcetusic
gcetusic / ajax_redirect_middleware.py
Created December 23, 2015 10:48
Don't redirect on ajax request because browser handles redirects automatically
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponseRedirect
class AjaxRedirect(object):
def process_response(self, request, response):
if request.is_ajax():
@gcetusic
gcetusic / flatten.py
Created December 21, 2015 14:20
flatten list
# [[1,2,[3]],4] -> [1,2,3,4]
flat = list()
def flatten(nested, flat):
for i in nested:
flatten(i, flat) if isinstance(i, list) else flat.append(i)
return flat
@gcetusic
gcetusic / app__init__.py
Created November 12, 2015 13:57
Override Django project global settings on application load
def inject_app_defaults(application):
"""Inject an application's default settings"""
try:
__import__('%s.settings' % application)
import sys
# Import our defaults, project defaults, and project settings
_app_settings = sys.modules['%s.settings' % application]
_def_settings = sys.modules['django.conf.global_settings']
_settings = sys.modules['django.conf'].settings