Skip to content

#56 - Update Python snippets #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog], [markdownlint],
and this project adheres to [Semantic Versioning].

## [0.0.9] - 2025-07-23

### Changed in 0.0.9

- Modify method names changed in SDK
- Additional snippets

## [0.0.8] - 2025-06-20

### Changed in 0.0.8
Expand Down
10 changes: 8 additions & 2 deletions python/configuration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Configuration related examples.

## Snippets
* **add_data_sources.py**
* Add new data source codes.
* **get_config_registry.py**
* Gets the configuration registry detailing existing configurations.
* **get_data_source_registry.py**
* Gets the data source registry detailing configured data sources in the current default configuration.
* **init_default_config.py**
* Initializes the repository with a default configuration using the default configuration template.
* **register_data_sources.py**
* Retrieves the current default configuration, registers new data source codes in it, registers the new configuration and makes it the current default configuration.

20 changes: 20 additions & 0 deletions python/configuration/get_config_registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#! /usr/bin/env python3

import os
import sys
from pathlib import Path

from senzing import SzError
from senzing_core import SzAbstractFactoryCore

INSTANCE_NAME = Path(__file__).stem
SETTINGS = os.getenv("SENZING_ENGINE_CONFIGURATION_JSON", "{}")


try:
sz_factory = SzAbstractFactoryCore(INSTANCE_NAME, SETTINGS, verbose_logging=False)
sz_configmanager = sz_factory.create_configmanager()
response = sz_configmanager.get_config_registry()
print(response)
except SzError as err:
print(f"{err.__class__.__name__} - {err}", file=sys.stderr)
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,12 @@


try:
sz_factory = SzAbstractFactoryCore("add_data_source", SETTINGS, verbose_logging=False)
sz_factory = SzAbstractFactoryCore(INSTANCE_NAME, SETTINGS, verbose_logging=False)
sz_configmanager = sz_factory.create_configmanager()

config_id = sz_configmanager.get_default_config_id()
sz_config = sz_configmanager.create_config_from_config_id(config_id)

for data_source in ("CUSTOMERS", "REFERENCE", "WATCHLIST"):
_ = sz_config.add_data_source(data_source)

new_config = sz_config.export()
new_config_id = sz_configmanager.set_default_config(new_config, "Add data source CUSTOMERS")

sz_config = sz_configmanager.create_config_from_config_id(new_config_id)
response = sz_config.get_data_sources()
response = sz_config.get_data_source_registry()
print(response)
except SzError as err:
print(f"{err.__class__.__name__} - {err}", file=sys.stderr)
28 changes: 28 additions & 0 deletions python/configuration/init_default_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#! /usr/bin/env python3

import os
import sys
from pathlib import Path

from senzing import SzError
from senzing_core import SzAbstractFactoryCore

EXISTING_CONFIG_MSG = "\nA configuration exists in the repository, replace it with a template configuration? (y/n) "
INSTANCE_NAME = Path(__file__).stem
SETTINGS = os.getenv("SENZING_ENGINE_CONFIGURATION_JSON", "{}")


try:
sz_factory = SzAbstractFactoryCore(INSTANCE_NAME, SETTINGS, verbose_logging=False)
sz_configmanager = sz_factory.create_configmanager()

if current_config_id := sz_configmanager.get_default_config_id():
if not input(EXISTING_CONFIG_MSG).lower() in ("y", "yes"):
sys.exit(1)

sz_config = sz_configmanager.create_config_from_template()
new_default_config = sz_config.export()
new_config_id = sz_configmanager.set_default_config(new_default_config, "Code snippet init_default_config example")
print(f"New default config ID: {new_config_id}")
except SzError as err:
print(f"{err.__class__.__name__} - {err}", file=sys.stderr)
29 changes: 29 additions & 0 deletions python/configuration/register_data_sources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#! /usr/bin/env python3

import os
import sys
from pathlib import Path

from senzing import SzError
from senzing_core import SzAbstractFactoryCore

