From 37796548fb1131d6549d68f292f3b6c888b305df Mon Sep 17 00:00:00 2001 From: yinjiaqi Date: Wed, 11 Dec 2024 19:44:48 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=9B=B4=E6=96=B0model=5Fname=E6=8A=BD?= =?UTF-8?q?=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/utils/json_schema_to_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/utils/json_schema_to_model.py b/python/utils/json_schema_to_model.py index 2056bdfa..9ce2c393 100644 --- a/python/utils/json_schema_to_model.py +++ b/python/utils/json_schema_to_model.py @@ -97,7 +97,7 @@ def json_schema_to_pydantic_model(json_schema: dict, name_override: str) -> Base with _delete_file_on_completion(file_path=temp_file_path): module = _load_module_from_file(file_path=temp_file_path) - main_model_name = _to_camel_case(name=class_title) + main_model_name = pydantic_models_as_str.split('class ')[-1].split('(BaseModel)')[0] pydantic_model: BaseModel = module.__dict__[main_model_name] # Override the pydantic model/parser name for nicer ValidationError messaging and logging pydantic_model.__name__ = name_override From 9e60cd45b30550d31c18fcf1519bc1ca284ba0a0 Mon Sep 17 00:00:00 2001 From: yinjiaqi Date: Wed, 11 Dec 2024 20:04:34 +0800 Subject: [PATCH 2/5] update --- python/utils/json_schema_to_model.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/python/utils/json_schema_to_model.py b/python/utils/json_schema_to_model.py index 9ce2c393..439064f8 100644 --- a/python/utils/json_schema_to_model.py +++ b/python/utils/json_schema_to_model.py @@ -22,7 +22,14 @@ class BadJsonSchema(Exception): def _to_camel_case(name: str) -> str: if any(NON_ALPHANUMERIC.finditer(name)): - return "".join(term.lower().title() for term in NON_ALPHANUMERIC.split(name)) + terms = NON_ALPHANUMERIC.split(name) + new_terms = [terms[0].capitalize()] + for term in terms[1:]: + if term and term[0].isdigit() and len(term) > 1 and term[1].islower(): + new_terms.append(term[0] + term[1:] if new_terms[-1][-1].isdigit() else term[0].lower() + term[1:]) + else: + new_terms.append(term.capitalize()) + return "".join(new_terms) if UPPER_CAMEL_CASE.match(name): return name if LOWER_CAMEL_CASE.match(name): @@ -97,7 +104,7 @@ def json_schema_to_pydantic_model(json_schema: dict, name_override: str) -> Base with _delete_file_on_completion(file_path=temp_file_path): module = _load_module_from_file(file_path=temp_file_path) - main_model_name = pydantic_models_as_str.split('class ')[-1].split('(BaseModel)')[0] + main_model_name = _to_camel_case(name=class_title) pydantic_model: BaseModel = module.__dict__[main_model_name] # Override the pydantic model/parser name for nicer ValidationError messaging and logging pydantic_model.__name__ = name_override From 08d861ff93f676de056a920145c5a99e407dd509 Mon Sep 17 00:00:00 2001 From: yinjiaqi Date: Wed, 11 Dec 2024 20:27:52 +0800 Subject: [PATCH 3/5] update --- python/tests/test_json_schema_to_model.py | 27 +++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 python/tests/test_json_schema_to_model.py diff --git a/python/tests/test_json_schema_to_model.py b/python/tests/test_json_schema_to_model.py new file mode 100644 index 00000000..3276d77c --- /dev/null +++ b/python/tests/test_json_schema_to_model.py @@ -0,0 +1,27 @@ +# Copyright (c) 2024 Baidu, Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import os +import unittest + +from appbuilder.utils.json_schema_to_model import _to_camel_case as to_camel_case + +@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_PARALLEL", "") +class TestJsonSchemaToModel(unittest.TestCase): + def test_to_camel_case(self): + model_name = to_camel_case("image_to_3d_vast") + print(model_name) + assert model_name == "ImageTo3dVast" + +if __name__ == '__main__': + unittest.main() From a08b99decb32fc31f852c373f061eba56c90d8e2 Mon Sep 17 00:00:00 2001 From: yinjiaqi Date: Thu, 12 Dec 2024 10:53:17 +0800 Subject: [PATCH 4/5] update --- python/tests/test_json_schema_to_model.py | 27 ----------------------- python/utils/json_schema_to_model.py | 12 +++------- 2 files changed, 3 insertions(+), 36 deletions(-) delete mode 100644 python/tests/test_json_schema_to_model.py diff --git a/python/tests/test_json_schema_to_model.py b/python/tests/test_json_schema_to_model.py deleted file mode 100644 index 3276d77c..00000000 --- a/python/tests/test_json_schema_to_model.py +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright (c) 2024 Baidu, Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -import os -import unittest - -from appbuilder.utils.json_schema_to_model import _to_camel_case as to_camel_case - -@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_PARALLEL", "") -class TestJsonSchemaToModel(unittest.TestCase): - def test_to_camel_case(self): - model_name = to_camel_case("image_to_3d_vast") - print(model_name) - assert model_name == "ImageTo3dVast" - -if __name__ == '__main__': - unittest.main() diff --git a/python/utils/json_schema_to_model.py b/python/utils/json_schema_to_model.py index 439064f8..db9c96c2 100644 --- a/python/utils/json_schema_to_model.py +++ b/python/utils/json_schema_to_model.py @@ -13,6 +13,7 @@ NON_ALPHANUMERIC = re.compile(r"[^a-zA-Z0-9]+") +STARTS_WITH_NUMBER = re.compile(r'[0-9]+') UPPER_CAMEL_CASE = re.compile(r"[A-Z][a-zA-Z0-9]+") LOWER_CAMEL_CASE = re.compile(r"[a-z][a-zA-Z0-9]+") @@ -22,14 +23,7 @@ class BadJsonSchema(Exception): def _to_camel_case(name: str) -> str: if any(NON_ALPHANUMERIC.finditer(name)): - terms = NON_ALPHANUMERIC.split(name) - new_terms = [terms[0].capitalize()] - for term in terms[1:]: - if term and term[0].isdigit() and len(term) > 1 and term[1].islower(): - new_terms.append(term[0] + term[1:] if new_terms[-1][-1].isdigit() else term[0].lower() + term[1:]) - else: - new_terms.append(term.capitalize()) - return "".join(new_terms) + return "".join(term.lower().title() if not STARTS_WITH_NUMBER.match(term) else term.lower() for term in NON_ALPHANUMERIC.split(name)) if UPPER_CAMEL_CASE.match(name): return name if LOWER_CAMEL_CASE.match(name): @@ -155,4 +149,4 @@ def json_schema_to_pydantic_model(json_schema: dict, name_override: str) -> Base schema['title'] = "general_ocr" model = json_schema_to_pydantic_model(json_schema=schema, name_override="GeneralOcr") print(model) - print(model.schema_json()) \ No newline at end of file + print(model.__dict__) \ No newline at end of file From e2b7832952366ed93980c9fb00752b7328059bbd Mon Sep 17 00:00:00 2001 From: yinjiaqi Date: Thu, 12 Dec 2024 10:54:25 +0800 Subject: [PATCH 5/5] update --- python/utils/json_schema_to_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/utils/json_schema_to_model.py b/python/utils/json_schema_to_model.py index db9c96c2..02a79c9b 100644 --- a/python/utils/json_schema_to_model.py +++ b/python/utils/json_schema_to_model.py @@ -149,4 +149,4 @@ def json_schema_to_pydantic_model(json_schema: dict, name_override: str) -> Base schema['title'] = "general_ocr" model = json_schema_to_pydantic_model(json_schema=schema, name_override="GeneralOcr") print(model) - print(model.__dict__) \ No newline at end of file + print(model.schema_json()) \ No newline at end of file