Best Python code snippet using avocado_python
tracemalloc.py
Source:tracemalloc.py
...174 line = linecache.getline(frame.filename, frame.lineno).strip()175 if line:176 lines.append(' %s' % line)177 return lines178def get_object_traceback(obj):179 """180 Get the traceback where the Python object *obj* was allocated.181 Return a Traceback instance.182 Return None if the tracemalloc module is not tracing memory allocations or183 did not trace the allocation of the object.184 """185 frames = _get_object_traceback(obj)186 if frames is not None:187 return Traceback(frames)188 else:189 return None190class Trace:191 """192 Trace of a memory block.193 """194 __slots__ = ("_trace",)195 def __init__(self, trace):196 # trace is a tuple: (size, traceback), see Traceback constructor197 # for the format of the traceback tuple198 self._trace = trace199 @property200 def size(self):201 return self._trace[0]202 @property203 def traceback(self):204 return Traceback(self._trace[1])205 def __eq__(self, other):206 return (self._trace == other._trace)207 def __hash__(self):208 return hash(self._trace)209 def __str__(self):210 return "%s: %s" % (self.traceback, _format_size(self.size, False))211 def __repr__(self):212 return ("<Trace size=%s, traceback=%r>"213 % (_format_size(self.size, False), self.traceback))214class _Traces(Sequence):215 def __init__(self, traces):216 Sequence.__init__(self)217 # traces is a tuple of trace tuples: see Trace constructor218 self._traces = traces219 def __len__(self):220 return len(self._traces)221 def __getitem__(self, index):222 if isinstance(index, slice):223 return tuple(Trace(trace) for trace in self._traces[index])224 else:225 return Trace(self._traces[index])226 def __contains__(self, trace):227 return trace._trace in self._traces228 def __eq__(self, other):229 return (self._traces == other._traces)230 def __repr__(self):231 return "<Traces len=%s>" % len(self)232def _normalize_filename(filename):233 filename = os.path.normcase(filename)234 if filename.endswith(('.pyc', '.pyo')):235 filename = filename[:-1]236 return filename237class Filter:238 def __init__(self, inclusive, filename_pattern,239 lineno=None, all_frames=False):240 self.inclusive = inclusive241 self._filename_pattern = _normalize_filename(filename_pattern)242 self.lineno = lineno243 self.all_frames = all_frames244 @property245 def filename_pattern(self):246 return self._filename_pattern247 def __match_frame(self, filename, lineno):248 filename = _normalize_filename(filename)249 if not fnmatch.fnmatch(filename, self._filename_pattern):250 return False251 if self.lineno is None:252 return True253 else:254 return (lineno == self.lineno)255 def _match_frame(self, filename, lineno):256 return self.__match_frame(filename, lineno) ^ (not self.inclusive)257 def _match_traceback(self, traceback):258 if self.all_frames:259 if any(self.__match_frame(filename, lineno)260 for filename, lineno in traceback):261 return self.inclusive262 else:263 return (not self.inclusive)264 else:265 filename, lineno = traceback[0]266 return self._match_frame(filename, lineno)267class Snapshot:268 """269 Snapshot of traces of memory blocks allocated by Python.270 """271 def __init__(self, traces, traceback_limit):272 # traces is a tuple of trace tuples: see _Traces constructor for273 # the exact format274 self.traces = _Traces(traces)275 self.traceback_limit = traceback_limit276 def dump(self, filename):277 """278 Write the snapshot into a file.279 """280 with open(filename, "wb") as fp:281 pickle.dump(self, fp, pickle.HIGHEST_PROTOCOL)282 @staticmethod283 def load(filename):284 """285 Load a snapshot from a file.286 """287 with open(filename, "rb") as fp:288 return pickle.load(fp)289 def _filter_trace(self, include_filters, exclude_filters, trace):290 traceback = trace[1]291 if include_filters:292 if not any(trace_filter._match_traceback(traceback)293 for trace_filter in include_filters):294 return False295 if exclude_filters:296 if any(not trace_filter._match_traceback(traceback)297 for trace_filter in exclude_filters):298 return False299 return True300 def filter_traces(self, filters):301 """302 Create a new Snapshot instance with a filtered traces sequence, filters303 is a list of Filter instances. If filters is an empty list, return a304 new Snapshot instance with a copy of the traces.305 """306 if not isinstance(filters, Iterable):307 raise TypeError("filters must be a list of filters, not %s"308 % type(filters).__name__)309 if filters:310 include_filters = []...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!