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

Cancel does NOT work when Perol() called multiple times #122

Open
clach04 opened this issue Sep 1, 2024 · 2 comments
Open

Cancel does NOT work when Perol() called multiple times #122

clach04 opened this issue Sep 1, 2024 · 2 comments

Comments

@clach04
Copy link

clach04 commented Sep 1, 2024

After reviewing main() and updating the sample code (from the readme), I now have:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from percol import Percol
from percol.actions import no_output


def main(candidates):
    with Percol(
            actions=[no_output],
            descriptors={'stdin': None, 'stdout': None, 'stderr': None},
            candidates=iter(candidates)) as p:
        exit_code = p.loop()
    if exit_code == 0:
        results = p.model_candidate.get_selected_results_with_index()
        return [r[0] for r in results]
    else:
        return []

if __name__ == "__main__":
    candidates = ['foo', 'bar', 'baz']
    f = open('sample_list.txt')
    for line in f:
        candidates.append(line)
    f.close()
    print('will prompt 3 times')
    results = main(candidates)
    print("You picked: {!r}".format(results))
    results = main(candidates)
    print("You picked: {!r}".format(results))
    results = main(candidates)
    print("You picked: {!r}".format(results))

This works fine if you select something.

But cancelling using default CTRL-C requires 2 presses of ctrl-c.
Which then results in only displaying once and then each call immediately returns with nothing resulting in nothing 😿

I tried with both:

    exit_code = p.cancel_with_exit_code()  # straight up fails first time
    exit_code = p.finish_with_exit_code(1)  # straight up fails first time

and returns none immediately.

Potentially related:

@clach04
Copy link
Author

clach04 commented Sep 1, 2024

EDIT ignore this comment :-)

This may be a bug in my modified library sample, i.e. user error 🥹 . Using original StringIO code seems to work fine:

from cStringIO import StringIO
from percol import Percol
from percol.actions import no_output

def main(candidates):
    si, so, se = StringIO(), StringIO(), StringIO()
    with Percol(
            actions=[no_output],
            descriptors={'stdin': si, 'stdout': so, 'stderr': se},
            candidates=iter(candidates)) as p:
        p.loop()  # this is here twice and is a mistake
        exit_code = p.loop()
        #exit_code = p.cancel_with_exit_code()  # straight up fails first time
        #exit_code = p.finish_with_exit_code(1)  # straight up fails first time
    if exit_code == 0:
        results = p.model_candidate.get_selected_results_with_index()
        return [r[0] for r in results]
    else:
        return []

if __name__ == "__main__":
    candidates = ['foo', 'bar', 'baz']
    results = main(candidates)
    print("You picked: {!r}".format(results))
    results = main(candidates)
    print("You picked: {!r}".format(results))
    results = main(candidates)
    print("You picked: {!r}".format(results))

@clach04
Copy link
Author

clach04 commented Sep 1, 2024

I spoke to soon, my modified stdio code had multiple loop() calls, so similar issue:

from cStringIO import StringIO
from percol import Percol
from percol.actions import no_output

def main(candidates):
    si, so, se = StringIO(), StringIO(), StringIO()
    with Percol(
            actions=[no_output],
            descriptors={'stdin': si, 'stdout': so, 'stderr': se},
            candidates=iter(candidates)) as p:
        #p.loop()  # accidentally left this in :-(
        exit_code = p.loop()
        #exit_code = p.cancel_with_exit_code()  # straight up fails first time
        #exit_code = p.finish_with_exit_code(1)  # straight up fails first time
    if exit_code == 0:
        results = p.model_candidate.get_selected_results_with_index()
        return [r[0] for r in results]
    else:
        return []

if __name__ == "__main__":
    candidates = ['foo', 'bar', 'baz']
    results = main(candidates)
    print("You picked: {!r}".format(results))
    results = main(candidates)
    print("You picked: {!r}".format(results))
    results = main(candidates)
    print("You picked: {!r}".format(results))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant