Skip to content

Instantly share code, notes, and snippets.

View lambdaman2's full-sized avatar

Lambdaman lambdaman2

View GitHub Profile
@lambdaman2
lambdaman2 / Snipplr-34616.py
Created October 1, 2012 12:44
Python: 'Dictionary Comprehensions'
emails = {'Dick': 'bob@example.com', 'Jane': 'jane@example.com', 'Stou': 'stou@example.net'}
email_at_dotcom = dict( [name, '.com' in email] for name, email in emails.iteritems() )
# email_at_dotcom now is {'Dick': True, 'Jane': True, 'Stou': False}
# ANOTHER OPTION TO CREATE DICTS ON THE FLY:
@lambdaman2
lambdaman2 / Snipplr-56858.py
Created October 1, 2012 12:45
Python: Colorizing Python Source Using the Built-in Tokenizer
""" MoinMoin - Python Source Parser """
import cgi, sys, cStringIO
import keyword, token, tokenize
# Python Source Parser (does highlighting into HTML)
_KEYWORD = token.NT_OFFSET + 1
_TEXT = token.NT_OFFSET + 2
_colors = {
token.NUMBER: '#0080C0',
token.OP: '#0000C0',
token.STRING: '#004080',
@lambdaman2
lambdaman2 / Snipplr-62190.py
Created October 1, 2012 12:45
Django: Clean up expired django.contrib.session\'s in a huge MySQL InnoDB table
DROP TABLE IF EXISTS `django_session_cleaned`;
CREATE TABLE `django_session_cleaned` (
`session_key` varchar(40) NOT NULL,
`session_data` longtext NOT NULL,
`expire_date` datetime NOT NULL,
PRIMARY KEY (`session_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `django_session_cleaned` SELECT * FROM `django_session` WHERE `expire_date` > CURRENT_TIMESTAMP;
@lambdaman2
lambdaman2 / Snipplr-60684.py
Created October 1, 2012 12:44
Django: Adding request object info to a template
## settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request",
)
@lambdaman2
lambdaman2 / Snipplr-29583.py
Created October 1, 2012 12:45
Python: consume xml from RestFul webservices
# Try to use the C implementation first, falling back to python
try:
from xml.etree import cElementTree as ElementTree
except ImportError, e:
from xml.etree import ElementTree
##########
def find(*args, **kwargs):
"""Find a book in the collection specified"""
@lambdaman2
lambdaman2 / Snipplr-25271.js
Created October 1, 2012 12:44
JavaScript: Check if array is empty in javascript
if (testarray.length <1) alert("array is empty");
//or
if(A && A.length==0)
//or if you have other objects that A may be:
if(A && A.constructor==Array && A.length==0)
@lambdaman2
lambdaman2 / Snipplr-25234.py
Created October 1, 2012 12:44
Python: Break a list into n sublists
mylist = range(150)
nestedlist = [mylist[start:start + 20] for start in range(0, len(mylist), 20)]
@lambdaman2
lambdaman2 / Snipplr-40794.js
Created October 1, 2012 12:44
JavaScript: Case insensitive jQuery :contains selector
// For jQuery 1.2
jQuery.extend(
jQuery.expr[':'], {
Contains : "jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0"
});
// For jQuery 1.3 (thanks @user95227) and later you need
@lambdaman2
lambdaman2 / Snipplr-27677.scm
Created October 1, 2012 12:44
Scheme: Change list in place : scheme/impromtu
(define-macro (change-list lst)
`(set! ,lst (map (lambda (i) (+ i 1)) ,lst)))
(define mylist '(1 2 3 4 5))
(change-list mylist)
@lambdaman2
lambdaman2 / Snipplr-29052.js
Created October 1, 2012 12:44
JavaScript: check url on delicious
javascript:location.href="http://delicious.com/url/view?url="%20+%20location.href;