Skip to content

Instantly share code, notes, and snippets.

@lambdamusic
lambdamusic / parseoptions.py
Last active December 12, 2015 06:39
Python: Shell Script With Options
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 February 7, 2013 21:24
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:
@lambdamusic
lambdamusic / Snipplr-54284.py
Created February 7, 2013 21:24
Django: Adding custom django package to WSGI settings
# 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 February 7, 2013 21:24
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",
)
@lambdamusic
lambdamusic / Snipplr-26506.txt
Created February 7, 2013 21:24
Altering Database Tables: Add More Columns :
mysql> ALTER TABLE Employee ADD (EMail VARCHAR(25), ICQ VARCHAR(15));
@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-25234.py
Created February 7, 2013 21:24
Python: Break a list into n sublists
mylist = range(150)
nestedlist = [mylist[start:start + 20] for start in range(0, len(mylist), 20)]
@lambdamusic
lambdamusic / Snipplr-40794.js
Last active April 4, 2024 22:05
JavaScript: Case insensitive jQuery :contains selector #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 December 12, 2015 07:08
Python: Catching and throwing exceptions
# --- catch:
try:
generateSomeException()
except Exception, e: # Mother of all exceptions
print e
# --- throw:
try:
@lambdamusic
lambdamusic / Snipplr-27677.scm
Created February 7, 2013 21:24
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)