Skip to content

Commit 3c23854

Browse files
authored
#52 - Additional Python snippets (#64)
1 parent ea18501 commit 3c23854

13 files changed

+79
-18
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog], [markdownlint],
66
and this project adheres to [Semantic Versioning].
77

8+
## [0.0.10] - 2025-08-11
9+
10+
### Added in 0.0.10
11+
12+
- New Python snippets
13+
814
## [0.0.9] - 2025-07-23
915

1016
### Changed in 0.0.9

python/deleting/delete_futures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def futures_del(engine, input_file):
6464

6565
del futures[f]
6666

67-
print(f"\nSuccessfully deleted {success_recs:,} records, with" f" {error_recs:,} errors")
67+
print(f"\nSuccessfully deleted {success_recs:,} records, with {error_recs:,} errors")
6868

6969

7070
try:

python/deleting/delete_with_info_futures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def futures_del(engine, input_file, output_file):
7474

7575
del futures[f]
7676

77-
print(f"\nSuccessfully deleted {success_recs:,} records, with" f" {error_recs:,} errors")
77+
print(f"\nSuccessfully deleted {success_recs:,} records, with {error_recs:,} errors")
7878
print(f"\nWith info responses written to {output_file}")
7979

8080

python/information/get_stats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def futures_add(engine, input_file):
7777

7878
del futures[f]
7979

80-
print(f"\nSuccessfully loaded {success_recs:,} records, with" f" {error_recs:,} errors")
80+
print(f"\nSuccessfully loaded {success_recs:,} records, with {error_recs:,} errors")
8181

8282

8383
try:

python/initialization/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818
- Priming the Senzing engine before use loads resource intensive assets upfront.
1919
- Without priming the first SDK call to the engine will appear slower than usual as it causes these assets to be loaded.
2020
- **factory_destroy.py**
21-
- Calls `destroy` on the abstract factory destroying the abstract factory and any Senzing objects it has created.
21+
- Calls `destroy()` on the abstract factory destroying the abstract factory and any Senzing objects it has created.
2222
- The abstract factory must exist for the life of Senzing objects it has created.
23-
- If the abstract factory goes out of scope `destroy` is automatically called
23+
- If the abstract factory goes out of scope `destroy()` is automatically called
2424
- **purge_repository.py**
2525
- **WARNING** This script will remove all data from a Senzing repository, use with caution! **WARNING**.
2626
- It will prompt first, still use with caution!.
27+
**signal_handler.py**
28+
- Catches signal.SIGINT (ctrl + c) and exits cleanly
29+
- Exiting cleanly on a signal ensures resource cleanup for the abstract factory is automatically called
30+
- If sz_abstract_factory goes out of scope 'destroy()` is automatically called, if signals are not caught and handled automatic resource cleanup does not happen
31+
- `destroy()` could also be called directly on the abstract factory by the signal handler
2732
- **sz_engine_config_ini_to_json.py**
2833
- The snippets herein utilize the `SENZING_ENGINE_CONFIGURATION_JSON` environment variable for Senzing abstract factory creation.
2934
- If you are familiar with working with a Senzing project you may be aware the same configuration data is held in the sz_engine_config.ini file.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#! /usr/bin/env python3
2+
3+
import os
4+
import signal
5+
import sys
6+
from pathlib import Path
7+
8+
from senzing import SzError
9+
from senzing_core import SzAbstractFactoryCore
10+
11+
INSTANCE_NAME = Path(__file__).stem
12+
SETTINGS = os.getenv("SENZING_ENGINE_CONFIGURATION_JSON", "{}")
13+
14+
15+
def handler(signum, frame):
16+
print("\nCaught ctrl-c, exiting")
17+
sys.exit(0)
18+
19+
20+
signal.signal(signal.SIGINT, handler)
21+
22+
try:
23+
sz_abstract_factory = SzAbstractFactoryCore(INSTANCE_NAME, SETTINGS)
24+
sz_engine = sz_abstract_factory.create_engine()
25+
# Do work...
26+
print("\nSimulating work, press ctrl-c to exit...")
27+
signal.pause()
28+
except SzError as err:
29+
print(f"\n{err.__class__.__name__} - {err}")

python/loading/add_futures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def futures_add(engine, input_file):
6464

6565
del futures[f]
6666

67-
print(f"\nSuccessfully loaded {success_recs:,} records, with" f" {error_recs:,} errors")
67+
print(f"\nSuccessfully loaded {success_recs:,} records, with {error_recs:,} errors")
6868

6969

7070
try:

python/loading/add_with_info_futures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def futures_add(engine, input_file, output_file):
8787

8888
del futures[f]
8989

90-
print(f"\nSuccessfully loaded {success_recs:,} records, with" f" {error_recs:,} errors")
90+
print(f"\nSuccessfully loaded {success_recs:,} records, with {error_recs:,} errors")
9191
print(f"\nWith info responses written to {output_file}")
9292

9393

python/redo/add_with_redo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def process_redo(engine):
6666

6767
success_recs += 1
6868
if success_recs % 1 == 0:
69-
print(f"Processed {success_recs:,} redo records, with" f" {error_recs:,} errors")
69+
print(f"Processed {success_recs:,} redo records, with {error_recs:,} errors")
7070
except SzBadInputError as err:
7171
mock_logger("ERROR", err)
7272
error_recs += 1
@@ -77,7 +77,7 @@ def process_redo(engine):
7777
mock_logger("CRITICAL", err)
7878
raise err
7979

80-
print(f"\nSuccessfully processed {success_recs:,} redo records, with" f" {error_recs:,} errors")
80+
print(f"\nSuccessfully processed {success_recs:,} redo records, with {error_recs:,} errors")
8181

8282

8383
try:

python/redo/redo_continuous.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#! /usr/bin/env python3
22

33
import os
4+
import signal
45
import sys
56
import time
67
from pathlib import Path
@@ -12,6 +13,11 @@
1213
SETTINGS = os.getenv("SENZING_ENGINE_CONFIGURATION_JSON", "{}")
1314

1415

16+
def handler(signum, frame):
17+
print("\nCaught ctrl-c, exiting")
18+
sys.exit(0)
19+
20+
1521
def mock_logger(level, error, error_record=None):
1622
print(f"\n{level}: {error.__class__.__name__} - {error}", file=sys.stderr)
1723
if error_record:
@@ -27,7 +33,7 @@ def process_redo(engine):
2733
if not (response := engine.get_redo_record()):
2834
print(
2935
"No redo records to process, pausing for 30 seconds. Total"
30-
f" processed {success_recs:,} . (CTRL-C to exit)..."
36+
f" processed: {success_recs:,} (ctrl-c to exit)..."
3137
)
3238
time.sleep(30)
3339
continue
@@ -36,7 +42,7 @@ def process_redo(engine):
3642

3743
success_recs += 1
3844
if success_recs % 100 == 0:
39-
print(f"Processed {success_recs:,} redo records, with" f" {error_recs:,} errors")
45+
print(f"Processed {success_recs:,} redo records, with {error_recs:,} errors")
4046
except SzBadInputError as err:
4147
mock_logger("ERROR", err)
4248
error_recs += 1
@@ -47,6 +53,8 @@ def process_redo(engine):
4753
raise err
4854

4955

56+
signal.signal(signal.SIGINT, handler)
57+
5058
try:
5159
sz_factory = SzAbstractFactoryCore(INSTANCE_NAME, SETTINGS, verbose_logging=False)
5260
sz_engine = sz_factory.create_engine()

0 commit comments

Comments
 (0)