Skip to content

Instantly share code, notes, and snippets.

View kissgyorgy's full-sized avatar

György Kiss kissgyorgy

View GitHub Profile
@kissgyorgy
kissgyorgy / closure_hack.py
Last active August 29, 2015 14:18
Python: Closure hack
# http://stackoverflow.com/a/23558809/720077
def fibonacci():
a, b = [1], [0]
def fibo():
a[0], b[0] = b[0], a[0] + b[0]
return a[0]
return fibo
@kissgyorgy
kissgyorgy / password_descriptor.py
Last active November 14, 2021 01:42
Python: Password descriptor
class Password(str):
def __eq__(self, other):
if not isinstance(other, self.__class__):
other = bcrypt.hashpw(other, self)
return str.__eq__(self, other)
def __ne__(self, other):
return not self.__eq__(other)
def __get__(self, obj, type=None):
@kissgyorgy
kissgyorgy / show_nice_flask_routes.py
Created February 8, 2015 00:39
Python: Show flask routes nicely aligned with rules, endpoints and methods
@main.command()
def routes():
from mainweb import app
sorted_rules = sorted(app.url_map.iter_rules(), key=lambda x: x.rule)
max_rule = max(len(rule.rule) for rule in sorted_rules)
max_ep = max(len(rule.endpoint) for rule in sorted_rules)
max_meth = max(len(', '.join(rule.methods)) for rule in sorted_rules)
@kissgyorgy
kissgyorgy / bool_param.py
Last active November 13, 2021 00:02
Python: decorator for converting parameters from possible yes/no values to bool
import inspect
import functools
from distutils.util import strtobool
def tobool(yesno):
"""Convert all kinds of possible "yes" values to True/False."""
return bool(strtobool(yesno))
@kissgyorgy
kissgyorgy / osx_commands.sh
Created November 14, 2014 10:12
OS X commands
sqlite3 ~/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV* 'select LSQuarantineDataURLString from LSQuarantineEvent'
"""
setup.py
py2app setup script for creating a semi-standalone .app
around a Maya API based PyQt4 application.
Only bundled PyQt4 with the app, and references the Maya install
location for the python environment.
Allows for a portable GUI application that does not require
@kissgyorgy
kissgyorgy / mainwindow.pyw
Created September 3, 2014 03:23
PySide: Load mainwindow from UI file.
import sys
from PySide.QtCore import QFile
from PySide.QtGui import QApplication, QMainWindow
from PySide.QtUiTools import QUiLoader
class MainWindow(QMainWindow):
def __new__(cls, *args, **kwargs):
"""Load widget from UI file."""
super().__new__(cls, *args, **kwargs)
@kissgyorgy
kissgyorgy / partition.py
Created March 24, 2014 16:32
Django: partition template tag
"""
https://djangosnippets.org/snippets/401/
Template filters to partition lists into rows or columns.
A common use-case is for splitting a list into a table with columns::
{% load partition %}
<table>
{% for row in mylist|columns:3 %}
@kissgyorgy
kissgyorgy / del_trailing_spaces.sh
Created February 20, 2014 01:25
Bash: delete trailing spaces
#!/bin/bash
# On OS X 10.9 (see: http://stackoverflow.com/questions/19242275/sed-re-error-illegal-byte-sequence-on-mac-os-x)
# export LC_CTYPE=C
# export LANG=C
# http://stackoverflow.com/questions/149057/how-to-remove-trailing-whitespace-of-all-files-recursively
find . -not \( -name .svn -prune -o -name .git -prune \) -type f -print0 | xargs -0 sed -i '' -E "s/[[:space:]]*$//"
@kissgyorgy
kissgyorgy / wrapper_arg_value.py
Created February 14, 2014 23:10
Python: get argument value in a decorator by name of the argument
import inspect
import functools
import Command
FORBIDDEN_SCHEMAS = {'public'}
def no_forbidden_schema(func):
"""Check if schema parameter is in FORBIDDEN_SCHEMAS."""
@functools.wraps(func)
def wrapper(self, *args, **kwargs):