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
static _else(data)[source]

Else operator. Just return the data.

Note that this method is added for readability in execute only.

Parameters:data (any) – Data to just return.
Returns:Condition.
Return type:str
execute()[source]

Execute data.

Returns:None and value from either ‘else’ or ‘then’.
Return type:tuple

jsontas.data_structures.datastructure module

Base datastructure.

class jsontas.data_structures.datastructure.DataStructure(jsonkey, datasubset, dataset, **data)[source]

Bases: object

Base datastructure class.

execute()[source]

Execute datastructure.

Implement this.

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"
    ]
}
execute()[source]

Execute expand.

Returns:None and a list of values.
Return type:tuple

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, the jsontas.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"}
execute()[source]

Execute the $from datastructure.

Returns:Key and the value(s) found.
Return type:tuple

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:
  • data (list, set or tuple) – Data to get index from.
  • index (int) – Index to get.
Returns:

Value on index.

Return type:

any

static slice(data, first, second)[source]

Slice a string based on first and second integers.

Parameters:
  • data (list, set or tuple) – Data to get slice from.
  • first (int or None) – Number to put before ‘:’ if not None.
  • second (int or None.) – Number to put after ‘:’ if not None.
Returns:

Sliced list.

Return type:

list

static split(value)[source]

Split a string on ‘:’ and return first and second value.

Parameters:value (str) – Value to split.
Returns:First and second value. Any of them can be None.
Return type:tuple

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:

_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"
    }
}
_regex()[source]

Operator ‘re.match’.

Example:

{
    "$operator": {
        "key": "key",
        "operator": "$regex",
        "value": "^[A-Za-z]+$"
    }
}
execute()[source]

Execute operator.

Returns:None and whether key matches value or not.
Return type:tuple

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"
    ]
}
execute()[source]

Execute reduce.

Returns:None and a list of values.
Return type:tuple

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"
}
execute()[source]

Execute data.

Returns:None and response as JSON (or None).
Return type:Tuple
request(url, method, json=None, headers=None, **requests_parameters)[source]

Make an HTTP request.

Parameters:
  • url (str) – URL to request.
  • method (str) – HTTP method.
  • json (dict) – Optional JSON data to request.
  • headers (dict) – Optional extra headers to request.
  • requests_parameters (dict) – Extra parameters to python requests.
Returns:

Wait generator for getting responses from request.

Return type:

generator

static wait(method, timeout=None, interval=5, **kwargs)[source]

Iterate over result from method call.

Parameters:
  • method – Method to call.
  • timeout (int or None) – How long, in seconds, to iterate.
  • interval (int) – How long, in seconds, to wait between method calls.
  • kwargs (dict) – Keyword arguments to pass to method call.

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
static wait(method, timeout, interval, **kwargs)[source]

Iterate over result from method call.

Parameters:
  • method – Method to call.
  • timeout (int or None) – How long, in seconds, to iterate.
  • interval (int) – How long, in seconds, to wait between method calls.
  • kwargs (dict) – Keyword arguments to pass to method call.

Module contents

Datastructure.