Forum

Author Topic: How to print/handle progress  (Read 3034 times)

awilson

  • Newbie
  • *
  • Posts: 18
    • View Profile
How to print/handle progress
« on: June 15, 2017, 04:07:28 AM »
Lots of processes take an optional argument like:

progress (Callable[[float], None]) – Progress callback.

I was trying to implement it like this:

Code: [Select]
class ProgressPrinter( object ):
  def __init__( self, name ):
    self.name = name
  def __call__( self, percent ):
    print("%s Progress: %f%"%(self.name, percent))
    sys.stdout.flush()

But that gives me an error:

Code: [Select]
RuntimeError: incomplete format
I thought that was everything needed for something to be recognized as callable. Does anyone have a working example of how to handle the progress?

Thank you!

awilson

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to print/handle progress
« Reply #1 on: June 29, 2017, 04:00:21 AM »
Any guidance on this? I'm running reconstructions via the command line, so I don't have the benefits of the gui progress bar. It would be REALLY nice to be able to know where I am in the current stage. Thanks!

Ryan Fox

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: How to print/handle progress
« Reply #2 on: June 29, 2017, 06:27:06 PM »
It's running into trouble on your last percent sign - Python is interpreting that as another substitution, and expecting a format spec to follow.  You can escape the last percent by putting two of them:
Code: [Select]
print("%s Progress: %f%%" % (self.name, percent))
You can also use the newer .format() style:
Code: [Select]
"{} Progress: {}%".format(self.name, percent)

awilson

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to print/handle progress
« Reply #3 on: June 29, 2017, 08:02:43 PM »
 :-[

Well that's embarrassing. Thanks for catching that, this is working fine now that I've gotten out of my own way.