INSTANCE_NAME = Path(__file__).stem
SETTINGS = os.getenv("SENZING_ENGINE_CONFIGURATION_JSON", "{}")


try:
sz_factory = SzAbstractFactoryCore(INSTANCE_NAME, SETTINGS, verbose_logging=False)
sz_configmanager = sz_factory.create_configmanager()

current_config_id = sz_configmanager.get_default_config_id()
sz_config = sz_configmanager.create_config_from_config_id(current_config_id)

for data_source in ("CUSTOMERS", "REFERENCE", "WATCHLIST"):
sz_config.register_data_source(data_source)

new_config = sz_config.export()
new_config_id = sz_configmanager.register_config(new_config, "Code snippet register_data_source example")
sz_configmanager.replace_default_config_id(current_config_id, new_config_id)
print(f"New default config ID: {new_config_id}")
except SzError as err:
print(f"{err.__class__.__name__} - {err}", file=sys.stderr)
8 changes: 4 additions & 4 deletions python/deleting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Deleting a record only requires the data source code and record ID for the recor
## Snippets

- **delete_futures.py**
- Read and delete source records from a file using multiple threads
- Read and delete source records from a file using multiple threads.
- **delete_loop.py**
- Basic read and delete source records from a file
- Basic read and delete source records from a file.
- **delete_with_info_futures.py**
- Read and delete source records from a file using multiple threads
- Collect the response using the [SZ_WITH_INFO flag](../../README.md#with-info) on the `delete_record()` method and write it to a file
- Read and delete source records from a file using multiple threads.
- Collect the response using the [SZ_WITH_INFO flag](../../README.md#with-info) on the `delete_record()` method and write it to a file.
14 changes: 7 additions & 7 deletions python/information/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ The information snippets outline the retrieval of different informational aspect

## Snippets

- **check_datastore_performance.py**
- Run an insert test against the Senzing repository to gauge performance
- **get_datastore_info.py**
- Return basic information about the Senzing repository(s)
- **check_repository_performance.py**
- Run an insert test against the Senzing repository to gauge performance.
- **get_license.py**
- Return the currently in use license details
- Return the currently in use license details.
- **get_repository_info.py**
- Return basic information about the Senzing repository(s).
- **get_stats.py**
- Return statistical information from the Senzing engine during entity resolution processing
- Return statistical information from the Senzing engine during entity resolution processing.
- **get_version.py**
- Return the current Senzing product version details
- Return the current Senzing product version details.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
try:
sz_factory = SzAbstractFactoryCore(INSTANCE_NAME, SETTINGS, verbose_logging=False)
sz_diagnostic = sz_factory.create_diagnostic()
print(sz_diagnostic.check_datastore_performance(SECONDS_TO_RUN))
print(sz_diagnostic.check_repository_performance(SECONDS_TO_RUN))
except SzError as err:
print(f"\n{err.__class__.__name__} - {err}")
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
try:
sz_factory = SzAbstractFactoryCore(INSTANCE_NAME, SETTINGS, verbose_logging=False)
sz_diagnostic = sz_factory.create_diagnostic()
print(sz_diagnostic.get_datastore_info())
print(sz_diagnostic.get_repository_info())
except SzError as err:
print(f"\n{err.__class__.__name__} - {err}")
33 changes: 23 additions & 10 deletions python/initialization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@
## Snippets

- **abstract_factory_parameters.py**
- Used to create a dictionary that can be unpacked when creating an SzAbstractFactoryCore, also useful for type annotations
- Used to create a dictionary that can be unpacked when creating an abstract factory, also useful for type annotations.
- **abstract_factory.py**
- Basic example of how to create an abstract factory and each of the available engines.
- **abstract_factory_single_instance_only.py**
- Try and create >1 abstract factories where any argument between them is different.
- Only one abstract factory instance can exist at a time.
- Destroying an abstract factory will destroy it and any engine objects it has created.
- **abstract_factory_with_config_id.py**
- Create an abstract factory using a specific configuration ID.
- **abstract_factory_with_debug.py**
- Create an abstract factory with debug turned on.
- **engine_priming.py**
- Priming the Senzing engine before use loads resource intensive assets upfront. Without priming the first SDK call to the engine will appear slower than usual as it causes these assets to be loaded
- **factory_and_engines.py**
- Basic example of how to create an abstract Senzing factory and each of the available engines
- **sz_engine_config_ini_to_json.py**
- The snippets herein utilize the `SENZING_ENGINE_CONFIGURATION_JSON` environment variable for Senzing abstract factory creation
- 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
- Example to convert sz_engine_config.ini to a JSON string for use with `SENZING_ENGINE_CONFIGURATION_JSON`
- Priming the Senzing engine before use loads resource intensive assets upfront.
- Without priming the first SDK call to the engine will appear slower than usual as it causes these assets to be loaded.
- **factory_destroy.py**
- Calls `destroy` on the abstract factory destroying the abstract factory and any Senzing objects it has created.
- The abstract factory must exist for the life of Senzing objects it has created.
- If the abstract factory goes out of scope `destroy` is automatically called
- **purge_repository.py**
- **WARNING** This script will remove all data from a Senzing repository, use with caution! **WARNING**
- It will prompt first, still use with caution!
- **WARNING** This script will remove all data from a Senzing repository, use with caution! **WARNING**.
- It will prompt first, still use with caution!.
- **sz_engine_config_ini_to_json.py**
- The snippets herein utilize the `SENZING_ENGINE_CONFIGURATION_JSON` environment variable for Senzing abstract factory creation.
- 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.
- Example to convert sz_engine_config.ini to a JSON string for use with `SENZING_ENGINE_CONFIGURATION_JSON`.
46 changes: 46 additions & 0 deletions python/initialization/abstract_factory_single_instance_only.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#! /usr/bin/env python3

import os

from senzing import SzError
from senzing_core import SzAbstractFactoryCore

INSTANCE_NAME_1 = "ABSTRACT_FACTORY_1"
INSTANCE_NAME_2 = "ABSTRACT_FACTORY_2"
SETTINGS = os.getenv("SENZING_ENGINE_CONFIGURATION_JSON", "{}")

# Try and create 2 abstract factories where any of the arguments differs
try:
print("\nCreating first abstract factory...")
sz_abstract_factory_1 = SzAbstractFactoryCore(INSTANCE_NAME_1, SETTINGS)
sz_engine_1 = sz_abstract_factory_1.create_engine()
print("\tFirst abstract factory and engine created")
print(f"\tUsing sz_engine_1: {sz_engine_1.get_active_config_id()}")

print("\nCreating second abstract factory...")
sz_abstract_factory_2 = SzAbstractFactoryCore(INSTANCE_NAME_2, SETTINGS)
sz_engine_2 = sz_abstract_factory_2.create_engine()
print("\tSecond abstract factory and engine created")
print(f"\tUsing sz_engine_2: {sz_engine_2.get_active_config_id()}")
except SzError as err:
print(f"\t{err.__class__.__name__} - {err}")
finally:
sz_abstract_factory_1.destroy()
print("\nFirst abstract factory has been destroyed")

# First abstract factory has been destroyed, try and use the engine object it created
try:
print("\nTrying sz_engine_1 from first abstract factory again...")
print(f"\tUsing sz_engine_1: {sz_engine_1.get_active_config_id()}")
except SzError as err:
print(f"\t{err.__class__.__name__} - {err}")

# Now abstract factory 1 has been destroyed, try and re-create it and an engine object
try:
print("\nTrying second abstract factory again...")
sz_abstract_factory_2 = SzAbstractFactoryCore(INSTANCE_NAME_2, SETTINGS)
sz_engine_2 = sz_abstract_factory_2.create_engine()
print("\tCreated second abstract factory and engine after first was destroyed")
print(f"\tUsing sz_engine_2: {sz_engine_2.get_active_config_id()}")
except SzError as err:
print(f"\n{err.__class__.__name__} - {err}")
21 changes: 21 additions & 0 deletions python/initialization/factory_destroy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#! /usr/bin/env python3

import os
from pathlib import Path

from senzing import SzError
from senzing_core import SzAbstractFactoryCore

INSTANCE_NAME = Path(__file__).stem
SETTINGS = os.getenv("SENZING_ENGINE_CONFIGURATION_JSON", "{}")

try:
sz_abstract_factory = SzAbstractFactoryCore(INSTANCE_NAME, SETTINGS)
sz_engine = sz_abstract_factory.create_engine()
# Do work...
except SzError as err:
print(f"\n{err.__class__.__name__} - {err}")
finally:
# Destroys the abstract factory and all objects it created, such as sz_engine above
# If sz_abstract_factory goes out of scope destroy() is automatically called
sz_abstract_factory.destroy()
14 changes: 7 additions & 7 deletions python/loading/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ The loading snippets outline adding new source records. Adding source records in
## Snippets

- **add_futures.py**
- Read and load source records from a file using multiple threads
- Read and load source records from a file using multiple threads.
- **add_queue.py**
- Read and load source records using a queue
- Read and load source records using a queue.
- **add_records_loop.py**
- Basic read and add source records from a file
- Basic read and add source records from a file.
- **add_records.py**
- Basic iteration over a few records, adding each one
- Basic iteration over a few records, adding each one.
- **add_truthset_loop.py**
- Read and load from multiple source files, adding a sample truth set
- Read and load from multiple source files, adding a sample truth set.
- **add_with_info_futures.py**
- Read and load source records from a file using multiple threads
- Collect the response using the [SZ_WITH_INFO flag](../../README.md#with-info) on the `add_record()` method and write it to a file
- Read and load source records from a file using multiple threads.
- Collect the response using the [SZ_WITH_INFO flag](../../README.md#with-info) on the `add_record()` method and write it to a file.
10 changes: 5 additions & 5 deletions python/redo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ When an entity requires additional work a record is automatically created in the
## Snippets

- **add_with_redo.py**
- Read and load source records from a file and then process any redo records
- Read and load source records from a file and then process any redo records.
- **redo_continuous_futures.py**
- Continuously monitor for redo records to process using multiple threads
- Continuously monitor for redo records to process using multiple threads.
- **redo_continuous.py**
- Basic example of continuously monitoring for redo records to process
- Basic example of continuously monitoring for redo records to process.
- **redo_with_info_continuous.py**
- Continuously monitor for redo records to process
- Collect the response using the [SZ_WITH_INFO flag](../../README.md#with-info) on the `process_redo_record()` method and write it to a file
- Continuously monitor for redo records to process.
- Collect the response using the [SZ_WITH_INFO flag](../../README.md#with-info) on the `process_redo_record()` method and write it to a file.
8 changes: 4 additions & 4 deletions python/searching/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ There are [considerations](https://senzing.zendesk.com/hc/en-us/articles/3600078
## Snippets

- **search_futures.py**
- Read and search for records from a file using multiple threads
- To see results first load records with [add_futures.py](../loading/add_futures.py)
- Read and search for records from a file using multiple threads.
- To see results first load records with [add_futures.py](../loading/add_futures.py).
- **search_records.py**
- Basic iteration over a few records, searching for each one
- To see results first load records with [add_truthset_loop.py](../loading/add_truthset_loop.py)
- Basic iteration over a few records, searching for each one.
- To see results first load records with [add_truthset_loop.py](../loading/add_truthset_loop.py).
4 changes: 2 additions & 2 deletions python/stewardship/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ In these examples, the current JSON data for a record is first retrieved and add
## Snippets

- **force_resolve.py**
- Force resolve records together to a single entity
- Force resolve records together to a single entity.
- **force_unresolve.py**
- Force un-resolve a record from an entity into a new entity
- Force un-resolve a record from an entity into a new entity.

## Example Usage

Expand Down
Loading