Skip to content

Instantly share code, notes, and snippets.

import json
from collections import OrderedDict
def sort_names():
with open("some_file.txt", "+") as f:
full_names = [line for line in f.readlines()]
result = "\n".join(full_names.sorted(full_names, key=lambda x: x.split()[-1]))
f.write(result)
@Bpless
Bpless / gist:8344382
Last active January 2, 2016 18:29
Setting up your Django Environment

Setting up your dev environment

The instructions that I've laid out below should handle most of the process, but if you get confused (or want to validate that something is correct), check out this awesome tutorial for setting up a Django environment -- https://docs.djangoproject.com/en/dev/intro/tutorial01/

Whenever you see a white background with red text coloring in the sections below, that is a bash command to be run in your terminal.

Start by installing the following:

  • Command Line Tools for Xcode or Xcode - Available at the Apple Dev Center. Command Line Tools for Xcode is a smaller package but is only available for OS X Lion or higher, if you don't have it, download and install Xcode.
  • Homebrew - Download and install at http://mxcl.github.com/homebrew. Please follow the instructions carefully and place your Brew directory (/usr/local/bin by default) at the beginning of your $PATH.
@Bpless
Bpless / shell_plus_reloader.py
Last active June 5, 2018 11:40
Shell_Plus reloader thread
class ReloaderEventHandler(FileSystemEventHandler):
"""
Listen for changes to modules within the Django project
On change, reload the module in the Python Shell
Custom logic required to reload django models.py modules
Due to the singleton AppCache, which caches model references.
For those models files, we must clear and repopulate the AppCache
"""
def __init__(self, *args, **kwargs):
@Bpless
Bpless / gist:2294720
Created April 3, 2012 19:00
Caching Mongo DealUser Documents
from django.core.cache import cache
DEALUSER_CACHE_THRESHOLD = 20000
HIGH_DEALUSER_CACHE_THRESHOLD = 50000
def make_cache_key(self, deal_id, user_id):
return "%s__%d__%d" % (self._document._class_name, deal_id, user_id)
def get_from_cache(self, deal_id, user_id, force=False):
cache_key = self.make_cache_key(deal_id, user_id)
@Bpless
Bpless / gist:1772125
Created February 8, 2012 18:48
Compressed Document Result
from test.app import DecisionDocument, TrialDocument
subdoc = DecisionDocument(guilt='definitely', details={'bad_person_score': 'Very Bad Man'})
doc = TrialDocument(decision=[subdoc], reporter='Tom', jury='peers')
doc.save()
-----Object Created------
{
"_id" : ObjectId("4f32c2f4d0ba3a4b5e000000"),
@Bpless
Bpless / gist:1772027
Created February 8, 2012 18:41
Changed Mapped Collection Example
{
"_id" : {
"set" : ISODate("2012-02-08T18:37:52.191Z"),
"db_key" : "_id"
},
"decision" : {
"set" : ISODate("2012-02-08T18:37:52.191Z"),
"db_key" : "d",
"embedded_fields" : {
"guilt" : {
@Bpless
Bpless / gist:1772013
Created February 8, 2012 18:38
Mapped Collection Example
{
"_id" : {
"set" : ISODate("2012-02-08T18:37:52.191Z"),
"db_key" : "_id"
},
"judge" : {
"set" : ISODate("2012-02-08T18:37:52.191Z"),
"db_key" : "j"
},
"decision" : {
@Bpless
Bpless / gist:1771997
Created February 8, 2012 18:35
Compressed Document Example
from mongoengine.document import Document, EmbeddedDocument
class DecisionDocument(EmbeddedDocument):
guilt = fields.StringField(required=True)
details = fields.DictField()
meta = {'allow_inheritance': False}
@Bpless
Bpless / gist:1771930
Created February 8, 2012 18:23
MongoEngine Compression Code
from mongoengine.base import TopLevelDocumentMetaclass
class CompressedKeyDocumentMetaclass(TopLevelDocumentMetaclass):
def __new__(cls, name, bases, attrs):
"""
MongoEngine Document Classes access the 'TopLevelDocumentMetaclass'
__metaclass__. We allow that metaclass to set attrs on the class
and then compress the fields in the event that the instantiated
Document Class contains the meta attr 'compress_keys'
@Bpless
Bpless / gist:1771461
Created February 8, 2012 17:26
DealUser Object Example
{
"_id" : ObjectId("4f15adc6e9f3d887f2ba7a31"),
"uid" : 274790,
"did" : 720717,
"city_id" : 4,
"score" : 10000,
"published" : ISODate("2012-01-17T17:20:54Z"),
"active" : true,
"recs" : [
{