Enhance Python Outputs for Better Human Comprehension
Written on
Introduction to Humanize
Python has solidified its status as a premier programming language among software developers, data scientists, and more. This popularity stems not only from its versatility and user-friendliness but also from a plethora of libraries that simplify various tasks. One such library is humanize, which helps make Python outputs more relatable and easier for humans to understand. Let’s explore some practical examples.
Getting Started with Humanize
To begin using the humanize library, you first need to install it via Python's package manager, pip:
pip3 install humanize
After installation, you should import the necessary libraries to successfully follow along with the tutorial. You will need getsize() from the os library to determine file sizes, datetime for date manipulations, and the humanize library itself.
from os.path import getsize
import datetime as dt
import humanize as h
Now that everything is set up, let’s dive into making large numbers more digestible.
Making Large Numbers Readable
Consider the number 1034503576643. It can be challenging to quickly determine whether this figure is closer to a billion or a trillion. The humanize library addresses this issue by formatting the number for better clarity.
One way to achieve this is by inserting commas using the intcomma function:
big_num = 1034503576643
human_big_num_comma = h.intcomma(big_num)
print(human_big_num_comma)
The output will be 1,034,503,576,643, which is significantly easier to read than the original format. Additionally, you can express this number in a more natural language format with the intword function:
human_big_num = h.intword(big_num)
print(human_big_num)
This yields 1.0 trillion.
Working with DateTime
The date format 2022/9/6 (YYYY/MM/DD) can be confusing compared to the more straightforward Sep 06 2022. The humanize library simplifies this with the naturaldate function:
date = dt.date(2022, 9, 6)
human_date = h.naturaldate(date)
print(human_date)
This returns Sep 06 2022. Alternatively, if you only want the month and day, you can use the naturalday function:
human_day = h.naturalday(date)
print(human_day)
This will output Sep 06.
Interpreting Duration
Just like with dates, durations can also be made more human-friendly using the naturaltime function:
current_time = dt.datetime.now()
few_days_before = dt.timedelta(days=3, hours=23, minutes=40)
past_time = current_time - few_days_before
human_time = h.naturaltime(past_time)
print(human_time)
The result will be 3 days ago, which is instantly comprehensible.
File Size Understanding
Suppose you have a file size of 278. The question arises—what unit does this refer to? Bytes, kilobytes, megabytes, or gigabytes? The naturalsize function helps clarify this:
file_size = getsize("./candidates.csv")
print(file_size) # Before humanization
print(h.naturalsize(file_size)) # After humanization
The output will convert 278 into 278 Bytes.
Scientific Notation and Fractions
Scientific notation is useful in many scenarios. You can easily convert numbers into this format using the scientific function:
value = 2304355
scientific_notation = h.scientific(value)
print(scientific_notation) # Without precision
scientific_notation = h.scientific(value, precision=5)
print(scientific_notation) # With precision
This will produce two outputs: 2.30 x 10⁶ and 2.30436 x 10⁶ respectively.
For fractional representation, use the fractional function:
float_value = 0.4646
fraction = h.fractional(float_value)
print(fraction)
The result is 105/226, a neat solution to a complex calculation.
Multi-Language Support
All the previous examples are in English, but the humanize library can support other languages like French and Russian. To enable internationalization (i18n), activate the desired language:
_t = h.i18n.activate("fr")
h.naturaltime(dt.timedelta(seconds=3))
This results in il y a 3 secondes, translating to 3 seconds ago in English.
Conclusion
Thank you for taking the time to read this article! 🎉 🍾
I trust you found it beneficial! If you'd like to buy me a coffee ☕️, feel free to do so! Connect with me on Medium, Twitter, and YouTube, or say hello on LinkedIn. I always enjoy discussing AI, ML, Data Science, NLP, and MLOps topics!
Before you leave, check out the last two parts of this series below:
- Pandas & Python Tricks for Data Science & Data Analysis — Part 1
- Pandas & Python Tricks for Data Science & Data Analysis — Part 2
- Pandas & Python Tricks for Data Science & Data Analysis — Part 3
Video Description: This video discusses how to connect Earth Engine with the Scientific Python Ecosystem.
Video Description: This video explores bridging the gap between end-user programmers and code generation.