Skip to content

Instantly share code, notes, and snippets.

@lambdamusic
lambdamusic / parseoptions.py
Last active Dec 12, 2015
Python: Shell Script With Options
View parseoptions.py
import sys
import time
import math
import optparse
__version__ = "0.1"
__copyright__ = "CopyRight (C) 2013 by Michele Pasin"
__license__ = "MIT"
@lambdamusic
lambdamusic / Snipplr-34616.py
Created Feb 7, 2013
Python: 'Dictionary Comprehensions'
View Snipplr-34616.py
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:
@lambdamusic
lambdamusic / Snipplr-54284.py
Created Feb 7, 2013
Django: Adding custom django package to WSGI settings
View Snipplr-54284.py
# in your project.wsgi file
import os, sys
#add the desired django version at the beginning of the path; mind you must provide the Django folder as downloaded!
sys.path.insert(1, "/path/to/Django/")
sys.path.append('/my/project/root/') # add things as usual...
# the rest is the normal wsgi stuff...
@lambdamusic
lambdamusic / Snipplr-60684.py
Created Feb 7, 2013
Django: Adding request object info to a template
View Snipplr-60684.py
## 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",
)
@lambdamusic
lambdamusic / Snipplr-26506.txt
Created Feb 7, 2013
Altering Database Tables: Add More Columns :
View Snipplr-26506.txt
mysql> ALTER TABLE Employee ADD (EMail VARCHAR(25), ICQ VARCHAR(15));
@lambdamusic
lambdamusic / Snipplr-49167.py
Created Feb 7, 2013
Python: Best way to choose a random file from a directory
View Snipplr-49167.py
import os, random
random.choice(os.listdir("C:\\")) #change dir name to whatever
@lambdamusic
lambdamusic / Snipplr-25234.py
Created Feb 7, 2013
Python: Break a list into n sublists
View Snipplr-25234.py
mylist = range(150)
nestedlist = [mylist[start:start + 20] for start in range(0, len(mylist), 20)]
@lambdamusic
lambdamusic / Snipplr-40794.js
Created Feb 7, 2013
JavaScript: Case insensitive jQuery :contains selector
View Snipplr-40794.js
// 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
@lambdamusic
lambdamusic / Catching_exceptions.py
Last active Dec 12, 2015
Python: Catching and throwing exceptions
View Catching_exceptions.py
# --- catch:
try:
generateSomeException()
except Exception, e: # Mother of all exceptions
print e
# --- throw:
try:
@lambdamusic
lambdamusic / Snipplr-27677.scm
Created Feb 7, 2013
Scheme: Change list in place : scheme/impromtu
View Snipplr-27677.scm
(define-macro (change-list lst)
`(set! ,lst (map (lambda (i) (+ i 1)) ,lst)))
(define mylist '(1 2 3 4 5))
(change-list mylist)