Skip to content

Commit 0df3d16

Browse files
--
1 parent 7052e18 commit 0df3d16

5 files changed

Lines changed: 52 additions & 4 deletions

File tree

energyml-utils/.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,8 @@ rc/**/*.hdf5
7474
# WIP
7575
src/energyml/utils/wip*
7676
scripts
77-
rc/camunda
77+
rc/camunda
78+
79+
80+
# code profiling
81+
*.prof

energyml-utils/example/attic/parsing_improvement_test.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def reexport_in_memory_par_read(filepath: str, output_folder: Optional[str] = No
3434
suffix = "opti" if is_opti else "std"
3535
if os.environ.get("__ENV__IMPROVEMENT_LXML__", "0") == "1":
3636
suffix += "_lxml"
37+
if os.environ.get("__ENV__IMPROVEMENT__GET_MEMBER__", "0") == "1":
38+
suffix += "_get_member"
3739

3840
path_in_memory = filepath.replace(".epc", f"_parsing_imp_xml_{suffix}.epc")
3941
if output_folder:
@@ -46,17 +48,20 @@ def reexport_in_memory_par_read(filepath: str, output_folder: Optional[str] = No
4648
epc.export_file(path_in_memory, parallel=True)
4749

4850

51+
# ===================================
52+
53+
4954
def time_test(f: callable, **kwargs):
50-
print(f" Testing {f.__name__}...")
55+
print(f" Testing {f.__name__}...")
5156
start = time.perf_counter()
5257
f(**kwargs)
5358
elapsed_inmem = time.perf_counter() - start
5459
# results.append(("In-Memory (Epc)", elapsed_inmem))
55-
print(f" Completed in {elapsed_inmem:.3f}s\n")
60+
print(f" Completed in {elapsed_inmem:.3f}s\n")
5661
return ("In-Memory (Epc)", elapsed_inmem)
5762

5863

59-
if __name__ == "__main__":
64+
if __name__ == "__main__xmlcontext__":
6065
logging.basicConfig(level=logging.DEBUG)
6166

6267
os.environ["__ENV__IMPROVEMENT__"] = "0"
@@ -82,3 +87,26 @@ def time_test(f: callable, **kwargs):
8287
filepath=sys.argv[1] if len(sys.argv) > 1 else "rc/epc/80wells_surf.epc",
8388
output_folder="results",
8489
)
90+
91+
if __name__ == "__main__":
92+
from energyml.resqml.v2_2.resqmlv2 import TriangulatedSetRepresentation
93+
94+
print(TriangulatedSetRepresentation.__class__.__module__)
95+
print(TriangulatedSetRepresentation.__dataclass_fields__.keys())
96+
97+
# logging.basicConfig(level=logging.DEBUG)
98+
99+
# os.environ["__ENV__IMPROVEMENT__GET_MEMBER__"] = "0"
100+
101+
time_test(
102+
reexport_in_memory_par_read,
103+
filepath=sys.argv[1] if len(sys.argv) > 1 else "rc/epc/80wells_surf.epc",
104+
output_folder="results",
105+
)
106+
107+
# os.environ["__ENV__IMPROVEMENT__GET_MEMBER__"] = "1"
108+
# time_test(
109+
# reexport_in_memory_par_read,
110+
# filepath=sys.argv[1] if len(sys.argv) > 1 else "rc/epc/80wells_surf.epc",
111+
# output_folder="results",
112+
# )

energyml-utils/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ black = "^22.3.0"
8383
pylint = "^2.7.2"
8484
click = ">=8.1.3, <=8.1.3" # upper version than 8.0.2 fail with black
8585
pdoc3 = "^0.10.0"
86+
snakeviz = "^2.1.0" # code perf tests
8687
pydantic = { version = "^2.0"}
8788
energyml-common2-0 = "^1.12.0"
8889
energyml-common2-1 = "^1.12.0"

energyml-utils/src/energyml/utils/constants.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,21 @@ def file_extension_to_mime_type(extension: str) -> Optional[str]:
415415
# OPTIMIZED UTILITY FUNCTIONS
416416
# ===================================
417417

418+
_SNAKE_CASE_PATTERNS = [
419+
(re.compile(r"(.)([A-Z][a-z]+)"), r"\1_\2"),
420+
(re.compile(r"__([A-Z])"), r"_\1"),
421+
(re.compile(r"([a-z0-9])([A-Z])"), r"\1_\2"),
422+
]
423+
418424

419425
def snake_case(string: str) -> str:
426+
"""Transform a string into snake_case (optimized with pre-compiled regexes)"""
427+
for pattern, repl in _SNAKE_CASE_PATTERNS:
428+
string = pattern.sub(repl, string)
429+
return string.lower()
430+
431+
432+
def snake_case_old(string: str) -> str:
420433
"""Transform a string into snake_case"""
421434
string = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", string)
422435
string = re.sub("__([A-Z])", r"_\1", string)

energyml-utils/src/energyml/utils/introspection.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) 2023-2024 Geosiris.
22
# SPDX-License-Identifier: Apache-2.0
3+
from functools import lru_cache
34
import inspect
45
import json
56
import logging
@@ -85,6 +86,7 @@ def get_module_classes_from_name(mod_name: str) -> List:
8586
return get_module_classes(sys.modules[mod_name])
8687

8788

89+
@lru_cache(maxsize=None)
8890
def get_module_classes(mod: ModuleType) -> List:
8991
return inspect.getmembers(mod, inspect.isclass)
9092

0 commit comments

Comments
 (0)