Giter Site home page Giter Site logo

Comments (10)

bersbersbers avatar bersbersbers commented on July 23, 2024 1

Sure! One possible application is this:

import datetime, humanize, time

start = datetime.datetime.now()
time.sleep(4)
delta = start - datetime.datetime.now()

print(f"Task took {humanize.naturaldelta(delta)} to complete")
print(f"2 subtasks took {humanize.naturaldelta(delta / 2)} each to complete")
print(f"1000 subtasks took {humanize.naturaldelta(delta / 1000)} each to complete")

This prints

Task took 4 seconds to complete
2 subtasks took 2 seconds each to complete
1000 subtasks took a moment each to complete

I'd like the third line to be something like

1000 subtasks took 4 milliseconds each to complete

from humanize.

bersbersbers avatar bersbersbers commented on July 23, 2024 1

Looks good to me, I'd use that :)

from humanize.

bersbersbers avatar bersbersbers commented on July 23, 2024 1

Thanks, the test cases look good so far. The one I posted above is the first odd one I encountered in a while, but I'll keep my eyes open for new ones.

One oddity to me (probably a side issue that might be further discussed elsewhere) is the rounding. While I can understand that

humanize.naturaltime(2*365*24*60*60 - 1)

is 1 year, 11 months ago and not 2 years ago, because the year is not complete yet and we don't celebrate birthdays early ("how long ago was it?" - "2 years?" - "not yet!"), my intuitive expectation of

humanize.naturaldelta(2*365*24*60*60 - 1)

is actually 2 years - because with deltas ("how long did it take?"), I feel rounding is much more natural.

So for 101_943 / 1_000_000, I'd probably expect 102 milliseconds.

from humanize.

hugovk avatar hugovk commented on July 23, 2024

Please can you provide a code snippet, with actual and expected results?

from humanize.

hugovk avatar hugovk commented on July 23, 2024

How about something like #104?

from humanize.

hugovk avatar hugovk commented on July 23, 2024

Thanks, merged!

from humanize.

bersbersbers avatar bersbersbers commented on July 23, 2024

I just got

Testing completed in 2 seconds, that is 101943 microseconds per sample.

By setting a minimum_unit, I had expected humanize to be smart enough to first choose milliseconds, then microseconds, etc. - that is, output "102 milliseconds", in this example. Is this intended?

from humanize.

hugovk avatar hugovk commented on July 23, 2024

It is, but I can see it can be confusing and improved.

Right now, the docstring says minimum_unit is to decide which of seconds, milliseconds or microseconds to use as the lowest unit:

def naturaldelta(value, months=True, minimum_unit="seconds"):
    """Return a natural representation of a timedelta or number of seconds.
    This is similar to naturaltime, but does not add tense to the result.
    Args:
        value: A timedelta or a number of seconds.
        months: If True, then a number of months (based on 30.5 days) will be used for
            fuzziness between years.
        minimum_unit: If microseconds or milliseconds, use those units for subsecond
            deltas.
    Returns:
        str: A natural representation of the amount of time elapsed.
    """

So it works like this:

import datetime as dt

import humanize

delta = dt.timedelta(seconds=101_943 / 1_000_000)

assert humanize.naturaldelta(delta, minimum_unit="microseconds") == "101943 microseconds"

assert humanize.naturaldelta(delta, minimum_unit="milliseconds") == "101 milliseconds"

Meaning it doesn't upgrade that minimum_unit, like it does with others (minutes -> hours -> days etc).

It's probably better to do something like this:

assert humanize.naturaldelta(delta, minimum_unit="microseconds") == "101 milliseconds"

assert humanize.naturaldelta(delta, minimum_unit="milliseconds") == "101 milliseconds"

from humanize.

hugovk avatar hugovk commented on July 23, 2024

@bersbersbers Please see PR #142 and in particular the changed test cases: d538da2

It'd also be really useful to have some example inputs and expected outputs for different minimum_units. Thanks!

from humanize.

hugovk avatar hugovk commented on July 23, 2024

Thanks, I've split off the rounding issue to #143!

from humanize.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.