Skip to content

Instantly share code, notes, and snippets.

Karol Kuczmarski Xion

Block or report user

Report or block Xion

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@Xion
Xion / sqla_enum.py
Created Oct 25, 2015
SQLAlchemy column type for storing Python enums
View sqla_enum.py
from enum import Enum
from inspect import isclass
import re
from sqlalchemy.types import TypeDecorator, TypeEngine
__all__ = ['EnumType']
View objectproperty.py
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 Dec 14, 2015
Chained list operations in Python
View l.py
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 Dec 14, 2015
Enhance Flask url_for() with optional _strict argument
View url_for_ex.py
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 Dec 14, 2015
split() for general iterables
View split.py
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 Mar 31, 2013
English plurals experiment
View english_plurals.py
#!/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 Dec 16, 2015
"No-op" import hook
View noop_import_hook.py
import imp
import inspect
import sys
__all__ = []
class PassthroughImporter(object):
"""Import hook that simulates the standard import flow
@Xion
Xion / missing_dict.py
Created Apr 5, 2012
dict with "missing" values
View missing_dict.py
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.
@Xion
Xion / listshellaliases.py
Created Dec 24, 2013
Mocking "the filesystem" by stubbing built-in open() in Python
View listshellaliases.py
"""
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 / debug_smtpd
Last active Jan 1, 2016
Debug SMTP server
View debug_smtpd
#!/bin/sh
# Start a debug SMTP server using Python's smtpd module
show() {
echo 1>&2 "$@"
}
port=${1-25}
smtpd_args="-c DebuggingServer localhost:$port"
You can’t perform that action at this time.