Best Python code snippet using yandex-tank
Trading.py
Source:Trading.py
...647 diff = price_max - price_min648 649 data = {}650 if price != 0 and (price <= price_min):651 data['ratio1'] = float(self.__truncate(price_min, 2))652 elif price == 0:653 data['ratio1'] = float(self.__truncate(price_min, 2))654 if price != 0 and (price > price_min) and (price <= (price_max - 0.768 * diff)):655 data['ratio1'] = float(self.__truncate(price_min, 2))656 data['ratio0_768'] = float(self.__truncate(price_max - 0.768 * diff, 2))657 elif price == 0:658 data['ratio0_768'] = float(self.__truncate(price_max - 0.768 * diff, 2)) 659 if price != 0 and (price > (price_max - 0.768 * diff)) and (price <= (price_max - 0.618 * diff)):660 data['ratio0_768'] = float(self.__truncate(price_max - 0.768 * diff, 2))661 data['ratio0_618'] = float(self.__truncate(price_max - 0.618 * diff, 2))662 elif price == 0:663 data['ratio0_618'] = float(self.__truncate(price_max - 0.618 * diff, 2)) 664 if price != 0 and (price > (price_max - 0.618 * diff)) and (price <= (price_max - 0.5 * diff)):665 data['ratio0_618'] = float(self.__truncate(price_max - 0.618 * diff, 2))666 data['ratio0_5'] = float(self.__truncate(price_max - 0.5 * diff, 2))667 elif price == 0:668 data['ratio0_5'] = float(self.__truncate(price_max - 0.5 * diff, 2))669 if price != 0 and (price > (price_max - 0.5 * diff)) and (price <= (price_max - 0.382 * diff)):670 data['ratio0_5'] = float(self.__truncate(price_max - 0.5 * diff, 2))671 data['ratio0_382'] = float(self.__truncate(price_max - 0.382 * diff, 2))672 elif price == 0:673 data['ratio0_382'] = float(self.__truncate(price_max - 0.382 * diff, 2))674 if price != 0 and (price > (price_max - 0.382 * diff)) and (price <= (price_max - 0.286 * diff)):675 data['ratio0_382'] = float(self.__truncate(price_max - 0.382 * diff, 2))676 data['ratio0_286'] = float(self.__truncate(price_max - 0.286 * diff, 2))677 elif price == 0:678 data['ratio0_286'] = float(self.__truncate(price_max - 0.286 * diff, 2))679 if price != 0 and (price > (price_max - 0.286 * diff)) and (price <= price_max):680 data['ratio0_286'] = float(self.__truncate(price_max - 0.286 * diff, 2)) 681 data['ratio0'] = float(self.__truncate(price_max, 2))682 elif price == 0:683 data['ratio0'] = float(self.__truncate(price_max, 2))684 if price != 0 and (price < (price_max + 0.272 * diff)) and (price >= price_max):685 data['ratio0'] = float(self.__truncate(price_max, 2))686 data['ratio1_272'] = float(self.__truncate(price_max + 0.272 * diff, 2))687 elif price == 0:688 data['ratio1_272'] = float(self.__truncate(price_max + 0.272 * diff, 2))689 if price != 0 and (price < (price_max + 0.414 * diff)) and (price >= (price_max + 0.272 * diff)):690 data['ratio1_272'] = float(self.__truncate(price_max, 2))691 data['ratio1_414'] = float(self.__truncate(price_max + 0.414 * diff, 2))692 elif price == 0:693 data['ratio1_414'] = float(self.__truncate(price_max + 0.414 * diff, 2))694 if price != 0 and (price < (price_max + 0.618 * diff)) and (price >= (price_max + 0.414 * diff)):695 data['ratio1_618'] = float(self.__truncate(price_max + 0.618 * diff, 2))696 elif price == 0:697 data['ratio1_618'] = float(self.__truncate(price_max + 0.618 * diff, 2))698 return data699 def saveCSV(self, filename: str='tradingdata.csv') -> None:700 """Saves the DataFrame to an uncompressed CSV."""701 p = compile(r"^[\w\-. ]+$")702 if not p.match(filename):703 raise TypeError('Filename required.')704 if not isinstance(self.df, DataFrame):705 raise TypeError('Pandas DataFrame required.')706 try:707 self.df.to_csv(filename)708 except OSError:709 Logger.critical(f'Unable to save: {filename}')710 def __calculateSupportResistenceLevels(self):711 """Support and Resistance levels. (private function)"""712 for i in range(2, self.df.shape[0] - 2):713 if self.__isSupport(self.df, i):714 l = self.df['low'][i]715 if self.__isFarFromLevel(l):716 self.levels.append((i, l))717 elif self.__isResistance(self.df, i):718 l = self.df['high'][i]719 if self.__isFarFromLevel(l):720 self.levels.append((i, l))721 return self.levels722 def __isSupport(self, df, i) -> bool:723 """Is support level? (private function)"""724 c1 = df['low'][i] < df['low'][i - 1]725 c2 = df['low'][i] < df['low'][i + 1]726 c3 = df['low'][i + 1] < df['low'][i + 2]727 c4 = df['low'][i - 1] < df['low'][i - 2]728 support = c1 and c2 and c3 and c4729 return support730 def __isResistance(self, df, i) -> bool:731 """Is resistance level? (private function)"""732 c1 = df['high'][i] > df['high'][i - 1]733 c2 = df['high'][i] > df['high'][i + 1]734 c3 = df['high'][i + 1] > df['high'][i + 2]735 c4 = df['high'][i - 1] > df['high'][i - 2]736 resistance = c1 and c2 and c3 and c4737 return resistance738 def __isFarFromLevel(self, l) -> float:739 """Is far from support level? (private function)"""740 s = mean(self.df['high'] - self.df['low'])741 return np_sum([abs(l-x) < s for x in self.levels]) == 0742 def __truncate(self, f, n) -> float:...
Writer.py
Source:Writer.py
...24 write_to_terminal()25 Print metrics for each class to the terminal26 write_to_csv()27 Print metrics for each class to the .csv file28 __truncate(number, digits=4)29 Trim number to n-th decimal place30 """31 def __init__(self, data):32 self._precision = data["data"]["pr"]33 self._recall = data["data"]["rc"]34 self._score = data["data"]["sc"]35 self._f1 = data["data"]["f1_m"]36 self._ap = data["data"]["ap"]37 self._classes = data["classes"]38 def write_to_terminal(self) -> None:39 """40 Print metrics for each class to the terminal41 """42 table = BeautifulTable()43 table.column_headers = [44 f"{colored('Class', 'blue', attrs=['bold'])}",45 f"{colored('Precision', 'blue', attrs=['bold'])}",46 f"{colored('Recall', 'blue', attrs=['bold'])}",47 f"{colored('F1', 'blue', attrs=['bold'])}",48 f"{colored('AP', 'blue', attrs=['bold'])}",49 ]50 for i, j, k, z, cl in zip(51 self._f1, self._precision, self._recall, range(len(self._ap)), self._classes52 ):53 index = np.argmax(i)54 table.append_row(55 [56 f"{colored(f'{cl}', 'green', attrs=['bold'])}",57 self.__truncate(j[index]),58 self.__truncate(k[index]),59 self.__truncate(2 * j[index] * k[index] / (k[index] + j[index])),60 self.__truncate(self._ap[z]),61 ]62 )63 print(table)64 print(65 f"{colored('mAP:', 'red', attrs=['bold'])} {self.__truncate(sum(self._ap) / len(self._ap))}"66 )67 def write_to_csv(self) -> None:68 """69 Print metrics for each class to the .csv file70 """71 precisions_list = [self.__truncate(i[np.argmax(i)]) for i in self._precision]72 recall_list = [self.__truncate(i[np.argmax(i)]) for i in self._recall]73 f1_list = [self.__truncate(i[np.argmax(i)]) for i in self._f1]74 ap_list = [self.__truncate(self._ap[i]) for i in range(len(self._ap))]75 frame = pd.DataFrame(76 {77 " ": self._classes,78 "precision": precisions_list,79 "recall": recall_list,80 "f1": f1_list,81 "ap": ap_list,82 "map": self.__truncate(sum(self._ap) / len(self._ap)),83 }84 )85 if not os.path.exists("./results"):86 os.mkdir("./results")87 frame.to_csv(88 f"results/meters_{datetime.datetime.today().strftime('%Y-%m-%d_%H:%M:%S')}.csv",89 sep=";",90 index=False,91 index_label=True,92 )93 @classmethod94 def __truncate(cls, number: float, digits=4) -> float:95 """96 Trim number to nth decimal place97 :param number: floating point number98 :param digits: the digit to which the number is to be truncated99 :return: truncated number100 """101 stepper = 10.0**digits...
vocab.py
Source:vocab.py
...12 self.__tok2id = OrderedDict()13 if add_null:14 self.__tok2id[NULL] = 015 for t in tokens:16 t = self.__truncate(t)17 if t not in self.__tok2id:18 self.__tok2id[t] = len(self.__tok2id)19 self.__id2tok = OrderedDict({i: t for (t, i) in self.__tok2id.items()})20 def has(self, token):21 token = self.__truncate(token)22 if isinstance(token, int):23 return token in self.__id2tok24 elif isinstance(token, str):25 return token in self.__tok2id26 else:27 raise TypeError(f"VocabDict only accepts str or int. "28 f"{type(token)} was given.")29 def to_id(self, token, str_cast=False):30 if str_cast:31 token = str(token)32 token = self.__truncate(token)33 return self.__tok2id[token]34 def to_id_list(self, tokens, str_cast=False):35 return [self.to_id(t, str_cast) for t in tokens]36 def to_id_batch(self, list_of_tokens, str_cast=False):37 return [self.to_id_list(lst, str_cast) for lst in list_of_tokens]38 def to_str(self, id, int_cast=False):39 if int_cast:40 id = int(id)41 return self.__id2tok[id]42 def to_str_list(self, ids, int_cast=False):43 return [self.to_str(i, int_cast=int_cast) for i in ids]44 def to_str_batch(self, list_of_ids, int_cast=False):45 return [self.to_str_list(lst, int_cast) for lst in list_of_ids]46 def __truncate(self, t):47 return t[:self.max_token_length]48 @property49 def tok2id(self):50 return self.__tok2id51 @property52 def id2tok(self):53 return self.__id2tok54 @property55 def size(self):56 return len(self.__tok2id)57 @property58 def is_empty(self):59 return self.size == 060 @property...
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!!