gaogaotiantian / objprint

A library that can print Python objects in human readable format
Apache License 2.0
510 stars 43 forks source link

Support print number in hexadecimal #77

Closed sledgeh4w closed 1 year ago

sledgeh4w commented 2 years ago

I use objpring to print the elf header object, hope the number can be displayed in hexadecimal.

gaogaotiantian commented 2 years ago

Do you mean in the elf header object, there are attributes that are int, and you want to print them as hex? That sounds like an interesting feature. I guess I can implement a way to customize print for a certain type.

sledgeh4w commented 2 years ago

Yes, the way you said would be more general.

happytree718 commented 1 year ago

I have a proposal for addressing this issue. I am considering adding two more arguments to the config: int_to_hex(False) and int_to_bin(False) to enable printing all int type objects as either hexadecimal or binary. We could potentially include an option for octal representation as well.

If both arguments are set to True, the op() call could either return an error indicating a conflicting configuration or prioritize the desired format (preferably hexadecimal).

I believe this solution would provide a more general and customizable approach to handling different number representations. If you agree that this would be a good fix for the problem, I would be happy to implement it.

gaogaotiantian commented 1 year ago

Sorry I forgot to reply to this proposal. I don’t think having a hook for the integer is a generic solution for this issue. Python already have many formatting capabilities and we should not reinvent the wheels. I believe we should add a register method for the op object so the users can register a representative function (similar to str) and return an arbitrary string they like. We can’t format every possibility, we should leave the flexibility to the user and provide them a way to hook in their formatting functions.

happytree718 commented 1 year ago

Sure, this sounds like a better solution. To what extent should this representative function cover? For example, should we only give users the ability to customize basic data types like int, float, str, etc. or should we also support custom representation for more complicated structures like dict, list, etc? BTW, I am closing the pull request.

gaogaotiantian commented 1 year ago

The register function should take a type and a function that returns a string. It would cover all the use cases, including customized classes.

gaogaotiantian commented 1 year ago

Solved in #91