Skip to content

Instantly share code, notes, and snippets.

import random
from typing import Optional, Any, List, Tuple
consonant_initial_digraphs = set(
["ch", "sh", "th", "thr", "ph", "wh", "ck", "kn", "wr"]
)
consonant_final_digraphs = set(["ch", "ng", "sh", "th", "tch"])
vowel_digraphs = [
"ai",
@SmileyChris
SmileyChris / urls.py
Last active June 18, 2018 22:23 — forked from rixx/urls.py
Delivering static files on fixed URL in Django
import os
import mimetypes
from django.contrib.staticfiles.storage import staticfiles_storage
from django.http import Http404, FileResponse
def static_file(request, path, content_type=None):
if not staticfiles_storage.exists(path):
raise Http404()
@SmileyChris
SmileyChris / ok.py
Last active May 29, 2018 00:35 — forked from converge/ok.py
class AccountsStatisticsDetail(TemplateView):
template_name = 'grow/accountsstatistics.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
username = self.kwargs.get('username')
if username:
context['username'] = username
context['account_stats'] = (
AccountsStatistics.objects
def user_in_group(user, group_id=None, group_id_list=None):
if group_id_list is None:
group_id_list = set(group_id]
if not hasattr(user, 'group_ids'):
user.group_ids = set(user.groups.values_list('pk', flat=True))
return user.group_ids.intersection(group_id_list)
@SmileyChris
SmileyChris / filter.py
Created January 6, 2016 02:44
Hexchat plugin to filter joins/parts except for recent talkers. Put it in ~/.config/hexchat/addons
__module_name__ = 'Filter'
__module_version__ = '1.0.custom'
__module_description__ = 'Filters join/part/voice messages'
import hexchat
import collections
from time import time
last_seen = {} # For each entry: the key is the user's nickname, the entry
# is a list: element 0: last seen time
import re
array_qs = re.compile(r'(\w+)\[(\d*)\]$')
class UnPHPify(object):
def process_request(self, request):
"""
Iterate request.GET, request.POST and request.FILES and amend the dict
class UpdateOrCreateMixin(object):
"""
A view mixin to allow views that use SingleObjectMixin to not fail if no pk/slug is provided.
Primarily useful for making an UpdateView that can create objects too. For example::
class MyView(UpdateOrCreateMixin, UpdateView):
...
url(r'^add/$', MyView.as_view(), name='my-add-view'),
@SmileyChris
SmileyChris / pipeline_finders.py
Last active November 10, 2015 17:54
Smarter django-pipeline finders
"""
Smarter django-pipeline finders.
In your settings file, you'll want something like this:
# Exclude pipelined sources from being found as static files.
STATICFILES_FINDERS = (
'pipeline_finders.PipelineFileSystemFinder',
'pipeline_finders.PipelineAppDirectoriesFinder',
'pipeline_finders.PipelineFinder',
@SmileyChris
SmileyChris / gist:10611277
Last active August 29, 2015 13:59
flatpages template tag
@register.assignment_tag
def flatpages(a, b, *order_first):
"""
Get the flatpages, optionally with specific ones ordered first.
Usage::
{% flatpages '/home/' '/about/' as flatpages %}
{% for flatpage in flatpages %}...{% endfor %}
"""
@SmileyChris
SmileyChris / conf.py
Last active August 29, 2015 13:57
AppSettings
from .conf_base import AppSettings
class Settings(AppSettings):
YOUR_SETTING = True
"""
Give each setting some documentation for sphinx autodoc if you like.
"""