Comments (10)
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.
Looks good to me, I'd use that :)
from humanize.
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.
Please can you provide a code snippet, with actual and expected results?
from humanize.
How about something like #104?
from humanize.
Thanks, merged!
from humanize.
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.
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.
@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_unit
s. Thanks!
from humanize.
Thanks, I've split off the rounding issue to #143!
from humanize.
Related Issues (20)
- Generate files during built process HOT 1
- Allow to specify gender for ordinals
- Humanize
- Importing humanize is slow HOT 9
- Translation activation does not work because of missing .mo files HOT 3
- 3.7.1: pytest is failing HOT 3
- Installing from source installs humanize with version = 0.0.0 HOT 4
- Incorrect precisedelta() calcualation HOT 6
- 3.11.0: pytest cannot find locale files during the tests HOT 1
- Documentation shows private functions HOT 6
- Test on Python 3.10.0 final HOT 8
- Use another augmented assignment statement HOT 1
- humanize raises DeprecationWarning upon import HOT 3
- humanize overflows before datetime does HOT 11
- Docs for naturaldelta are wrong/incomplete HOT 5
- Short scale instead of long scale for Polish locale HOT 2
- Issues with odd months when restricting to "day" precision HOT 1
- Single letter instead of, e.g. "million"? HOT 1
- Test issue
- Test issue 2 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from humanize.