Skip to content

Instantly share code, notes, and snippets.

@lambdamusic
lambdamusic / Snipplr-39372.py
Created February 7, 2013 21:24
Python: changing default version when updating Python on Mac
defaults write com.apple.versioner.python Version 2.6 # or 2.5, 2.4 ..
@lambdamusic
lambdamusic / Snipplr-25271.js
Last active April 4, 2024 22:05
JavaScript: Check if array is empty in javascript #js
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)
@lambdamusic
lambdamusic / Snipplr-44970.py
Created February 7, 2013 21:24
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
@lambdamusic
lambdamusic / Snipplr-62190.py
Created February 7, 2013 21:24
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;
@lambdamusic
lambdamusic / Snipplr-56858.py
Created February 7, 2013 21:24
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',
@lambdamusic
lambdamusic / Snipplr-25265.bash
Created February 7, 2013 21:25
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.
@lambdamusic
lambdamusic / Snipplr-29583.py
Created February 7, 2013 21:25
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"""
@lambdamusic
lambdamusic / Snipplr-56927.py
Created February 7, 2013 21:25
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']))
@lambdamusic
lambdamusic / Snipplr-37719.py
Created February 7, 2013 21:25
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.'''
@lambdamusic
lambdamusic / Snipplr-55137.py
Created February 7, 2013 21:25
Python: Counting elements in a list
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> word_list = ['Jellicle', 'Cats', 'are', 'black', 'and', 'white,', 'Jellicle', 'Cats', 'are', 'rather', 'small;', 'Jellicle', 'Cats', 'are', 'merry', 'and', 'bright,', 'And', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.', 'Jellicle', 'Cats', 'have', 'cheerful', 'faces,', 'Jellicle', 'Cats', 'have', 'bright', 'black', 'eyes;', 'They', 'like', 'to', 'practise', 'their', 'airs', 'and', 'graces', 'And', 'wait', 'for', 'the', 'Jellicle', 'Moon', 'to', 'rise.', '']
>>> word_counter = {}
>>> for word in word_list:
... if word in word_counter:
... word_counter[word] += 1
... else:
... word_counter[word] = 1