Skip to content

Instantly share code, notes, and snippets.

@kyle-eshares
Last active January 17, 2017 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyle-eshares/287a305583259159a2081513c3134f2a to your computer and use it in GitHub Desktop.
Save kyle-eshares/287a305583259159a2081513c3134f2a to your computer and use it in GitHub Desktop.
# 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 = [
sec.label for sec in transfered_to_ix[mdl.id]
]
return inner
...
# data.py
all_securities = list(Security.objects.filter(company=company).all())
all_shareholders = list(ShareHolders.objects.filter(company=company).all())
# Many to One
shareholder_ix = {sh.pk: sh for shareholder in all_shareholders}
# Reverse foreign key
transfered_to_ix = defaultdict(list)
for security in all_securities:
if security.transformed_from_id:
transfered_to_ix[security.transformed_from_id].append(security)
flat_securities = (
conduit(all_securities)
...
.filter(should_show_security) # Filter securities since now we grab all of them
.map(
flatten(
...
add_shareholder_name(shareholder_ix),
add_transfered_to_labels(transfered_to_ix)
)
)
...
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment