Skip to content

Instantly share code, notes, and snippets.

View spookylukey's full-sized avatar

Luke Plant spookylukey

View GitHub Profile
@spookylukey
spookylukey / hollerith.py
Created September 27, 2017 06:57
Hollerith constant parsing in pyparsing
import pyparsing as pp
def hollerith():
intExpr = pp.Word(pp.nums).setParseAction(lambda t: int(t[0]))
stringExpr = pp.Forward()
def countedParseAction(toks):
n = toks[0]
contents = pp.CharsNotIn('', exact=n)
stringExpr << (pp.Suppress(pp.CaselessLiteral('H')) + contents)
@spookylukey
spookylukey / convert_leading_spaces_to_en
Created June 22, 2017 07:05
Make source code readable on forums that have poor support
#!/usr/bin/env python3
import sys
def fix_line(l):
if not l:
return l
if l[0] == " ":
# Non breaking: "\u00a0" - but munged by some browsers
# EM space: "\u2003" - not munged by browsers
# If it's complex, Django's Generic CBVs will probably
# make your life harder. And if it's simple, they probably
# will too.
# Before:
from django.views import generic
class ReportPDFDetailView(generic.DetailView):
model = DesignerReport
@spookylukey
spookylukey / stackoverflow_docs_no_thanks.rst
Last active January 31, 2017 12:31
Stackoverflow docs no thanks (at least for Django)

I'm talking about this: http://stackoverflow.com/documentation/django

Compared to well written documentation, this kind of documentation is pretty bad. Yes, many projects have terrible documentation, and for them it might be an improvement, but for many that have good docs, this is a major step backwards.

Good documentation requires serious effort and organisation. It can't be achieved by throwing together lots of examples.

@spookylukey
spookylukey / two_forms_one_view.txt
Created June 29, 2016 10:49
Two forms one view
# Views
def two_form_view(request):
context = {}
if request.method == "POST":
question_form = QuestionForm(request.POST)
answer_form = AnswerForm(request.POST)
success = False
if 'q_button' in request.POST and question_form.is_valid()
question_form.save()
@spookylukey
spookylukey / gist:fc9fa268de67fd19a567
Last active August 29, 2015 14:22
How to post code snippets (especially Python) into comment systems that don't preserve whitespace
#!/usr/bin/env python
# How to post code snippets (especially Python) into comment systems
# that don't preserve whitespace.
# Save this as a script called 'convert_leading_spaces_to_nonbreaking'
# in your PATH and do "chmod +x" on it.
#
# In Linux, you can then use it by installing xsel and doing:
#
@spookylukey
spookylukey / gist:36acb563acd54fc3ddf0
Created December 11, 2014 15:44
Bug with Django migrations - migrations created with Python 2 need 'fixing' when run with Python 3
$ mkdir bugtest
$ cd bugtest
$ virtualenv --python=python2.7 env_p27
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in env_p27/bin/python2.7
Also creating executable in env_p27/bin/python
Please make sure you remove any previous custom paths from your /home/luke/.pydistutils.cfg file.
Installing setuptools, pip...done.
$ source env_p27/bin/activate
Firefox dev console:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://avosapi.delicious.com/api/v1/account/public/profile/spookylukey?_=1413809903947. This can be fixed by moving the resource to the same domain or enabling CORS.
Chrome dev console:
XMLHttpRequest cannot load https://avosapi.delicious.com/api/v1/account/public/profile/spookylukey?_=1413809942129. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://delicious.com' is therefore not allowed access. The response had HTTP status code 503.
@spookylukey
spookylukey / gist:eb6029bc48ab8410227b
Created May 28, 2014 12:52
How not to do currency exchange...
def exchange(self, price):
#default = Currency.objects.get(iso4217=DEFAULT_CURRENCY)
try:
rate = self.exchangerate_set.all()[0]
return Decimal(str(float(rate.denominator) * float(price)))
except IndexError:
return price
@spookylukey
spookylukey / gist:589f9d3f05be56351fec
Created May 28, 2014 12:51
How not to do currncy exchange...
def exchange(self, price):
#default = Currency.objects.get(iso4217=DEFAULT_CURRENCY)
try:
rate = self.exchangerate_set.all()[0]
return Decimal(str(float(rate.denominator) * float(price)))
except IndexError:
return price