PEP: 538
Title: Coercing the legacy C locale to a UTF-8 based locale
Version:
This file contains 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
# Example based on http://lukeplant.me.uk/blog/posts/djangos-cbvs-were-a-mistake/ | |
# Not tested, just designed to give a general idea of the games CBVs let you play | |
from django.core.urlresolvers import reverse_lazy | |
from django.views.generic.edit import ProcessFormView | |
# Utilities for interacting with a user | |
class UserViewMixin(object): | |
_high_priority_user = None | |
@property |
This file contains 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
$ python | |
Python 2.6.6 (r266:84292, May 1 2012, 13:52:17) | |
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> exit | |
Use exit() or Ctrl-D (i.e. EOF) to exit | |
>>> quit | |
Use quit() or Ctrl-D (i.e. EOF) to exit | |
>>> exit() | |
$ python |
This file contains 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 concurrent.futures import Future | |
# This first part is a helper function to wait for the first Future in a group to complete | |
def _wait_first(futures): | |
# futures must be a set as items will be removed as they complete | |
# we create a signalling future to return to our caller. We will copy | |
# the result of the first future to complete to this signalling future | |
signal = Future() | |
signal.completed = None | |
def copy_result(completed): |
This file contains 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
Github won't let me comment on https://github.com/pculture/unisubs/commit/e41f74fa5a17573b08001cc1431b615abca930ba | |
so just saving this here for now. | |
unisubs is hitting the double import problem that is caused by Django's old default project layout and management script, which creates a broken Python import configuration. Django's default behaviour has been fixed (https://docs.djangoproject.com/en/dev/releases/1.4/#updated-default-project-layout-and-manage-py) but projects using the old layout may still see the issue. | |
The only reliable solution is to fix the layout to avoid the broken configuration that allows the same module to be imported under two different names. The simplest way to do this is to: | |
1. Remove any sys.path manipulation from manage.py | |
2. Ensure that manage.py is *not* inside a Python package (there should *not* be an adjacent __init__.py file) | |
3. Adjust imports as needed for any changed made in steps 1 and 2 |
This file contains 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 MixedTypeKey: | |
"""Sort key for mixed type lists in Python 3 | |
>>> sorted([None, ..., 1, 1j, "", (), {}, []]) | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
TypeError: unorderable types: ellipsis() < NoneType() | |
>>> sorted([None, ..., 1, 1j, "", (), {}, []], key=MixedTypeKey) | |
[None, Ellipsis, 1, 1j, '', (), {}, []] | |
""" |
This file contains 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 Madness(dict): | |
... def __setitem__(self, attr, value): | |
... if isinstance(value, type): | |
... value.__name__ = attr | |
... dict.__setitem__(self, attr, value) | |
... | |
>>> code = """\ | |
... class Example(object): pass | |
... NewName = Example | |
... print(Example.__name__) |
This file contains 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
# Using even one additional keyword arg forces CPython to make an additional copy of the entire original keyword dictionary | |
$ python3 -m timeit -s "kwds = {str(k):v for k, v in enumerate(range(1000))}" -s "def f(**kwargs): return kwargs" "f(**kwds)" | |
10000 loops, best of 3: 50.5 usec per loop | |
$ python3 -m timeit -s "kwds = {str(k):v for k, v in enumerate(range(1000))}" -s "def f(**kwargs): return kwargs" "f(other=1, **kwds)" | |
10000 loops, best of 3: 82.4 usec per loop | |
# Using a non-dict subclass forces CPython to copy it to a real dictionary as part of the call | |
$ python3 -m timeit -s "import collections; kwds = collections.UserDict((str(k), v) for k, v in enumerate(range(1000)))" -s "def f(**kwargs): return kwargs" "f(**kwds)" | |
1000 loops, best of 3: 573 usec per loop |
This file contains 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
# Prompted by this article about implementing "Round half away from zero" in Go | |
# https://www.cockroachlabs.com/blog/rounding-implementations-in-go/ | |
# Python uses round-half-even by default to reduce statistical bias, | |
# but round-half-away-from-zero can be implemented more efficiently | |
# (since it doesn't need to examine all the bits of the number) | |
# I figured the folks doing computational analysis in Python would | |
# have made sure this was handled correctly years ago, but it never | |
# hurts to check that kind of assumption :) |
This file contains 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
Copyright 2017 Nicholas Coghlan. All rights reserved. | |
Redistribution and use in source and binary forms, with or without modification, are | |
permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this list of | |
conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, this list | |
of conditions and the following disclaimer in the documentation and/or other materials |
OlderNewer