Skip to content

Instantly share code, notes, and snippets.

@pirate
Created June 30, 2016 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pirate/2659b242bded82c3c58f2458e6885738 to your computer and use it in GitHub Desktop.
Save pirate/2659b242bded82c3c58f2458e6885738 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# ~/.pythonrc
# add this to your ~/.bashrc:
# export PYTHONSTARTUP="~/.pythonrc"
### Common Useful Imports
import os, sys
import datetime, time, random
import socket, json, re, pdb
try:
from Queue import Queue, Empty
from collections import OrderedDict, defaultdict
from functools import wraps
except:
pass
### Set Up Instant Command Shortcuts
def cd(folder):
"""e.g. cd('/bin')"""
os.chdir(folder)
def quit():
print('[√] Insta-quit')
raise SystemExit(0)
def warn_repr_globals():
raise Exception('__repr__ of globals was stopped to protect from insta-quit command being accidentally triggered.')
def listdir(dir=None):
return os.listdir(dir or os.getcwd())
class InstantShortcut:
"""run a command instantly on __repr__, without needing parens to call it"""
callable = lambda: []
def __init__(self, command, output_delimiter='\n'):
self.callable = command
self.output_delimiter = output_delimiter
def __call__(self, *args, **kwargs):
return self.callable(*args, **kwargs)
def __repr__(self):
return self.output_delimiter.join(self())
_GLOBALS_REPR_WARNING_ = InstantShortcut(warn_repr_globals)
quit = q = InstantShortcut(quit)
ls = InstantShortcut(listdir, ' ')
ll = InstantShortcut(listdir, '\n')
_GLOBALS_REPR_WARNING_ = InstantShortcut(warn_repr_globals)
# print order of the globals() dict is essintially random, so this
# tries to make sure _GLOBALS_REPR_WARNING_ is repr'ed before quit()
### Load django environment and all models
import django
oldpath = os.getcwd()
try:
# find the parent with settings.py if you are in a subdirectory
while "settings.py" not in os.listdir(os.getcwd()) and os.getcwd() != "/":
os.chdir("..")
if not "settings.py" in os.listdir(os.getcwd()):
raise Exception("Not in a django project.")
if "bin" in os.listdir(os.getcwd()):
sys.path.append(os.getcwd() + "/bin")
import activate_this
sys.path.remove(os.getcwd() + "/bin")
# add the django prject root to the path
sys.path.append(os.path.abspath(os.getcwd() + "/.."))
# sys.path.append(os.getcwd())
# import settings and set django settings environment variable
import settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.settings" % os.path.split(os.getcwd())[-1])
# Import all the installed_apps
try:
exec_strs = ["import %s" % app for app in settings.INSTALLED_APPS ] + ["from %s.models import *" % app for app in settings.INSTALLED_APPS ]
for x in exec_strs:
try:
exec(x)
except Exception as e1:
pass
print('imported django models')
except Exception as e2:
pass
# finally, setup the django environment
django.setup()
except Exception as e3:
# reset current dir to the one python was opened in
os.chdir(oldpath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment