I'm looking for some advice on what's the best way of implementing a set of data value only 'interfaces' in Python that are equivalent to their typescript counterpart (we've got a project where we use both, and want to enforce a consistent interface for their communication, which would be via serialising the python into json to pull into the TS component)
The interfaces will be compositions to keep things modular and simple.
Given a set of TS interfaces defined as:
interface TestOutput {
phantom: string
testDateTime: datetime
author: string
result: boolean
report_summaryFile?: string // the '?' means this field is optional
// ... more values
series: Array<Series>
soloImages: Array<Images>
}
interface Series {
number: number
filter: string
kernel: string
// ... more values
images: Array<TestImage>
}
I was thinking of using dataclasses and doing the following:
from dataclasses import dataclass
from typing import List
import datetime
#dataclass
class TestSeries:
seriesNum: int
modality: str
description: str = ''
#dataclass
class TestOutput:
phantom: str
testDateTime: datetime.datetime
author: str
result: bool
series: List[TestSeries]
soloImages: List[Images]
report_summaryFile: str = ''
Is dataclasses the best approach for this?
pydantic is a good library.
I did something similar, but only for dataclasses - ValidatedDC:
from dataclasses import dataclass
from typing import List
from validated_dc import ValidatedDC
import json
#dataclass
class Series(ValidatedDC):
series_num: int
modality: str
description: str = ''
#dataclass
class Output(ValidatedDC):
phantom: str
date_time: str
author: str
result: bool
series: List[Series]
report_summary_file: str = ''
# For example, by API we got a JSON string:
input_json_string = '''
{
"phantom": "test_phantom",
"date_time": "2020.01.01",
"author": "Peter",
"result": true,
"series": [{
"series_num": 1,
"modality": "test_modality"
}]
}
'''
# Load the string into the dictionary:
input_data = json.loads(input_json_string)
# Then create a dataclass to check the types of values and for the
# convenience of further work with data:
output = Output(**input_data)
# Since valid data were obtained, there are no errors
assert output.get_errors() is None
# Let's say we got data with an error:
input_data['series'][0]['series_num'] = '1' # The string is not an integer!
output = Output(**input_data)
assert output.get_errors()
print(output.get_errors())
# {
# 'series': [
# InstanceValidationError(
# value_repr="{'series_num': '1', 'modal...}",
# value_type=<class 'dict'>,
# annotation=<class '__main__.Series'>, exception=None,
# errors={
# 'series_num': [
# BasicValidationError(
# value_repr='1', value_type=<class 'str'>,
# annotation=<class 'int'>, exception=None
# )
# ]
# }
# ),
# ListValidationError(
# item_index=0, item_repr="{'series_num': '1', 'modal...}",
# item_type=<class 'dict'>, annotation=<class '__main__.Series'>
# )
# ]
# }
See here for more details:
https://github.com/EvgeniyBurdin/validated_dc
Related
the following FastAPI code is producing unexpected behaviour to me:
import uvicorn
from fastapi import FastAPI, Depends, Query
from typing import Optional
from pydantic.dataclasses import dataclass
app = FastAPI()
#dataclass
class Catz:
qqq: Optional[str] = Query(None, alias="q")
#app.get("/productz/")
def search_products(query: Catz = Depends(Catz) ):
products = [{"name": "Computer"}, {"name": "HDD"}]
if not query.qqq:
query.qqq = ""
return {"query": query, "results": [product for product in products if query.qqq in product["name"]]}
#dataclass
class Cats:
qqq: Optional[str] = Query(None )
#app.get("/products/")
def search_products(query: Cats = Depends(Cats) ):
products = [{"name": "Computer"}, {"name": "HDD"}]
if not query.qqq:
query.qqq = ""
return {"query": query, "results": [product for product in products if query.qqq in product["name"]]}
if __name__ == "__main__":
uvicorn.run("main:app", port=11978, log_level="info", reload=True)
when I use the service via curl, I get the following outputs:
the expected behaviour with the endpoint /products/ that has no aliases
>> curl -X 'GET' 'http://localhost:11978/products/?qqq=H' -H 'accept: application/json' -H 'api-version: 1.0' ; echo
{"query":{"qqq":"H"},"results":[{"name":"HDD"}]}
not the expected behaviour with the endpoint /productz/ that has no aliases (regardless of me using a query parameter with its own name or with the alias I have in the code)
>> curl -X 'GET' 'http://localhost:11978/productz/?qqq=H' -H 'accept: application/json' -H 'api-version: 1.0' ; echo
{"query":{"qqq":""},"results":[{"name":"Computer"},{"name":"HDD"}]}
>> curl -X 'GET' 'http://localhost:11978/productz/?q=H' -H 'accept: application/json' -H 'api-version: 1.0' ; echo
{"query":{"qqq":""},"results":[{"name":"Computer"},{"name":"HDD"}]}
any idea why that would be?
Do not import dataclass from pydantic.dataclasses - it should be imported from Python's own built-in dataclasses module:
from fastapi import FastAPI, Depends, Query
from fastapi.exceptions import RequestValidationError
from dataclasses import dataclass
from typing import Optional
app = FastAPI()
#dataclass
class Catz:
qqq: Optional[str] = Query(None, alias="q")
#app.get("/productz/")
def search_products(query: Catz = Depends()):
products = [{"name": "Computer"}, {"name": "HDD"}]
if not query.qqq:
query.qqq = ""
return {"query": query, "results": [product for product in products if query]}
Outputs for /productz?q=123:
{"query":{"qqq":"123"},"results":[{"name":"Computer"},{"name":"HDD"}]}
Hello everyone we are running a Falcon application that uses the falcon-apispec library to generate OpenAPI specifications.
Here is our code that initializes the definition:
import falcon
from apispec import APISpec
from falcon_apispec import FalconPlugin
from kubernetes import config
from api.admission_response import AdmissionResponse
from api.health import Health
from api.k8s_config_validator import K8sConfigValidator
from api.middleware.json import RequireJSON, JSONTranslator
from api.apidocs import ApiDocs
def create_app(config_validator):
api = falcon.API(middleware=[
RequireJSON(),
JSONTranslator(),
])
resources ={
'/': AdmissionResponse(config_validator),
'/api-docs': ApiDocs(),
'/health': Health()
}
for r in resources:
api.add_route(r, resources[r])
setup_swagger_documentation(api, resources)
# initialize k8s client
config.load_incluster_config()
return api
def get_app():
return create_app(K8sConfigValidator())
def setup_swagger_documentation(api, resources):
spec = APISpec(
title='Admission Controller API',
version='latest',
openapi_version='2.0',
plugins=[
FalconPlugin(api)
],
info=dict(description="Admission Controller API"),
)
for r in resources:
spec.path(resource=resources[r])
with open('./api/config/openapi/openapi_spec.yaml', 'w') as f:
f.write(spec.to_yaml())
Here is our openapi-spec defintion defined:
openapi: 3.0.0
info:
description: Admission Controller API
title: Admission Controller API
version: latest
paths:
/:
post:
tags:
- "API"
parameters:
- in: "query"
name: "body"
description: "List of user object"
required: true
schema:
type: string
responses:
"200":
description: "Success"
/api-docs:
get:
tags:
- "API Doc Endpoints"
responses:
"200":
description: "Success"
/health:
get:
tags:
- "Health Endpoints"
responses:
"200":
description: "Success"
And here is one of the classes that defines what should be done on a post:
class AdmissionResponse(object):
def __init__(self, k8s_config_validator):
self.k8s_config_validator = k8s_config_validator
#falcon.before(validate_schema)
def on_post(self, req, resp):
"""
---
tags: ['API']
parameters:
- in: "query"
name: "body"
description: "List of user object"
required: true
type: string
responses:
"200":
description: "Success"
"""
admission_review = AdmissionReview(req.context['doc'])
errors = self.k8s_config_validator.validate(admission_review)
if errors:
resp.context['result'] = ResponseBuilder(admission_review).not_allowed(errors)
api.logger.info("Validations for %s of kind %s in %s failed with %s", admission_review.name(), admission_review.kind(), admission_review.namespace(), errors)
else:
resp.context['result'] = ResponseBuilder(admission_review).allowed()
api.logger.info("Validations for %s of kind %s in %s passed", admission_review.name(), admission_review.kind(), admission_review.namespace())
Whenever we try to hit our hosted swagger-ui we run into this error:
Unable to render this definition The provided definition does not specify a valid version field.
Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n (for example, openapi: 3.0.0).
Does anyone know how we could resolve this? When we paste in our openapi specification to the swagger editor located here: https://editor.swagger.io/ it works just fine. Any help would be awesome!
When calling APISpec, not all strings are supported.
Try something like "0.0.1" (you supplied 'latest').
Also, using falcon_swagger_ui, an error may be reported if the openapi_version format is not correct (in such case, examples of valid formats are provided).
This works for me:
spec = APISpec(
title="My APP",
version="0.0.1",
openapi_version='3.0.0',
plugins=[FalconPlugin(api)]
)
redis veersion 3.4.1
must be use hash, can't use str or other data type
data:
{'_anno': {
'ctp': 'list',
'dt': [],
'ml': 0,
'na': 'apple',
'pos': -1,
'rel': '',
'st_var': '',
'tp': 'object'},
'_att': {'_cuser': 'apple card',
'_last_editor': 'apple card',
'_protext': 'authorize',
'_status': 'normal',
'_theme_id': 'apple card',
'_view': '12'},
}
my code
pool = redis.ConnectionPool(host=host, port=port)
conn = redis.StrictRedis(connection_pool=pool)
conn.hmset("aaaaaa",data)
raise error
DataError: Invalid input of type: 'dict'. Convert to a bytes, string,
int or float first.
now code
pool = redis.ConnectionPool(host=host, port=port)
conn = redis.StrictRedis(connection_pool=pool)
new_data={}
for key,value in data.items():
new_data[key]=json.dumps(value)
conn.hmset("aaaaaa",new_data)
Is there a more pythonic way?
The solution for you problem is to use hexdigest() or digest() to convert your dictionary, you can use that:
hashlib.sha256(mdp.encode()).hexdigest()
I can't handle this below.
XMLParseError: Unexpected element 'metadata', expected 'id' error.
I also try strict=False settings but that time zeep returned as none.
from zeep import Client
import zeep
wsdl = 'https://api.n11.com/ws/CategoryService.wsdl'
cl = Client(wsdl=wsdl)
request_data = {"auth":{'appKey' : ***,
'appSecret' : **},'categoryId':int(1003221),"pagingData":{'pageSize':1,'currentPage':0}}
print(cl.service.GetCategoryAttributes(**request_data))
Thanks.
I faced the same problem with the same API. My solution is changing zeep's settings.
You should use xsd_ignore_sequence_order=True.
I hope it's not too late.
settings = Settings(strict=False, xml_huge_tree=True, xsd_ignore_sequence_order=True)
read more:
https://docs.python-zeep.org/en/master/settings.html#module-zeep.settings
Solution:
firstly, to check logs:
import logging.config
logging.config.dictConfig({
'version': 1,
'formatters': {
'verbose': {
'format': '%(name)s: %(message)s'
}
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose',
},
},
'loggers': {
'zeep.transports': {
'level': 'DEBUG',
'propagate': True,
'handlers': ['console'],
},
}
})
Second:
from zeep import Client
from zeep.plugins import HistoryPlugin
wsdl = 'https://api.n11.com/ws/CategoryService.wsdl'
from zeep import Client, Settings
settings = Settings(strict=False, xml_huge_tree=True)
history = HistoryPlugin()
from zeep.transports import Transport
transport = Transport(timeout=10)
key={'appKey' : '**',
'appSecret' : '**',
}
cat= {'categoryId':1003221}
dat= {'pageSize':1,'currentPage':1}
client = Client(wsdl=wsdl, transport=transport, plugins=[history], settings=settings)
with client.settings(raw_response=False,strict=False, xml_huge_tree=True):
response = client.service.GetCategoryAttributes(key,1003221,pagingData=dat)
#assert response.status_code == 200
#assert response.content
#node = client.create_message(client.service, 'GetCategoryAttributes',key,1003221,pagingData=dat)
from lxml import etree
try:
for hist in [history.last_sent, history.last_received]:
print(etree.tostring(hist["envelope"], encoding="unicode", pretty_print=True))
except (IndexError, TypeError):
# catch cases where it fails before being put on the wire
pass
lastly, you can parse with bs4 for xml content:
from bs4 import BeautifulSoup
soup = BeautifulSoup(ass, 'lxml')
soup.find('category').find('metadata')
How do I make a C extension for python 3.x when a module has sub-modules? For example, I have a file called pet.c:
#include <Python.h>
PyObject* CatMeow(PyObject* self) {
return PyUnicode_FromString( ">*<" );
}
static PyMethodDef CatFunctions[] = {
{(char*) "meow", (PyCFunction) CatMeow, METH_NOARGS, NULL},
{NULL, NULL, 0, NULL}
};
static PyModuleDef CatDef = {
PyModuleDef_HEAD_INIT, "cat", "cat ext", -1, CatFunctions,
NULL, NULL, NULL, NULL
};
PyMODINIT_FUNC PyInit_cat(void) {
return PyModule_Create(&CatDef);
}
static PyModuleDef PetDef = {
PyModuleDef_HEAD_INIT, "pet", "pet ext", -1, NULL,
NULL, NULL, NULL, NULL
};
PyMODINIT_FUNC PyInit_pet(void) {
PyObject* p = PyModule_Create(&PetDef);
PyObject* c = PyInit_cat();
Py_INCREF(c);
PyModule_AddObject( p, "cat", c );
return p;
}
When I build it with the following setup.py:
from distutils.core import setup, Extension
setup(
name='pet',
version='0.0',
ext_modules=[Extension('pet', ['pet.c'])]
)
I can see
>>> import pet
>>> pet.cat.meow()
'>*<'
or
>>> from pet import cat
>>> cat.meow()
'>*<'
which is as intended, but when I try
>>> from pet.cat import meow
I have a ModuleNotFoundError saying ... No module named 'pet.cat'; 'pet' is not a package, and if I try
>>> from pet import cat
>>> from cat import meow
I have a ModuleNotFoundError saying ... No module named 'cat'. But if I check the type of cat
>>> type(cat)
<class 'module'>
which says it is a module.
How do I make this work? Adding a module object to another module used to work well in python 2.7. Is it not supposed to work in python3 due to absolute import style? Or do I have to work with multi-phase initialisation as described in PEP 489?
Regarding the first error which complains about pet not being a package. If pet is there only to provide a parent for cat, there is an easy way to turn it into a package: remove all the pet related code from pet.c and use ext_package in setup.py
from distutils.core import setup, Extension
setup(
name = 'pet',
version = '0.0',
ext_package = 'pet',
ext_modules = [Extension('cat', ['pet.c'])]
)
Running the above will create a directory called 'pet' and a shared library of which name starts with 'cat'. This effectively creates a namespace package -- there are two types of packages, regular and namespace, and the latter is the one without requiring __init__.py (see PEP 420 for details). From outside of pet, you can do
>>> from pet import cat
>>> from pet.cat import meow
>>> meow()
'>*<'
>>> cat.meow()
'>*<'
The reason you can't do from cat import meow is because the fully qualified name of the module is 'pet.cat' not 'cat', which you can confirm it from cat.__name__. If you are running the interpreter inside of the directory pet, then you can do from cat import meow.