Skip to content

Instantly share code, notes, and snippets.

View Xion's full-sized avatar

Karol Kuczmarski Xion

View GitHub Profile
@Xion
Xion / scramble.py
Last active October 11, 2015 00:18
Scrambling word letters in legible way
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
@Xion
Xion / balls.kv
Created October 21, 2012 21:29
Bouncing balls experiment in Kivy
#:kivy 1.4.1
<Ball>:
size: 25, 25
canvas:
Color:
rgb: 1, 0, 1
Ellipse:
pos: self.pos
size: self.size
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::
@Xion
Xion / l.py
Last active December 22, 2021 23:45
Chained list operations in Python
class L(object):
def __init__(self, iterable):
self.value = iterable
def map(self, fn):
self.value = map(fn, self.value)
return self
def join(self, s):
self.value = s.join(self.value)
return self
@Xion
Xion / url_for_ex.py
Last active December 14, 2015 18:39
Enhance Flask url_for() with optional _strict argument
import re
from flask import url_for
from myflaskapp import app
def url_for_ex(endpoint, **values):
"""Improved version of standard Flask's :func:`url_for`
that accepts an additional, optional ``_strict`` argument.
:param _strict: If ``False``, values for the endpoint are not checked
@Xion
Xion / split.py
Last active December 22, 2021 23:45
split() for general iterables
import inspect
def split(iterable, by):
"""Generalized split function that works on any iterable."""
separator = by if inspect.isfunction(by) else lambda x: x == by
res = []
curr = []
for elem in iterable:
if separator(elem):
@Xion
Xion / english_plurals.py
Created March 31, 2013 19:29
English plurals experiment
#!/usr/bin/env python
"""
Experiment with automatic generation of English plurals.
"""
import os
import random
def main():
total_count = 0
@Xion
Xion / noop_import_hook.py
Last active December 22, 2021 23:45
"No-op" import hook
import imp
import inspect
import sys
__all__ = []
class PassthroughImporter(object):
"""Import hook that simulates the standard import flow
@Xion
Xion / deprecated.py
Created May 26, 2013 11:49
@deprecated decorator for Python classes
import functools
import inspect
import os
import warnings
class _DeprecatedDecorator(object):
MESSAGE = "%s is @deprecated"
def __call__(self, symbol):
@Xion
Xion / fluent.py
Created June 30, 2013 12:18
Fluent interface decorators
#!/usr/bin/env python
"""
Decorator for fluent interface classes.
"""
import functools
import inspect
def chained(method):
"""Method decorator to allow chaining."""