You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Start by creating an empty dictionary called query_parts. This will be used to store pieces of the query you're building.
Process each item in the input list:
Go through every item in the queryData list. Each item is expected to be a tuple with three elements: category, subcategory, and target.
Extract category, subcategory, and target:
For each item in the list:
Assign the first element of the tuple to category.
Assign the second element to subcategory.
Assign the third element to target.
Organize data by category:
Check if the current category is already present in the query_parts dictionary.
If it’s not, create a new list for this category.
Build the query string for each item:
If the subcategory is provided (i.e., it’s not empty):
Create a string in the format category.subcategory:target (e.g., set.name:Myset) and add it to the list for that category.
If no subcategory is provided:
Create a simpler string in the format category:target (e.g., type:Creature) and add it to the list for that category.
Prepare to build the final query:
Create an empty list called final_query_parts. This will hold the final pieces of the query that will be combined into the full query string.
Combine query parts by category:
For each category in query_parts:
If the category has more than one query string:
Combine the strings with " OR " between them and enclose the group in parentheses.
If the category only has one query string:
Just add it as-is to final_query_parts.
Construct the final query:
Once all query parts are processed, join them together with " AND " between each part to form the full query string.
Return the final query string:
The result is the final query that combines all conditions with "AND" and "OR" as needed.
Function Design Document: constructQuery
Function Name:
constructQuery
Brief Description:
The construct_query function constructs a query string suitable for use in an API call. The resulting string will follow the format necessary for querying, such as:
"q='(set.name:<set-name>) AND (name:<card-name>) AND (types:<type-name> OR types:<type-name>)'"
Assumptions:
The function assumes that all query parameters passed to it are valid and recognized by the API.
It is expected that the queryData will follow the defined structure.
Notes: The name and purpose of each variable used in the pseudo code is described after the pseudo code.
FUNCTIONconstructQuery( queryData : List[Tuples] )
# Initialize an empty dictionary to hold query partsINITIALIZEquery_partsASEMPTYDICTIONARY# Loop through each item in the queryData listFOREACHitemINqueryDataDO# Extract the category, subcategory, and target from the itemSETcategoryTOitem[0]
SETsubcategoryTOitem[1]
SETtargetTOitem[2]
# If the category is not already in query_parts, create a new list for itIFcategoryNOTINquery_partsTHENINITIALIZEquery_parts[category] ASEMPTYLISTENDIF# Check if subcategory is providedIFsubcategoryISNOTEMPTYTHEN# Create a string in the format "category.subcategory:target" and add it to the listAPPEND-ELEMENT(query_parts[category], category+"."+subcategory+":"+target)
ELSE# Create a string in the format "category:target" and add it to the listAPPEND-ELEMENT(query_parts[category], category+":"+target)
ENDIFENDFOR# Prepare to construct the final queryINITIALIZEfinal_query_partsASEMPTYLIST# Loop through each list of items in query_partsFOREACHitemsINquery_partsDO# If there is more than one item in the listIFLENGTH(items) >1THEN# Combine the items with "OR" and add parentheses around themAPPEND-ELEMENT(final_query_parts, "("+JOIN(items, " OR ") +")")
ELSE# If there's only one item, add it directlyAPPEND-ELEMENT(final_query_parts, items[0])
ENDIFENDFOR# Join all parts with "AND" to form the final query stringRETURNJOIN(final_query_parts, " AND ")
ENDFUNCTION
Variables used in Pseudo and their intents
queryData: The data structure passed to us containing the data in a 'List of Tuples'
List[ Tuple( Category, Subcategory, Target ) ]
Category: The primary grouping for the query (e.g., "set" or "types").
Subcategory: An optional secondary grouping for the query (e.g., "name"). This can be empty.
Target: The specific value associated with the category and subcategory (e.g., "Myset", "Grass").
query_parts: A dictionary that the function will use to store all of the separate elements of the final query. Each key represents a unique category, and the value is a list of formatted query strings related to that category.
item: A single tuple from queryData, representing a single query component consisting of Category, Subcategory, and Target.
category: A variable that stores the first element of the current tuple from queryData, representing the main category of the query.
subcategory: A variable that stores the second element of the current tuple from queryData, representing the subcategory of the query. It can be empty.
target: A variable that stores the third element of the current tuple from queryData, representing the specific target value.
final_query_parts: A list that accumulates the final parts of the constructed query. It will contain either formatted strings representing groups of query items combined with "OR" or individual query items.
items: A variable that represents the list of formatted query strings associated with a specific category in query_parts during the final construction phase of the query.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Construct Query Algorithm
A Step-by-Step Breakdown
Initialize an empty dictionary:
query_parts. This will be used to store pieces of the query you're building.Process each item in the input list:
queryDatalist. Each item is expected to be a tuple with three elements:category,subcategory, andtarget.Extract category, subcategory, and target:
category.subcategory.target.Organize data by category:
categoryis already present in thequery_partsdictionary.Build the query string for each item:
subcategoryis provided (i.e., it’s not empty):category.subcategory:target(e.g.,set.name:Myset) and add it to the list for that category.subcategoryis provided:category:target(e.g.,type:Creature) and add it to the list for that category.Prepare to build the final query:
final_query_parts. This will hold the final pieces of the query that will be combined into the full query string.Combine query parts by category:
query_parts:" OR "between them and enclose the group in parentheses.final_query_parts.Construct the final query:
" AND "between each part to form the full query string.Return the final query string:
"AND"and"OR"as needed.Function Design Document:
constructQueryFunction Name:
constructQueryBrief Description:
The
construct_queryfunction constructs a query string suitable for use in an API call. The resulting string will follow the format necessary for querying, such as:"q='(set.name:<set-name>) AND (name:<card-name>) AND (types:<type-name> OR types:<type-name>)'"Assumptions:
queryDatawill follow the defined structure.Parameters:
Structure of Each Tuple:
Each tuple in the list may contain any of the following formats:
Return Type:
The function returns a string containing the constructed query in the following format:
Notes:
'OR'is necessary for multiple type filters, allowing the construction of logical OR conditions for types.'AND'is necessary to group the parameters logically, resulting in a structure like:And later on could easily be:
( (set1 OR set2 OR setN ) AND (card) AND (type1 OR type2 OR typeM) )
Exceptions:
Example Usage:
Here’s how to use the
constructQueryfunction in pseudocode:Pseudo Code of Function:
Notes: The name and purpose of each variable used in the pseudo code is described after the pseudo code.
Variables used in Pseudo and their intents
queryData: The data structure passed to us containing the data in a 'List of Tuples'
List[ Tuple( Category, Subcategory, Target ) ]
query_parts: A dictionary that the function will use to store all of the separate elements of the final query. Each key represents a unique category, and the value is a list of formatted query strings related to that category.
item: A single tuple from queryData, representing a single query component consisting of Category, Subcategory, and Target.
category: A variable that stores the first element of the current tuple from queryData, representing the main category of the query.
subcategory: A variable that stores the second element of the current tuple from queryData, representing the subcategory of the query. It can be empty.
target: A variable that stores the third element of the current tuple from queryData, representing the specific target value.
final_query_parts: A list that accumulates the final parts of the constructed query. It will contain either formatted strings representing groups of query items combined with "OR" or individual query items.
items: A variable that represents the list of formatted query strings associated with a specific category in query_parts during the final construction phase of the query.
All reactions