Best Python code snippet using fMBT_python
cb2_6_12_sol_1.py
Source:cb2_6_12_sol_1.py  
1import copy2class ChangeCheckerMixin(object):3    containerItems = {dict: dict.iteritems, list: enumerate}4    immutable = False5    def snapshot(self):6        ''' create a snapshot of self's state -- like a shallow copy, but7            recursing over container types (not over general instances:8            instances must keep track of their own changes if needed).  '''9        if self.immutable:10            return11        self._snapshot = self._copy_container(self.__dict__)12    def makeImmutable(self):13        ''' the instance state can't change any more, set .immutable '''14        self.immutable = True15        try:16            del self._snapshot17        except AttributeError:18            pass19    def _copy_container(self, container):20        ''' semi-shallow copy, recursing on container types only '''21        new_container = copy.copy(container)22        for k, v in self.containerItems[type(new_container)](new_container):23            if type(v) in self.containerItems:24                new_container[k] = self._copy_container(v)25            elif hasattr(v, 'snapshot'):26                v.snapshot()27        return new_container28    def isChanged(self):29        ''' True if self's state is changed since the last snapshot '''30        if self.immutable:31            return False32        # remove snapshot from self.__dict__, put it back at the end33        snap = self.__dict__.pop('_snapshot', None)34        if snap is None:35            return True36        try:37            return self._checkContainer(self.__dict__, snap)38        finally:39            self._snapshot = snap40    def _checkContainer(self, container, snapshot):41        ''' return True if the container and its snapshot differ '''42        if len(container) != len(snapshot):43            return True44        for k, v in self.containerItems[type(container)](container):45            try:46                ov = snapshot[k]47            except LookupError:48                return True49            if self._checkItem(v, ov):50                return True51        return False52    def _checkItem(self, newitem, olditem):53        ''' compare newitem and olditem.  If they are containers, call54            self._checkContainer recursively.  If they're an instance with55            an 'isChanged' method, delegate to that method.  Otherwise,56            return True if the items differ. '''57        if type(newitem) != type(olditem):58            return True59        if type(newitem) in self.containerItems:60            return self._checkContainer(newitem, olditem)61        if newitem is olditem:62            method_isChanged = getattr(newitem, 'isChanged', None)63            if method_isChanged is None:64                return False65            return method_isChanged()...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!!
