This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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__)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- 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(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # [[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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |