jsontas.data_structures package¶
Submodules¶
jsontas.data_structures.condition module¶
Condition datastructure.
-
class
jsontas.data_structures.condition.
Condition
(jsonkey, datasubset, dataset, **data)[source]¶ Bases:
jsontas.data_structures.datastructure.DataStructure
Condition datastructure.
Example:
{ "$condition": { "if": { "key": "Key to match", "operator": "$eq", "value": "something", }, "then": "returnvalue", "else": "Default value" } }
Example:
{ "$condition": { "if": [ { "key": "Key to match", "operator": "$eq", "value": "something", }, { "key": "Another key to match", "operator": "$in", "value": "somethingelse" } ], "then": "returnvalue", "else": "Default value" } }
Supported operators defined here:
jsontas.data_structures.operator.Operator
-
_if
(operator)[source]¶ If operator.
Parameters: operator (dict) – Data to check “if” on. Returns: Operator result. Return type: bool
-
jsontas.data_structures.datastructure module¶
Base datastructure.
jsontas.data_structures.expand module¶
Expand datastructure.
-
class
jsontas.data_structures.expand.
Expand
(jsonkey, datasubset, dataset, **data)[source]¶ Bases:
jsontas.data_structures.datastructure.DataStructure
Expand datastructure.
Expand a value into a list of a certain number of elements.
Example:
{ "a_list": { "$expand": { "value": { "hello": "world" }, "to": 5 } } }
Result:
{ "a_list": [ { "hello": "world" }, { "hello": "world" }, { "hello": "world" }, { "hello": "world" }, { "hello": "world" } ] }
Example:
{ "another_list": { "$expand": { "value": "something" "to": 5 } } }
Result:
{ "another_list": [ "something", "something", "something", "something", "something" ] }
jsontas.data_structures.filter module¶
Filter datastructure.
-
class
jsontas.data_structures.filter.
Filter
(jsonkey, datasubset, dataset, **data)[source]¶ Bases:
jsontas.data_structures.datastructure.DataStructure
Filter datastructure.
Example:
{ "data": { "$filter": { "items": [ { "status": "success", "value": "1" }, { "status": "failure", "value": "2" }, { "status": "success", "value": "3" } ], "filters": [ { "key": "status", "operator": "$eq", "value": "success" } ] } } } # {"data": [{"status": "success", "value": "1"}, {"status": "success", "value": "3"}]}
-
execute
()[source]¶ Execute the filter datastructure.
Returns: Key and the value(s) found. Return type: tuple
-
filter
(item)[source]¶ Execute the filtering list against item.
The filtering list is the list of key,operator,value dictionaries following the format of
jsontas.data_structures.operator.Operator
(in fact, thejsontas.data_structures.operator.Operator
datastructure is used in this method to validate the filters)Note that when running the filter, the dataset is appended with an “item” which is a single item from the “items” filter list (can be seen above).
The dataset could then be this:
{ **current_dataset, "item": { "status": "success", "value": "1"} }
With the filter being:
{ "key": "status", "operator": "$eq", "value": "success" }
And the filter would be True (the status of the item is ‘success’). Note that this method will only ever run on the “item”. If there is not “$item” in the “key”, the method will add it. That is why it’s possible to write:
{"key": "status"}
instead of:
{"key": "$item.status"}
But it would also mean that if one were to write:
{"key": "$anotherkey.something.else"}
it would become:
{"key": "$item.$anotherkey.something.else"}
which would fail.
This is a design choice. The filter shall not utilize any data aside from the ‘item’ key in the dataset.
Parameters: item (dict) – Item to filter. Returns: Whether or not all items in filter evaluates to True. Return type: bool
-
jsontas.data_structures.from_item module¶
From datastructure.
-
class
jsontas.data_structures.from_item.
From
(jsonkey, datasubset, dataset, **data)[source]¶ Bases:
jsontas.data_structures.datastructure.DataStructure
From datastructure.
Example:
{ "hello" { "$from": { "item": { "value": "something", "text": "world" }, "get": "text" } } } # {"hello": "world"}
jsontas.data_structures.list module¶
List datastructure.
-
class
jsontas.data_structures.list.
List
(jsonkey, datasubset, dataset, **data)[source]¶ Bases:
jsontas.data_structures.datastructure.DataStructure
List datastructure.
Not to be used directly in JSON structure. Use it with the .int operator.
index
- Index 0:{ "something": "$value.0" }
index
- Last index:{ "something": "$value.-1" }
slice
- Everything after index 1:{ "something": "$value.1:" }
slice
- Everything before index 4:{ "something": "$value.:4" }
slice
- Everything between index 2 and 4:{ "something": "$value.2:4" }
-
execute
()[source]¶ Execute the list datastructure.
Returns: Key and the value(s) found. Return type: tuple
-
static
index
(data, index)[source]¶ Get an index from data.
Parameters: Returns: Value on index.
Return type: any
-
jsontas.data_structures.operator module¶
Operator datastructure.
-
class
jsontas.data_structures.operator.
Operator
(*args, **kwargs)[source]¶ Bases:
jsontas.data_structures.datastructure.DataStructure
Operator datastructure.
Example:
{ "$operator": { "key": "key to match", "operator": "$eq", "value": "value to match against key" } }
Supported operators:
$eq: _equal
$in: _in
$notin: _notin
$startswith: _startswith
$regex: _regex
-
_equal
()[source]¶ Operator ‘==’.
Example:
{ "$operator": { "key": "key to match", "operator": "$eq", "value": "value to match against key" } }
-
_in
()[source]¶ Operator ‘in’.
Example - In list:
{ "$operator": { "key": "key", "operator": "$in", "value": ["key", "anotherkey"] } }
Example - In string:
{ "$operator": { "key": "k", "operator": "$in", "value": "key" } }
-
_notin
()[source]¶ Operator ‘not in’.
Example - In list:
{ "$operator": { "key": "key", "operator": "$notin", "value": ["key", "anotherkey"] } }
Example - In string:
{ "$operator": { "key": "k", "operator": "$notin", "value": "key" } }
-
_startswith
()[source]¶ Operator ‘str.startswith()’.
Example:
{ "$operator": { "key": "key", "operator": "$startswith", "value": "k" } }
jsontas.data_structures.reduce module¶
Reduce datastructure.
-
class
jsontas.data_structures.reduce.
Reduce
(jsonkey, datasubset, dataset, **data)[source]¶ Bases:
jsontas.data_structures.datastructure.DataStructure
Reduce a list from end to beginning (from right) to a specific value
Example:
{ "reduced_list": { "$reduce": { "list": [ "element 1", "element 2", "element 3" ], "to": 2 } } }
Result:
{ "reduced_list": [ "element 1", "element 2" ] }
jsontas.data_structures.request module¶
Request datastructure.
-
class
jsontas.data_structures.request.
Request
(jsonkey, datasubset, dataset, **data)[source]¶ Bases:
jsontas.data_structures.datastructure.DataStructure
HTTP request datastructure.
Example:
{ "$request" { "url": "http://localhost:8000/something.json", "method": "GET" } }
Example:
{ "$request" { "url": "http://localhost:8000/something.json", "method": "POST", "json": { "my_json": "hello" }, "headers": { "Content-Type": "application/json" }, "auth": { "username": "admin", "password": "admin", "type": "basic" } } }
Example getting response after:
# Assume response from request is: {"hello": "world"} { "data": { "$request" { "url": "http://localhost:8000/something.json", "method": "GET" } }, "text": "$response.json.hello" } # Resulting JSON will be: { "data": { "hello": "world" }, "text": "world" }
-
request
(url, method, json=None, headers=None, **requests_parameters)[source]¶ Make an HTTP request.
Parameters: Returns: Wait generator for getting responses from request.
Return type: generator
-
jsontas.data_structures.wait module¶
Wait datastructure.
-
class
jsontas.data_structures.wait.
Wait
(jsonkey, datasubset, dataset, **data)[source]¶ Bases:
jsontas.data_structures.datastructure.DataStructure
Wait datastructure.
Wait for a query tree to evaluate to True.
Example:
{ "$wait": { "for": { "$request": { "url": "http://example.com", "method": "GET" } }, "interval": 1, "timeout": 20, "else": {} } }
Request example.com until a non-null response. Maximum 1 request/s for 20s
Due to the storage of the ‘query_tree’ in dataset it is possible to nest queries:
Example:
{ "response": { "$from": { "item": { "$wait": { "for": { "$condition": { "then": { "$request": { "url": "http://example.com", "method": "GET" } }, "if": { "key": "$response.status_code", "operator": "$eq", "value": 200 }, "else": null } }, "interval": 1, "timeout": 20, "else": {} } }, "get": "items" } } }
Wait for example.com to respond with status_code 200 and a non-null response and get the ‘items’ key from the response. Maximum 1 request/s for 20s
-
execute
()[source]¶ Execute wait datastructure.
Waiting for will requires a ‘query_tree’ which is a collection of all requests that are ‘below’ the ‘wait’ datastructure. This is to keep track of the queries that have executed before (but not the results of these queries).
This query tree is then executed in the
jsontas.jsontas.JsonTas.resolve()
method, which can be considered weird. We’re executing JsonTas inside of a JsonTas query. But this is the only way to re-run a query tree.Returns: None and the result of re-running a query tree. Return type: Tuple
-
Module contents¶
Datastructure.