-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path.pythonrc
More file actions
67 lines (58 loc) · 2.6 KB
/
Copy path.pythonrc
File metadata and controls
67 lines (58 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Author: Lucas Cimon (github.com/Lucas-C/linux_configuration)
# FROM: http://dlo.me/archives/2014/09/08/pythonrc/
# USAGE: export PYTHONSTARTUP="$HOME/.pythonrc"
# vim: set ft=python :
from __future__ import unicode_literals, print_function, absolute_import
# Automatically importing useful standard libs
import json, os, re, sys
from pprint import pprint
# quick aliases
p = print
pp = pprint
# so you can just use 'quit' to exit - FROM: https://www.reddit.com/r/Python/comments/4ivd2k/what_is_your_favorite_python_error_message/
class Quit(object):
def __repr__(self):
exit()
quit = Quit()
q = quit
try:
from django import test
from django.db import connection
from django.core.management import call_command
# Providing a handy 'load_fixture' function in the Django shell,
# and registering a cleanup function executed when exiting the shell
# FROM: http://testedwebdev.blogspot.ru/2012/05/django-shell-testing.html
DJANGO_TEST_DB = None
def load_fixture(filename):
global DJANGO_TEST_DB
if DJANGO_TEST_DB:
print('A Django test DB already exists, please first call django_testdb_teardown()')
return
import atexit
atexit.register(django_testdb_teardown)
test.utils.setup_test_environment()
DJANGO_TEST_DB = connection.creation.create_test_db()
call_command('loaddata', filename)
def django_testdb_teardown():
global DJANGO_TEST_DB
if not DJANGO_TEST_DB:
return
connection.creation.destroy_test_db(DJANGO_TEST_DB)
test.utils.teardown_test_environment()
# We are in django shell, hence we store the command history in a dedicated file
# (this has to be done manually, as by defaukt nothing is read/stored in any file)
# FROM: http://jpadilla.com/post/59673924412/python-shell-history
DJANGO_HISTORY_FILE = os.path.join(os.environ['HOME'], '.django_history')
if os.path.exists(DJANGO_HISTORY_FILE):
readline.read_history_file(DJANGO_HISTORY_FILE)
import atexit
atexit.register(readline.write_history_file, DJANGO_HISTORY_FILE)
except ImportError:
pass
if sys.version_info >= (3, 0) and hasattr(sys, 'real_prefix'): # in a VirtualEnv, we need to manual read/write the history, cf. https://github.com/berdario/pew/issues/90
PYTHON_HISTORY_FILE = os.path.join(os.environ['HOME'], '.python_history')
if os.path.exists(PYTHON_HISTORY_FILE):
readline.read_history_file(PYTHON_HISTORY_FILE)
import atexit
atexit.register(readline.write_history_file, PYTHON_HISTORY_FILE)
print('.pythonrc successfully LOADED')