Eric V. Smith <e...@trueblade.com> added the comment:
Do you have some concrete use case for this, or is this a speculative feature request? This will do what you want, although it uses some undocumented internals of the _string module. I've only tested it a little, and I've done especially little testing for the error conditions: from _string import formatter_parser, formatter_field_name_split def partial_format(spec, *args, **kwargs): idx = 0 auto_number = False manual_number = False result = [] for literal_text, field_name, format_spec, conversion in formatter_parser(spec): result.append(literal_text) found = False if field_name is None: # We're at the end of the input. break if not field_name: # Auto-numbering fields. if manual_number: raise ValueError( "cannot switch from manual field specification to automatic field numbering" ) auto_number = True try: value = args[idx] idx += 1 found = True except IndexError: pass elif isinstance(field_name, int): # Numbered fields. if auto_number: raise ValueError( "cannot switch from automatic field number to manual field specification" ) manual_number = True try: value = args[field_name] found = True except IndexError: pass else: # Named fields. try: value = kwargs[field_name] found = True except KeyError: pass spec = f":{format_spec}" if format_spec else "" conv = f"!{conversion}" if conversion else "" if found: s = f"{{{conv}{spec}}}" result.append(s.format(value)) else: result.append(f"{{{field_name}{conv}{spec}}}") return "".join(result) ---------- nosy: +eric.smith _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue39842> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com