# Homework 2021-12-06 ### Review: Speed Consider this code snippet: ```python useful_client = [] for entry in useful: usefulSecurityid = entry['metaData']['metaData']['SECURITY_ID'] for row in clientSecurities: if row['securityId'] == usefulSecurityid: break if row['securitymaster']['clientspecificsecurity'] is False: useful_client.append(entry) ``` 1. Review: Why did we run into problems with this code? <span style="color:red">1g. The nested loops are an inefficient way to find the matching entry </span> <span style="color:red">1g.The 'break' pattern only works if there is guranteed a match, else you just end up inserting the final itteration of the loop </span> 1a. Under what circumstances would this code be fine to use? <span style="color:red">1ag. known finite-length list of client securities with all clientSecurities contains all Useful securities </span> 2. Review: Why would the `semaphore` pattern not (significantly) help this code? <span style="color:red"> 2g. The number of itterations would be the same </span> 2a. Extra: What is a semaphore? <span style="color:red"> 2ag. a signaling system to control flow </span> ```python semaphore = 50 def go(thread): if semaphore == 0: sleep(1) else: old_sem = semaphore semaphore = old_sem - 1 spawn_next_thread() do_stuff() # 1/0 semaphore = semaphore + 1 ``` ### Data Access Pipeline We have a number of data access functions. $$ \begin{aligned} get\_url\_transform & : (errorName,date) -> [issue] \\ get\_ultimate\_parent & : clientId \rightarrow clientId \\ get\_account\_data & : [accountId] \rightarrow [accountData] \\ get\_support\_level & : ([securityId], date) \rightarrow [(securityId,supportLevel)] \\ \end{aligned} $$ 1. What is the "type" of `get_client_specific_data`? Extra: Why do each of these have their specific type? ie, why does `get_ultimate_parent` take a single clientId while the others take lists? <span style="color:red"> 1g. get_client_specific_data([security_ids]) -> [{'securityId','securitymaster'}] 1g.The request/response characteristics of the endpoints i.e. get_ultimate_parent does not return a map of the requested inputs, simply a list of all outputs </span> 2. How does our current data flow work? <span style="color:red"> 1g. Get all errors, get_support_level for all securities, get account data for all accounts, eliminate client </span> 3. Using the same functions, how can we change our data flow? What would you predict to be the "best" data flow (usually measured by speed)? 4. Code: attempt to code up your data flow. Extra: Prove (or disprove) that you have found a good data flow. ### Look Ahead: Aggregating the data Begin manipulating the data towards something we can ship to the UI. See the `create_summary_data` in the original script for ideas. 1. What security-level data should we bring to the UI? 2. Code 2a. Attempt to create a list with one entry per security we want to report on. 2b. Begin including data like those in the provided script