{ "cells": [ { "cell_type": "markdown", "id": "90c49ee3", "metadata": {}, "source": [ "# Anonymize an activity report" ] }, { "cell_type": "markdown", "id": "c4fb7f2b", "metadata": {}, "source": [ "This example shows how to anonymize an activity report." ] }, { "cell_type": "markdown", "id": "83dd0d5d", "metadata": {}, "source": [ "## Fetch the activity report" ] }, { "cell_type": "markdown", "id": "4788e718", "metadata": {}, "source": [ "Import the ``Connection`` class, create the connection, and fetch the activity report. For more\n", "information, see the [Basic Usage](../1_Basic_usage.ipynb) and\n", "[Access an activity report](2-1_Access_activity_report.ipynb) examples." ] }, { "cell_type": "code", "execution_count": null, "id": "b3fa6b5e", "metadata": {}, "outputs": [], "source": [ "from ansys.grantami.system import Connection\n", "\n", "connection = Connection(\"http://my_grantami_server/mi_servicelayer\").with_autologon()\n", "client = connection.connect()\n", "items = client.get_activity_report()" ] }, { "cell_type": "markdown", "id": "7bb52ad9", "metadata": {}, "source": [ "## Anonymize the activity report" ] }, { "cell_type": "markdown", "id": "82b6c373", "metadata": {}, "source": [ "`ActivityItem` objects are immutable, and so the `username` for an `ActivityItem` cannot be changed directly.\n", "However, the `ActivityItem` class is a dataclass, so we can use the `dataclasses.replace()` function to create a new\n", "object with the required modification." ] }, { "cell_type": "code", "execution_count": null, "id": "399f77dd", "metadata": {}, "outputs": [], "source": [ "from dataclasses import replace\n", "\n", "anonymized_usernames = {}\n", "anonymized_items = []\n", "\n", "for item in items:\n", " current_username = item.username\n", "\n", " try:\n", " # If an anonymized username has already been created, retrieve it\n", " anonymized_name = anonymized_usernames[current_username]\n", " except KeyError:\n", " # If not,\n", " anonymized_name = f\"Anonymous user {len(anonymized_items)}\"\n", " anonymized_usernames[current_username] = anonymized_name\n", "\n", " anonymized_item = replace(item, username=anonymized_name)\n", " anonymized_items.append(anonymized_item)" ] }, { "cell_type": "markdown", "id": "6d60da45", "metadata": {}, "source": [ "Each item in the activity report now contains an anonymized username. Any analytics that aggregate unique numbers of\n", "users will produce the same results, but usernames of individuals will not be included in any more granular results." ] }, { "cell_type": "code", "execution_count": null, "id": "df2d888a", "metadata": {}, "outputs": [], "source": [ "print(anonymized_items[0])" ] } ], "metadata": { "jupytext": { "formats": "ipynb,py:light" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }