def test_operator(self): import operator self.assertIs(operator.truth(0), False) self.assertIs(operator.truth(1), True) self.assertIs(operator.not_(1), False) self.assertIs(operator.not_(0), True) self.assertIs(operator.contains([], 1), False) self.assertIs(operator.contains([1], 1), True) self.assertIs(operator.lt(0, 0), False) self.assertIs(operator.lt(0, 1), True) self.assertIs(operator.is_(True, True), True) self.assertIs(operator.is_(True, False), False) self.assertIs(operator.is_not(True, True), False) self.assertIs(operator.is_not(True, False), True)
def test_operator(self): import operator self.assertIs(operator.truth(0), False) self.assertIs(operator.truth(1), True) # self.assertIs(operator.not_(1), False) # self.assertIs(operator.not_(0), True) self.assertIs(operator.contains([], 1), False) self.assertIs(operator.contains([1], 1), True) self.assertIs(operator.lt(0, 0), False) self.assertIs(operator.lt(0, 1), True) self.assertIs(operator.is_(True, True), True) self.assertIs(operator.is_(True, False), False) self.assertIs(operator.is_not(True, True), False) self.assertIs(operator.is_not(True, False), True)
def eq(var_name1, var_name2, env): from operator import is_ value1 = toy(var_name1, env)[0] value2 = toy(var_name2, env)[0] if type(value1) != type(value2): return "0" if is_(value1, value2): return "1" if stringIsNumber(value1): value1 = str(eval(value1)) if stringIsNumber(value2): value2 = str(eval(value2)) if type(value1) == str: if value1 == value2: return "1" else: return "0" else: if value1 == [] and value2 == []: return "1" else: return "0"
def compare(value1, value2, comparision): if comparision == 'is': return operator.is_(value1, value2) elif comparision == 'or': return operator.or_(value1, value2) elif comparision == 'and': return operator.and_(value1, value2)
def test_operator(): # stdlib import operator assert operator.truth(0) == SyFalse assert operator.truth(1) == SyTrue assert operator.not_(1) == SyFalse assert operator.not_(0) == SyTrue assert operator.contains([], 1) == SyFalse assert operator.contains([1], 1) == SyTrue assert operator.lt(0, 0) == SyFalse assert operator.lt(0, 1) == SyTrue assert operator.is_(SyTrue, SyTrue) == SyTrue assert operator.is_(SyTrue, SyFalse) == SyFalse assert operator.is_not(SyTrue, SyTrue) == SyFalse assert operator.is_not(SyTrue, SyFalse) == SyTrue
def parse_parameter_joint(joint: ParameterJoint, instance, variables): results = [] if joint.jointType is not None: if joint.jointType == "and": for filter_ in joint.filters: results.append(parse_parameter_joint(filter_, instance, variables)) if False in results: return False else: return True elif joint.jointType == "or": for filter_ in joint.filters: results.append(parse_parameter_joint(filter_, instance, variables)) if True in results: return True else: return False else: left = parse_parameter(joint.left, instance, variables) operator_ = joint.operator right = parse_parameter(joint.right, instance, variables) if operator_ == "equals": return do_equals_with_value_type_check(left, right) elif operator_ == "not-equals": return do_not_equals_with_value_type_check(left, right) elif operator_ == 'empty': if left == "": return operator.is_(None, None) return operator.is_(left, None) elif operator_ == 'not-empty': if left == "": return operator.is_not(None, None) return operator.is_not(left, None) elif operator_ == "more": return do_more_with_value_type_check(left, right) elif operator_ == "more-equals": return do_more_equals_with_value_type_check(left, right) elif operator_ == "less": return do_less_with_value_type_check(left, right) elif operator_ == "less-equals": return do_less_equals_with_value_type_check(left, right) elif operator_ == 'in': return do_in_with_value_type_check(left, right) elif operator_ == 'not-in': return do_not_in_with_value_type_check(left, right) else: raise Exception("operator is not supported")
def layout_rename_item(layout, current_name, new_name): return pipe( layout, map( R.if_else( comp(operator.is_(current_name), first), comp(tuple, partial(concatv, [new_name]), comp(list, drop(1))), identity), ), list)
def main(): a = -1 b = 5 print("a =", a) print("b =", b) print("not_(a) :", operator.not_(a)) print("truth(a) :", operator.truth(a)) print("is_(a, b) :", operator.is_(a, b)) print("is_not(a, b):", operator.is_not(a, b))
def operator_Boolean(): a = -1 b = 5 print('a = ', a) print('b = ', b) print() print('not_(a) :', operator.not_(a)) print('truth(a) :', operator.truth(a)) print('is_(a, b) :', operator.is_(a, b)) print('is_not(a, b) :', operator.is_not(a, b))
def test_operator(self): import operator self.assertIs(operator.truth(0), False) self.assertIs(operator.truth(1), True) self.assertIs(operator.isNumberType(None), False) self.assertIs(operator.isNumberType(0), True) self.assertIs(operator.not_(1), False) self.assertIs(operator.not_(0), True) self.assertIs(operator.isSequenceType(0), False) self.assertIs(operator.isSequenceType([]), True) self.assertIs(operator.contains([], 1), False) self.assertIs(operator.contains([1], 1), True) self.assertIs(operator.isMappingType(1), False) self.assertIs(operator.isMappingType({}), True) self.assertIs(operator.lt(0, 0), False) self.assertIs(operator.lt(0, 1), True) self.assertIs(operator.is_(True, True), True) self.assertIs(operator.is_(True, False), False) self.assertIs(operator.is_not(True, True), False) self.assertIs(operator.is_not(True, False), True)
def test_operator(self): import operator self.assertIs(operator.truth(0), False) self.assertIs(operator.truth(1), True) self.assertIs(operator.isCallable(0), False) self.assertIs(operator.isCallable(len), True) self.assertIs(operator.isNumberType(None), False) self.assertIs(operator.isNumberType(0), True) self.assertIs(operator.not_(1), False) self.assertIs(operator.not_(0), True) self.assertIs(operator.isSequenceType(0), False) self.assertIs(operator.isSequenceType([]), True) self.assertIs(operator.contains([], 1), False) self.assertIs(operator.contains([1], 1), True) self.assertIs(operator.isMappingType(1), False) self.assertIs(operator.isMappingType({}), True) self.assertIs(operator.lt(0, 0), False) self.assertIs(operator.lt(0, 1), True) self.assertIs(operator.is_(True, True), True) self.assertIs(operator.is_(True, False), False) self.assertIs(operator.is_not(True, True), False) self.assertIs(operator.is_not(True, False), True)
def test_operator(self): import operator self.assertIs(operator.truth(0), False) self.assertIs(operator.truth(1), True) with test_support.check_py3k_warnings(): self.assertIs(operator.isCallable(0), False) self.assertIs(operator.isCallable(len), True) self.assertIs(operator.isNumberType(None), False) self.assertIs(operator.isNumberType(0), True) self.assertIs(operator.not_(1), False) self.assertIs(operator.not_(0), True) self.assertIs(operator.isSequenceType(0), False) self.assertIs(operator.isSequenceType([]), True) self.assertIs(operator.contains([], 1), False) self.assertIs(operator.contains([1], 1), True) self.assertIs(operator.isMappingType(1), False) self.assertIs(operator.isMappingType({}), True) self.assertIs(operator.lt(0, 0), False) self.assertIs(operator.lt(0, 1), True) self.assertIs(operator.is_(True, True), True) self.assertIs(operator.is_(True, False), False) self.assertIs(operator.is_not(True, True), False) self.assertIs(operator.is_not(True, False), True)
def before_scenario(context, scenario): try: if "sequential" in scenario.tags: assert_that( Logger.previous_scenario_has_failed, is_(False), "Sequential scenario - previous one failed. Skipping . . .") finally: Logger.previous_scenario_has_failed = False if "sequential" not in scenario.tags: Logger.data_used = {} # REMOVE PREVIOUS SCENARIO LOGS try: os.remove("logs/logs.csv") except OSError: print("No file to remove.")
def if_is(self, obj: Any) -> XIterator[T]: """Filter elements using :func:`operator.is_` checks (equivalent to ``xiter(item for item in self if item is obj)``). Examples: >>> it = xiter([1, None, 1, 123456789, None, 123456789]) >>> list(it.if_is(None)) [None, None] >>> it = xiter([1, None, 1, 123456789, None, 123456789]) >>> list(it.if_is(1)) [1, 1] >>> it = xiter([1, None, 1, 123456789, None, 123456789]) >>> list(it.if_is(123456789)) [] """ return XIterator(filter(lambda x: operator.is_(x, obj), self.iterator))
def if_is_not(self, obj: Any) -> XIterator[T]: """Filter elements using negated :func:`operator.is_` checks. Equivalent to ``xiter(item for item in self if item is not obj)``. Examples: >>> it = xiter([1, None, 1, 123456789, None, 123456789]) >>> list(it.if_is_not(None)) [1, 1, 123456789, 123456789] >>> it = xiter([1, None, 1, 123456789, None, 123456789]) >>> list(it.if_is_not(1)) [None, 123456789, None, 123456789] >>> it = xiter([1, None, 1, 123456789, None, 123456789]) >>> list(it.if_is_not(123456789)) [1, None, 1, 123456789, None, 123456789] """ return XIterator(filter(lambda x: not operator.is_(x, obj), self.iterator))
def which_open(url_list): ''' 山内先生のgithubを開く ''' while True: put = input('何章を表示しますか?(0はホーム): ') if op.is_(put, 'q'): print('終了') break try: int_put = int(put) if op.lt(int_put, len(url_list)): webbrowser.open('https://github.com{}'.format( url_list[int_put])) print('山内先生のgithubを開きます') break else: print('{}章はありません'.format(put)) continue except (ValueError, TypeError, IndexError): print('keyが間違っています') continue
def _event_loop(self): """Process event bound""" while not self._quit: event = self.droid.eventWait().result if self.debug: print event if event["name"]=="screen" and event["data"]=="destroy": # Quit App return for e in self._events: # e[0] = event_name # e[1] = widget_id # e[2] = func event_name = event.get('name', None) data = event.get('data',None) if data is None: event_id = None else: event_id = data.get('id', None) # See if e[1] (the widget id) is a lambda. If not, make one if isinstance(e[1], basestring): id_func = lambda x: operator.eq(x, e[1]) elif callable(e[1]): id_func = e[1] elif e[1] is None: id_func = lambda x: operator.is_(x, None) else: raise ValueError() if event_name == e[0] and id_func(event_id): # Matched to an event, execute binding e[2](event) # End event loop self.droid.fullDismiss()
print("0x0010 & 0x0011 = operator.and_(0x0010, 0x0011) = ", operator.and_(0x0010, 0x0011)) # 二進位XOR運算 print("0x0010 ^ 0x0011 = operator.xor(0x0010, 0x0011) = ", operator.xor(0x0010, 0x0011)) # 二進位NOT運算 print("~ 0x1000 = operator.invert(0x1000) = ", operator.invert(0x1000)) # 二進位OR運算 print("0x0010 | 0x0011 = operator.or_(0x0010, 0x0011) = ", operator.or_(0x0010, 0x0011)) # 次方運算 print("2 ** 16 = operator.pow(2, 16) = ", operator.pow(2, 16)) # 辨識運算 print("1 is 1 = operator.is_(1,1) = ", operator.is_(1,1)) # 辨識運算 print("1 is not 2 = operator.is_not(1,2) = ", operator.is_not(1,2)) # 以索引指派值 obj = [1,2,3] operator.setitem(obj, 1, 4) print("obj[1] = 4 = operator.setitem(obj, 1, 4) = ", obj) # 以索引刪除值 operator.delitem(obj, 1) print("del obj[1] = operator.delitem(obj, 1) = ", obj) # 以索引取值 print("obj[1] = operator.getitem(obj,1) = ", operator.getitem(obj, 1))
#序列翻倍 同序列的 *= a = [1,2,3] print operator.repeat(a,5) print operator.irepeat(a,5) #判断值相等 同 = print operator.eq(1,"1") print operator.eq("a","a") #判断值不等 同 != print operator.ne(1,"1") print operator.ne("a","a") #判断地址相等 同 is print operator.is_(1,"1") print operator.eq("a","a") #判断地址不相等 同 is not print operator.is_(1,"1") print operator.eq("a","a") #布尔值取反 同 not print operator.not_(True) print operator.not_(1==1) #判断是否为真 print operator.truth(True) print operator.truth(1==1) #大于等于 >=
def parse_parameter_joint(joint: ParameterJoint, instance, variables): results = [] if joint.jointType is not None: if joint.jointType == "and": for filter_ in joint.filters: results.append( parse_parameter_joint(filter_, instance, variables)) if False in results: return False else: return True elif joint.jointType == "or": for filter_ in joint.filters: results.append( parse_parameter_joint(filter_, instance, variables)) if True in results: return True else: return False else: left = parse_parameter(joint.left, instance, variables) operator_ = joint.operator right = parse_parameter(joint.right, instance, variables) if operator_ == "equals": if isinstance(right, str): return operator.eq(str(left), right) return operator.eq(str(left), right) elif operator_ == "not-equals": if isinstance(right, str): return operator.eq(str(left), right) return operator.ne(left, right) elif operator_ == 'empty': return operator.is_(left, None) elif operator_ == 'not-empty': return operator.is_not(left, None) elif operator_ == "more": return operator.gt(left, right) elif operator_ == "more-equals": return operator.ge(left, right) elif operator_ == "less": return operator.lt(left, right) elif operator_ == "less-equals": return operator.le(left, right) elif operator_ == 'in': value_list = right.split(',') values: List = [] for value in value_list: if value.isdigit(): values.append(int(value)) else: values.append(value) return left.isin(values) elif operator_ == 'not-in': value_list = right.split(',') values: List = [] for value in value_list: if value.isdigit(): values.append(int(value)) else: values.append(value) return left.notin(values) else: raise Exception("operator is not supported")
x = 1 + 2 operator.add(1, 2) print('\n*** Operators ***') print("add: ", operator.add(1, 2)) print("mul: ", operator.mul(1, 2)) print("truediv: ", operator.truediv(1, 2)) print("floordiv: ", operator.floordiv(13, 2)) from functools import reduce print("Mult all:", reduce(lambda x, y: x * y, [1, 2, 3, 4])) print("Mul operator:", reduce(operator.mul, [1, 2, 3, 4, 5])) from operator import is_ print(is_('abc', "def")) print('\n*** Attribute getters and setters ***') l = [1, 2, 3, 4] g = operator.getitem(l, 1) print(g) l[1] = 10 del l[3] print(l) l = [1, 2, 3, 4] g = operator.setitem(l, 1, 10) g = operator.delitem(l, 3) print(l) print('\n*** Item getter, think like a partial function ***')
def test_is(self): a = b = 'xyzpdq' c = a[:3] + b[3:] self.failUnlessRaises(TypeError, operator.is_) self.failUnless(operator.is_(a, b)) self.failIf(operator.is_(a,c))
def notnull(val): """Return True if val is neither None nor UNKNOWN""" return not any(operator.is_(val, obj) for obj in NULLS)
def test_is(self): a = b = 'xyzpdq' c = a[:3] + b[3:] self.failUnless(operator.is_(a, b)) self.failIf(operator.is_(a,c))
def fulfill(cls: Type[_T], actor) -> _T: cla = type(actor) positional = list(map(attrgetter('name'), filter(lambda f: is_(MISSING, f.default), fields(cls)))) return cls(**{k: getattr(actor, k).__get__(actor, cla)( # type: ignore new_measurement=not bool(i), ) for i, k in enumerate(positional)})
print operator.ge(1, 2) print operator.ge(2, 2) print operator.gt(2, 1) print operator.gt(1, 2) # Not implemented # print operator.not_("hello") print operator.truth(True) print operator.truth(False) print operator.truth(1) print operator.truth(0) print operator.is_("hello", "hello") print operator.is_("hello", "goodbye") print operator.is_(1, 1) print operator.is_(2, 1) print operator.is_not("hello", "goodbye") print operator.is_not("hello", "hello") print operator.is_not(1, 2) print operator.is_not(1, 1) print operator.abs(5) print operator.abs(-5) print operator.abs(1.1) print operator.abs(-1.1) print operator.add(1, 2)
def is_(a): return operator.is_(a)
def test_is(a: object, b: object): assert op.is_(a)(b) == operator.is_(a, b)
from operator import not_, truth, is_, is_not a = -1 b = 5 print('a = ', a) print('b = ', b) print() print('not_(a) :', not_(a)) # not(a) print('truth(a) :', truth(a)) # bool(a) print('is_(a,b) :', is_(a, b)) # a is b print('is_not(a,b) :', is_not(a, b)) # a is not b
print('#' * 52 + ' These would have been very handy in our previous section:') from functools import reduce print(reduce(lambda x, y: x * y, [1, 2, 3, 4])) print('#' * 52 + ' Instead of defining a lambda, we could simply use **operator.mul**:') print(reduce(operator.mul, [1, 2, 3, 4])) print('#' * 52 + ' #### Comparison and Boolean Operators') print(operator.lt(10, 100)) print(operator.le(10, 10)) print(operator.is_('abc', 'def')) print('#' * 52 + ' We can even get the truthyness of an object:') print(operator.truth([1, 2])) print(operator.truth([])) print(operator.and_(True, False)) print(operator.or_(True, False)) print('#' * 52 + ' #### Element and Attribute Getters and Setters') my_list = [1, 2, 3, 4] print(my_list[1]) print('#' * 52 + ' We can do the same thing using:')
from functools import reduce print(reduce(lambda x,y : x*y,[1,2,3,4])) # still i did not get how pass three element in lamda in using reduce #using operator we can do without lamda function print(reduce(operator.mul,[1,2,3,4])) print(operator.lt(10,3)) print(operator.truth([])) #empty list is false print(operator.truth([1])) print(operator.truth('')) print(operator.truth(None)) print(operator.truth([0])) #list with any element is True print(operator.truth(0)) print(operator.truth(1)) print(operator.is_("abc","def")) print(operator.is_("abc","abc")) my_list = [1,2,3,4] print(my_list[1]) print(operator.getitem(my_list,3)) #it require two elements #if sequence is mutable then we can delete or set item my_list[1] = 100 print(my_list) del my_list[1] print(my_list) #now we use operator for set and delete the item
def _missing(field, value): return lambda obj: operator.is_(_x_f(obj, field), None)
# Ejercicio 940: Comprobar cuáles objetos de una lista son iguales a otro objeto con la función is_(). from operator import is_ class Persona: def __init__(self, id, nombre): self.id = id self.nombre = nombre personas = [] personas.append(Persona(1001, 'Mario')) personas.append(Persona(1002, 'Angela')) personas.append(personas[0]) personas.append(Persona(1003, 'Fabián')) personas.append(Persona(1004, 'Andrea')) personas.append(personas[0]) primera_persona = personas[0] resultado = list(filter(lambda p: is_(p, primera_persona), personas)) for r in resultado: print(r.id, r.nombre) print() print('Cantidad de elementos en el resultado:', len(resultado))
def test_is(self): #operator = self.module a = b = 'xyzpdq' c = a[:3] + b[3:] self.assertRaises(TypeError, operator.is_) self.assertTrue(operator.is_(a, b))
print("{:*^30}".format("math模块")) import math print(abs(-10)) #取绝对值 print(math.floor(4.65)) #返回数字的下舍整数 print(math.ceil(4.65)) #返回数字的上舍整数 print("{:*^30}".format("operator模块")) import operator print(operator.eq(10, 20)) #等于True,不等于False print(operator.ne(10, 20)) #不等于 print(operator.lt(10, 20)) #小于 print(operator.le(10, 20)) #小于等于 print(operator.gt(10, 20)) #大于 print(operator.ge(10, 20)) #大于等于 print("contains:", operator.contains("abcde", "abcd")) #contains(a,b),如果b in a 则True print("concat:", operator.concat("ab", "cd")) #字符串拼接 print("is:", operator.is_("ab", "ab")) #识别字符串 list2 = {"a": "b", "k": "d"} operator.setitem(list2, "k", "v") #索引赋值 print("索引赋值obj[k]=v:{}".format(list2)) operator.delitem(list2, "k") #删除赋值 print("删除赋值 obj[k]:{}".format(list2)) operator.getitem(list2, "a") #删除赋值 print("索引 obj[a]:{}".format(operator.getitem(list2, "a")))
def test_is(self): a = b = 'xyzpdq' c = a[:3] + b[3:] self.assertRaises(TypeError, operator.is_) self.assertTrue(operator.is_(a, b)) self.assertFalse(operator.is_(a,c))
#序列翻倍 同序列的 *= a = [1, 2, 3] print operator.repeat(a, 5) print operator.irepeat(a, 5) #判断值相等 同 = print operator.eq(1, "1") print operator.eq("a", "a") #判断值不等 同 != print operator.ne(1, "1") print operator.ne("a", "a") #判断地址相等 同 is print operator.is_(1, "1") print operator.eq("a", "a") #判断地址不相等 同 is not print operator.is_(1, "1") print operator.eq("a", "a") #布尔值取反 同 not print operator.not_(True) print operator.not_(1 == 1) #判断是否为真 print operator.truth(True) print operator.truth(1 == 1) #大于等于 >=
def test_is(self): a = b = 'xyzpdq' c = a[:3] + b[3:] self.failUnlessRaises(TypeError, operator.is_) self.failUnless(operator.is_(a, b)) self.failIf(operator.is_(a, c))
y = False print('x and y is',x and y) print('x or y is',x or y) print('not x is',not x) print(" sequence operator ") print(" + ", "Hello" + "World") print(" concat ", op.concat("Hello", "World")) print(" __add__ ", "Hello".__add__("World")) print(" * ", "Hello" * 3) print(" __mul__ ", "Hello".__mul__(3)) print(" repeat ", op.repeat("Hello", 3)) print(" is ", "abc" is "abc") print(" is_", op.is_("abc","abc")) print(" is not ", "abc" is not "abcd") print(" is_", op.is_not("abc","abcd")) print(" in ", "a" in "abc") print(" __contains__", "abc".__contains__('a')) print(" contains", op.contains("abc",'a')) print(" in ", "a" not in "abc") print(" __contains__", not("abc".__contains__('a'))) print(" contains", op.not_(op.contains("abc",'a'))) print(" __getslice__ :", [0,1,2,3].__getslice__(0,2)) print(" getslice :", op.getslice([0,1,2,3],0,2)) l=[0,1,2,3]
def test_is(self): a = b = 'xyzpdq' c = a[:3] + b[3:] self.assertRaises(TypeError, operator.is_) self.assertTrue(operator.is_(a, b)) self.assertFalse(operator.is_(a, c))
import operator from prestring.utils import LazyFormat # https://docs.python.org/ja/3/library/operator.html class Symbol: def __eq__(self, x): return Symbol(str(LazyFormat("{} == {}", self, x))) def __ne__(self, x): return Symbol(str(LazyFormat("{} != {}", self, x))) def __gt__(self, x): return Symbol(str(LazyFormat("{} < {}", self, x))) print(operator.is_(True, True)) print(operator.not_(True)) foo = Symbol("foo") print(foo) print((foo == 1)) print((foo != 1))
def is_(a, b) -> bool: return operator.is_(a, b)
import operator print operator.lt(2,4) print operator.__lt__(2,4) print operator.truth(0) a = 1 print id(a) b = 1 print operator.is_(a, b) b = 2 print operator.is_(a, b) a = -1 print id(a) print operator.index(1) print operator.inv(8) print operator.lshift(4, 1) print operator.or_(2,4) print operator.pos(30)
def eq_p(a, b): return op.is_(a, b)