,

Humanize Package in Python – How Does it Work ?

The Humanize package in Python provides a way to convert data into human-readable formats, making it easier to understand and interpret. This package is particularly useful when presenting data to users, as it converts complex and technical information into a more intuitive format. In this article, we will explore how the Humanize package works, along with relevant coding examples to demonstrate its functionality.

Installing Humanize
Before we can use the Humanize package, we need to install it. You can install Humanize using pip:

pip install humanize

Using Humanize

Once installed, we can start using the various functions provided by Humanize. Here are some common use cases with examples:

1. Humanizing Numbers

Humanize can convert large numbers into more readable formats. For example, it can add commas to large numbers or convert numbers into words.

import humanize

# Adding commas to large numbers
print(humanize.intcomma(1234567890))  

# Converting numbers into words
print(humanize.intword(1234567890))  

Output:

2. Humanizing Dates and Times

Humanize can also format dates and times in a more human-readable way. This includes converting time intervals into words or formatting dates.

import humanize
import datetime

# Getting the current time
now = datetime.datetime.now()

# Formatting time intervals
print(humanize.naturaltime(now - datetime.timedelta(seconds=10)))  
print(humanize.naturaltime(now - datetime.timedelta(days=1)))     

# Formatting dates
print(humanize.naturaldate(datetime.date(2023, 1, 1)))  

Output:

3. Humanizing Filesizes

Humanize can convert file sizes into a more readable format, such as converting bytes into kilobytes, megabytes, etc.

import humanize

# Converting file sizes
print(humanize.naturalsize(1024))          
print(humanize.naturalsize(1048576))       
print(humanize.naturalsize(1073741824))    

Output:

4. Humanizing Decimal Numbers

Humanize can also format decimal numbers in a human-readable way by adding appropriate suffixes.

import humanize

# Formatting decimal numbers
print(humanize.fractional(1/3))  
print(humanize.fractional(0.5))  

Output:

Customizing Humanize

The Humanize package allows for customization to better fit your needs. You can change the language, adjust the formatting, or extend the package with your custom functions.

Changing the Language

Humanize supports multiple languages. To use a different language, you can set the locale.

import humanize
import locale

# Setting the locale to French
locale.setlocale(locale.LC_TIME, 'fr_FR')
humanize.i18n.activate('fr_FR')

# Formatting time intervals in French
print(humanize.naturaltime(datetime.datetime.now() - datetime.timedelta(seconds=10)))  # Output: il y a 10 secondes

Custom Functions

You can also create your custom functions to extend the functionality of Humanize.

import humanize

def custom_humanize_number(number):
    if number < 1000:
        return str(number)
    else:
        return f"{number // 1000}k"

# Using the custom function
print(custom_humanize_number(999))     
print(custom_humanize_number(1000))    
print(custom_humanize_number(1500))    

Output:

Scientific Notation: This utility is used to add scientific notation to the program. This utility also gives an option to add precision to the number. Precision here means the number of digits needed in the number. For example:

# Program to demonstrate the 
# scientific notation utility 
import humanize 

# Scientific notation using 
# integer without precision 
gfg = humanize.scientific(2000) 
print('Without Precision: '+gfg) 

# Scientific notation using 
# integer with precision 
gfg = humanize.scientific(2**10, precision = 5) 
print('With Precision: '+gfg) 

Output:

Date & Time Utility: Many a times, we encounter few scenarios where the date or time is returned in the form of numbers. This utility is used to convert the date into a human understandable format. For example:

# Program to demonstrate the 
# date time utility 
import humanize 
import datetime as dt 

# Converting the date represented 
# as a number 
gfg = humanize.naturaldate(dt.date(2024, 6, 20))
print(gfg) 

# Converting seconds to a 
# better representation 
gfg = humanize.naturaldelta(dt.timedelta(seconds = 900)) 
print(gfg) 

Output:

Summary and Advantages of the Humanize Package in Python

Summary

The Humanize package in Python offers a variety of functions to convert data into human-readable formats. It simplifies the representation of dates, times, numbers, and other data types, making them more intuitive and accessible. The package can add commas to large numbers, convert numerical data into words, format dates and times in a user-friendly way, and convert file sizes into readable units. This tool is particularly useful when presenting data to users, as it transforms complex and technical information into a more understandable format. The installation of Humanize is straightforward via pip, and its functions can be easily customized to fit specific needs.

Advantages

Improved Readability: Humanize makes data more understandable by converting it into human-friendly formats, such as adding commas to large numbers or converting file sizes into kilobytes, megabytes, etc.

Enhanced User Experience: By presenting information in a more intuitive way, Humanize enhances the user experience. This is particularly important in applications where non-technical users need to interpret data.

Flexibility and Customization: The package supports multiple languages and allows for customization to meet specific requirements. You can set different locales and create custom functions to extend its functionality.

Time-Saving: Humanize saves time by providing ready-made functions for common formatting tasks. This eliminates the need to write custom code for data presentation.

Consistency: Using Humanize ensures consistent formatting across different parts of an application, leading to a more polished and professional look.

Error Reduction: By using predefined functions for data formatting, the chances of errors in manual formatting are significantly reduced.

Conclusion

The Humanize package in Python is an invaluable tool for developers looking to make their data more user-friendly and intuitive. By converting complex data into human-readable formats, it enhances readability and improves the overall user experience. Whether you’re formatting large numbers, converting file sizes, or presenting dates and times, Humanize provides a simple and efficient solution. Its flexibility allows for customization to fit various needs, and its support for multiple languages makes it versatile for international applications. Overall, Humanize is a powerful addition to any Python project that involves data presentation, helping to bridge the gap between technical data and user-friendly information.

Author

Sona Avatar

Written by

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>