I know one could extend Formatter to provide additional presentation types as discussed in PEP3101, but this is entirely too slow for my needs. I’m curious what some other options might be for injecting custom presentation types for strings.
Currently, the only other option that comes to mind is to inspect the {vars} of a string, note the presentation type and index, strip custom presentation type, format, and then post format the result for my needs.
Are there any other options to avoid the post processing while taking advantage of the speed of format?
I worked out a solution that seems to have significantly less overhead in cython, though the same could be done in python and ill provide the example here (not sure of the overhead).
According to the python docs, an object can implement the __ format__ method and receive the format spec. In cython Im implementing my own uobj type that acts as a generic for args and kwargs passed to str.format. The same thing in python ( as a generic example for escaping < and > ) looks like this.
And so now, uobj can store a user object (str or dict in following example) and then can be accessed like
And where the casting to uobj would occur inside of a fmt function on *args and **kwargs. There’s still some more details to solve here, such as i noticed ints failed to parse in my unittests, and uobj would need to be unpackable via *uobj and **uobj to convert objects on demand, though i may need to break this out into respective clones of a list and a dict. Still, this seems to be the best path for me.
edit
seems ill be reading up on emulating container types here
http://docs.python.org/reference/datamodel.html#emulating-container-types