question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add label/description decorator for "cleaner" code

See original GitHub issue

Perhaps it’s something that only irks me, but I find that this syntax quickly becomes readable after the function goes beyond a few lines:

   def publish_this(self, request, obj):
       publish_obj(obj)
   publish_this.label = "Publish"  # optional
   publish_this.short_description = "Submit this article"  # optional

I’m currently using this little decorator to take care of the labels and description which works great:

def create_action(label=None, short_description=None):

    def _create_action(function):
        if label:
            function.label = label

        if short_description:
            function.short_description = short_description

        return function

    return _create_action

Usage is like this:

    @create_action('Publish', 'Submit this article')
    def publish_this(self, request, obj):
        publish_obj(obj)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:14 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
crccheckcommented, Nov 14, 2022

There was some sort of cleanup thing I was going to do before a release but now I can’t remember. So I’ll just hit the button to release!

1reaction
alfonsrvcommented, Nov 13, 2022

New release on pypi?

Read more comments on GitHub >

github_iconTop Results From Across the Web

NestJS: Clean Code With Decorators | by LiftOff LLC - Medium
TL;DR Decorator is a sort of declaration that can be used changing behaviour of a property or adding meta data to a class/property/method...
Read more >
VS Code API | Visual Studio Code Extension API
VS Code API is a set of JavaScript APIs that you can invoke in your Visual Studio Code extension. This page lists all...
Read more >
Decorators in Python: A Complete Guide (with Examples)
Python decorators offer a way to extend the behavior of a function or method. Use decorators avoid repetition and improve the code quality....
Read more >
Toyota bZ4X - Take Charge of the Future | Toyota.com
Visit the official Toyota site to explore the features of the new bZ4X, or find a local dealer to learn more about this...
Read more >
Primer on Python Decorators
As you saw in the previous section, when a decorator uses arguments, you need to add an extra outer function. The challenge is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found