Skip to main content

Add a property that is an alias for an indexer to a class.

Reason this release was yanked:

faulty import

Project description

Overview

Add a property that is an alias for an indexer to a class.

Installation

To install keyalias, you can use pip. Open your terminal and run:

pip install keyalias

Implementation

import functools
from tofunc import tofunc

__all__ = ["keyalias"]

def keyalias(**kwargs):
    return functools.partial(decorator, **kwargs)

def propertyget(self, /, *, key):
    return self[key]
def propertyset(self, value, /, *, key):
    self[key] = value
def propertydel(self, /, *, key):
    del self[key]
def decorator(cls, /, **kwargs):
    raws = [propertyget, propertyset, propertydel]
    for alias, key in kwargs.items():
        bindings = list()
        for raw in raws:
            b = functools.partial(raw, key=key)
            b = tofunc(b)
            bindings.append(b)
        pro = property(*bindings)
        setattr(cls, alias, pro)
    return cls

Example

from keyalias import keyalias

@keyalias(six=6)
class MyClass:
    def __init__(self):
        self.data = list(range(10))  # Example list with 10 elements

    def __getitem__(self, index):
        return self.data[index]

    def __setitem__(self, index, value):
        self.data[index] = value

    def __delitem__(self, index):
        del self.data[index]

# Create an instance of MyClass
my_instance = MyClass()

# Use the alias property to access index 6
print(my_instance.six)  # prints 6
my_instance.six = 42
print(my_instance.six)  # prints 42
del my_instance.six
print(my_instance.six)  # prints 7

License

This project is licensed under the MIT License.

Credits

Thank you for using keyalias!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

keyalias-1.0.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

keyalias-1.0.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file keyalias-1.0.0.tar.gz.

File metadata

  • Download URL: keyalias-1.0.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for keyalias-1.0.0.tar.gz
Algorithm Hash digest
SHA256 cde81bed1521f9a8e3b6ed83da839c2c03886bb366bbc4148c6b2679c492c3d8
MD5 eb6f9fde24df79870af9f4892af85ca0
BLAKE2b-256 066d4666925588c6a53e3cde8ca3060eb3561f1c3e0c59046e59f908557e9b27

See more details on using hashes here.

File details

Details for the file keyalias-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: keyalias-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for keyalias-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50a7c59197b1f98460ea48955dd19bf77a8f31c675686ece3363df7d467ca1e8
MD5 74e97ed4edca7739ffa2c203cfac1b95
BLAKE2b-256 f1292ec318c5689e60ba1703b50765420016f75390db8a57de05c0ad696ab783

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page