Belle II Software development
TolerateMissingKeyFormatter Class Reference
Inheritance diagram for TolerateMissingKeyFormatter:

Public Member Functions

def get_value (self, key, args, kwds)
 
def convert_field (self, value, conversion)
 
def format_field (self, value, format_spec)
 

Detailed Description

A string formatter that implements format most equivalent to the str.format, but does not replace keys,
that are not present in the replacements dictionary

Example
-------
>>> formatter = TolerateMissingKeyFormatter()
>>> template = "{present}_{missing}"
>>> partially_substituted = formatter.format(template, present="replaced")
>>> print partially_substituted
"replaced_{missing}"

Definition at line 68 of file tolerate_missing_key_formatter.py.

Member Function Documentation

◆ convert_field()

def convert_field (   self,
  value,
  conversion 
)
Applies the conversion to the value.

Overrides the standard method such that a potential conversion is attached to the NoReplacementField

Definition at line 99 of file tolerate_missing_key_formatter.py.

99 def convert_field(self, value, conversion):
100 """Applies the conversion to the value.
101
102 Overrides the standard method such that a potential conversion is attached to the NoReplacementField
103 """
104
105 if isinstance(value, NoReplacementField):
106 conversion = conversion or None
107 value.conversion = conversion
108 return value
109 else:
110 return super().convert_field(value, conversion)
111

◆ format_field()

def format_field (   self,
  value,
  format_spec 
)
Applies the conversion to the value.

Overrides the standard method such that a potential format_spec is attached to the NoReplacementField.
Than composes the replacement_field specification to be inserted in the formatted string.
The outcome should be equivalent to the unformatted string for missing keys.

Definition at line 112 of file tolerate_missing_key_formatter.py.

112 def format_field(self, value, format_spec):
113 """Applies the conversion to the value.
114
115 Overrides the standard method such that a potential format_spec is attached to the NoReplacementField.
116 Than composes the replacement_field specification to be inserted in the formatted string.
117 The outcome should be equivalent to the unformatted string for missing keys.
118 """
119
120 if isinstance(value, NoReplacementField):
121 format_spec = format_spec or None
122 value.format_spec = format_spec
123 return value.compose()
124 else:
125 return super().format_field(value, format_spec)
126
127

◆ get_value()

def get_value (   self,
  key,
  args,
  kwds 
)
Retrieves the value that corresponds to the key from either the positional or
the keyword arguments given to format

Overrides the standard lookup such that missing keys in the keyword arguments or
transformed in a NoReplacementField signal object.

Definition at line 83 of file tolerate_missing_key_formatter.py.

83 def get_value(self, key, args, kwds):
84 """Retrieves the value that corresponds to the key from either the positional or
85 the keyword arguments given to format
86
87 Overrides the standard lookup such that missing keys in the keyword arguments or
88 transformed in a NoReplacementField signal object.
89 """
90
91 if isinstance(key, str):
92 try:
93 return kwds[key]
94 except KeyError:
95 return NoReplacementField(key)
96 else:
97 return super().get_value(key, args, kwds)
98

The documentation for this class was generated from the following file: