Set numpy printoptions based on bit-width/specific int dtype in a single print statement

I’m working with a lot of binary data, where my data can be a mix of bit-widths.
I want to print or log my data correctly formatted by bit-width, all in a single statement.

Simple example showing only 16 and 32 bit data:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import numpy as np
numbers_32_bit = np.array((0x1000_0000, 0x2000_0000, 0x3000_0000, 0x4000_0000),dtype=np.uint32)
numbers_16_bit = np.array((0x5000, 0x6000, 0x7000, 0x8000), dtype=np.uint16)
np_formatter = {
# Doesn't throw an error, but has no effect, as expected as per numpy docs
"uint32": lambda x: f"{x:08X}",
"uint16": lambda x: f"{x:04X}",
# Applies to all ints
"int": lambda x: f"0x{x:08X}"
}
np.set_printoptions(formatter=np_formatter)
print(f"32 bits: {numbers_32_bit}, 16 bits: {numbers_16_bit}")
</code>
<code>import numpy as np numbers_32_bit = np.array((0x1000_0000, 0x2000_0000, 0x3000_0000, 0x4000_0000),dtype=np.uint32) numbers_16_bit = np.array((0x5000, 0x6000, 0x7000, 0x8000), dtype=np.uint16) np_formatter = { # Doesn't throw an error, but has no effect, as expected as per numpy docs "uint32": lambda x: f"{x:08X}", "uint16": lambda x: f"{x:04X}", # Applies to all ints "int": lambda x: f"0x{x:08X}" } np.set_printoptions(formatter=np_formatter) print(f"32 bits: {numbers_32_bit}, 16 bits: {numbers_16_bit}") </code>
import numpy as np

numbers_32_bit = np.array((0x1000_0000, 0x2000_0000, 0x3000_0000, 0x4000_0000),dtype=np.uint32)
numbers_16_bit = np.array((0x5000, 0x6000, 0x7000, 0x8000), dtype=np.uint16)

np_formatter = {
    # Doesn't throw an error, but has no effect, as expected as per numpy docs
    "uint32": lambda x: f"{x:08X}",
    "uint16": lambda x: f"{x:04X}",
    
    # Applies to all ints
    "int": lambda x: f"0x{x:08X}"
}

np.set_printoptions(formatter=np_formatter)
print(f"32 bits: {numbers_32_bit}, 16 bits: {numbers_16_bit}")

Print output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>32 bits: [0x10000000 0x20000000 0x30000000 0x40000000], 16 bits: [0x00005000 0x00006000 0x00007000 0x00008000]
</code>
<code>32 bits: [0x10000000 0x20000000 0x30000000 0x40000000], 16 bits: [0x00005000 0x00006000 0x00007000 0x00008000] </code>
32 bits: [0x10000000 0x20000000 0x30000000 0x40000000], 16 bits: [0x00005000 0x00006000 0x00007000 0x00008000]

As you can see, the 16 bit numbers still have the 32-bit formatter applied, as it uses the int formatter, rather than uint16.

This works on a statement-by-statement basis, but isn’t great when you have lots of print/log statements throughout a project:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def format_bitwidths(n8=0, n16=0, n32=0, n64=0):
np.set_printoptions(formatter={"int": lambda x: f"0x{x:02X}"})
s8 = f"{n8}"
np.set_printoptions(formatter={"int": lambda x: f"0x{x:04X}"})
s16 = f"{n16}"
np.set_printoptions(formatter={"int": lambda x: f"0x{x:08X}"})
s32 = f"{n32}"
np.set_printoptions(formatter={"int": lambda x: f"0x{x:016X}"})
s64 = f"{n64}"
return s8, s16, s32, s64
_, str_16_bits, str_32_bits, _ = format_bitwidths(n16=numbers_16_bit, n32=numbers_32_bit)
print(f"32 bits: {str_32_bits}, 16 bits: {str_16_bits}")
</code>
<code>def format_bitwidths(n8=0, n16=0, n32=0, n64=0): np.set_printoptions(formatter={"int": lambda x: f"0x{x:02X}"}) s8 = f"{n8}" np.set_printoptions(formatter={"int": lambda x: f"0x{x:04X}"}) s16 = f"{n16}" np.set_printoptions(formatter={"int": lambda x: f"0x{x:08X}"}) s32 = f"{n32}" np.set_printoptions(formatter={"int": lambda x: f"0x{x:016X}"}) s64 = f"{n64}" return s8, s16, s32, s64 _, str_16_bits, str_32_bits, _ = format_bitwidths(n16=numbers_16_bit, n32=numbers_32_bit) print(f"32 bits: {str_32_bits}, 16 bits: {str_16_bits}") </code>
def format_bitwidths(n8=0, n16=0, n32=0, n64=0):
    np.set_printoptions(formatter={"int": lambda x: f"0x{x:02X}"})
    s8 = f"{n8}"
    np.set_printoptions(formatter={"int": lambda x: f"0x{x:04X}"})
    s16 = f"{n16}"
    np.set_printoptions(formatter={"int": lambda x: f"0x{x:08X}"})
    s32 = f"{n32}"
    np.set_printoptions(formatter={"int": lambda x: f"0x{x:016X}"})
    s64 = f"{n64}"
    return s8, s16, s32, s64


_, str_16_bits, str_32_bits, _ = format_bitwidths(n16=numbers_16_bit, n32=numbers_32_bit)
print(f"32 bits: {str_32_bits}, 16 bits: {str_16_bits}")

Print output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>32 bits: [0x10000000 0x20000000 0x30000000 0x40000000], 16 bits: [0x5000 0x6000 0x7000 0x8000]
</code>
<code>32 bits: [0x10000000 0x20000000 0x30000000 0x40000000], 16 bits: [0x5000 0x6000 0x7000 0x8000] </code>
32 bits: [0x10000000 0x20000000 0x30000000 0x40000000], 16 bits: [0x5000 0x6000 0x7000 0x8000]

Is there a way to set this once without having to call np.set_printoptions or format_bitwidths before each print/log?

If not, is there some other method which I can set once, and apply it across an entire script?

New contributor

rici1241 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật