Skip to content

Instantly share code, notes, and snippets.

View tdsymonds's full-sized avatar

Tom Symonds tdsymonds

View GitHub Profile
@tdsymonds
tdsymonds / remove_non_printables.py
Last active August 9, 2019 21:56
How to remove non printable characters from a string in Python, taken from: https://www.tutorialspoint.com/How-to-trim-down-non-printable-characters-from-a-string-in-Python
import sys, unicodedata, re
# Get all unicode characters
all_chars = (unichr(i) for i in xrange(sys.maxunicode))
# Get all non printable characters
control_chars = ''.join(c for c in all_chars if unicodedata.category(c) == 'Cc')
# Create regex of above characters
control_char_re = re.compile('[%s]' % re.escape(control_chars))
# Substitute these characters by empty string in the original string.
def remove_control_chars(s):
return control_char_re.sub('', s)
@tdsymonds
tdsymonds / backbutton-closebootstrap-modal.js
Last active June 1, 2021 08:13 — forked from thedamon/backbutton close bootstrap modal
Cause back button to close Bootstrap modal windows
// For the below to work with the escape key
// I needed to add data-keyboard="false" to the modal
// in the HTML so that the standard bootstrap function
// doesn't fire, the below fires instead
$('div.modal').on('show.bs.modal', function() {
var modal = this;
var hash = modal.id;
window.location.hash = hash;
window.onhashchange = function() {
@tdsymonds
tdsymonds / polygon.py
Created March 1, 2017 12:54
How to create a polygon object for GeoDjango polygon field
from django.contrib.gis.geos import Polygon
polygon = Polygon([ [ -0.300718341203409, 51.499348080169298 ], [ -0.305825965105626, 51.498332099170185 ], [ -0.307022220327252, 51.502946258541684 ], [ -0.307484352569642, 51.502956525191117 ], [ -0.307389790901562, 51.50270877049892 ], [ -0.308467442785522, 51.502477927448041 ], [ -0.308630258983584, 51.502711378579441 ], [ -0.315328507266898, 51.501115424124485 ], [ -0.315307276005044, 51.500597160763 ], [ -0.31700604605745, 51.499968706315791 ], [ -0.316747263949783, 51.499679935641787 ], [ -0.317115403364906, 51.499542240450104 ], [ -0.322434764868367, 51.505306090523163 ], [ -0.326171329324033, 51.503962067782354 ], [ -0.32920456109009, 51.50959043971325 ], [ -0.326859549034093, 51.509719773727142 ], [ -0.319498302693188, 51.51090313757738 ], [ -0.319724317133101, 51.513533929252844 ], [ -0.320820301819518, 51.514534281758522 ], [ -0.32142240605236, 51.516032924757987 ], [ -0.322601530580793, 51.517087503992336 ], [ -0.323069801167743, 51.52081970904073 ], [
@tdsymonds
tdsymonds / mptt_m2m_admin.py
Created October 7, 2016 15:13
Improves the filter_horizontal widget for many to many admin relationship to model using django-mptt and django-parler.
# models.py
from mptt.models import MPTTModel, TreeForeignKey
from parler.models import TranslatableModel, TranslatedFields
from .managers import CategoryManager
class Category(MPTTModel, TranslatableModel):
parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)
@tdsymonds
tdsymonds / print_table.py
Last active October 7, 2016 15:14
A simple Python script that prints a table with nicely formatted columns
def printTable(table, first_row_is_header=True, spacing=4):
# caluclate the number of columns
cols = len(table[0])
# calculate the max width for each column
max_list = [None]*cols
for i in xrange(cols):
max_list[i] = max([len(str(x[i])) if i<len(x) else 0 for x in table])
# calc row length for lines