Skip to content

Instantly share code, notes, and snippets.

@lambdamusic
lambdamusic / Snipplr-25273.txt
Created February 7, 2013 21:26
JQuery: package stuff into a plugin
jQuery('div#message').addClass(
'borderfade'
).animate({
'borderWidth': '+10px'
}, 1000).fadeOut();
// Can be 'packaged' as:
jQuery.fn.dumbBorderFade = function() {
@lambdamusic
lambdamusic / Snipplr-49167.py
Created February 7, 2013 21:24
Python: Best way to choose a random file from a directory
import os, random
random.choice(os.listdir("C:\\")) #change dir name to whatever
@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-34614.py
Created February 7, 2013 21:25
Python: Creating Dictionaries
dict(a=1, b=2, c=3)
returns {'a': 1, 'b': 2, 'c': 3}
@lambdamusic
lambdamusic / Snipplr-34617.py
Created February 7, 2013 21:25
Python: dict.items vs dict.iteritems
# Both seem to work for something like:
mydict = {'a' : 1, 'b' : 2}
for key,val in mydict.items():
print key,val
# ==> a 1 b 2
for key,val in mydict.iteritems():
@lambdamusic
lambdamusic / Snipplr-40878.py
Created February 7, 2013 21:25
Python: File overwrite and append in python
#!/usr/local/bin/python
import sys
import time
havevalue = 0
while havevalue < 1:
        try:
                table = input("What table to you want? ")
@lambdamusic
lambdamusic / Snipplr-41106.py
Created February 7, 2013 21:25
Python: Global variables in Python
globvar = 0
def set_globvar_to_one():
global globvar # Needed to modify global copy of globvar
globvar = 1
def print_globvar():
print globvar # No need for global declaration to read value of globvar
set_globvar_to_one()
@lambdamusic
lambdamusic / Snipplr-57154.py
Created February 7, 2013 21:26
Python: Indenting source code in python
#! /usr/bin/env python
# Released to the public domain, by Tim Peters, 03 October 2000.
"""reindent [-d][-r][-v] [ path ... ]
-d (--dryrun) Dry run. Analyze, but don't make any changes to, files.
-r (--recurse) Recurse. Search for all .py files in subdirectories too.
-n (--nobackup) No backup. Does not make a ".bak" file before reindenting.
-v (--verbose) Verbose. Print informative msgs; else no output.
@lambdamusic
lambdamusic / Snipplr-25257.py
Created February 7, 2013 21:27
Python: Python: lambda in a dictionary
// add a function as an element of a dictionary
>>> {1: lambda: len('ciao')}
>>> {1: <function <lambda> at 0x101d530>}
// get element with specified key
>>> {1: lambda: len('ciao')}[1]
>>> <function <lambda> at 0x101d5f0>
>>> a = {1: lambda: len('ciao')}[1]
>>> a
>>> <function <lambda> at 0x101d670>