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 conduit(object): | |
def __init__(self, iterator): | |
self.iterator = iterator | |
def filter(self, predicate): | |
return conduit(itertools.ifilter(predicate, self.iterator)) | |
def map(self, func): | |
return conduit(itertools.imap(func, self.iterator)) |
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 connection | |
def flatten(*args): | |
def transform_pipeline(mdl): | |
data = FlatData() | |
num_before = len(connection.queries) | |
for fn in args: | |
fn(data, mdl) | |
assert len(connection.queries) == num_before | |
return data |
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
# computations.py | |
def add_shareholder_name(shareholder_ix): | |
def inner(flat, mdl): | |
flat.shareholder_name = shareholder_ix[mdl.shareholder_id] | |
return inner | |
def add_transfered_to_labels(transfered_to_ix): | |
def inner(flat, mdl): | |
flat.exercised_from_labels = [ |
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 is_canceled(report_date) | |
def inner(flat, mdl): | |
flat.is_canceled = mdl.canceled_date < report_date | |
return inner | |
report_date = datetime.today() | |
flat_securities = ( | |
conduit(all_securities) | |
.map( |
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
Author | |
------ | |
authors.id | authors.name | |
------------------- | |
1 | "paul" | |
2 | "peter" | |
3 | "john" | |
Books | |
----- |
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
# Don't waste a query if you are using the queryset | |
books = Book.objects.filter(..) | |
if len(books) > 5: | |
do_stuff_with_books(books) | |
# If you aren't using the queryset use count | |
books = Book.objects.filter(..) | |
if books.count() > 5: | |
do_some_stuff() |
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
# Don't waste a query if you are using the queryset | |
books = Book.objects.filter(..) | |
if books: | |
do_stuff_with_books(books) | |
# If you aren't using the queryset use exist | |
books = Book.objects.filter(..) | |
if books.exists(): | |
do_some_stuff() |
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 author_name_length(book): | |
return len(book.author.name) | |
def process_author_books(author): | |
for book in author.books.all(): | |
do_stuff(book) |
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
# Get the Author's name of a Book | |
book = Book.objects.first() | |
book.author.name |
NewerOlder