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.

Use function both as API and click function?

See original GitHub issue

I am trying to reduce boilerplate code and it was my idea to just decorate an existing function with click decorators to make it available via click. It’s unclear to me from the documentation if this use case is supported. I also search the issues for the below error message and couldn’t find anything related.

When I decorate a function with click, I am not able to use it as a function within Python:

Here’s the function code:

@click.command()
@click.argument('datestr')
def nasa_date_to_iso(datestr):
    """Convert the day-number based NASA format to ISO.

    Parameters
    ----------
    datestr : str
        Date string in the form Y-j

    Returns
    -------
    Datestring in ISO standard yyyy-mm-ddTHH:MM:SS.MMMMMM
    """
    date = dt.datetime.strptime(datestr, nasa_date_format)
    click.echo(date.isoformat())
    return date.isoformat()

Simple enough, right? I install it via setup.py like so:

    entry_points={
        "console_scripts": [
            'nasa_date_to_iso = planetpy.utils:nasa_date_to_iso',
        ]
    },

but when I call it within ipython I get this:

In [1]: from planetpy.utils import nasa_date_to_iso

In [2]: nasa_date_to_iso('2017-090')
Usage: ipython [OPTIONS] DATESTR

Error: Got unexpected extra arguments (0 1 7 - 0 9 0)
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

/Users/klay6683/miniconda3/envs/stable/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2971: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

So, is it not possible to have the same function both as a click-cli function and an python-internal API?

System: Py3.6 via conda on macOS 10.12.6, click version 6.7

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kootenpvcommented, Feb 5, 2020

I would still call this a bad experience as a user. Why do we have to write the boilerplate? You have 5 args, you need to duplicate 5 args. If you go kwargs then you lose the signature.

1reaction
davidismcommented, Jun 26, 2018
def nasa_date_to_iso(value):
    ...

@click.command('nasa_date_to_iso')
def nasa_date_to_iso_command(value):
    click.echo(nasa_date_to_iso(value))

This would be pretty easy to write a decorator for to automate.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Call two functions from same onclick [duplicate] - Stack Overflow
You can create a single function that calls both of those, and then use it in the event.
Read more >
.click() | jQuery API Documentation
An object containing data that will be passed to the event handler. ... A function to execute each time the event is triggered....
Read more >
Call multiple JavaScript functions in onclick event
Given multiple functions, the task is to call them by just one onclick event using JavaScript. Here are few methods discussed.
Read more >
EventTarget.addEventListener() - Web APIs | MDN
Event listener with anonymous function. Here, we'll take a look at how to use an anonymous function to pass parameters into the event...
Read more >
onclick Event - W3Schools
Example. Execute a JavaScript when a button is clicked: <button onclick="myFunction()">Click me</button> ... object.onclick = function(){myScript};.
Read more >

github_iconTop Related Medium Post

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