Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mbylstra
mbylstra / py_datetime_to_mysql.py
Last active December 23, 2015 04:19
convert a python date to a mysql date string
from django.db import connection
from django.db.backends.mysql.base import DatabaseOperations
def py_datetime_to_mysql(d):
db_ops = DatabaseOperations(connection)
return db_ops.value_to_db_datetime(d)
#eg:
from django.db import connection
py_datetime_to_mysql(datetime.now())
#modified version off http://code.activestate.com/recipes/576602-safe-print/
def sprint(*args, **kwargs):
"""Safely print the given string.
If you want to see the code points for unprintable characters then you
can use `errors="xmlcharrefreplace"`.
"""
errors = kwargs.get('errors', 'replace')
@mbylstra
mbylstra / django_runserver_debug_false
Created July 11, 2013 13:32
If you want to test logging with DEBUG=True on your local Django dev server, you need to do this.
manage.py runserver --insecure
#http://stackoverflow.com/questions/5836674/why-does-debug-false-setting-make-my-django-static-files-access-fail
@mbylstra
mbylstra / python_source_code_unicode
Last active December 19, 2015 12:19
allow unicode in python source code
# -*- coding: utf-8 -*-
@mbylstra
mbylstra / sort_git_branches_by_last_commit
Last active December 19, 2015 12:19
sort git branches by last commit
git for-each-ref --sort=-committerdate refs/heads/
#http://stackoverflow.com/questions/5188320/how-can-i-get-a-list-of-git-branches-ordered-by-most-recent-commit
@mbylstra
mbylstra / django_template_string_interp.py
Last active December 19, 2015 08:09
using Django templates for fancy string interpolation
t = Template("The first stooge in the list is {{ stooges.0 }}.")
c = Context({"stooges": ["Larry", "Curly", "Moe"]})
t.render(c)
#or if you want to render from an existing html template
from django.template.loader import render_to_string
rendered = render_to_string('my_template.html', context)
@mbylstra
mbylstra / djr_simple_api_view
Last active December 19, 2015 06:59
If you just want to use Django Rest Framework for authentication and just want to convert a dictionary to json.
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
class SimpleView(APIView):
renderer_classes = (JSONRenderer,)
permission_classes = (IsAuthenticated,)
def get(self, request):
content = {'testing': 123}
@mbylstra
mbylstra / gist:385fa8cbf22e58b3aa62
Last active December 3, 2015 13:38
django 1.7+ standalone script
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
django.setup()
################################################################################
@mbylstra
mbylstra / gist:c33077198227115ae590
Created March 18, 2015 06:19
turn on/off psql pager (annoying for \dt)
\pset pager
@mbylstra
mbylstra / gist:813d1a9c25cf671bb1a4
Created March 18, 2015 05:27
linux: temporarily change timezone for ls command
env TZ=Australia/Melbourne ls -al