Skip to content

Instantly share code, notes, and snippets.

@igniteflow
igniteflow / middleware.py
Created March 11, 2014 12:02
Django language middleware. Detect the user's browser language settings and activate the language. If the default language is not supported, try secondary options. If none of the user's languages are supported, then do nothing.
class LanguageMiddleware(object):
"""
Detect the user's browser language settings and activate the language.
If the default language is not supported, try secondary options. If none of the
user's languages are supported, then do nothing.
"""
def is_supported_language(self, language_code):
supported_languages = dict(settings.LANGUAGES).keys()
return language_code in supported_languages
@miki725
miki725 / .bash_prompt.sh
Last active April 5, 2024 00:04
Custom bash prompt which displays: (virtualenv) user:/path (git-branch)
#!/bin/bash
#
# DESCRIPTION:
#
# Set the bash prompt according to:
# * the active virtualenv
# * the branch of the current git/mercurial repository
# * the return value of the previous command
# * the fact you just came from Windows and are used to having newlines in
# your prompts.
@darkopetrovic
darkopetrovic / gist:11292725
Created April 25, 2014 15:04
Datalog and stream light sensor and voltage reading from ADC with Raspberry PI to Plot.ly
#!/usr/bin/python
import os, time, signal, sys
from Adafruit_ADS1x15 import ADS1x15
import RPi.GPIO as GPIO
from datetime import datetime
import subprocess
import argparse
import requests
import json
@gubatron
gubatron / multiple-deploy-keys-multiple-private-repos-github-ssh-config.md
Last active March 12, 2024 07:34
How to configure multiple deploy keys for different private github repositories on the same computer without using ssh-agent

How to configure multiple deploy keys for different private github repositories on the same computer without using ssh-agent

Let's say alice is a github.com user, with 2 or more private repositories repoN. For this example we'll work with just two repositories named repo1 and repo2

https://github.com/alice/repo1

https://github.com/alice/repo2

You need to be to pull from these repositories without entering a passwords probably on a server, or on multiple servers.

@Zulko
Zulko / cubes_pyode_vapory.py
Last active December 27, 2023 12:29
3D cubes animation with PyODE and Vapory
"""
Physics simulation with PyODE followed by a (basic) rendering with Vapory
See the result here: http://i.imgur.com/TdhxwGz.gifv
Zulko 2014
This script is placed in the Public Domain (Licence Creative Commons 0)
"""
@fabiosussetto
fabiosussetto / gist:c534d84cbbf7ab60b025
Last active December 2, 2023 01:11
Enable Django toolbar for JSON responses
class NonHtmlDebugToolbarMiddleware(object):
"""
The Django Debug Toolbar usually only works for views that return HTML.
This middleware wraps any JSON response in HTML if the request
has a 'debug' query parameter (e.g. http://localhost/foo?debug)
"""
@staticmethod
def process_response(request, response):
if request.GET.get('debug'):
@willcharlton
willcharlton / maps_url.py
Last active July 2, 2021 10:40
Python (3) snippet to generate google maps url and timezone for a given address.
#!/usr/bin/env python3
"""
So far only tested on python3 interpreter.
Given an address, this script outputs a google maps url to the address.
"""
import requests, sys, time
from urllib.parse import quote_plus
@rokcarl
rokcarl / change-url-parameters.py
Last active January 29, 2024 12:25
Update URL parameters in Python 3.
import urllib.parse
url = "http://stackoverflow.com/search?q=question"
params = {'lang':'en','tag':'python'}
url_parts = list(urllib.parse.urlparse(url))
query = dict(urllib.parse.parse_qsl(url_parts[4]))
query.update(params)
url_parts[4] = urllib.parse.urlencode(query)
@alexgleason
alexgleason / m2m.py
Last active May 9, 2023 17:49
Many to many relationships in Wagtail
from django.db import models
from wagtail.wagtailsnippets.models import register_snippet
from wagtail.wagtailcore.models import Page
from modelcluster.fields import ParentalKey
from wagtail.wagtailadmin.edit_handlers import InlinePanel
@register_snippet
class Category(models.Model):
@yrevar
yrevar / imagenet1000_clsidx_to_labels.txt
Last active April 30, 2024 12:39
text: imagenet 1000 class idx to human readable labels (Fox, E., & Guestrin, C. (n.d.). Coursera Machine Learning Specialization.)
{0: 'tench, Tinca tinca',
1: 'goldfish, Carassius auratus',
2: 'great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias',
3: 'tiger shark, Galeocerdo cuvieri',
4: 'hammerhead, hammerhead shark',
5: 'electric ray, crampfish, numbfish, torpedo',
6: 'stingray',
7: 'cock',
8: 'hen',
9: 'ostrich, Struthio camelus',