This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from django.core.exceptions import ValidationError | |
from django.contrib.auth.models import User | |
class UserEmailField(forms.EmailField): | |
''' | |
This field subclasses EmailField, so we get the validation that no blanks are supplied and that an actual email address was supplied | |
This custom field adds validation for the existence of a registered user with this email. | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from django.core.exceptions import ValidationError | |
import re | |
''' | |
pass a list of excluded domain names upon instantiation. | |
subclasses forms.EmailField, which validates against empty values, and that it is a correct email. | |
then validate_pro will check whether not part of the excluded domains. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "test/unit" | |
def binary_search(array, key) | |
#binary search of sorted array implemented using deferred detection of equality | |
#returns the smallest index found for a matching key | |
array = array.sort | |
min = 0 | |
max = array.count - 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from functools import partial | |
class MethodMissing: | |
def method_missing(self, name, *args, **kwargs): | |
'''please implement''' | |
raise NotImplementedError('please implement a "method_missing" method') | |
def __getattr__(self, name): | |
return partial(self.method_missing, name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db.models.signals import pre_save, post_save, m2m_changed | |
def save_handler(sender, instance, *args, **kwargs): | |
m2m_changed.connect(m2m_handler, sender=sender.m2mfield.through, weak=False) | |
def m2m_handler(sender, instance, action, *args, **kwargs): | |
if action =='post_clear': | |
succesfully_manipulate_m2m(instance) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from django.conf import settings | |
import dropbox | |
class RequestToken(models.Model): | |
key = models.CharField(max_length=100) | |
secret = models.CharField(max_length=100) | |
user = models.ForeignKey('auth.User') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''add all the django relevant imports''' | |
import dropbox | |
dropbox_session = dropbox.session.DropboxSession(settings.DROPBOX_KEY, settings.DROPBOX_SECRET, settings.DROPBOX_ACCESS_TYPE) | |
def dropbox_authorize_url(request): | |
request_token = dropbox_session.obtain_request_token() | |
saved_token = RequestToken(user=request.user, secret=request_token.secret, key=request_token.key) | |
saved_token.save() | |
url = dropbox_session.build_authorize_url(request_token, oauth_callback=settings.DROPBOX_CALLBACK) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sum_of_primes_for(n): | |
s = range(3, n, 2) | |
for m in xrange(3, int(n**0.5)+1, 2): | |
if s[(m-3)/2]: | |
for t in xrange((m*m-3)/2,(n>>1)-1,m): | |
s[t]=0 | |
return sum([2]+[t for t in s if t>0]) | |
#>>> sum_of_primes_for(2000000) | |
#142913828922 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn generate(num_chan: Sender<PipelineMsg>) { | |
let mut num = 2; | |
let _ = thread::Builder::new().spawn(move || { | |
// Note the while condition, | |
// Err will be returned by send when the corresponding receiver, | |
// is dropped. That will result in this stage quitting. | |
while let Ok(_) = num_chan.send(PipelineMsg::Generated(num)) { | |
num = num + 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn square(merge_chan: Sender<PipelineMsg>) -> Sender<PipelineMsg> { | |
let (chan, port) = channel(); | |
let _ = thread::Builder::new().spawn(move || { | |
// Iteration will stop, | |
// once the sender returned by "square" is dropped. | |
for msg in port { | |
let num = match msg { | |
PipelineMsg::Generated(num) => num, | |
_ => panic!("unexpected message receiving at square stage"), | |
}; |
OlderNewer