Function Repository Resource:

GetKeyValuePattern

Source Notebook

Create a suitable KeyValuePattern from an example association

Contributed by: Richard Hennigan (Wolfram Research)

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"Falseformat the output for readability
"QuantityPatterns"Falsecreate patterns for Quantity expressions

Examples

Basic Examples (2) 

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 (3) 

MakeBoxes (1) 

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 (2) 

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]=

Do not 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 (4) 

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 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 you encounter data types that you did not plan for, you can have error handling:

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

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 11 October 2018

License Information