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
def objectproperty(func): | |
"""Alternate version of the standard ``@property`` decorator, | |
useful for proeperties that expose setter (or deleter) in addition to getter. | |
It allows to contain all two/three functions and prevent PEP8 warnings | |
about redefinion of ``x`` when using ``@x.setter`` or ``@x.deleter``. | |
Usage:: |
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
#:kivy 1.4.1 | |
<Ball>: | |
size: 25, 25 | |
canvas: | |
Color: | |
rgb: 1, 0, 1 | |
Ellipse: | |
pos: self.pos | |
size: self.size |
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
import random | |
def scramble(text): | |
"""Scrambles the letters in given text, except for first | |
and last one in every word. | |
""" | |
words = text.split() | |
for i in xrange(words): | |
if len(words[i]) < 3: | |
continue |
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
#!/usr/bin/env python | |
import curses | |
SHIFTS = { | |
curses.KEY_LEFT: (0, -1), | |
curses.KEY_UP: (-1, 0), | |
curses.KEY_RIGHT: (0, 1), | |
curses.KEY_DOWN: (1, 0), |
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
""" | |
Analyzing keywords from different programming languages. | |
""" | |
def word_stats(words): | |
count = len(words) | |
len_sum = sum(map(len, words)) | |
return { | |
'count': count, | |
'total_chars': len_sum, |
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
""" | |
Import hook extending pytz package with usable, generic timezones: | |
GMT-14 up to GMT+12. | |
Note that pytz already has Etc/GMT+X timezones, but | |
(quoting Wikipedia): | |
"In order to conform with the POSIX style, those zones beginning with "Etc/GMT" | |
have their sign reversed from what most people expect. In this style, | |
zones west of GMT have a positive sign and those east have a negative sign." |
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
import collections | |
missing = object() | |
class MissingDict(dict): | |
"""Dictionary that supports a special 'missing' value. | |
Assigning a missing value to key will remove the key from dictionary. | |
Initializing a key with missing value will result in key not being | |
added to dictionary at all. |
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
# Monadic IO | |
def print_(s): | |
def _(): | |
print s | |
return _ | |
def some_io(): | |
for x in ['Hello', 'World']: | |
yield print_(x) |
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
This puzzle is (allegedly) solved by elementary school children in 5 to 10 minutes, | |
but adults usually struggle with it much longer. | |
8809=6 | |
7111=0 | |
2172=0 | |
6666=4 | |
1111=0 | |
3213=0 | |
7662=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
from jinja2.ext import Extension | |
from jinja2.nodes import CallBlock, Output, MarkSafe | |
class CaptureExtension(Extension): | |
''' Generic capture extension, useful for providing | |
content that should go into specific places in HTML template, | |
such as the <head> tag for JavaScript includes. | |
Syntax for providing content for so-called 'dataset' (here it's "js"): | |