Belle II Software light-2406-ragdoll
LogReplacementFilter Class Reference

Public Member Functions

def __init__ (self, out, replacements)
 
def write (self, data)
 
def __getattr__ (self, name)
 

Protected Attributes

 _out
 where to forward output
 
 _replacements
 what to replace
 
 _regex
 build a regular expression from dictionary keys
 

Detailed Description

Simple class to intercept anything written to python stdout and replace
a given set of strings with placehholders to improve reproducibility.

Definition at line 14 of file logfilter.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  out,
  replacements 
)
Setup the forwarding and replacements

Parameters:
    out (file object): Where to forward the output too
    replacements (dict(str, str)): Dictionary of strings and their replacements

Definition at line 20 of file logfilter.py.

20 def __init__(self, out, replacements):
21 """Setup the forwarding and replacements
22
23 Parameters:
24 out (file object): Where to forward the output too
25 replacements (dict(str, str)): Dictionary of strings and their replacements
26 """
27
28 self._out = out
29
30 self._replacements = replacements
31
32 self._regex = re.compile("|".join(re.escape(e) for e in replacements))
33

Member Function Documentation

◆ __getattr__()

def __getattr__ (   self,
  name 
)
Forward all other methods from the out stream

Definition at line 39 of file logfilter.py.

39 def __getattr__(self, name):
40 """Forward all other methods from the out stream"""
41 return getattr(self._out, name)

◆ write()

def write (   self,
  data 
)
Check all messages for strings to replace

Definition at line 34 of file logfilter.py.

34 def write(self, data):
35 """Check all messages for strings to replace"""
36 replaced = self._regex.sub(lambda m: self._replacements[m[0]], data)
37 self._out.write(replaced)
38

Member Data Documentation

◆ _out

_out
protected

where to forward output

Definition at line 28 of file logfilter.py.

◆ _regex

_regex
protected

build a regular expression from dictionary keys

Definition at line 32 of file logfilter.py.

◆ _replacements

_replacements
protected

what to replace

Definition at line 30 of file logfilter.py.


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