Name | jsonQM JSON |
Version |
0.3.4
JSON |
| download |
home_page | |
Summary | A simple tool to easily make your API endpoint json queries more versatile. |
upload_time | 2023-06-08 00:42:51 |
maintainer | |
docs_url | None |
author | William A. Lim |
requires_python | |
license | |
keywords |
python
json
request
query
model
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# jsonQM
A simple tool to easily make your API endpoint json queries more versatile.
`pip install jsonQM`
## Quick Start
The example model explains how to set up a model and query that model.
*tests/ExampleModel.py*
```py
from jsonQM import *
# Make the model.
# Declare a model serializer
ms = ModelSerializer() # Each model must have its own serializer
class MyModel(Model):
def __init__(self) -> None:
# define the structure of the query model:
self.model = {
# we have a single query scope/section called functions
"functions":{},
"msg":"test" # non functional attributes also need to be included in the json query if you wish for them to be present in the response (see Example query 3 below)
}
# sync/add the model attribute functions to the model
ms.sync(self)
# Define attributes of the model (argument 2) along with the scope/section they are found in (argument 1)
@ms.add(["functions"], "repeater") # This attribute key is "repeater" and can be found in the "functions" dict
def repeater(self, arg:int):
# when the attribute is queried it will run this code and return the value
return ["repeat" for _ in range(arg)]
# You can use anything as the attribute key that is a valid python dictionary key.
@ms.add(["functions"], 7)# Keep in mind that if used with json, you are limited to what is a valid json key.
def number_7(self):
return "abc"
@ms.token()# this marks this attribute as the model's token
@ms.add([], "token")
def token(self, tok:str):
#insert some logic to compare token to tokens in database
return tok == "token" # The token function must return true for the token to be valid.
@ms.requires_token()# this marks this attribute as requiring the token attribute to return true
@ms.add([], "secret")
def secret(self):
return "My super secret message" # if the token is false this will return 'errot:token:invalid' if the token is missing from the query this will return 'error:token:missing'
#LOGIC
if __name__ == "__main__":
# If you are experiencing bugs related to concurrency, you should reinstantiate your model per thread/concurrent task where it is being used.
# note that instantiation may require many itterations depending on the ammount of attributes you've added and the length of their parent path.
# (parent path is the first list argument in `ms.add(["some","dictionary","key","path"], "attribute name")`)
model_instance = MyModel()
# If you can avoid instantiating the model more than once for optimal performance.
#Example query 1
print(model_instance.get({
"functions":{
# programmed attribute values should be a list containing the function arguments.
"repeater":[5],
7:[]
}
}))
# prints:
# {'functions': {'repeater': ['repeat', 'repeat', 'repeat', 'repeat', 'repeat'], 7: 'abc'}}
#Example query 2
print(model_instance.get({
"functions":{
# The model will only run/return attributes which have been specified
"repeater":[5]
}
}))
# prints:
# {'functions': {'repeater': ['repeat', 'repeat', 'repeat', 'repeat', 'repeat']}}
#Example query 3
print(model_instance.get({
"token":["token"],
"secret":[]
}))
# prints:
# {'token': True, 'secret': 'My super secret message'}
#Example query 3
print(model_instance.get({
"token":["abc"],
"secret":[]
}))
# prints:
# {'token': False, 'secret': 'error:token:invalid'}
#Example query 3
print(model_instance.get({
"secret":[]
}))
# prints:
# {'secret': 'error:token:missing'}
#Example query 3
print(model_instance.get({
"msg":1
}))
# prints:
# {'msg': 'test'}
```
Raw data
{
"_id": null,
"home_page": "",
"name": "jsonQM",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "python,json,request,query,model",
"author": "William A. Lim",
"author_email": "william.lim@csu.fullerton.edu",
"download_url": "https://files.pythonhosted.org/packages/2f/95/e6b256be876a9ad380711221a8381593a65d74424789d59dc943be82b79c/jsonQM-0.3.4.tar.gz",
"platform": null,
"description": "# jsonQM\r\n\r\nA simple tool to easily make your API endpoint json queries more versatile.\r\n\r\n`pip install jsonQM`\r\n\r\n## Quick Start\r\n\r\nThe example model explains how to set up a model and query that model.\r\n\r\n*tests/ExampleModel.py*\r\n\r\n```py\r\nfrom jsonQM import *\r\n\r\n# Make the model.\r\n\r\n# Declare a model serializer\r\nms = ModelSerializer() # Each model must have its own serializer\r\nclass MyModel(Model):\r\n def __init__(self) -> None:\r\n # define the structure of the query model:\r\n self.model = {\r\n # we have a single query scope/section called functions\r\n \"functions\":{},\r\n \"msg\":\"test\" # non functional attributes also need to be included in the json query if you wish for them to be present in the response (see Example query 3 below)\r\n }\r\n # sync/add the model attribute functions to the model\r\n ms.sync(self)\r\n \r\n # Define attributes of the model (argument 2) along with the scope/section they are found in (argument 1)\r\n @ms.add([\"functions\"], \"repeater\") # This attribute key is \"repeater\" and can be found in the \"functions\" dict\r\n def repeater(self, arg:int):\r\n # when the attribute is queried it will run this code and return the value\r\n return [\"repeat\" for _ in range(arg)]\r\n\r\n # You can use anything as the attribute key that is a valid python dictionary key.\r\n @ms.add([\"functions\"], 7)# Keep in mind that if used with json, you are limited to what is a valid json key.\r\n def number_7(self):\r\n return \"abc\"\r\n \r\n @ms.token()# this marks this attribute as the model's token\r\n @ms.add([], \"token\")\r\n def token(self, tok:str):\r\n #insert some logic to compare token to tokens in database\r\n return tok == \"token\" # The token function must return true for the token to be valid.\r\n \r\n\r\n @ms.requires_token()# this marks this attribute as requiring the token attribute to return true\r\n @ms.add([], \"secret\")\r\n def secret(self):\r\n return \"My super secret message\" # if the token is false this will return 'errot:token:invalid' if the token is missing from the query this will return 'error:token:missing'\r\n\r\n#LOGIC\r\nif __name__ == \"__main__\":\r\n # If you are experiencing bugs related to concurrency, you should reinstantiate your model per thread/concurrent task where it is being used.\r\n # note that instantiation may require many itterations depending on the ammount of attributes you've added and the length of their parent path.\r\n # (parent path is the first list argument in `ms.add([\"some\",\"dictionary\",\"key\",\"path\"], \"attribute name\")`)\r\n model_instance = MyModel()\r\n\r\n # If you can avoid instantiating the model more than once for optimal performance.\r\n\r\n #Example query 1\r\n print(model_instance.get({\r\n \"functions\":{\r\n # programmed attribute values should be a list containing the function arguments.\r\n \"repeater\":[5],\r\n 7:[]\r\n }\r\n }))\r\n\r\n # prints:\r\n # {'functions': {'repeater': ['repeat', 'repeat', 'repeat', 'repeat', 'repeat'], 7: 'abc'}}\r\n\r\n #Example query 2\r\n print(model_instance.get({\r\n \"functions\":{\r\n # The model will only run/return attributes which have been specified\r\n \"repeater\":[5]\r\n }\r\n }))\r\n\r\n # prints:\r\n # {'functions': {'repeater': ['repeat', 'repeat', 'repeat', 'repeat', 'repeat']}}\r\n\r\n #Example query 3\r\n print(model_instance.get({\r\n \"token\":[\"token\"],\r\n \"secret\":[]\r\n }))\r\n\r\n # prints:\r\n # {'token': True, 'secret': 'My super secret message'}\r\n\r\n #Example query 3\r\n print(model_instance.get({\r\n \"token\":[\"abc\"],\r\n \"secret\":[]\r\n }))\r\n\r\n # prints:\r\n # {'token': False, 'secret': 'error:token:invalid'}\r\n\r\n #Example query 3\r\n print(model_instance.get({\r\n \"secret\":[]\r\n }))\r\n\r\n # prints:\r\n # {'secret': 'error:token:missing'}\r\n\r\n #Example query 3\r\n print(model_instance.get({\r\n \"msg\":1\r\n }))\r\n\r\n # prints:\r\n # {'msg': 'test'}\r\n```\r\n",
"bugtrack_url": null,
"license": "",
"summary": "A simple tool to easily make your API endpoint json queries more versatile.",
"version": "0.3.4",
"project_urls": {
"Source": "https://github.com/FrewtyPebbles/jsonQM",
"Tracker": "https://github.com/FrewtyPebbles/jsonQM/issues"
},
"split_keywords": [
"python",
"json",
"request",
"query",
"model"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "853e3e6ff571efa0edb1028c7c7714f5cd48c8ef36551e33bc2ab1a93687704d",
"md5": "904290d616dc89f71084125a80f45967",
"sha256": "53bff3f25bb2b306d9f31c86c04920570b1fcd5e33109d1d5b61ea6545940ddb"
},
"downloads": -1,
"filename": "jsonQM-0.3.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "904290d616dc89f71084125a80f45967",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 5171,
"upload_time": "2023-06-08T00:42:49",
"upload_time_iso_8601": "2023-06-08T00:42:49.666834Z",
"url": "https://files.pythonhosted.org/packages/85/3e/3e6ff571efa0edb1028c7c7714f5cd48c8ef36551e33bc2ab1a93687704d/jsonQM-0.3.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "2f95e6b256be876a9ad380711221a8381593a65d74424789d59dc943be82b79c",
"md5": "254598a613849843d01c49d2de8179b7",
"sha256": "48b4703bc85059a20bf3edda4ef966e53f605ec61fd92435cbca36c0820c3d57"
},
"downloads": -1,
"filename": "jsonQM-0.3.4.tar.gz",
"has_sig": false,
"md5_digest": "254598a613849843d01c49d2de8179b7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 5247,
"upload_time": "2023-06-08T00:42:51",
"upload_time_iso_8601": "2023-06-08T00:42:51.035732Z",
"url": "https://files.pythonhosted.org/packages/2f/95/e6b256be876a9ad380711221a8381593a65d74424789d59dc943be82b79c/jsonQM-0.3.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-06-08 00:42:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "FrewtyPebbles",
"github_project": "jsonQM",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "jsonqm"
}