Created
December 18, 2018 22:02
-
-
Save nathanhinchey/f05b04cac6847856cf7471736fde093d to your computer and use it in GitHub Desktop.
first test for carta
This file contains hidden or 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
""" | |
Reproduce a dictionary structure represented by certificates_grouped_by_date_and_share_class_id | |
""" | |
from datetime import date | |
class Certificate: | |
def __init__(self, unique_label, date, share_class_id): | |
self.unique_label = unique_label | |
self.date = date | |
self.share_class_id = share_class_id | |
certificate_1 = Certificate('CS-1', date(2016, 5, 10), 7) | |
certificate_2 = Certificate('CS-2', date(2015, 1, 1), 7) | |
certificate_3 = Certificate('CS-3', date(2016, 5, 10), 9) | |
certificate_4 = Certificate('CS-4', date(2016, 5, 10), 9) | |
certificates = [certificate_1, certificate_2, certificate_3, certificate_4] | |
def group_certificates(certificates): | |
output = {} | |
for c in certificates: | |
if c.date in output: | |
if c.share_class_id in output[c.date]: | |
output[c.date][c.share_class_id].append(c.unique_label) | |
else: | |
output[c.date][c.share_class_id] = [c.unique_label] | |
else: | |
output[c.date] = {c.share_class_id: [c.unique_label]} | |
return output | |
""" | |
Should look like this after: | |
certificates_grouped_by_date_and_share_class_id = { | |
date(2016, 5, 10): { | |
7: [ | |
'CS-1', | |
], | |
9: [ | |
'CS-3' | |
] | |
}, | |
date(2015, 1, 1): { | |
7: [ | |
'CS-2', | |
] | |
} | |
} | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment