This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MiniMock(object): | |
""" | |
A callable object that will always return a value for any | |
attribute/index/slice/call accessed. The value returned is a new | |
MiniMock instance allowing for successful chained access. | |
""" | |
def __getitem__(self, value): | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A one-liner for calculating the cost of a project using a Mercurial repo. | |
# Replace: | |
# ignore|paths - pipe separated strings in paths to ignore | |
# eg: embedded libraries and generated code. | |
# mins_per_loc - estimated minutes per line of code. | |
# hourly_rate - hourly dollar rate being charged. | |
$ hg log --stat | egrep -v 'ignore|paths' | grep '|' | awk '{s+=$3} END {print "$" s / 60 * mins_per_loc * hourly_rate}' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo(object): | |
def __getattribute__(self, name): | |
attr = object.__getattribute__(self, name) | |
if callable(attr): | |
def wrapper(*args, **kwargs): | |
try: | |
return attr(*args, **kwargs) | |
except Exception, e: | |
print "%s failed with exc %s" % (name, e) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sort_by_keylist(objects, attr, keylist): | |
indexes = dict([(x, i) for i, x in enumerate(keylist)]) | |
return sorted(objects, key=lambda x: indexes.get(getattr(x, attr))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TagCloser(HTMLParser): | |
""" | |
HTMLParser that closes open tags. Call close_tags with a HTML | |
string arg which returns it with any required closing tags | |
appended. | |
""" | |
def __init__(self): | |
HTMLParser.__init__(self) | |
self.tags = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
cwd = os.path.abspath(os.getcwd()) | |
for project in os.listdir("."): | |
path = os.path.join(cwd, project) | |
if not os.path.isdir(path): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
The Django test client implements the session API but doesn't persist values in it: | |
https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.client.Client.session | |
This Client subclass can be used to maintain a persistent session during test cases. | |
""" | |
from django.conf import settings | |
from django.test import TestCase |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import namedtuple | |
from django.test import TestCase | |
from requests.auth import HTTPBasicAuth | |
import slumber | |
class SlumberTestClientAPI(slumber.API): | |
""" | |
Subclass of ``slumber.API`` that patches ``requests.request`` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
XSS privilege escalation by malicious non-superuser admin users. | |
Fixed in Mezzanine 1.0.9: | |
https://bitbucket.org/stephenmcd/mezzanine/changeset/40cbc47b8d8a | |
If an admin user was to create their own POST submit to any forms with a | |
RichTextField, they could include JavaScript that does the following: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.template.loader_tags import ExtendsNode | |
def get_extends_parent(self, context): | |
""" | |
Patched onto Django's ``ExtendsNode.get_parent`` which is | |
responsible for loading the template to extend from with | |
the ``extends`` template tag. | |
This patch allows the template foo/bar.html to extend | |
foo/bar.html, given that there is another version of it that |