Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide decorator for alias_property #8641

Open
ikus060 opened this issue Mar 8, 2024 · 0 comments
Open

Provide decorator for alias_property #8641

ikus060 opened this issue Mar 8, 2024 · 0 comments
Labels
Type: Feature Issue is a feature request

Comments

@ikus060
Copy link
Contributor

ikus060 commented Mar 8, 2024

Is your feature request related to a problem? Please describe.
Defining AliasProperty explicitly with getter and setter is a bit painful and not very clear as we need to define 2 function and then create the AliasProperty.

Describe the solution you'd like
I'm proposing to kivy to provide alias_property decorator to easily create the alias property.

Current implementation

    def _get_disk_total_text(self):
        total = self.disk_usage.get('total', 0)
        humanfriendly.format_size(total)
        if total <= 0:
            return ""
        return _('Total space %s') % humanfriendly.format_size(total, binary=1)

    disk_total_text = AliasProperty(_get_disk_total_text, None, bind=['disk_usage'])

New implementation:

@alias_property(bind=['disk_usage']
def disk_total_text():
        total = self.disk_usage.get('total', 0)
        humanfriendly.format_size(total)
        if total <= 0:
            return ""
        return _('Total space %s') % humanfriendly.format_size(total, binary=1)

It's clean and concise.

A basic implementation supporting getter would look like this:

def alias_property(bind=[]):
    """
    Alias property decorator.
    """
    return functools.partial(AliasProperty, setter=None, bind=bind)
@Julian-O Julian-O added the Type: Feature Issue is a feature request label Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature Issue is a feature request
Projects
None yet
Development

No branches or pull requests

2 participants