Skip to content

Instantly share code, notes, and snippets.

@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-50966.py
Created February 7, 2013 21:25
Python: CSV reading in python
# Use reader() to create a an object for reading data from a CSV file. The reader can be used as an iterator
# to process the rows of the file in order. For example:
import csv
import sys
f = open(sys.argv[1], 'rt')
try:
reader = csv.reader(f)
for row in reader:
@lambdamusic
lambdamusic / Snipplr-25243.py
Created February 7, 2013 21:25
Django: Django: access the attributes of a model dynamically
def attrs_verbose(self):
model = self.__class__
# using this form: Record._meta.get_field('created_by').verbose_name
items = []
for k, v in self.__dict__.items():
try:
x = model._meta.get_field(k).verbose_name
except:
x = k
items += [(x, v)]
@lambdamusic
lambdamusic / Snipplr-25238.py
Created February 7, 2013 21:25
Django: Django: adding new admin_tags
# FIRST:
from django import template
from poms.pomsapp import models
register = template.Library()
# for People template
@register.inclusion_tag('admin/snippets/personfactoid_info.html')
@lambdamusic
lambdamusic / Snipplr-55134.py
Created February 7, 2013 21:25
Python: Extracting a URL in Python
import re
myString = "This is my tweet check it out http://tinyurl.com/blah"
print re.search("(?P<url>https?://[^\s]+)", myString).group("url")
@lambdamusic
lambdamusic / Snipplr-51138.py
Created February 7, 2013 21:25
Python: Getting the difference between two lists - Python
>>> lst1 = [1,2,3,4,5,6,7,8,9,0,11,12,13]
>>> lst2 = [5,6,7,12,45,67,89,99]
>>> [i for i in lst1+lst2 if i not in lst1 or i not in lst2]
[1234890,&nbsp;111345678999]
>>> 
@lambdamusic
lambdamusic / Snipplr-28212.txt
Created February 7, 2013 21:26
How do I launch a URL using Cocoa (Objective C) - macosx.com
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.ithinksw.com/"]];
@lambdamusic
lambdamusic / Snipplr-35729.txt
Created February 7, 2013 21:26
How to Convert Degrees to Radians, Radians to Degrees
CGFloat DegreesToRadians(CGFloat degrees)
{
return degrees * M_PI / 180;
};
CGFloat RadiansToDegrees(CGFloat radians)
{
return radians * 180 / M_PI;
};
@lambdamusic
lambdamusic / Snipplr-28525.py
Created February 7, 2013 21:26
Python: joning lists
return ";".join(["%s=%s" % (k, v) for k, v in params.items()])
# or
x = range(100)
print ";".join(["%s"%(f) for f in x])
@lambdamusic
lambdamusic / Snipplr-25269.py
Created February 7, 2013 21:26
Python: Iterating over multiple sequences at the same time
for i, j, q in [1, 2, 3], [4, 5, 6], [7, 8, 9]:
print 'i =', i
print 'j = ', j
print 'q =', q