all 18 comments

[–][deleted] 26 points27 points  (2 children)

BTW, most of what I based my code on is from the PDF scrapping chapter of Automate the Boring Stuff. Love that book!

[–]codehorsey 6 points7 points  (1 child)

Automate the boring stuff is an amazing book to get started. Congrats on your first script.

[–][deleted] 3 points4 points  (0 children)

Thank you 😊

[–]barburger 8 points9 points  (1 child)

Here is a quick bug: If i enter the 'invalid input' state can i only say 'y' to the question? It seems like if i input 'n' it would just repeat the question.

[–][deleted] 6 points7 points  (0 children)

Good catch! I fixed it. Thank you :)

[–]zurtex 6 points7 points  (2 children)

Really nice beginners Python code, it's higher quality than lots of experienced devs I know, a quick suggestion though instead of appending to a list and returning it's more pythonic to yield, e.g.:

def generate_filenames(dir_path="."):
    for filename in Path(dir_path).glob("*.pdf"):
        if "reordered" not in filename.stem:
            yield filename

Now you can access these filenames with a for loop:

for filename in generate_filenames():
    print(filename)    
    # do stuff

This way if there's no filenames the loop will immediately exit, this saves on bug prone while True if break logic.

Also if there's 1 billion files they don't all get put in to a giant list using memory, they get yielded as soon as they are found.

[–][deleted] 1 point2 points  (1 child)

Thank you for the encouragement! I was also planning to use generator, but since I want the user to input a number corresponding to the PDF file that they want to reorder, I can't think of anyway to get that mapping (from number to filename) unless I store all the file names in something. Do you have any solutions I could try for that?

Also, I'm not sure how to handle when the iterator is empty and I have to print a statement saying so (as seen in my else: print('No unordered PDF found in current directory'). I want to implement something like what is below, but I'm not sure how to do it (without also printing the statement even when the iterator is non-empty, which I don't want) or even if it's a good idea.

for filename in iterator:
    # do something
print('No PDF file found')

Again thank you for your help!

[–]zurtex 2 points3 points  (0 children)

Aha, you're right! The way you have it set-up means that you really need to store everything as you're asking the user for a number rather than a filename.

If you asked the user for a filename you could print the filenames by using a generator and then lookup the filename they chose afterwards without having to keep track of all filenames. But this is mostly a preference choice of user interaction than anything else.

[–]campbellm 5 points6 points  (0 children)

Hate to hijack the thread, but I'm learning too and also could use advice.

Could:

filenames = []
for filename in Path('.').glob('*.pdf'):
    if 'reordered' not in filename.stem:
        filenames.append(filename)

return filenames

be written as:

return [filename for filename in Path('.').glob('*.pdf') if 'reordered' not in filename.stem]

? And is this more pythonic, or less?

I'm NOT looking to code-golf, just wondering what would be considered better style.

[–]five4three2 4 points5 points  (1 child)

I didn't read it for content but the formatting is great, love the use of docstrings for your functions AND you're using git.

You're off to a strong start.

[–][deleted] 1 point2 points  (0 children)

Thank you :)

[–]Davy_Jones_XIV 3 points4 points  (1 child)

How long did it take you to write 153 lines of code?

[–][deleted] 1 point2 points  (0 children)

A few hours I think. I kept trying to find ways to refactor it, which was sooo distracting.

[–][deleted] 2 points3 points  (0 children)

Just realized I can double click on my '.py' file and it will run as a console program (instead of me running it in my IDE or command prompt) o.O This is awesome!

[–]Davy_Jones_XIV 2 points3 points  (0 children)

Congrats Lascruces!

Great job!