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
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
'''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
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
// Cloning merge_chan first, then moving it. | |
// This move ensures that when square_workers drop, | |
// and the workers quit their loop, | |
// all merge_chan senders drop as well, | |
// triggerign the quit of the loop in the "merge" stage... | |
let mut square_workers: VecDeque<Sender<PipelineMsg>> = vec![square(merge_chan.clone()), | |
square(merge_chan)] | |
.into_iter() | |
.collect(); |
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
// Results chan, to get the final merged results to the main thread | |
let (results_chan, results_port) = channel(); | |
// Gen chan, to get generated numbers from the "generator". | |
let (gen_chan, gen_port) = channel(); | |
// Share the sender of the results chan to the merge component. | |
let merge_chan = merge(results_chan); | |
// Start the workers, each worker receives a clone of the "merge chan sender". | |
let mut square_workers: VecDeque<Sender<PipelineMsg>> = vec![square(merge_chan.clone()), | |
square(merge_chan.clone())] | |
.into_iter() |
OlderNewer