How to modify Python str object to produce a provenance report noting all files that modify a string -
i'd figure out how modify or replace instances of base string type str in python 3 program. want add provenance string. idea being anytime string created notes source file created string. there time string assigned i'd prior string's history follow string.
import sys import inspect import builtins class provenancestring(str): def __new__(cls, string, source=none): return super().__new__(cls, string) def __init__(self, value=none, source=none): super().__init__() self._myinit(value, source) def _myinit(self, value, source): self.history = [] self.sources = [] if hasattr(value, 'history'): self.history = value.history callerframerecord = inspect.stack()[2] frame = callerframerecord[0] info = inspect.getframeinfo(frame) self.history.append("{}:{}".format(info.filename, info.lineno)) if hasattr(value, 'sources'): self.sources = value.sources if source: self.sources.append(source) return def get_history(self): return self.history def get_sources(self): return self.sources #builtins.str = provenancestring # doesn't work if __name__ == '__main__': mystr = "this string" print (type(mystr)) #print(mystr.get_history()) # doesn't work #print(mystr.get_sources()) # doesn't work myobjstr = provenancestring("my prov string", "src1") print (type(myobjstr)) print(myobjstr.get_history()) print(myobjstr.get_sources()) myobjstr2 = provenancestring(myobjstr, "src2") print(myobjstr2.get_history()) print(myobjstr2.get_sources()) sys.exit(0)
results:
<class 'str'> <class '__main__.provenancestring'> ['./provenance.py:44'] ['src1'] ['./provenance.py:44', './provenance.py:50'] ['src1', 'src2']
so how provenancestring replace instances of base str can trace strings being generated , how flow through system. potentially might want report on how strings changed throughout system well.
Comments
Post a Comment