Can I Use Decorator Under The Terms?
I have my own python library. And I use the decorator
@send_transport from it.
@send_transport(program_version=__version__, entity=1) def post(self): pass
Can I implement something like this?
try: from spo_client import send_transport except ImportError: pass else: @send_transport(program_version=__version__, entity=1) def post(self): pass
In order to achieve your goal, you can do
try: from spo_client import send_transport decorator = send_transport(program_version=__version__, entity=1) except ImportError: decorator = lambda f: f @decorator def post(self): pass
This creates either the "correct" decorator or a dummy on which will transform the function into itself, i. e. leave it alone.
Another option could be to define a dummy
def dummy_send_transport(*a, **k): return lambda f: f try: from spo_client import send_transport except ImportError: send_transport = dummy_send_transport # or just send_transport = lambda *a, **k: lambda f: f # but then the code would be less self-documenting. @send_transport(program_version=__version__, entity=1) def post(self): pass
In this case, on an import error, the
send_transport() is substituted by a version which just returns a NOP decorator, but which can be called as its "original". Here, you can have several different
send_transport calls in your code and don't have to predefine a variable for them.
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module