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
class Student(models.Model): | |
email = models.EmailField() | |
... | |
s = Student() | |
s.email = "[email protected]" |
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 validate_email import validate_email | |
class EmailField(object): | |
def __get__(self, instance, owner): | |
return self.value | |
def __set__(self, instance, value): | |
validate_email(value) | |
self.value = value | |
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
user1 = User() | |
user1.email = '123' # Throws exception | |
user1.email = '[email protected]' | |
print user1.email # [email protected] | |
user2 = User() | |
user2.email = '[email protected]' | |
print user2.email # [email protected] | |
print user1.email # [email protected] !!! |
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
class EmailField(object): | |
__current_id = 0 # unique id of the instance | |
__email_dict = {} # dict containing all email values | |
__dict_key = "__email_field_key" # name of member attached to instances | |
def __get__(self, instance, owner): | |
if not hasattr(instance, self.__class__.__dict_key): | |
return None | |
# Retrieve the email value from the dict |
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
class User(object): | |
email = EmailField() | |
... | |
class Company(object): | |
contact = EmailField() | |
... | |
user1 = 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
assert(majority([]) is None) | |
assert(majority(None) is None) | |
assert(majority([1, 2, 3]) is None) | |
assert(majority([3, 3, 3]) == 3) | |
assert(majority([1, 2, 3, 3, 3]) == 3) | |
assert(majority([1, 2, 3, 4, 5]) is None) | |
assert(majority([1, 3, 2, 3, 3, 6, 5, 8, 3, 3, 3, 6, 3]) == 3) |
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 majority(collection): | |
if not collection: | |
return None | |
# initialize a candidate | |
candidate = collection[0] | |
chances = 1 | |
# go through the rest of the elements | |
for c in collection[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
CREATE TABLE posts ( | |
post_id SERIAL PRIMARY KEY, | |
title text, | |
content text | |
); | |
INSERT INTO | |
posts(title, content) | |
VALUES('This is my first post', | |
'We are going to have so much fun!') |
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
SELECT * | |
FROM posts | |
WHERE content @@ 'watch' |
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
SELECT * | |
FROM posts | |
WHERE to_tsvector(content) @@ to_tsquery('english', 'watch'); |
OlderNewer