3

I am measuring run times of a program in seconds. Depending on the amount of data I input, that can take milliseconds to days. Is there a Python module that I can use to convert the number of seconds to the most useful unit and display that? Approximations are fine.

For example, 50 should become 50 seconds, 590 should become 10 minutes, 100000 should become 1 day or something like that. I could write the basic thing myself, but I am sure people have thought about this more than I have and have considered many of the edge case I wouldn't think about in a 1000 years :)

Edit: I noticed tqdm must have some logic associated with that, as it selects the length of the ETA string accordingly. Compare

for _ in tqdm.tqdm(range(10)): time.sleep(1)

with

for _ in tqdm.tqdm(range(100000)): time.sleep(1)

Edit: I have also found this Gist, but I would prefer code with at least some maintanence :) https://gist.github.com/alexwlchan/73933442112f5ae431cc

bers
  • 4,817
  • 2
  • 40
  • 59

2 Answers2

2

Close the question if you want, humanize.naturaldelta is the answer:

This modest package contains various common humanization utilities, like turning a number into a fuzzy human readable duration ('3 minutes ago') or into a human readable size or throughput. It works with python 2.7 and 3.3 and is localized to Russian, French, Korean and Slovak.

https://github.com/jmoiron/humanize

bers
  • 4,817
  • 2
  • 40
  • 59
2

I just found arrow:

Arrow is a Python library that offers a sensible and human-friendly approach to creating, manipulating, formatting and converting dates, times and timestamps.

It has humanize(), too, and is much more well-maintainted, it seems: https://arrow.readthedocs.io/en/latest/#humanize

bers
  • 4,817
  • 2
  • 40
  • 59