{"id":15444538,"url":"https://github.com/cariad/nvalues","last_synced_at":"2025-03-28T08:13:52.507Z","repository":{"id":63624675,"uuid":"569347487","full_name":"cariad/nvalues","owner":"cariad","description":"Python package for working with n-dimensional volumes of data","archived":false,"fork":false,"pushed_at":"2022-11-24T14:38:25.000Z","size":525,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-03T07:35:18.740Z","etag":null,"topics":["data-science","data-structures","python"],"latest_commit_sha":null,"homepage":"https://nvalues.dev","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cariad.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"cariad"}},"created_at":"2022-11-22T16:10:54.000Z","updated_at":"2022-11-23T13:02:16.000Z","dependencies_parsed_at":"2023-01-22T15:46:16.409Z","dependency_job_id":null,"html_url":"https://github.com/cariad/nvalues","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cariad%2Fnvalues","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cariad%2Fnvalues/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cariad%2Fnvalues/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cariad%2Fnvalues/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cariad","download_url":"https://codeload.github.com/cariad/nvalues/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245991585,"owners_count":20706129,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-science","data-structures","python"],"created_at":"2024-10-01T19:41:40.123Z","updated_at":"2025-03-28T08:13:52.470Z","avatar_url":"https://github.com/cariad.png","language":"Python","funding_links":["https://github.com/sponsors/cariad"],"categories":[],"sub_categories":[],"readme":"# NValues\n\n[![codecov](https://codecov.io/gh/cariad/nvalues/branch/main/graph/badge.svg?token=Qj6KxDEoVA)](https://codecov.io/gh/cariad/nvalues)\n\n**NValues** is a Python package for working with _n_-dimensional volumes of data.\n\nFull documentation is online at **[nvalues.dev](https://nvalues.dev)**.\n\n## Installation\n\n**NValues** requires Python 3.9 or later and can be installed via [PyPI](https://pypi.org/project/nvalues/):\n\n```console\npip install nvalues\n```\n\n## The `Volume` class\n\nThe [`Volume` class](https://nvalues.dev/volume) represents a strongly-typed _n_-dimensional volume of values.\n\n### Construction\n\nYou must pass two generic types on construction:\n\n1. Tuple of any number of key types\n2. Value type\n\n```python\nfrom nvalues import Volume\n\n# A spreadsheet-like grid of floats with string x and integer y keys:\nvolume = Volume[tuple[str, int], float]()\n\n# A cube of booleans with integer x, string y and float z keys:\nvolume = Volume[tuple[int, str, float], bool]()\n```\n\n### Default values\n\nBy default, volumes will raise `nvalues.exceptions.NKeyError` if you try to read a key that doesn't exist.\n\nTo return a default value instead, you can either:\n\n- Pass the default value as the `default` argument:\n\n    ```python\n    from nvalues import Volume\n\n    volume = Volume[tuple[int, int], str](default=\"default\")\n    print(volume[0, 0])\n    # \"default\"\n    ```\n\n- Pass a function that generates a default value as the `default_maker` argument:\n\n    ```python\n    from nvalues import Volume\n\n    def make_default(key: tuple[int, int]) -\u003e str:\n        return f\"default for {key}\"\n\n    volume = Volume[tuple[int, int], str](default_maker=make_default)\n    print(volume[0, 0])\n    # \"default for (0, 0)\"\n    ```\n\nDefault values generated at runtime will be added to the volume, while static defaults will not.\n\n### Reading, setting and deleting values\n\nValues are read, set and deleted via their keys.\n\n```python\nfrom nvalues import Volume\n\nvolume = Volume[tuple[str, int], float](default=0)\n\nvolume[\"A\", 0] = 1.2\nprint(volume[\"A\", 0])\n# 1.2\n\ndel volume[\"A\", 0]\nprint(volume[\"A\", 0])\n# 0\n```\n\n### Key validation\n\nA `Volume` can be configured to reject invalid keys if a validator is passed in the initialiser or set on the `key_validator` property.\n\nIf set, the key validator is a function that examines the key and raises any exception if it's invalid. Any attempts to access an invalid key will result in `InvalidKey` being raised.\n\n```python\nfrom nvalues import Volume\nfrom nvalues.exceptions import InvalidKey\n\nmax_x = 3\nmax_y = 4\n\ndef check_key_range(key: tuple[int, int]) -\u003e None:\n    x = key[0]\n    if x \u003c 0 or x \u003e max_x:\n        raise ValueError(f\"x {x} must be 0-{max_x} inclusive\")\n\n    y = key[1]\n    if y \u003c 0 or y \u003e max_y:\n        raise ValueError(f\"y {y} must be 0-{max_y} inclusive\")\n\nvolume = Volume[tuple[int, int], str](key_validator=check_key_range)\n\ntry:\n    volume[0, 17] = \"foo\"\nexcept InvalidKey as ex:\n    print(ex)\n\n# Key (0, 17) failed validation (y 17 must be 0-4 inclusive)\n```\n\n### Iterating values\n\nNative iteration yields the key and value for each item in the volume.\n\n```python\nfrom nvalues import Volume\n\nvolume = Volume[tuple[int, int], str]()\n\nvolume[0, 0] = \"zero-zero\"\nvolume[4, 0] = \"four-zero\"\nvolume[0, 4] = \"zero-four\"\n\nfor item in volume:\n    print(f\"Found {item.value} at {item.key}\")\n\n# Found zero-zero at (0, 0)\n# Found zero-four at (0, 4)\n# Found four-zero at (4, 0)\n```\n\n## Other classes\n\nThe [`Line`](https://nvalues.dev/line/), [`Grid`](https://nvalues.dev/grid/), [`Cube`](https://nvalues.dev/cube/), [`Tesseract`](https://nvalues.dev/tesseract/) and [`Penteract`](https://nvalues.dev/penteract/) classes wrap and simplify the `Volume` class if you don't need more than five dimensions.\n\n## Support\n\nPlease raise bugs, feature requests and ask questions at [cariad/nvalues/issues](https://github.com/cariad/nvalues/issues).\n\n## The Project\n\n**NValues** is \u0026copy; 2022 Cariad Eccleston and released under the [MIT License](https://github.com/cariad/nvalues/blob/main/LICENSE) at [cariad/nvalues](https://github.com/cariad/nvalues).\n\n## The Author\n\nHello! 👋 I'm **Cariad Eccleston** and I'm a freelance backend and infrastructure engineer in the United Kingdom. You can find me at [cariad.earth](https://cariad.earth), [github/cariad](https://github.com/cariad), [linkedin/cariad](https://linkedin.com/in/cariad) and on Mastodon at [@cariad@tech.lgbt](https://tech.lgbt/@cariad).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcariad%2Fnvalues","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcariad%2Fnvalues","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcariad%2Fnvalues/lists"}