In the exciting world of coding, where cool stuff like algorithms and data structures usually steal the spotlight, it's super important not to lose sight of the fact that real people are the ones using our creations. Whether you're building a website, playing around with data visuals, or tackling massive datasets, making information easy for humans to understand is key for getting your message across.
Let's dive into the magic of making your code speak human with relatable formats, because, let's face it, we want our creations to be user-friendly and not just a jumble of techy stuff!
The "humanize" is a Python library that can help us format numbers, dates, file sizes, metrics, etc in human-readable form. In this blog post, i'll explain various functions of the "humanize" library to transform mundane numbers, dates, and sizes into easily understandable and relatable formats.
import humanize
print(humanize.__version__)
In this section, I have explained functions available from "humanize" to format numbers.
Here are list of functions for formating numbers.
Below, I have included few examples explaining the usage of all functions.
humanize.intcomma(12000000)
humanize.intcomma(12000000.5)
humanize.intcomma(12000000.56758, ndigits=2)
humanize.intword(1200)
humanize.intword(1200000)
humanize.intword(12000000000)
humanize.intword(12000000000000)
humanize.intword(12000000000000000)
humanize.apnumber(3)
humanize.ordinal(1)
humanize.ordinal(11)
humanize.ordinal(125)
humanize.ordinal(122)
humanize.ordinal(123)
In this section, I have included examples of functions for formating dates, time and time deltas.
from datetime import datetime, date, time, timedelta
humanize.naturaldate(datetime(2022,1,7))
humanize.naturaldate(date(2022,1,7))
humanize.naturalday(date(2022,1,7))
humanize.naturalday(datetime.now())
humanize.naturalday(datetime.now() - timedelta(days=1))
humanize.naturalday(datetime.now() - timedelta(days=2))
humanize.naturalday(datetime.now() + timedelta(days=1))
humanize.naturaltime(datetime.now())
humanize.naturaltime(datetime(2023,1,1))
humanize.naturaltime(timedelta(5,5,5))
humanize.naturaltime(datetime.now() - timedelta(days=5))
humanize.naturaltime(datetime.now() - timedelta(hours=5))
humanize.naturaltime(datetime.now() - timedelta(seconds=60))
humanize.naturaltime(datetime.now() - timedelta(seconds=30))
humanize.naturaldelta(timedelta(days=5))
humanize.naturaldelta(timedelta(days=5, hours=5))
humanize.precisedelta(timedelta(days=5, hours=5))
humanize.precisedelta(timedelta(days=5, hours=5, minutes=10))
humanize.precisedelta(timedelta(days=5, hours=5, minutes=10, milliseconds=300))
humanize.precisedelta(timedelta(seconds=1001))
humanize.naturalsize(5)
humanize.naturalsize(500)
humanize.naturalsize(1500)
humanize.naturalsize(15000000)
humanize.naturalsize(15000000000)
humanize.naturalsize(15000000000000)
humanize.naturalsize(15000000000000, binary=True) ## It uses 2^10 base instead of 10^3
humanize.naturalsize(15000000000000, gnu=True)
humanize.metric(12000000, unit="W")
humanize.metric(12000000, unit="V")
humanize.metric(12e-10, unit="V")
humanize.metric(12e-6, unit="V")
humanize.metric(12.4567e-6, unit="V", precision=4)
humanize.metric(12.4567e-3, unit="V", precision=4)
humanize.scientific(0.3)
humanize.scientific("200000")
humanize.scientific(0.00005)
humanize.fractional(0.5)
humanize.fractional(0.75)
humanize.fractional(0.25)
humanize.fractional(1.25)
humanize.fractional(0.33)
In this blog post, I explain how you can format numbers, dates, time, time deltas, file sizes, etc using Python library humanize. Feel free to contact us if you have any doubts or questions.
If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.
When going through coding examples, it's quite common to have doubts and errors.
If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.
You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.
If you want to