Skip to content

Instantly share code, notes, and snippets.

View Xion's full-sized avatar

Karol Kuczmarski Xion

View GitHub Profile
@Xion
Xion / listshellaliases.py
Created December 24, 2013 16:53
Mocking "the filesystem" by stubbing built-in open() in Python
"""
Example function that reads a file and does something with it.
"""
import re
import sys
def list_shell_aliases(script):
"""Find all command aliases defined in a shell script.
Aliases are created though the ``alias`` command::
@Xion
Xion / setup_gae_virtualenv.sh
Created October 18, 2013 20:20
Setup virtualenv for a Google App Engine project
#!/bin/sh
# Script for setting up virtualenv to work correctly with Google App Engine projects
# @author Karol Kuczmarski "Xion"
DEFAULT_APPENGINE_SDK_PATH="/opt/google_appengine"
DEFAULT_PROJECT_LIB_PATH="./lib"
from collections import Mapping
import unittest
import warnings
from flask import get_template_attribute
from myapplication import app
class JinjaTestCase(unittest.TestCase):
@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."""
@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 / 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 / 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 / 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 / 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 / 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