Skip to content

Instantly share code, notes, and snippets.

View mitchellrj's full-sized avatar

Richard Mitchell mitchellrj

View GitHub Profile
@mitchellrj
mitchellrj / unicode_tools.py
Created February 21, 2012 15:58
How to beat unicode
# http://pypi.python.org/pypi/chardet
import chardet
# List of your preferred charsets, or detect those available automatically (see below)
CHARSETS = ['ascii', 'latin_1', 'utf_8']
def get_charset(string):
"""Given a string or unicode object, returns the simplest available
@mitchellrj
mitchellrj / get_teaser.py
Created February 20, 2012 15:42 — forked from enko/teaser.py
teaser behavior
## Script (Python) "get_teaser"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##
# Put this file in a skins directory, call it by doing context/get_teaser or context.get_teaser()
@mitchellrj
mitchellrj / buildout.cfg
Created January 31, 2012 10:39
Patch PasteScript to support safe termination of daemon threads
[buildout]
eggs = ...
parts = ...
pastescript
[pastescript]
recipe = zc.recipe.egg
eggs = PasteScript # required to generate the specific paster script if not already in eggs list
${buildout:eggs}
extra-paths = path/to/pastescriptpatches.py
@mitchellrj
mitchellrj / ie.py
Created January 24, 2012 14:58
Internet Explorer detection
def is_ie(request, min_version=None, max_version=None):
user_agent = request.environ.get('HTTP_USER_AGENT', '')
ie = user_agent.find('MSIE')
if ie==-1:
return False
if not min_version and not max_version:
return True
version_match = re.match('[\d\.]*', user_agent[ie+5:])
@mitchellrj
mitchellrj / link-remap.sh
Created December 19, 2011 14:09
Remap links
@mitchellrj
mitchellrj / configure.zcml
Created November 25, 2011 14:58
z3c.form data manager for persistent dictionaries
<configure
xmlns="http://namespaces.zope.org/zope">
<adapter
factory=".datamanager.PersistentDictionaryField" />
</configure>
@mitchellrj
mitchellrj / debug_login.py
Created November 15, 2011 14:59
Plone debug mode login
from AccessControl.SecurityManagement import newSecurityManager
from Testing.makerequest import makerequest
from zope.component.hooks import setSite
def get_manager(app):
for uid in app.acl_users.users.listUserIds():
user = app.acl_users.users.getUser(uid)
if 'Manager' in user.getRoles():
return user
@mitchellrj
mitchellrj / tree.html
Created October 28, 2011 00:09
HTML & CSS vertical tree layout
<html>
<head>
<title>HTML &amp; CSS tree</title>
<!-- tree -->
<style type="text/css">
ul.tree {
overflow-x: auto;
white-space: nowrap;
}
@mitchellrj
mitchellrj / flatten.py
Created October 12, 2011 14:44
Flatten in Python
def flatten(iterable):
if not isinstance(iterable, (list, tuple)):
yield iterable
else:
for i in iterable:
if isinstance(i, (list, tuple)):
for j in flatten(i):
yield j
else:
yield i
@mitchellrj
mitchellrj / wrap.py
Created September 26, 2011 14:15
Wrap text in Python
def wrap_text(text, limit):
#s = map(str.split, text.split('\n'))
#i = 0
#for n in range(len(s)):
# while i < len(s[n]) - 1:
# if (len(s[n][i]) + len(s[n][i+1])) <= limit:
# s[n][i] += ' ' + s[n][i+1]
# s[n] = s[n][:i+1] + s[n][i+2:]
# elif len(s[n][i+1]) > limit:
# i += 1