Best Python code snippet using yandex-tank
passfail.py
Source:passfail.py  
...130                    self.config['subject'],131                    self.config['condition'],132                    self.config['threshold'],133                    self.window_logic,134                    self.get_counting())135            return "%s: %s%s%s %s %d sec" % data136    def process_criteria_logic(self, tstmp, get_value):137        value = self.agg_logic(tstmp, get_value)138        state = self.condition(value, self.threshold)139        if self.window_logic == 'for':140            if state:141                self._start = min(self._start, tstmp)142                self._end = tstmp143            else:144                self._start = sys.maxsize145                self._end = 0146            if self.get_counting() >= self.window:147                self.trigger()148        elif self.window_logic == 'within' and state:149            self._start = tstmp - self.window + 1150            self._end = tstmp151            self.trigger()152        elif self.window_logic == 'over' and state:153            min_buffer_tstmp = min(self.agg_buffer.keys())154            self._start = min_buffer_tstmp155            self._end = tstmp156            if self.get_counting() >= self.window:157                self.trigger()158        logging.debug("%s %s: %s", tstmp, self, state)159    def trigger(self):160        if not self.is_triggered:161            logging.warning("%s", self)162        self.is_triggered = True163    def check(self):164        """165        Interrupt the execution if desired condition occured166        :raise AutomatedShutdown:167        """168        if self.stop and self.is_triggered:169            if self.fail:170                logging.info("Pass/Fail criterion triggered shutdown: %s", self)171                raise AutomatedShutdown("%s" % self)172            else:173                return True174        return False175    @abstractmethod176    def _get_field_functor(self, subject, percentage):177        pass178    def _get_condition_functor(self, cond):179        if cond == '=' or cond == '==':180            return lambda x, y: x == y181        elif cond == '>':182            return lambda x, y: x > y183        elif cond == '>=':184            return lambda x, y: x >= y185        elif cond == '<':186            return lambda x, y: x < y187        elif cond == '<=':188            return lambda x, y: x <= y189        else:190            raise TaurusConfigError("Unsupported fail criteria condition: %s" % cond)191    def _get_aggregator_functor(self, logic, _subject):192        if logic == 'for':193            return lambda tstmp, value: value194        elif logic in ('within', 'over'):195            return self._within_aggregator_avg  # FIXME: having simple average for percented values is a bit wrong196        else:197            raise TaurusConfigError("Unsupported window logic: %s" % logic)198    def _get_windowed_points(self, tstmp, value):199        self.agg_buffer[tstmp] = value200        keys = list(self.agg_buffer.keys())201        for tstmp_old in keys:202            if tstmp_old <= tstmp - self.window:203                del self.agg_buffer[tstmp_old]204                continue205            break206        return viewvalues(self.agg_buffer)207    def _within_aggregator_sum(self, tstmp, value):208        return sum(self._get_windowed_points(tstmp, value))209    def _within_aggregator_avg(self, tstmp, value):210        points = self._get_windowed_points(tstmp, value)211        return sum(points) / len(points)212    def get_counting(self):213        return self._end - self._start + 1214class DataCriterion(FailCriterion):215    """216    errors?217    duration (less or more than expected)218    errors in tools log?219    steady and threshold220    negate condition221    a way to inform other modules about the reason and mark the moment of start counting222    and trigger countdown for windowed223    :type config: dict224    :type owner: bzt.engine.EngineModule225    """226    def __init__(self, config, owner):227        super(DataCriterion, self).__init__(config, owner)228        self.label = config.get('label', '')229        self.selector = DataPoint.CURRENT if self.window > 0 else DataPoint.CUMULATIVE230    def aggregated_second(self, data):231        """232        Main criteria logic contained here233        :type data: bzt.modules.aggregator.DataPoint234        """235        part = data[self.selector]236        if self.label not in part:237            logging.debug("No label %s in %s", self.label, part.keys())238            return239        val = self.get_value(part[self.label])240        self.process_criteria_logic(data[DataPoint.TIMESTAMP], val)241    def _get_field_functor(self, subject, percentage):242        if subject == 'avg-rt':243            if percentage:244                raise TaurusConfigError("Percentage threshold is not applicable for %s" % subject)245            return lambda x: x[KPISet.AVG_RESP_TIME]246        elif subject == 'avg-lt':247            if percentage:248                raise TaurusConfigError("Percentage threshold is not applicable for %s" % subject)249            return lambda x: x[KPISet.AVG_LATENCY]250        elif subject == 'avg-ct':251            if percentage:252                raise TaurusConfigError("Percentage threshold is not applicable for %s" % subject)253            return lambda x: x[KPISet.AVG_CONN_TIME]254        elif subject == 'stdev-rt':255            if percentage:256                raise TaurusConfigError("Percentage threshold is not applicable for %s" % subject)257            return lambda x: x[KPISet.STDEV_RESP_TIME]258        elif subject.startswith('concurr'):259            if percentage:260                raise TaurusConfigError("Percentage threshold is not applicable for %s" % subject)261            return lambda x: x[KPISet.CONCURRENCY]262        elif subject == 'hits':263            if percentage:264                raise TaurusConfigError("Percentage threshold is not applicable for %s" % subject)265            return lambda x: x[KPISet.SAMPLE_COUNT]266        elif subject.startswith('succ'):267            if percentage:268                return lambda x: 100.0 * x[KPISet.SUCCESSES] / x[KPISet.SAMPLE_COUNT]269            else:270                return lambda x: x[KPISet.SUCCESSES]271        elif subject.startswith('fail'):272            if percentage:273                return lambda x: 100.0 * x[KPISet.FAILURES] / x[KPISet.SAMPLE_COUNT]274            else:275                return lambda x: x[KPISet.FAILURES]276        elif subject.startswith('p'):277            if percentage:278                raise TaurusConfigError("Percentage threshold is not applicable for %s" % subject)279            level = str(float(subject[1:]))280            return lambda x: x[KPISet.PERCENTILES][level] if level in x[KPISet.PERCENTILES] else 0281        elif subject.startswith('rc'):282            count = lambda x: sum([283                                      x[KPISet.RESP_CODES][y]284                                      for y in x[KPISet.RESP_CODES].keys()285                                      if fnmatch.fnmatch(y, subject[2:])286                                      ])287            if percentage:288                return lambda x: 100.0 * count(x) / float(x[KPISet.SAMPLE_COUNT])289            else:290                return count291        else:292            raise TaurusConfigError("Unsupported fail criteria subject: %s" % subject)293    def _get_aggregator_functor(self, logic, subj):294        if logic in ('within', "over") and not self.percentage:295            if subj in ('hits',) or subj.startswith('succ') or subj.startswith('fail') or subj.startswith('rc'):296                return self._within_aggregator_sum297        return super(DataCriterion, self)._get_aggregator_functor(logic, subj)298    @staticmethod299    def string_to_config(crit_config):300        """301        Parse string like "avg-rt of label>100ms for 1m, continue as non-failed"302        into config dict303        :type crit_config: str304        :rtype: dict305        """306        res = BetterDict()307        res.merge({308            "subject": None,309            "condition": None,310            "threshold": None,311            "logic": "for",312            "timeframe": 0,313            "label": "",314            "stop": True,315            "fail": True,316            "message": None,317        })318        if ':' in crit_config:319            res['message'] = crit_config[:crit_config.index(':')].strip()320            crit_config = crit_config[crit_config.index(':') + 1:].strip()321        if ',' in crit_config:322            crit_str = crit_config[:crit_config.index(',')].strip()323            action_str = crit_config[crit_config.index(',') + 1:].strip()324        else:325            crit_str = crit_config326            action_str = ""327        crit_pat = re.compile(r"([\w?*.-]+)(\s*of\s*([\S ]+))?\s*([<>=]+)\s*(\S+)(\s+(for|within|over)\s+(\S+))?")328        crit_match = crit_pat.match(crit_str.strip())329        if not crit_match:330            raise TaurusConfigError("Criteria string is malformed in its condition part: %s" % crit_str)331        crit_groups = crit_match.groups()332        res["subject"] = crit_groups[0]333        res["condition"] = crit_groups[3]334        res["threshold"] = crit_groups[4]335        if crit_groups[2]:336            res["label"] = crit_groups[2]337        if crit_groups[6]:338            res["logic"] = crit_groups[6]339        if crit_groups[7]:340            res["timeframe"] = crit_groups[7]341        if action_str:342            action_pat = re.compile(r"(stop|continue)(\s+as\s+(failed|non-failed))?")343            act_match = action_pat.match(action_str.strip())344            if not act_match:345                raise TaurusConfigError("Criteria string is malformed in its action part: %s" % action_str)346            action_groups = act_match.groups()347            res["stop"] = action_groups[0] != "continue"348            res["fail"] = action_groups[2] is None or action_groups[2] == "failed"349        return res350class PassFailWidget(Pile, PrioritizedWidget):351    """352    Represents console widget for pass/fail criteria visualisation353    If criterion is failing, it will be displayed on the widget354    return urwid widget355    :type failing_criteria: list[FailCriterion]356    """357    def __init__(self, pass_fail_reporter):358        self.pass_fail_reporter = pass_fail_reporter359        self.failing_criteria = []360        self.text_widget = Text("")361        super(PassFailWidget, self).__init__([self.text_widget])362        PrioritizedWidget.__init__(self)363    def __prepare_colors(self):364        """365        returns tuple ("color", text)366        :return:367        """368        result = []369        for failing_criterion in self.failing_criteria:370            if failing_criterion.window:371                percent = failing_criterion.get_counting() / failing_criterion.window372            else:373                percent = 1374            color = 'stat-txt'375            if 0.5 <= percent < 0.8:376                color = 'pf-3'377            elif 0.8 <= percent < 1:378                color = 'pf-4'379            elif 1 <= percent:  # pylint: disable=misplaced-comparison-constant380                color = 'pf-5'381            result.append((color, "%s\n" % failing_criterion))382        return result383    def update(self):384        """385        updates widget text386        :return:387        """388        self.text_widget.set_text("")389        self.failing_criteria = [x for x in self.pass_fail_reporter.criteria if x.get_counting() > 0]390        if self.failing_criteria:391            widget_text = self.__prepare_colors()392            self.text_widget.set_text(widget_text)...object-oriented.py
Source:object-oriented.py  
...65    @classmethod66    def how_many(cls):67        return cls.count68    @classmethod69    def get_counting(cls):70        return cls.__counting71print Person.how_many(), Person.get_counting()72bob = Person("Bob")73print Person.how_many(), Person.get_counting()...strategy.py
Source:strategy.py  
1def get_counting(n_train, epoch):2    return StopPolicy(n_train, epoch)3class StopPolicy:4    def __init__(self, n_train, epoch):5        self.epoch = epoch6        self.n_train = n_train7        self.step_loss = None8        self.epoch_losses = []9        self.val_losses=[]10        self.min_val_loss = None11        self.count_epoch=012        self.count_step=013    def update_step(self,loss):14        self.count_step +=115        if self.step_loss == None:16            self.step_loss = loss17        else:18            self.step_loss+=loss19    def update_validation_loss(self,loss):20        self.val_losses.append(loss)21        if self.min_val_loss == None:22            self.min_val_loss = loss23        elif loss < self.min_val_loss:24            self.min_val_loss = loss25    def update_validation(self,sth):26        self.update_validation_loss(sth['loss'])27    def update_epoch(self):28        self.count_epoch+=129        epoch_loss = self.step_loss/self.n_train30        self.epoch_losses.append(epoch_loss)31        self.step_loss = None32        return epoch_loss33    #def check_validation(self):34    #    return self.count_step%(self.n_train//self.bs)==035    def check_continue(self):36        return self.count_epoch < self.epoch37    38if __name__ == '__main__':39    from datasets import *40    import torch.nn as nn41    base_dir = '/mnt/md0/_datasets/OralCavity/TMA_arranged/WU/data4disease/patch10x224s1.0e0.8'42    r=043    f=044    augments='flip,simple_color'45    bs=3246    data = get_loaders(base_dir,r,f,bs,augments)47    train,val=data48    criterion = nn.CrossEntropyLoss()49    # test stop policy50    stopPolicy = get_counting(data, 2, bs)51    while stopPolicy.check_continue():52        for batch in train:53            size = batch['image'].size(0)54            outputs = torch.rand(size,2)55            y = torch.randint(0,2,(size,))56            #outputs = torch.randn(3,5,requires_grad=True)57            #y = torch.empty(3,dtype=torch.long).random_(5)58            loss = criterion(outputs, y)59            stopPolicy.update_step(loss.item()*size)60            if stopPolicy.check_validation():61                print('check validation')...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!!
