Skip to content

Commit 035b719

Browse files
--
1 parent 3d97700 commit 035b719

4 files changed

Lines changed: 44 additions & 24 deletions

File tree

energyml-utils/example/attic/arrays_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from energyml.utils.storage_interface import EnergymlStorageInterface
1818
from energyml.utils.epc import Epc
1919
from energyml.utils.epc_stream import EpcStreamReader, RelsUpdateMode
20-
from energyml.utils.introspection import get_obj_title
20+
from energyml.utils.introspection import get_obj_title, search_attribute_matching_name, get_object_attribute
2121
from energyml.resqml.v2_2.resqmlv2 import Point3DLatticeArray
2222
from energyml.eml.v2_3.commonv2 import TimeSeries
2323
from energyml.eml.v2_1.commonv2 import TimeSeries as TimeSeries21
@@ -299,6 +299,8 @@ def read_props_and_cbt(
299299
_return_none_if_no_category_lookup=True,
300300
)
301301
print("=" * 40)
302+
# print("TS: ", search_attribute_matching_name(prop_or_cbt, "\\w*.time_series"))
303+
# print(f"\t {get_object_attribute(prop_or_cbt, 'time_or_interval_series.time_series')}")
302304
print(f"{type(prop_or_cbt)} : {get_obj_title(prop_or_cbt)} - uuid: {uuid}")
303305
print(array)
304306

energyml-utils/src/energyml/utils/data/helper.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -759,19 +759,17 @@ def _array_name_mapping(array_type_name: str) -> str:
759759
:param array_type_name:
760760
:return:
761761
"""
762-
array_type_name = array_type_name.replace("3D", "3d").replace("2D", "2d").lower()
763-
# logging.debug(f"=============> Mapping array type name '{array_type_name}' to reader function name...")
764-
if array_type_name.endswith("constantarray"):
762+
array_type_name = array_type_name.replace("3D", "3d").replace("2D", "2d")
763+
if array_type_name.endswith("ConstantArray"):
765764
return "ConstantArray"
766-
elif "external" in array_type_name or "hdf5" in array_type_name:
765+
elif "External" in array_type_name or "Hdf5" in array_type_name:
767766
return "ExternalArray"
768-
elif "xml" in array_type_name:
769-
# logging.debug("=============> XML array detected, be careful with the performance !")
767+
elif "Xml" in array_type_name:
770768
return "XmlArray"
771-
elif "jagged" in array_type_name:
769+
elif "Jagged" in array_type_name:
772770
return "JaggedArray"
773-
elif "lattice" in array_type_name:
774-
if "integer" in array_type_name or "double" in array_type_name or "floatingpoint" in array_type_name:
771+
elif "Lattice" in array_type_name:
772+
if "Integer" in array_type_name or "Double" in array_type_name or "Floating" in array_type_name:
775773
return "int_double_lattice_array"
776774
return array_type_name
777775

energyml-utils/src/energyml/utils/data/mesh.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ def read_property_interpreted_with_cbt(
10721072
elif isinstance(category_lookup_data, dict):
10731073
# Transpose so that each index corresponds to a category (column), not a row
10741074
category_lookup_matrice = np.array(list(category_lookup_data.values())).T
1075-
print(f"category_lookup_matrice : {category_lookup_matrice}")
1075+
# logging.debug(f"category_lookup_matrice : {category_lookup_matrice}")
10761076
# return a matrice with the same shape as prop_arrays but with the values from the category lookup array using the prop value as key in the category lookup array
10771077
result = (
10781078
np.array(
@@ -1251,7 +1251,7 @@ def read_column_based_table(
12511251
def read_time_series(
12521252
energyml_object: Any,
12531253
workspace: EnergymlStorageInterface,
1254-
) -> List[Dict[str, Tuple[str, int]]]:
1254+
) -> List[Tuple[str, int]]:
12551255
"""
12561256
Read a time series from an Energyml object.
12571257

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -387,24 +387,26 @@ def get_class_attribute_type(cls: Union[type, Any], attribute_name: str):
387387
return None
388388

389389

