Download this example as a Jupyter notebook or a Python script.


Anonymize an activity report#

This example shows how to anonymize an activity report.

Fetch the activity report#

Import the Connection class, create the connection, and fetch the activity report. For more information, see the Basic Usage and Access an activity report examples.

[1]:
from ansys.grantami.system import Connection

connection = Connection("http://my_grantami_server/mi_servicelayer").with_autologon()
client = connection.connect()
items = client.get_activity_report()

Anonymize the activity report#

ActivityItem objects are immutable, and so the username for an ActivityItem cannot be changed directly. However, the ActivityItem class is a dataclass, so we can use the dataclasses.replace() function to create a new object with the required modification.

[2]:
from dataclasses import replace

anonymized_usernames = {}
anonymized_items = []

for item in items:
    current_username = item.username

    try:
        # If an anonymized username has already been created, retrieve it
        anonymized_name = anonymized_usernames[current_username]
    except KeyError:
        # If not,
        anonymized_name = f"Anonymous user {len(anonymized_items)}"
        anonymized_usernames[current_username] = anonymized_name

    anonymized_item = replace(item, username=anonymized_name)
    anonymized_items.append(anonymized_item)

Each item in the activity report now contains an anonymized username. Any analytics that aggregate unique numbers of users will produce the same results, but usernames of individuals will not be included in any more granular results.

[3]:
print(anonymized_items[0])
ActivityItem(activity_date=datetime.date(2025, 8, 4), application_names=['PyGranta RecordLists'], username='Anonymous user 0', usage_mode=<ActivityUsageMode.VIEW: 'view'>, database_key=None)