A "Best of the Best Practices" (BOBP) guide to developing in Python.
- "Build tools for others that you want to be built for you." - Kenneth Reitz
- "Simplicity is alway better than functionality." - Pieter Hintjens
#!/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 |
# -*- 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' |
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 |
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='*', |
# -*- 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): |
Beautiful is better than ugly. Explicit is better than implicit.
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
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, |