390-
def get_matching_class_attribute_name(
390+
def get_all_matching_class_attribute_name(
391391
cls: Union[type, Any],
392392
attribute_name: str,
393393
re_flags=re.IGNORECASE,
394-
) -> Optional[str]:
394+
) -> List[str]:
395395
"""
396-
From an object and an attribute name, returns the correct attribute name of the class.
397-
Example : "ObjectVersion" --> object_version.
398-
This method doesn't only transform to snake case but search into the obj class attributes (or dict keys)
396+
From an object and an attribute name, returns all the correct attribute names of the class matching with the attribute_name.
397+
Example : "\\w*.Version" --> ["object_version", "ObjectVersion", "obj_version", ...]
398+
This method doesn't only transform to snake case but search into the obj class attributes (or dict keys)
399399
"""
400+
matching_names = []
400401
if isinstance(cls, dict):
401402
for name in cls.keys():
402403
if snake_case(name) == snake_case(attribute_name):
403-
return name
404+
matching_names.append(name)
404405
pattern = re.compile(attribute_name, flags=re_flags)
405406
for name in cls.keys():
406407
if pattern.match(name):
407-
return name
408+
matching_names.append(name)
409+
return matching_names
408410
else:
409411
class_fields = get_class_fields(cls)
410412
try:
@@ -413,7 +415,7 @@ def get_matching_class_attribute_name(
413415
if snake_case(name) == snake_case(attribute_name) or (
414416
hasattr(cf, "metadata") and "name" in cf.metadata and cf.metadata["name"] == attribute_name
415417
):
416-
return name
418+
matching_names.append(name)
417419

418420
# search regex after to avoid shadowing perfect match
419421
pattern = re.compile(attribute_name, flags=re_flags)
@@ -422,11 +424,27 @@ def get_matching_class_attribute_name(
422424
if pattern.match(name) or (
423425
hasattr(cf, "metadata") and "name" in cf.metadata and pattern.match(cf.metadata["name"])
424426
):
425-
return name
427+
matching_names.append(name)
426428
except Exception as e:
427429
logging.error(f"Failed to get attribute {attribute_name} from class {cls}")
428430
logging.error(e)
429431

432+
return matching_names
433+
434+
435+
def get_matching_class_attribute_name(
436+
cls: Union[type, Any],
437+
attribute_name: str,
438+
re_flags=re.IGNORECASE,
439+
) -> Optional[str]:
440+
"""
441+
From an object and an attribute name, returns the correct attribute name of the class.
442+
Example : "ObjectVersion" --> object_version.
443+
This method doesn't only transform to snake case but search into the obj class attributes (or dict keys)
444+
"""
445+
matched = get_all_matching_class_attribute_name(cls, attribute_name, re_flags)
446+
if len(matched) > 0:
447+
return matched[0]
430448
return None
431449

432450

@@ -931,22 +949,24 @@ def search_attribute_matching_name_with_path(
931949
else:
932950
not_match_path_and_obj.append((f"{current_path}{k}", s_o))
933951
elif not is_primitive(obj):
934-
match_value = get_matching_class_attribute_name(obj, current_match.replace("\\.", "."))
935-
if match_value is not None:
952+
# logging.debug(f"searching {current_match} in {type(obj)} with path {current_path} and next match {next_match}")
953+
match_values = get_all_matching_class_attribute_name(obj, current_match, re_flags)
954+
for match_value in match_values:
936955
match_path_and_obj.append(
937956
(
938957
f"{current_path}{match_value}",
939958
get_object_attribute_no_verif(obj, match_value),
940959
)
941960
)
942961
for att_name in get_class_attributes(obj):
943-
if att_name != match_value:
962+
if att_name not in match_values:
944963
not_match_path_and_obj.append(
945964
(
946965
f"{current_path}{att_name}",
947966
get_object_attribute_no_verif(obj, att_name),
948967
)
949968
)
969+
# logging.debug(f"\tmatch_path_and_obj: {match_path_and_obj}")
950970

951971
for matched_path, matched in match_path_and_obj:
952972
if next_match is not None: # next_match is different, match is not final

0 commit comments

Comments
 (0)