Best Python code snippet using assertpy_python
varprecspinbox.py
Source:varprecspinbox.py
1# -*- coding: utf-8 -*-2#3# Author: Tomi Jylhä-Ollila, Finland 2018-20194#5# This file is part of Kunquat.6#7# CC0 1.0 Universal, http://creativecommons.org/publicdomain/zero/1.0/8#9# To the extent possible under law, Kunquat Affirmers have waived all10# copyright and related or neighboring rights to Kunquat.11#12import math13import string14from kunquat.tracker.ui.qt import *15class VarPrecSpinBox(QAbstractSpinBox):16 valueChanged = Signal(float, name='valueChanged')17 def __init__(self, step_decimals=0, max_decimals=6):18 super().__init__()19 assert step_decimals >= 020 assert max_decimals >= 121 self._step_decimals = step_decimals22 self._max_decimals = max(step_decimals, max_decimals)23 self._value = 024 self._min_value = 025 self._max_value = 100 * 10**self._max_decimals26 self._minimum_width = None27 line_edit = self.lineEdit()28 line_edit.setText(str(self._value))29 line_edit.textChanged.connect(self._change_value)30 self.editingFinished.connect(self._minimise_string)31 def _to_internal(self, value):32 return int(round(value * 10**self._max_decimals))33 def _from_internal(self, value):34 return value / 10**self._max_decimals35 def _clamp_value(self):36 self._value = min(max(self._min_value, self._value), self._max_value)37 def setMinimum(self, minimum):38 self._min_value = self._to_internal(minimum)39 self._max_value = max(self._min_value, self._max_value)40 self._clamp_value()41 def setMaximum(self, maximum):42 self._max_value = self._to_internal(maximum)43 self._min_value = min(self._min_value, self._max_value)44 self._clamp_value()45 def setRange(self, minimum, maximum):46 assert minimum <= maximum47 self._min_value = self._to_internal(minimum)48 self._max_value = self._to_internal(maximum)49 self._clamp_value()50 def setDecimals(self, decimals):51 assert decimals >= 152 if decimals == self._max_decimals:53 return54 if decimals < self._max_decimals:55 factor = 10**(self._max_decimals - decimals)56 scale = lambda x: int(round(x / factor))57 else:58 factor = 10**(decimals - self._max_decimals)59 scale = lambda x: x * factor60 self._value = scale(self._value)61 self._min_value = scale(self._min_value)62 self._max_value = scale(self._max_value)63 self._max_decimals = decimals64 def _set_value(self, value):65 old_block = self.blockSignals(True)66 self._value = self._to_internal(value)67 self._clamp_value()68 new_text = self.text()69 if new_text != self.lineEdit().text():70 self.lineEdit().setText(new_text)71 self.blockSignals(old_block)72 self.update()73 def setValue(self, value):74 self._set_value(value)75 self.valueChanged.emit(self.value())76 def text(self):77 is_negative = (self._value < 0)78 abs_str = str(abs(self._value))79 digit_count = len(abs_str)80 if digit_count <= self._max_decimals:81 abs_str = '0.' + ('0' * (self._max_decimals - digit_count)) + abs_str82 else:83 whole_count = digit_count - self._max_decimals84 abs_str = abs_str[:whole_count] + '.' + abs_str[whole_count:]85 abs_str = abs_str.rstrip('0').rstrip('.')86 if not abs_str:87 abs_str = '0'88 return '-' + abs_str if is_negative else abs_str89 def _minimise_string(self):90 line_edit = self.lineEdit()91 old_text = line_edit.text()92 new_text = self.text()93 if new_text != old_text:94 old_block = line_edit.blockSignals(True)95 line_edit.setText(self.text())96 line_edit.blockSignals(old_block)97 def fixup(self, in_str):98 if not in_str:99 in_str = self.text()100 return super().fixup(in_str)101 def value(self):102 return self._from_internal(self._value)103 def _str_to_internal(self, in_str):104 if not in_str:105 raise ValueError('No input string')106 is_negative = (in_str[0] == '-')107 abs_str = in_str[1:] if is_negative else in_str108 parts = abs_str.split('.')109 if len(parts) > 2:110 raise ValueError('Illegal number format')111 if not all(all(c in string.digits for c in p) for p in parts):112 raise ValueError('Illegal number format')113 if len(parts) == 1:114 parts.append('0' * self._max_decimals)115 else:116 decimal_count = len(parts[1])117 parts[1] += '0' * (self._max_decimals - decimal_count)118 if len(parts[1]) > self._max_decimals:119 raise ValueError('Too many decimals')120 whole, decimals = (int(p) for p in parts)121 value = whole * 10**self._max_decimals + decimals122 if is_negative:123 value = -value124 if not (self._min_value <= value <= self._max_value):125 raise ValueError('Number outside range')126 return value127 def _change_value(self, value_str):128 try:129 self._value = self._str_to_internal(value_str)130 except ValueError:131 return132 self.valueChanged.emit(self.value())133 def stepEnabled(self):134 if self.wrapping():135 return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled136 flags = QAbstractSpinBox.StepNone137 if self._value > self._min_value:138 flags |= QAbstractSpinBox.StepDownEnabled139 if self._value < self._max_value:140 flags |= QAbstractSpinBox.StepUpEnabled141 return flags142 def stepBy(self, steps):143 step_size = 10**(self._max_decimals - self._step_decimals)144 self._value += steps * step_size145 interval = self._max_value - self._min_value146 if self.wrapping() and interval > 0:147 if self._value < self._min_value:148 excess = (self._min_value - self._value) % interval149 self._value = self._max_value - excess150 elif self._value > self._max_value:151 excess = (self._value - self._max_value) % interval152 self._value = self._min_value + excess153 self._clamp_value()154 line_edit = self.lineEdit()155 old_block = line_edit.blockSignals(True)156 line_edit.setText(self.text())157 line_edit.blockSignals(old_block)158 self.valueChanged.emit(self.value())159 self.update()160 def _can_be_extended_to_valid(self, test_str):161 if not test_str:162 return True163 if not all(c in (string.digits + '-.') for c in test_str):164 return False165 if '-' in test_str[1:]:166 return False167 test_is_negative = (test_str[0] == '-')168 if test_is_negative and (self._min_value >= 0):169 return False170 def test_with_decimal_point(test_str):171 if self._max_decimals == 0:172 return False173 parts = test_str.split('.')174 if len(parts) > 2:175 return False176 whole_str, dec_str = parts177 if len(dec_str) > self._max_decimals:178 return False179 # See if the extension(s) with smallest absolute value is acceptable180 whole_abs_str = whole_str.lstrip('-')181 whole_abs = int(whole_abs_str) if whole_abs_str else 0182 dec_abs = int(dec_str) if dec_str else 0183 min_result_abs = whole_abs * 10**self._max_decimals + dec_abs184 test_value = -min_result_abs if test_is_negative else min_result_abs185 def test_fixed_sign(test_value):186 if test_value >= 0:187 if self._max_value < 0:188 return False189 bound = self._max_value190 else:191 if self._min_value >= 0:192 return False193 bound = abs(self._min_value)194 test_value = abs(test_value)195 return (test_value <= bound)196 if not test_fixed_sign(test_value):197 if test_is_negative:198 return False199 elif not test_fixed_sign(-test_value):200 return False201 return True202 if '.' in test_str:203 return test_with_decimal_point(test_str)204 # No decimal point so place it as far left as possible205 test_abs_str = test_str.lstrip('-')206 split_point = max(0, len(test_abs_str) - self._max_decimals)207 test_str = test_abs_str[:split_point] + '.' + test_abs_str[split_point:]208 if test_is_negative:209 test_str = '-' + test_str210 return test_with_decimal_point(test_str)211 def validate(self, in_str, pos):212 maybe_intermediate = (213 QValidator.Intermediate214 if self._can_be_extended_to_valid(in_str)215 else QValidator.Invalid)216 if not in_str:217 return (QValidator.Intermediate, in_str, pos)218 try:219 value = self._str_to_internal(in_str)220 except ValueError:221 return (maybe_intermediate, in_str, pos)222 if not (self._min_value <= value <= self._max_value):223 return (maybe_intermediate, in_str, pos)224 return (QValidator.Acceptable, in_str, pos)225 def update_style(self, style_mgr):226 self._minimum_width = None227 def minimumSizeHint(self):228 if not self._minimum_width:229 def get_longest_str(bound):230 return '.' + str(bound)231 min_longest = get_longest_str(self._min_value)232 max_longest = get_longest_str(self._max_value)233 longest = min_longest if len(min_longest) > len(max_longest) else max_longest234 fm = self.fontMetrics()235 width = fm.width(longest) + 2236 height = self.lineEdit().minimumSizeHint().height()237 opt = QStyleOptionSpinBox()238 self.initStyleOption(opt)239 self._minimum_width = self.style().sizeFromContents(240 QStyle.CT_SpinBox, opt, QSize(width, height), self).expandedTo(241 QApplication.globalStrut()).width()242 height = super().minimumSizeHint().height()...
testNumericalOperations.py
Source:testNumericalOperations.py
...54 assert_equal(vv, vv)55 assert_equal(ww, ww)56 echo_single_test("One less trivial equalities")57 assert_equal(vv, ww)58def test_is_negative():59 echo_function("test_is_negative")60 echo_single_test("some values")61 a = -sin(0.5*pi)62 assert_true(numerical_is_negative(a))63 a = -pi64 assert_true(numerical_is_negative(a))65 a = pi66 assert_false(numerical_is_negative(a))67 echo_single_test("zero is not negative")68 assert_false(numerical_is_negative(0))69def test_number_to_string():70 echo_function("test_number_to_string")71 a = 7.73542889062775*cos(11/9*pi + 1.30951587282752) - \72 7.55775391156456*cos(5/18*pi) + 2.5*cos(2/9*pi)73 assert_equal(number_to_string(a, digits=7), "0.329851")74 assert_equal(number_to_string(0, digits=15), "0.00000000000000")75 assert_equal(number_to_string(120, digits=3), "120")76 assert_equal(number_to_string(120, digits=5), "120.00")77 assert_equal(number_to_string(120.67, digits=3), "120")78 assert_equal(number_to_string(120.67, digits=4), "120.6")79 assert_equal(number_to_string(120.67, digits=14), "120.67000000000")80 assert_equal(number_to_string(-1, digits=3), "-1.00")81 assert_equal(number_to_string(-12, digits=2), "-12")82 assert_equal(number_to_string(-0.1234, digits=6), "-0.12340")83 assert_equal(number_to_string(-0.12, digits=3), "-0.12")84def testNumericalOperations():85 test_vector_equality()86 test_visual_length()87 test_is_negative()...
test_sent_algorithms.py
Source:test_sent_algorithms.py
...4class TestSentimentAlgorithms():5 def test_is_positive():6 print('If output greater than 0.0, test has passed for postive news headline:\n')7 print(headline_sentiment(positive_news_test))8 def test_is_negative():9 print('If output 0.0 or less, test has passed for negative news headline:\n')10 print(headline_sentiment(negative_news_test))11if __name__ == "__main__":12 TestSentimentAlgorithms.test_is_positive()...
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!!