-
Notifications
You must be signed in to change notification settings - Fork 39
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
if lines is used once, define it as a generator #23
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, and glad you find pyp useful.
and that mention is either in a comprehension, loop or the second argument to map.
The second part of that sentence is important, since we can't do list-like things to lines if it's a generator. Most notably the following would no longer work: cat README.md | pyp 'lines[4]'
(but people may also want to pass lines
to a function that expects a list or assign another variable to the value of lines
and do arbitrary things to that variable, etc)
It's a little tricky to do right, especially while keeping the code clean looking. Out of curiosity, is lines
being a list something that's actually caused an issue for you?
not really, although I'm still a very casual user. I've been a beginner with bash my entire life (happily, I guess) but I'm good enough with Python, so this looks pretty appropriate to calm my itches So, I've just wanted to be helpful on this interesting project, nothing in particular.
I see. Well, I've just defined a wrapper that work as an indexable/sliceable iterator that also caches computed values. It seems to cover those cases
However, I don't know if it worth the code pollution for little gain on most cases. |
Thanks, that's a neat solution! Pros:
Cons:
Missing feature:
I think this could cross into "mysterious" behaviour that pyp in general tries to avoid. Ideally, if I add support for user defined magic variables, Indexable would be something that people could just define in their pyp config and that way things would never be mysterious / people could customise the logic further. (I guess even today you could add Indexable into your pyp config, but you'd have to always do Anyway, I think for now I'm not going to merge. If I get this more often as a feature request, or if I never get around to adding user defined magic vars, I might reconsider. Thanks again for this PR! |
@hauntsaninja I agree! let me know if I could help with something less controversial :D |
I implemented the magic variable support in configs described in the roadmap issue, see https://github.com/hauntsaninja/pyp#pyp-lets-you-configure-your-own-magic This means users should now be able to do:
where config contains the Indexable class you thought of:
|
from #22
I implemented exactly that.
PS: pyp is great, thanks!