Jeffrey Yasskin wrote: > I'm looking through a couple of the OS X tests and have run into the > question of what to do with four-character codes. (For those of you > who are unfamiliar with these, Apple, around the dawn of time, decided > that C constants like 'TEXT' (yes, those are single quotes) would > compile to the uint32_t 0x54455854 (or maybe the other-endian version > of that) so they could use these as cheap-but-readable type > identifiers.) In Python 2, these are represented as 'str' instances, > which PyMac_GetOSType() in Python/mactoolboxglue.c converts to the > native int format. For Python 3, right now they're str8's, but str8 is > theoretically supposed to go away. Because they're binary constants > displayed as ASCII, not unicode text, I initially thought that 'bytes' > was the appropriate type. Unfortunately, bytes is mutable, and I think > it makes sense to hash these constants (and some code in aepack.py > does). > > So, I'm stuck and wanted to ask the list for input. I see 5 options: > 1) Make these str instances so they're immutable and just rely on > convention and runtime errors to keep them in ascii. > 2) Make them bytes, and cast them to something else when you want to > make them keys in a dict. > 3) Keep them str8 and give up on getting rid of it. > 4) Make bytes immutable, add a 'buffer' type which acts like the > current bytes type, and make these codes instances of bytes. [probably > impossible this late in the game] > 5) Make a new hashable class for these codes which converts them to > and from ints and bytes and becomes the general argument type for the > apple platform interface. [Cleanest, but lots of work that I'm not > volunteering to do] > > Thoughts? > Jeffrey
Yeah. I like the idea of converting them to integers, but I don't think you need a special hash table class for that. Instead, create a wrapper class for the four character codes: TextId = FourCharId("TEXT") i = int(TextId) # Integer value s = str(TextId) # String representation some_map[TextId] = "Some Text" # Can use as dict key The wrapper class is an immutable class that handles conversion to integer form in the constructor, hashing, and has a __str__ and __repr__ method that produces the original input string. Then you can use that as a key to a regular dict. -- Talin _______________________________________________ Python-3000 mailing list Python-3000@python.org http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com