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

Introduce a sort-by flag that accepts a python comparator function #569

Open
tionis opened this issue Jan 19, 2025 · 2 comments
Open

Introduce a sort-by flag that accepts a python comparator function #569

tionis opened this issue Jan 19, 2025 · 2 comments

Comments

@tionis
Copy link

tionis commented Jan 19, 2025

I'd like to add some more complex sorting logic, it would be nice if power users could pass a python function that is used as a comparator to the program.

@WhyNotHugo
Copy link
Member

We store data in an sqlite cache, and query that. So your function would need to write SQL to query todos.

I have some vague plans to move caching out into a separate helper, which indexes all todos and provides an API for querying. todoman would become mostly a UI on top of that.

So I'm not sure if I'd implement this right now, or wait until that happens.

@tionis
Copy link
Author

tionis commented Jan 21, 2025

This would only influence the sorting of the result though, so something like

from functools import cmp_to_key
from dopy.core import Dopy

# Using dopy to get python without indentation to make it easier to specify the func in a flag
dopy = Dopy()

if flags.sort_by not None:
  processed = dopy.preprocess(source)
  def compare(x, y):
    return exec(processed, namespace={"x": x, "y": y})
  data = sorted(data, key=cmp_to_key(compare))

could work without really increasing processing time or memory footprint significantly.

At least I think so, I haven't tried it yet.

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

2 participants