5
osmarks
8y

I wrote a simple Python script to split a Wikipedia page into manageable chunks. But it took a while to load, so I decided to add a loading indicator. Just a few dots appearing and disappearing. How hard could it be?

"Okay, so I just need a few dots as a loading thing."
"Right, so I suppose I'll need a separate thread for this... Better look up Python's threading again"
"So the thread is working, but it keeps printing it out on separate lines"
"Right, that should fix it ... nope."
"I should probably fix the horrible mess here"
"Hmm... maybe if I replace the weird print() calls with all those extra parameters with sys.stdout.write()..."
"Right, that kind of works, but now there's just a permanent row of dots"
"Okay, that's fixed... Ish."

Well, it works now, but there's a weird mess of two \r's and a somewhat odd loop. Oh, and there's more code for the loading indicator than for the actual functionality. This is CLI by the way.

Comments
  • 0
    You better tweak your parser to do things in a jiffy to avoid showing "loading icons" at all ;-)
  • 1
    @asgs Wikipedia is on the Internet. There'll always be a delay.
  • 1
    @osmarks you could have used a library to do that loader instead....
    Something like https://github.com/rasbt/pyprind could do the trick
  • 0
    @ramk I do not know how long querying the Internet will take, so a progress bar won't help.
  • 0
    You need a Mutex for your threads so that they don't write simultaneously to stdout.
  • 1
    @zantekk It didn't get quite that complicated. When the page content was downloaded it joined the thread, cleared the line and did its actual work.
  • 0
    Or maybe you can just print out: "Downloading page from Wikipedia..." and be done with it.

    #justsaying
Add Comment