Skip to content

Commit 74b2218

Browse files
authored
85 ant 1 (#87)
* #85 - Continued improvements * #85 - Continued improvements * #85 - Continued improvements * #85 - Continued improvements * #85 - Continued improvements * #85 - Continued improvements * #85 - Continued improvements * #85 - Continued improvements * #85 - Continued improvements * #85 - Continued improvements * #85 * #85 * #85 * #85 - Continued improvements * #85 * #85 * #85 - Continued improvements * #85 - Continued initial * Update _helpers.py #85 * Update _helpers.py #85 * #85 - Fix for 3.11 * #85 - Fix issues
1 parent bd05786 commit 74b2218

File tree

8 files changed

+97
-49
lines changed

8 files changed

+97
-49
lines changed

examples/szconfig/add_data_source.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from senzing import SzConfig, SzError
55

6-
DATA_SOURCE_CODE = "TEST2"
6+
DATA_SOURCE_CODE = "test2"
77
INSTANCE_NAME = "Example"
88
settings = {
99
"PIPELINE": {

src/senzing/_helpers.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@
3131
from types import TracebackType
3232
from typing import Any, Callable, Dict, List, Optional, Type, TypeVar, Union
3333

34-
from senzing_abstract.szerror import _ENGINE_EXCEPTION_MAP
34+
from senzing_abstract.szerror import ENGINE_EXCEPTION_MAP
3535

3636
from senzing import SzError
3737

38-
if sys.version_info < (3, 10):
39-
from typing_extensions import ParamSpec
38+
# if sys.version_info < (3, 10):
39+
if sys.version_info < (3, 11):
40+
from typing_extensions import ParamSpec, Self
4041
else:
41-
from typing import ParamSpec
42+
from typing import ParamSpec, Self
43+
44+
# TODO Add metadata, should use __all__ ?
4245

4346
T = TypeVar("T")
4447
P = ParamSpec("P")
@@ -64,8 +67,8 @@ def __init__(self, handle: CDLL, resource: _Pointer[c_char]) -> None:
6467
self.handle = handle
6568
self.resource = resource
6669

67-
def __enter__(self) -> None:
68-
pass
70+
def __enter__(self) -> Self:
71+
return self
6972

7073
def __exit__(
7174
self,
@@ -155,9 +158,10 @@ def load_sz_library(lib: str = "") -> CDLL:
155158
# TODO Change to Sz library when the libG2.so is changed in a build
156159
# TODO Wording & links for V4
157160
print(
158-
"ERROR: Unable to load Senzing library. Did you remember to setup your environment by sourcing the setupEnv file?\n"
159-
"ERROR: For more information see https://senzing.zendesk.com/hc/en-us/articles/115002408867-Introduction-G2-Quickstart\n"
160-
"ERROR: If you are running Ubuntu or Debian please also review the ssl and crypto information at https://senzing.zendesk.com/hc/en-us/articles/115010259947-System-Requirements\n",
161+
f"ERROR: Unable to load the Senzing library: {err}\n"
162+
"ERROR: Did you remember to setup your environment by sourcing the setupEnv file?\n"
163+
"ERROR: For more information: https://senzing.zendesk.com/hc/en-us/articles/115002408867-Introduction-G2-Quickstart\n"
164+
"ERROR: If you are running Ubuntu or Debian also review the ssl and crypto information at https://senzing.zendesk.com/hc/en-us/articles/115010259947-System-Requirements\n",
161165
)
162166
raise sdk_exception(1) from err
163167

@@ -235,6 +239,7 @@ def check_list_types(to_check: List[Any]) -> None:
235239
)
236240

237241

242+
# TODO - Ant - And Jira
238243
def escape_json_str(to_escape: str) -> str:
239244
"""
240245
Escape strings when building a new JSON string.
@@ -245,6 +250,9 @@ def escape_json_str(to_escape: str) -> str:
245250
raise TypeError(f"expected a str, got{to_escape}")
246251
# TODO ensure_ascii=False = èAnt\\n👍
247252
# TODO =True = \\u00e8Ant\\n\\ud83d\\udc4d'
253+
print(f"{to_escape = }")
254+
jdumps = json.dumps({"escaped": to_escape}["escaped"])
255+
print(f"{jdumps = }")
248256
return json.dumps({"escaped": to_escape}["escaped"])
249257

250258

@@ -440,7 +448,7 @@ def engine_exception(
440448
"""
441449
sz_error_code = get_last_exception_code()
442450
sz_error_text = get_senzing_error_text(get_last_exception, clear_last_exception)
443-
senzing_error_class = _ENGINE_EXCEPTION_MAP.get(sz_error_code, SzError)
451+
senzing_error_class = ENGINE_EXCEPTION_MAP.get(sz_error_code, SzError)
444452
return senzing_error_class(sz_error_text)
445453

446454

src/senzing/szdiagnostic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ def get_datastore_info(self, **kwargs: Any) -> str:
235235
return as_python_str(result.response)
236236

237237
# NOTE This is included but not to be documented
238+
# NOTE Is used by sz_explorer
238239
def get_feature(self, feature_id: int, **kwargs: Any) -> str:
239240
result = self.library_handle.G2Diagnostic_getFeature_helper(feature_id)
240241
with FreeCResources(self.library_handle, result.response):

src/senzing/szengine.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ def __init__(
595595
)
596596
self.initialized = True
597597

598+
# TODO Add garbage collection to ensure destruction
598599
def __del__(self) -> None:
599600
"""Destructor"""
600601
if self.initialized:
@@ -749,6 +750,7 @@ def find_network_by_entity_id(
749750
build_out_max_entities,
750751
flags,
751752
)
753+
752754
with FreeCResources(self.library_handle, result.response):
753755
self.check_result(result.return_code)
754756
return as_python_str(result.response)

src/senzing/szproduct.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
catch_exceptions,
3131
check_result_rc,
3232
load_sz_library,
33-
sdk_exception,
3433
)
3534

3635
# Metadata
@@ -104,7 +103,9 @@ class SzProduct(SzProductAbstract):
104103
def __init__(
105104
self,
106105
instance_name: str = "",
107-
settings: Union[str, Dict[Any, Any]] = "",
106+
# TODO
107+
# settings: Union[str, Dict[Any, Any]] = "",
108+
settings: Union[str, Dict[Any, Any]] = "{}",
108109
verbose_logging: int = 0,
109110
**kwargs: Any,
110111
) -> None:
@@ -144,8 +145,9 @@ def __init__(
144145
self.library_handle.G2GoHelper_free.argtypes = [c_char_p]
145146

146147
# NOTE both get_license and get_version will work if "", "{}" are passed in
147-
if not self.instance_name or len(self.settings) == 0:
148-
raise sdk_exception(2)
148+
# TODO
149+
# if not self.instance_name or len(self.settings) == 0:
150+
# raise sdk_exception(2)
149151

150152
# Initialize Senzing engine.
151153
self._initialize(self.instance_name, self.settings, self.verbose_logging)

src/senzing_abstract/szengineflags.py

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"""
44

55
from enum import IntFlag
6-
from typing import Any, List
6+
from typing import List, Union
7+
8+
from senzing_abstract.szerror import SzError
79

810
# Metadata
911

@@ -18,25 +20,57 @@
1820
# -----------------------------------------------------------------------------
1921

2022

23+
# TODO - Ant - Flag instead? https://realpython.com/python-enum/#exploring-other-enumeration-classes
2124
class SzEngineFlags(IntFlag):
22-
"""Engine Flags ..."""
25+
"""Engine Flags"""
2326

2427
@classmethod
25-
def combine_flags(
26-
cls, list_of_strings: List[str], *args: Any, **kwargs: Any
27-
) -> int:
28-
"""OR together all strings in list_of_strings"""
29-
# pylint: disable=unused-argument
28+
def combine_flags(cls, flags: Union[List[IntFlag], List[str]]) -> int:
29+
"""
30+
The `combine_flags` method ORs together all flags in a list of strings.
31+
32+
Args:
33+
flags (List[str]): A list of strings each representing an engine flag.
34+
35+
Returns:
36+
int: Value of ORing flags together.
37+
38+
Raises:
39+
40+
.. collapse:: Example:
41+
42+
.. literalinclude:: ../../examples/misc/engine_flags_combine_flags.py
43+
:linenos:
44+
:language: python
45+
46+
**Output:**
3047
48+
.. literalinclude:: ../../examples/misc/engine_flags_combine_flags.txt
49+
:linenos:
50+
:language: json
51+
"""
3152
result = 0
32-
for string in list_of_strings:
33-
result = result | SzEngineFlags[string]
53+
try:
54+
for flag in flags:
55+
if isinstance(flag, str):
56+
result = result | cls[flag.upper()]
57+
else:
58+
result = result | flag
59+
except (AttributeError, KeyError) as err:
60+
raise SzError(f"{err} is not a valid engine flag") from err
3461
return result
3562

3663
@classmethod
37-
def get_flag_int(cls, flag: IntFlag) -> int:
64+
# TODO - Ant - Correct type?
65+
def get_flag_int(cls, flag: Union[IntFlag, str]) -> int:
3866
"""# TODO"""
39-
return flag.value
67+
try:
68+
if isinstance(flag, str):
69+
flag = cls[flag.upper()]
70+
flag_int = flag.value
71+
except (AttributeError, KeyError) as err:
72+
raise SzError(f"{err} is not a valid engine flag") from err
73+
return flag_int
4074

4175
# Flags for exporting entity data.
4276

src/senzing_abstract/szerror.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# Metadata
1515

1616
__all__ = [
17-
"_ENGINE_EXCEPTION_MAP",
17+
"ENGINE_EXCEPTION_MAP",
1818
"SzBadInputError",
1919
"SzConfigurationError",
2020
"SzDatabaseConnectionLostError",
@@ -130,7 +130,7 @@ class SzUnhandledError(SzUnrecoverableError):
130130
# -----------------------------------------------------------------------------
131131

132132
# fmt: off
133-
_ENGINE_EXCEPTION_MAP = {
133+
ENGINE_EXCEPTION_MAP = {
134134
2: SzBadInputError, # EAS_ERR_INVALID_MESSAGE "Invalid Message"
135135
5: SzError, # EAS_ERR_EXCEEDED_MAX_RETRIES "Exceeded the Maximum Number of Retries Allowed"
136136
7: SzBadInputError, # EAS_ERR_EMPTY_MESSAGE "Empty Message"

tests/szproduct_test.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55
from pytest_schema import Regex, schema
66

7-
from senzing import SzError, SzProduct
7+
from senzing import SzProduct
88

99
# -----------------------------------------------------------------------------
1010
# SzProduct testcases
@@ -26,26 +26,27 @@ def test_constructor(engine_vars: Dict[Any, Any]) -> None:
2626
assert isinstance(actual, SzProduct)
2727

2828

29-
def test_constructor_bad_instance_name(engine_vars: Dict[Any, Any]) -> None:
30-
"""Test constructor."""
31-
bad_instance_name = ""
32-
with pytest.raises(SzError):
33-
actual = SzProduct(
34-
bad_instance_name,
35-
engine_vars["SETTINGS"],
36-
)
37-
assert isinstance(actual, SzProduct)
38-
39-
40-
def test_constructor_bad_settings(engine_vars: Dict[Any, Any]) -> None:
41-
"""Test constructor."""
42-
bad_settings = ""
43-
with pytest.raises(SzError):
44-
actual = SzProduct(
45-
engine_vars["INSTANCE_NAME"],
46-
bad_settings,
47-
)
48-
assert isinstance(actual, SzProduct)
29+
# NOTE szproduct can be initialized without an instance name
30+
# def test_constructor_bad_instance_name(engine_vars: Dict[Any, Any]) -> None:
31+
# """Test constructor."""
32+
# bad_instance_name = ""
33+
# with pytest.raises(SzError):
34+
# actual = SzProduct(
35+
# bad_instance_name,
36+
# engine_vars["SETTINGS"],
37+
# )
38+
# assert isinstance(actual, SzProduct)
39+
40+
# NOTE szproduct can be initialized without settings
41+
# def test_constructor_bad_settings(engine_vars: Dict[Any, Any]) -> None:
42+
# """Test constructor."""
43+
# bad_settings = ""
44+
# with pytest.raises(SzError):
45+
# actual = SzProduct(
46+
# engine_vars["INSTANCE_NAME"],
47+
# bad_settings,
48+
# )
49+
# assert isinstance(actual, SzProduct)
4950

5051

5152
def test_double_destroy(engine_vars: Dict[Any, Any]) -> None:

0 commit comments

Comments
 (0)