Skip to content

Instantly share code, notes, and snippets.

View lambdaman2's full-sized avatar

Lambdaman lambdaman2

View GitHub Profile
@lambdaman2
lambdaman2 / Snipplr-39372.py
Created October 1, 2012 12:44
Python: changing default version when updating Python on Mac
defaults write com.apple.versioner.python Version 2.6 # or 2.5, 2.4 ..
@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-44970.py
Created October 1, 2012 12:44
Python: Check time difference between now and specified time
from datetime import datetime, timedelta
now = datetime.now()
sometime_in_the_future = datetime(2020, 1, 1, now.hour, now.minute, now.second)
MAX_AGE = timedelta(hours=2, minutes=0)
if (now - sometime_in_the_future) > MAX_AGE:
return True
else:
return False
@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;
@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-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-25265.bash
Created October 1, 2012 12:45
Bash: Command line delete matching files and directories recursively
# This searches recursively for all directories (-type d) in the hierarchy starting at "." (current directory), and finds those whose name is '.svn'; the list of the found directories is then fed to rm -rf for removal.
find . -name '.svn' -type d | xargs rm -rf
# If you want to try it out, try
find . -name '.svn' -type d | xargs echo
# This should provide you with a list of all the directories which would be recursively deleted.
@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-56927.py
Created October 1, 2012 12:45
Django: Convert the time.struct_time object into a datetime.datetime object:
from time import mktime
from datetime import datetime
dt = datetime.fromtimestamp(mktime(item['updated_parsed']))
@lambdaman2
lambdaman2 / Snipplr-37719.py
Created October 1, 2012 12:45
Python: Count items and sort by incidence
## {{{ http://code.activestate.com/recipes/52231/ (r1)
class Counter:
def __init__(self):
self.dict = {}
def add(self,item):
count = self.dict.setdefault(item,0)
self.dict[item] = count + 1
def counts(self,desc=None):
'''Returns list of keys, sorted by values.
Feed a 1 if you want a descending sort.'''