Skip to content

Instantly share code, notes, and snippets.

View nitely's full-sized avatar
🟢
online

Esteban C Borsani nitely

🟢
online
View GitHub Profile
@nitely
nitely / utils
Created October 10, 2012 00:54
Python, subprocess: hide console on Windows
import subprocess
IS_WIN32 = 'win32' in str(sys.platform).lower()
def subprocess_call(*args, **kwargs):
#also works for Popen. It creates a new *hidden* window, so it will work in frozen apps (.exe).
if IS_WIN32:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW
@nitely
nitely / idle_queue.py
Created October 15, 2012 22:19
Pyqt / Pyside: thread-safe global queue + main loop integration
import Queue
#Global queue, import this from anywhere, you will get the same object.
#real life example: https://github.com/nitely/ochDownloader/blob/master/core/idle_queue.py
idle_loop = Queue.Queue()
@nitely
nitely / weak_ref.py
Created October 22, 2012 15:18
Python, Bound Method Weakref workaround
import weakref
class _BoundMethodWeakref:
def __init__(self, func):
self.func_name = func.__name__
self.wref = weakref.ref(func.__self__) #__self__ returns the class http://docs.python.org/reference/datamodel.html
def __call__(self):
func_cls = self.wref()
@nitely
nitely / event.py
Created October 29, 2012 12:26
Python, A event/signal dispatcher
import threading
#import logging
#logger = logging.getLogger(__name__)
import idle_queue
from weak_ref import weak_ref
class Event:
def __init__(self, name):
@nitely
nitely / single_instance.py
Created February 10, 2013 15:31
Python application cross-platform single instance.
import sys
import os
try:
import fcntl
except ImportError:
fcntl = None
LOCK_PATH = os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), "lock")
OS_WIN = False
@nitely
nitely / decorators.py
Created March 20, 2013 04:20
Django, django-ratelimit on login admin view
#-*- coding: utf-8 -*-
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.shortcuts import redirect
# https://github.com/jsocol/django-ratelimit
from ratelimit.decorators import ratelimit
@nitely
nitely / token_bucket.py
Created September 15, 2013 04:30
Python 3 Token bucket
#! /usr/bin/python3
from time import time
from threading import Lock
class TokenBucket:
"""
An implementation of the token bucket algorithm.
"""
@nitely
nitely / forms.py
Created January 21, 2014 05:36
ModelChoiceField with choice groups for recursive relationships
#-*- coding: utf-8 -*-
"""
A subclass of ModelChoiceField which represents the tree level of each node when generating option labels. It's limited to one level of nesting, if you need more, you should consider the django-mptt package.
For example, where a form which used a ModelChoiceField:
category = ModelChoiceField(queryset=Category.objects.all())
...would result in a select with the following options:
@nitely
nitely / admin.py
Created February 5, 2014 02:30
Group permissions selection in admin filtered by your app models
"""
Sometimes you just don't want to display every permission django has,
you just want a short list showing the permissions for some of your apps (or even django core apps).
GROUP_PERMISSIONS_MODELS must be a list of your app models.
Dotted path (in lowercase) required, app name + model class name.
tested on Django 1.6 (should work on +1.3)
"""
@nitely
nitely / emoji.py
Created February 5, 2014 20:28
Emojis for python-Markdown
#-*- coding: utf-8 -*-
# Tested on Markdown 2.3.1
#
# Copyright (c) 2014, Esteban Castro Borsani
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights