Skip to content

Instantly share code, notes, and snippets.

@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-59359.py
Created February 7, 2013 21:25
Django: Django: check whether an object already exists before adding
role, created = UserToUserRole.objects.get_or_create(
from_user=current_user, to_user=user, role='follow')
@lambdamusic
lambdamusic / Snipplr-27157.py
Created February 7, 2013 21:25
Django: Django: instantiating (simple) M2M models
>>> from mysite.models import Publication, Article
# Create a couple of Publications.
>>> p1 = Publication(id=None, title='The Python Journal')
>>> p1.save()
>>> p2 = Publication(id=None, title='Science News')
>>> p2.save()
>>> p3 = Publication(id=None, title='Science Weekly')
>>> p3.save()
@lambdamusic
lambdamusic / Snipplr-27156.py
Created February 7, 2013 21:25
Python: Django: instantiating M2M-through models
>>> ringo = Person.objects.create(name="Ringo Starr")
>>> paul = Person.objects.create(name="Paul McCartney")
>>> beatles = Group.objects.create(name="The Beatles")
>>> m1 = Membership(person=ringo, group=beatles,
... date_joined=date(1962, 8, 16),
... invite_reason= "Needed a new drummer.")
>>> m1.save()
>>> beatles.members.all()
[<Person: Ringo Starr>]
>>> ringo.group_set.all()
@lambdamusic
lambdamusic / Snipplr-25236.py
Created February 7, 2013 21:25
Django: Django: using properties on models
# still to be implemented
@lambdamusic
lambdamusic / Snipplr-35534.txt
Created February 7, 2013 21:25
Examples of Objective-C Loops
//Loops
//For loop
     for (int y = 0; y < 3; y ) {
     NSLog(@\"y = %i\", y);
}
//Do loop
x = 0;
do{
@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-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-42627.js
Last active April 4, 2024 22:05
JavaScript: Forcee a page to reload #js
// This will forcefully reload the current page. No jQuery needed here.
location.reload(true);
// If your content might be cached by proxy servers and your URL has no query string, try this:
location.replace(
location.href.replace(/\?.*$/, '') + '?' + Math.random());