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 __future__ import unicode_literals | |
from django.db import models | |
from django.db.models.fields.related_descriptors import ForwardManyToOneDescriptor # noqa | |
class RelationNotLoaded(Exception): | |
pass | |
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
""" | |
Logical deletion for Django models. Uses is_void flag | |
to hide discarded items from queries. Overrides delete | |
methods to set flag and soft-delete instead of removing | |
rows from the database. | |
""" | |
from django.apps import apps | |
from django.contrib.admin.utils import NestedObjects | |
from django.db import models | |
from django.db.models import signals |
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 | |
class AppQueryset(models.QuerySet): | |
pass | |
class AppManager(models.Manager): | |
queryset_class = AppQuerySet | |
def get_queryset(self): | |
return self.queryset_class(self.model) |
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
>>> Author.objects.count() | |
20 | |
>>> Book.objects.count() | |
100 | |
# This block is 101 queries. | |
# 1 for the books and 1 for each author that lazy-loaded | |
books = Book.objects.all() | |
for book in books: | |
do_stuff(book.title, book.author.name) |
NewerOlder