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

Feature suggestion: walk #35

Open
boxed opened this issue Jan 17, 2023 · 5 comments · May be fixed by #37
Open

Feature suggestion: walk #35

boxed opened this issue Jan 17, 2023 · 5 comments · May be fixed by #37

Comments

@boxed
Copy link

boxed commented Jan 17, 2023

I get the feeling that a common use case for this lib would be to walk directory trees, ignoring stuff. I think it's easy to make a mistake that will lead to suboptimal performance when implementing this, and it feels a bit silly that everyone should write this.

I would propose adding a function walk:

def walk(path):
    for root, dirs, files in os.walk(path):
        dirs[:] = [x for x in dirs if not ignored(x)]
        files[:] = [x for x in files if not ignored(x)]
        yield root, dirs, files
@boxed
Copy link
Author

boxed commented Jan 17, 2023

This feature only works if the .git folder is ignored btw, otherwise you get weird stuff.

@excitoon
Copy link
Owner

Personally I don't think it's good idea to call os.walk() from the library. What do you think about this? My point is that it shall be user who calls it.

def gitignore(walker): # or something like walkignore \o/
    for root, dirs, files in walker:
        ...

@excitoon
Copy link
Owner

Probably 4-tuples from os.fwalk() shall be supported too.

@boxed
Copy link
Author

boxed commented Jan 19, 2023

I think you're overengineering :P But ok, let's concede the point:

def walk(path, walker=os.walk):
    for root, dirs, files, *rest in walker(path):
        dirs[:] = [x for x in dirs if not ignored(x)]
        files[:] = [x for x in files if not ignored(x)]
        yield root, dirs, files, *rest

this makes the API as clean as what I suggested, and supports fwalk, and will allow you to pass your own walker.

@excitoon excitoon linked a pull request Jan 19, 2023 that will close this issue
@excitoon
Copy link
Owner

@boxed Okay, sounds reasonable. Can you propose some tests?

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

Successfully merging a pull request may close this issue.

2 participants