Skip to content

Commit 5e44242

Browse files
committed
31 | Switch to c++
1 parent 29797f6 commit 5e44242

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM python:3.7-slim
22

33
WORKDIR /app
44

5-
RUN apt-get update && apt-get install -y gcc python3-dev git
5+
RUN apt-get update && apt-get install -y gcc g++ python3-dev git
66

77
COPY pyproject.toml /app
88

env_configs/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ RUN apt-get update && apt-get install -y \
66
build-essential \
77
curl \
88
gcc \
9+
g++ \
910
git \
1011
libbz2-dev \
1112
libffi-dev \

flask_inputfilter/InputFilter.pyx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
# cython: language_level=3, cython: binding=True
1+
# cython: language=c++
2+
# cython: language_level=3
3+
# cython: binding=True
4+
# cython: cdivision=True
5+
# cython: boundscheck=False
6+
# cython: initializedcheck=False
27
from __future__ import annotations
38

49
import json
510
import logging
6-
import re
711
from typing import Any, Dict, List, Optional, Tuple, Type, TypeVar, Union
812

913
from flask import Response, g, request
1014

1115
from flask_inputfilter.Condition import BaseCondition
1216
from flask_inputfilter.Exception import ValidationError
1317
from flask_inputfilter.Filter import BaseFilter
18+
from flask_inputfilter.Mixin import ExternalApiMixin
1419
from flask_inputfilter.Model import ExternalApiConfig, FieldModel
1520
from flask_inputfilter.Validator import BaseValidator
16-
from flask_inputfilter.Mixin import ExternalApiMixin
1721

1822
T = TypeVar("T")
1923

@@ -199,7 +203,7 @@ cdef class InputFilter:
199203
value = self.validated_data.get(copy)
200204

201205
if external_api:
202-
value = ExternalApiMixin.callExternalApi(
206+
value = ExternalApiMixin().callExternalApi(
203207
external_api, fallback, self.validated_data
204208
)
205209

flask_inputfilter/Mixin/ExternalApiMixin.pyx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1+
# cython: language=c++
2+
# cython: language_level=3
3+
# cython: binding=True
4+
# cython: cdivision=True
5+
# cython: boundscheck=False
6+
# cython: initializedcheck=False
7+
import re
8+
from typing import Any, Dict, Optional
9+
10+
from flask_inputfilter.Exception import ValidationError
11+
from flask_inputfilter.Model import ExternalApiConfig
12+
13+
114
cdef class ExternalApiMixin:
215

3-
@staticmethod
4-
cdef Optional[object] callExternalApi(
5-
config: ExternalApiConfig, fallback: Any, validated_data: Dict[str, Any]
16+
cpdef Optional[object] callExternalApi(
17+
self, config: ExternalApiConfig, fallback: Any, validated_data: Dict[str, Any]
618
):
719
"""
820
Makes a call to an external API using provided configuration and
@@ -59,11 +71,11 @@ cdef class ExternalApiMixin:
5971
requestData["headers"].update(config.headers)
6072

6173
if config.params:
62-
requestData["params"] = InputFilter.replacePlaceholdersInParams(
74+
requestData["params"] = ExternalApiMixin.replacePlaceholdersInParams(
6375
config.params, validated_data
6476
)
6577

66-
requestData["url"] = InputFilter.replacePlaceholders(
78+
requestData["url"] = ExternalApiMixin.replacePlaceholders(
6779
config.url, validated_data
6880
)
6981
requestData["method"] = config.method
@@ -142,7 +154,7 @@ cdef class ExternalApiMixin:
142154
with the corresponding values from validated_data.
143155
"""
144156
return {
145-
key: InputFilter.replacePlaceholders(value, validated_data)
157+
key: ExternalApiMixin.replacePlaceholders(value, validated_data)
146158
if isinstance(value, str)
147159
else value
148160
for key, value in params.items()

setup.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
import os
2+
13
from Cython.Build import cythonize
24
from setuptools import setup
35

6+
os.environ["CC"] = "g++"
7+
os.environ["CXX"] = "g++"
8+
49
setup(
510
ext_modules=cythonize(
6-
["flask_inputfilter/InputFilter.pyx"], language_level=3
11+
[
12+
"flask_inputfilter/Mixin/ExternalApiMixin.pyx",
13+
"flask_inputfilter/InputFilter.pyx",
14+
],
15+
language_level=3,
716
),
817
)

0 commit comments

Comments
 (0)