Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6976673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:31:38+00:00 2026-05-27T17:31:38+00:00

I know one could extend Formatter to provide additional presentation types as discussed in

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:31:39+00:00Added an answer on May 27, 2026 at 5:31 pm

    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.

    class uobj:
        def __init__(self, obj):
            self.obj = obj
        def __format__(self, format_spec):
            if format_spec == 's':
                return str(self.obj)
            else:
                # edit, shoehorning this in for completeness
                # to call an original format spec as should probably
                # happen after you do your own processing, use __format__
                if isinstance(self.obj, (int, float)):
                    return self.obj.__format__(format_spec)
                    # so then a :.2f spec on uobj(123.456) would work as expected
                return str(self.obj).replace('<', '&lt;').replace('>', '&gt;')
        def __getitem__(self, key):
            return uobj(self.obj[key])
    

    And so now, uobj can store a user object (str or dict in following example) and then can be accessed like

    d = uobj({'a': '<b>asdf'})
    s = uobj('<span>qwer</span>')
    '{0:s} {d[a]}'.format(s, d=d)
    
    # ouputs: '<span>qwer</span>&lt;asdf'
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know one awkward solution for this taks will be : first use ct
This is a very very basic question and I know one way is to
I know one can add event listener for window.error. However when working with Iframes,
I'm learning Java, and I know one of the big complaints about newbie programmers
Suppose that my index has 3 fields: title, x and y. I know one
Any one know a way in delphi get a simple list (eg tstrings) of
Some one know what are development packages for X11 for CentOS 3?
does any one know how to get the current motherboard, processor or HD temperature
Does any one know how can I convert a BSTR to an int in
I know that one of the defining principles of Test driven development is that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.