Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
gh_url = 'https://api.github.com'
req = urllib2.Request(gh_url)
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
// using jQuery
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
# -*- coding: utf-8 -*-
import scrapy
from scrapy.http.request import Request
from scrapy.selector import Selector
import urllib2
import re
import PyV8
import json
@Guest007
Guest007 / upprint.py
Created September 28, 2015 07:58 — forked from shvechikov/upprint.py
UnicodePrettyPrinter
# -*- coding: utf-8 -*-
import sys
from pprint import PrettyPrinter
class UnicodePrettyPrinter(PrettyPrinter):
"""Unicode-friendly PrettyPrinter
Prints:
- u'привет' instead of u'\u043f\u0440\u0438\u0432\u0435\u0442'
@Guest007
Guest007 / bobp-python.md
Created November 5, 2015 08:13 — forked from sloria/bobp-python.md
A "Best of the Best Practices" (BOBP) guide to developing in Python.

The Best of the Best Practices (BOBP) Guide for Python

A "Best of the Best Practices" (BOBP) guide to developing in Python.

In General

Values

  • "Build tools for others that you want to be built for you." - Kenneth Reitz
  • "Simplicity is alway better than functionality." - Pieter Hintjens
@Guest007
Guest007 / Deploy Notes
Created November 19, 2015 14:16 — forked from fission6/Deploy Notes
Deployment notes for Ubuntu Instance Nginx, uwsgi, postgres, django, virtualenv stack
Deployment notes for Ubuntu Instance
Nginx, uwsgi, postgres, django, virtualenv stack
* ssh root
root access (need pem)
ssh -i whatever.pem ubuntu@ec2-*-*-*-*.compute-1.amazonaws.com
* secure box
- cut off all ports but 22 and 80 using AWS Management Console
- edit /etc/ssh/sshd_config ensure PasswordAuthentication no
@Guest007
Guest007 / gist:9e82e7d5edc6fcc8ca79
Last active February 4, 2016 14:28
HyperlinkedNestedRelatedField
from rest_framework.relations import HyperlinkedRelatedField, PKOnlyObject
class HyperlinkedNestedRelatedField(HyperlinkedRelatedField):
def __init__(self, view_name, parent_fields_by_kwargs, **kwargs):
super(HyperlinkedNestedRelatedField, self).__init__(
view_name,
read_only=True,
source='*',
@Guest007
Guest007 / rename_table_migration.py
Created March 10, 2016 14:22 — forked from jamesmfriedman/rename_table_migration.py
Renaming a Django app that has migrations already sucks. This Is a way I found to do it that preserves your old migration history and keeps your contenttypes in order. The trick is, this migration cannot be in the app you are migrating, so stick it in your "core" app or another app you have installed. Just plug in your own old and new app names.
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
from django.db.models import get_app, get_models
class Migration(SchemaMigration):
@Guest007
Guest007 / comprehensions.md
Created March 30, 2016 09:06 — forked from bearfrieze/comprehensions.md
Comprehensions in Python the Jedi way

Comprehensions in Python the Jedi way

Beautiful is better than ugly. Explicit is better than implicit.

-- The Zen of Python

I frequently deal with collections of things in the programs I write. Collections of droids, jedis, planets, lightsabers, starfighters, etc. When programming in Python, these collections of things are usually represented as lists, sets and dictionaries. Oftentimes, what I want to do with collections is to transform them in various ways. Comprehensions is a powerful syntax for doing just that. I use them extensively, and it's one of the things that keep me coming back to Python. Let me show you a few examples of the incredible usefulness of comprehensions.

All of the tasks presented in the examples can be accomplished with the extensive standard library available in Python. These solutions would arguably be more terse and efficient in some cases. I don't have anything against the standard library. To me there is a certain

@Guest007
Guest007 / paginator.py
Created July 22, 2016 03:43 — forked from vaad2/paginator.py
paginator
class VTPaginator(dict):
def __init__(self, num_rows, **kwargs):
defaults = {
'offset': 0,
'limit': 10,
'view_num': 11,
'splitter': '...',
'view_one_page': False,
'min_limit': 10,
'max_limit': 50,