Wolfram Computation Meets Knowledge

GetKeyValuePattern

Contributed by: Richard Hennigan (Wolfram Research)

Creates a suitable KeyValuePattern from an example association

ResourceFunction["GetKeyValuePattern"][assoc]

creates a KeyValuePattern using assoc as a template.

ResourceFunction["GetKeyValuePattern"][assoc,level]

creates a nested KeyValuePattern using assoc down to level.

Details and Options

ResourceFunction["GetKeyValuePattern"] has the following options:
"MakeBoxes" False format the output for readability
"QuantityPatterns" False create patterns for Quantity expressions

Examples

Basic Examples

Retrieve the ResourceFunction:

In[1]:=
ResourceFunction["GetKeyValuePattern"]
Out[1]=

Get a suitable KeyValuePattern from an example Association:

In[2]:=
ResourceFunction[
 "GetKeyValuePattern"][<|"key1" -> 1, "key2" -> "Hello world", "key3" -> True|>]
Out[2]=

By default, the first level is used:

In[3]:=
a = <|"a" -> Quantity[1, "Seconds"], "b" -> "Hello world", "c" -> <|"d" -> 2.2, "e" -> <|"f" -> x|>|>|>;
ResourceFunction["GetKeyValuePattern"][a]
Out[4]=

Specify a different level:

In[5]:=
ResourceFunction["GetKeyValuePattern"][a, 2]
Out[5]=

Use every level:

In[6]:=
ResourceFunction["GetKeyValuePattern"][a, Infinity]
Out[6]=

Options

MakeBoxes

Get formatted output for easier copy and paste into an editor:

In[7]:=
ResourceFunction["GetKeyValuePattern"][
 <|"a" -> Quantity[1, "Seconds"], "b" -> "Hello world", "c" -> <|"d" -> 2.2, "e" -> <|"f" -> x, "g" -> Range[10]|>|>|>,
 Infinity,
 "MakeBoxes" -> True
 ]
Out[7]=

QuantityPatterns

Create patterns using Quantity units:

In[8]:=
kvp = ResourceFunction[
  "GetKeyValuePattern"][<|"Time" -> Quantity[20, "Seconds"]|>, "QuantityPatterns" -> True]
Out[8]=
In[9]:=
MatchQ[<|"Time" -> Quantity[2, "Seconds"]|>, kvp]
Out[9]=
In[10]:=
MatchQ[<|"Time" -> Quantity[2, "Miles"]|>, kvp]
Out[10]=

Don't match specific Quantity types:

In[11]:=
kvp = ResourceFunction[
  "GetKeyValuePattern"][<|"Time" -> Quantity[20, "Seconds"]|>, "QuantityPatterns" -> False]
Out[11]=
In[12]:=
MatchQ[<|"Time" -> Quantity[2, "Seconds"]|>, kvp]
Out[12]=
In[13]:=
MatchQ[<|"Time" -> Quantity[2, "Miles"]|>, kvp]
Out[13]=

Applications

Create a function that returns data from the cloud while making sure the structure of the data matches a sample:

In[14]:=
obj = CloudDeploy[
   Delayed[<|"ID" -> $RequesterWolframID, "Data" -> <|"Timestamp" -> Now, "TimeRemaining" -> UnitConvert[DateDifference[Now, DayRound[Now, "Weekend"]], "Hours"]|>|>, "WL"], Permissions -> "Public"];
In[15]:=
sample = URLExecute[obj, {}, "WL"]
Out[15]=

Now we'll create a pattern to test against for all responses from the server:

In[16]:=
$expectedPattern = ResourceFunction["GetKeyValuePattern"][sample, Infinity, "QuantityPatterns" -> True]
Out[16]=
In[17]:=
myCloudInfo[] := Replace[URLExecute[obj], Except[$expectedPattern] :> Failure["UnrecognizedData", <|
      "MessageTemplate" :> "Recieved unexpected data from the server."|>]];

Under normal circumstances, the function just returns the output from the deployed CloudObject:

In[18]:=
myCloudInfo[]
Out[18]=

Now if we encounter data types that we didn't plan for, we can have error handling:

In[19]:=
CloudDisconnect[];
In[20]:=
myCloudInfo[]
Out[20]=

Resource History