Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you're trying to do here is look up an item (task, folder) based on the id. Python's dictionaries provide an easy way to do this. You will only save if you'll be doing the searches several times (e.g. if there are many tasks with the same id, or if you'll be running the function several times).</p> <p>Additionally, for forbiddenFolders you just have a list of names (you're not looking up an item, you're just checking if it's present) for which Python's sets are suitable.</p> <p>Anyway, here is how you build the dictionaries and sets:</p> <pre><code>tasks_dict = dict((task['id'], task) for task in tasks) folders_dict = dict((folder['id'], folder) for folder in folders) forbidden_folders_set = set(forbiddenFolders) </code></pre> <p>Now, <code>task = tasks_dict[id]</code> is a task such that <code>task['id'] == id</code>, and similarly for folders, so you can replace the loops above with these expressions. The set doesn't allow this, but it allows you to check for presence with <code>folder in forbidden_folders_set</code>. </p> <p>(Bear in mind that each of those <code>dict(...)</code> operations may take longer than running through one of the for loops above, but they are an investment for faster lookup in future.)</p> <pre><code>if entry['item_id'] in tasks_dict: task = tasks_dict[entry['item_id']] if task['parent_id'] in folders_dict: folder = folders_dict[task['parent_id']] if folder in forbidden_folders_set: body_of_email += ... </code></pre> <p>The <code>x in y</code> and <code>..._dict[x]</code> operations above are very efficient.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload