Skip to content

Instantly share code, notes, and snippets.

View gterzian's full-sized avatar

Gregory Terzian gterzian

View GitHub Profile
@gterzian
gterzian / UserEmailField.py
Created November 14, 2012 04:01
Django custom form field checks that any input email is from a registered user, multiple comma delimitated input accepted
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.
'''
@gterzian
gterzian / ProEmailFormField.py
Created November 14, 2012 21:59
Django ProEmailFormField
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.
@gterzian
gterzian / gist:5411703
Last active December 16, 2015 09:18
Ruby binary search of sorted array implemented using deferred detection of equality will return the smallest index found for a matching key attached some tests...
#!/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
@gterzian
gterzian / gist:6462324
Created September 6, 2013 10:52
Django Recipes: Manipulating a model’s m2m from within a signal handler
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)
@gterzian
gterzian / gist:6649545
Last active December 23, 2015 14:29
Django views for Dropbox oauth
'''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)
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
@gterzian
gterzian / gist:6649526
Last active February 22, 2017 09:22
Django model for working with the Dropbox API
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')
// 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();
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;
}
// 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()