Metadata-Version: 2.4
Name: lexicon
Version: 3.0.0
Summary: Powerful dict subclass(es) with aliasing & attribute access
Author-email: Jeff Forcier <jeff@bitprophet.org>
License-Expression: BSD-2-Clause
Project-URL: Homepage, https://github.com/bitprophet/lexicon#what
Project-URL: Source, https://github.com/bitprophet/lexicon
Project-URL: Changelog, https://github.com/bitprophet/lexicon/blob/main/docs/changelog.rst
Project-URL: CI, https://app.circleci.com/pipelines/github/bitprophet/lexicon
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Description-Content-Type: text/x-rst
License-File: LICENSE
Dynamic: license-file

|version| |python| |license| |ci| |coverage|

.. |version| image:: https://img.shields.io/pypi/v/lexicon
    :target: https://pypi.org/project/lexicon/
    :alt: PyPI - Package Version
.. |python| image:: https://img.shields.io/pypi/pyversions/lexicon
    :target: https://pypi.org/project/lexicon/
    :alt: PyPI - Python Version
.. |license| image:: https://img.shields.io/pypi/l/lexicon
    :target: https://github.com/bitprophet/lexicon/blob/main/LICENSE
    :alt: PyPI - License
.. |ci| image:: https://img.shields.io/circleci/build/github/bitprophet/lexicon/main
    :target: https://app.circleci.com/pipelines/github/bitprophet/lexicon
    :alt: CircleCI
.. |coverage| image:: https://img.shields.io/codecov/c/gh/bitprophet/lexicon
    :target: https://app.codecov.io/gh/bitprophet/lexicon
    :alt: Codecov

WHAT
====

Lexicon is a simple collection of Python ``dict`` subclasses providing extra
power:

- ``AliasDict``, a dictionary supporting both simple and complex key aliasing:

    - Alias a single key to another key, so that e.g. ``mydict['bar']`` points
      to ``mydict['foo']``, for both reads and writes.
    - Alias a single key to a list of other keys, for writing only, e.g. with
      ``active_groups = AliasDict({'ops': True, 'biz': True, 'dev': True,
      'product': True})`` one can make an alias ``'tech'`` mapping to ``('ops',
      'dev')`` and then e.g. ``active_groups['tech'] = False``.
    - Aliasing is recursive: an alias pointing to another alias will behave as
      if it points to the other alias' target.

- ``AttributeDict``, supporting attribute read & write access, e.g. ``mydict =
  AttributeDict({'foo': 'bar'})`` exhibits ``mydict.foo`` and ``mydict.foo =
  'new value'``.
- ``Lexicon``, a subclass of both of the above which exhibits both sets of
  behavior.

HOW
===

Regular use:

- ``pip install lexicon``
- ``from lexicon import Lexicon`` (or one of the superclasses)
- Use as needed.

Development/hacking/test suite:

- Install dev dependencies via any of the following:
    - ``pip install --group dev`` (assumes pip >= 25.1)
    - ``uv sync``
    - anything else that understands PEP 735 dependency groups
- ``inv test`` (or ``inv --list`` to see other dev tasks)

API
===

``AliasDict``
-------------

In all examples, ``'myalias'`` is the alias and ``'realkey'`` is the "real",
unaliased key.

- ``alias(from_'myalias', to='realkey')``: Alias ``myalias`` to ``realkey`` so
  ``d['myalias']`` behaves exactly like ``d['realkey']`` for both reads and
  writes.
  
    - ``from_`` is the first keyword argument, but typically it can be omitted
      and still reads fine. See below examples for this usage. See below for
      details on how an alias affects other dict operations.

- ``alias('myalias', to=('realkey', 'otherrealkey'))``: Alias ``myalias`` to
  both ``realkey`` and ``otherrealkey``. As you might expect, this only works
  well for writes, as there is never any guarantee that all targets of the
  alias will contain the same value.
- ``unalias('myalias')``: Removes the ``myalias`` alias; any subsequent
  reads/writes to ``myalias`` will behave as normal for a regular ``dict``.
- ``'myalias' in d`` (aka ``__contains__``): Returns True when given an alias,
  so if ``myalias`` is an alias to some other key, dictionary membership tests
  will behave as if ``myalias`` is set.
- ``del d['myalias']`` (aka ``__delitem__``): This effectively becomes ``del
  d['realkey']`` -- to remove the alias itself, use ``unalias()``.
- ``del d['realkey']``: Deletes the real key/value pair (i.e. it calls
  ``dict.__del__``) but doesn't touch any aliases pointing to ``realkey``.

    - As a result, "dangling" aliases pointing to nonexistent keys will raise
      ``KeyError`` on access, but will continue working if the target key is
      repopulated later.

Caveats:

- Because of the single-key/multi-key duality, ``AliasDict`` is incapable of
  honoring non-string-type keys when aliasing (it must test ``isinstance(key,
  basestring)`` to tell strings apart from non-string iterables).

    - ``AliasDict`` instances may still *use* non-string keys, of course -- it
      just can't use them as alias targets.

``AttributeDict``
-----------------

- ``d.key = 'value'`` (aka ``__setattr__``): Maps directly to ``d['key'] =
  'value'``.
- ``d.key`` (aka ``__getattr__``): Maps directly to ``d['key']``.
- ``del d.key`` (aka ``__delattr__``): Maps directly to ``del d['key']``.
- Collisions between "real" or pre-existing attributes, and
  attributes-as-dict-keys, always results in the real attribute winning. Thus
  it isn't possible to use attribute access to access e.g. ``d['get']``.

``Lexicon``
-----------

Lexicon subclasses from ``AttributeDict`` first, then ``AliasDict``, with the
end result that attribute access will honor aliases. E.g.:

    d = Lexicon()
    d.alias('myalias', to='realkey')
    d.myalias = 'foo'
    print d.realkey # prints 'foo'
