Python operator.__add__() Examples
The following are 20
code examples of operator.__add__().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
operator
, or try the search function
.
Example #1
Source File: backend_vsa.py From claripy with BSD 2-Clause "Simplified" License | 6 votes |
def __init__(self): Backend.__init__(self) # self._make_raw_ops(set(expression_operations) - set(expression_set_operations), op_module=BackendVSA) self._make_expr_ops(set(expression_set_operations), op_class=self) self._make_raw_ops(set(backend_operations_vsa_compliant), op_module=BackendVSA) self._op_raw['StridedInterval'] = BackendVSA.CreateStridedInterval self._op_raw['ValueSet'] = ValueSet.__init__ self._op_raw['AbstractLocation'] = AbstractLocation.__init__ self._op_raw['Reverse'] = BackendVSA.Reverse self._op_raw['If'] = self.If self._op_expr['BVV'] = self.BVV self._op_expr['BoolV'] = self.BoolV self._op_expr['BVS'] = self.BVS # reduceable self._op_raw['__add__'] = self._op_add self._op_raw['__sub__'] = self._op_sub self._op_raw['__mul__'] = self._op_mul self._op_raw['__or__'] = self._op_or self._op_raw['__xor__'] = self._op_xor self._op_raw['__and__'] = self._op_and self._op_raw['__mod__'] = self._op_mod
Example #2
Source File: genericmatrix.py From pyfinite with MIT License | 5 votes |
def __add__(self,other): if (self.cols != other.cols or self.rows != other.rows): raise ValueError("dimension mismatch") result = self.MakeSimilarMatrix(size=self.Size(),fillMode='z') for i in range(self.rows): for j in range(other.cols): result.data[i][j] = self.add(self.data[i][j],other.data[i][j]) return result
Example #3
Source File: test_solve.py From pyx with GNU General Public License v2.0 | 5 votes |
def testMath(self): self.failUnlessEqual(str(-vector(2, "a")), "unnamed_vector{=(unnamed_scalar{=-1.0} * a[0], unnamed_scalar{=-1.0} * a[1])}") self.failUnlessEqual(str(vector(2, "a") + vector(2, "b")), "unnamed_vector{=(a[0] + b[0], a[1] + b[1])}") self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, 1, vector(2)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(2), 1) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, scalar(), vector(2)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(2), scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, scalar() + scalar(), vector(2)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(2), scalar() + scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(2), vector(3)) self.failUnlessEqual(str(vector(2, "a") - vector(2, "b")), "unnamed_vector{=(unnamed_scalar{=-1.0} * b[0] + a[0], unnamed_scalar{=-1.0} * b[1] + a[1])}") self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, 1, vector(2)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(2), 1) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, scalar(), vector(2)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(2), scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, scalar() + scalar(), vector(2)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(2), scalar() + scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(2), vector(3)) self.failUnlessEqual(str(2 * vector(2, "a")), "unnamed_vector{=(a[0] * unnamed_scalar{=2.0}, a[1] * unnamed_scalar{=2.0})}") self.failUnlessEqual(str(vector(2, "a") * 2), "unnamed_vector{=(a[0] * unnamed_scalar{=2.0}, a[1] * unnamed_scalar{=2.0})}") self.failUnlessEqual(str(scalar(name="s") * vector(2, "a")), "unnamed_vector{=(a[0] * s, a[1] * s)}") self.failUnlessEqual(str(scalar(name="s") * (vector(2, "a") + vector(2, "b"))), "unnamed_vector{=(a[0] * s + b[0] * s, a[1] * s + b[1] * s)}") self.failUnlessEqual(str((scalar(name="s") + scalar(name="t")) * vector(2, "a")), "unnamed_vector{=(a[0] * s + a[0] * t, a[1] * s + a[1] * t)}") self.failUnlessEqual(str((scalar(name="s") + scalar(name="t")) * (vector(2, "a") + vector(2, "b"))), "unnamed_vector{=(a[0] * s + b[0] * s + a[0] * t + b[0] * t, a[1] * s + b[1] * s + a[1] * t + b[1] * t)}") self.failUnlessEqual(str(vector(2, "a") * scalar(name="s")), "unnamed_vector{=(a[0] * s, a[1] * s)}") self.failUnlessEqual(str(vector(2, "a") * vector(2, "b")), "a[0] * b[0] + a[1] * b[1]") self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, vector(2, "a"), vector(3)) self.failUnlessEqual(str(vector(2, "a") / 2.0), "unnamed_vector{=(unnamed_scalar{=0.5} * a[0], unnamed_scalar{=0.5} * a[1])}") self.failUnlessEqual(str(vector(2, "a") / 2), "unnamed_vector{=(unnamed_scalar{=0.0} * a[0], unnamed_scalar{=0.0} * a[1])}") # integer logic! self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar(), vector(1)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar() + scalar(), vector(1)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), scalar() + scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), vector(1))
Example #4
Source File: test_solve.py From pyx with GNU General Public License v2.0 | 5 votes |
def testMath(self): self.failUnlessEqual(str(-matrix([2, 3], "A")), "unnamed_matrix{=((unnamed_scalar{=-1.0} * A[0, 0], unnamed_scalar{=-1.0} * A[0, 1], unnamed_scalar{=-1.0} * A[0, 2]), (unnamed_scalar{=-1.0} * A[1, 0], unnamed_scalar{=-1.0} * A[1, 1], unnamed_scalar{=-1.0} * A[1, 2]))}") self.failUnlessEqual(str(matrix([2, 3], "A") + matrix([2, 3], "B")), "unnamed_matrix{=((A[0, 0] + B[0, 0], A[0, 1] + B[0, 1], A[0, 2] + B[0, 2]), (A[1, 0] + B[1, 0], A[1, 1] + B[1, 1], A[1, 2] + B[1, 2]))}") self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), 1) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), scalar() + scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), vector(2)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), vector(3)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), matrix([2, 4])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), matrix([3, 3])) self.failUnlessEqual(str(matrix([2, 3], "A") - matrix([2, 3], "B")), "unnamed_matrix{=((unnamed_scalar{=-1.0} * B[0, 0] + A[0, 0], unnamed_scalar{=-1.0} * B[0, 1] + A[0, 1], unnamed_scalar{=-1.0} * B[0, 2] + A[0, 2]), (unnamed_scalar{=-1.0} * B[1, 0] + A[1, 0], unnamed_scalar{=-1.0} * B[1, 1] + A[1, 1], unnamed_scalar{=-1.0} * B[1, 2] + A[1, 2]))}") self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), 1) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), scalar() + scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), vector(2)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), vector(3)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), matrix([2, 4])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), matrix([3, 3])) self.failUnlessEqual(str(2 * matrix([2, 3], "A")), "unnamed_matrix{=((A[0, 0] * unnamed_scalar{=2.0}, A[0, 1] * unnamed_scalar{=2.0}, A[0, 2] * unnamed_scalar{=2.0}), (A[1, 0] * unnamed_scalar{=2.0}, A[1, 1] * unnamed_scalar{=2.0}, A[1, 2] * unnamed_scalar{=2.0}))}") self.failUnlessEqual(str(matrix([2, 3], "A") * 2), "unnamed_matrix{=((A[0, 0] * unnamed_scalar{=2.0}, A[0, 1] * unnamed_scalar{=2.0}, A[0, 2] * unnamed_scalar{=2.0}), (A[1, 0] * unnamed_scalar{=2.0}, A[1, 1] * unnamed_scalar{=2.0}, A[1, 2] * unnamed_scalar{=2.0}))}") self.failUnlessEqual(str(matrix([2, 3], "A") * vector(3, "a")), "unnamed_vector{=(A[0, 0] * a[0] + A[0, 1] * a[1] + A[0, 2] * a[2], A[1, 0] * a[0] + A[1, 1] * a[1] + A[1, 2] * a[2])}") self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, vector(2, "a"), matrix([2, 3], "A")) self.failUnlessEqual(str(matrix([2, 3], "A") * matrix([3, 2], "B")), "unnamed_matrix{=((A[0, 0] * B[0, 0] + A[0, 1] * B[1, 0] + A[0, 2] * B[2, 0], A[0, 0] * B[0, 1] + A[0, 1] * B[1, 1] + A[0, 2] * B[2, 1]), (A[1, 0] * B[0, 0] + A[1, 1] * B[1, 0] + A[1, 2] * B[2, 0], A[1, 0] * B[0, 1] + A[1, 1] * B[1, 1] + A[1, 2] * B[2, 1]))}") self.failUnlessEqual(str(matrix([2, 3], "A") / 2.0), "unnamed_matrix{=((unnamed_scalar{=0.5} * A[0, 0], unnamed_scalar{=0.5} * A[0, 1], unnamed_scalar{=0.5} * A[0, 2]), (unnamed_scalar{=0.5} * A[1, 0], unnamed_scalar{=0.5} * A[1, 1], unnamed_scalar{=0.5} * A[1, 2]))}") self.failUnlessEqual(str(matrix([2, 3], "A") / 2), "unnamed_matrix{=((unnamed_scalar{=0.0} * A[0, 0], unnamed_scalar{=0.0} * A[0, 1], unnamed_scalar{=0.0} * A[0, 2]), (unnamed_scalar{=0.0} * A[1, 0], unnamed_scalar{=0.0} * A[1, 1], unnamed_scalar{=0.0} * A[1, 2]))}") self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar(), matrix([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar() + scalar(), matrix([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), matrix([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), scalar() + scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), vector(1)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), matrix([2, 3]))
Example #5
Source File: stencils.py From findiff with MIT License | 5 votes |
def __add__(self, other): return self._binaryop(other, operator.__add__)
Example #6
Source File: type_check.py From chainer with MIT License | 5 votes |
def eval(self): """Evaluates the tree to get actual value. Behavior of this function depends on an implementation class. For example, a binary operator ``+`` calls the ``__add__`` function with the two results of :meth:`eval` function. """ raise NotImplementedError()
Example #7
Source File: backend_vsa.py From claripy with BSD 2-Clause "Simplified" License | 5 votes |
def _op_add(*args): return reduce(operator.__add__, args)
Example #8
Source File: target_selector.py From zvt with MIT License | 5 votes |
def run(self): """ """ if self.filter_factors: musts = [] for factor in self.filter_factors: df = factor.result_df if not pd_is_not_null(df): raise Exception('no data for factor:{},{}'.format(factor.factor_name, factor)) if len(df.columns) > 1: s = df.agg("and", axis="columns") s.name = 'score' musts.append(s.to_frame(name='score')) else: df.columns = ['score'] musts.append(df) self.filter_result = list(accumulate(musts, func=operator.__and__))[-1] if self.score_factors: scores = [] for factor in self.score_factors: df = factor.result_df if not pd_is_not_null(df): raise Exception('no data for factor:{},{}'.format(factor.factor_name, factor)) if len(df.columns) > 1: s = df.agg("mean", axis="columns") s.name = 'score' scores.append(s.to_frame(name='score')) else: df.columns = ['score'] scores.append(df) self.score_result = list(accumulate(scores, func=operator.__add__))[-1] self.generate_targets()
Example #9
Source File: backend_z3.py From claripy with BSD 2-Clause "Simplified" License | 5 votes |
def _op_add(*args): return reduce(operator.__add__, args)
Example #10
Source File: backend_concrete.py From claripy with BSD 2-Clause "Simplified" License | 5 votes |
def __init__(self): Backend.__init__(self) self._make_raw_ops(set(backend_operations) - { 'If' }, op_module=bv) self._make_raw_ops(backend_strings_operations, op_module=strings) self._make_raw_ops(backend_fp_operations, op_module=fp) self._op_raw['If'] = self._If self._op_raw['BVV'] = self.BVV self._op_raw['StringV'] = self.StringV self._op_raw['FPV'] = self.FPV # reduceable self._op_raw['__add__'] = self._op_add self._op_raw['__sub__'] = self._op_sub self._op_raw['__mul__'] = self._op_mul self._op_raw['__or__'] = self._op_or self._op_raw['__xor__'] = self._op_xor self._op_raw['__and__'] = self._op_and # unary self._op_raw['__invert__'] = self._op_not self._op_raw['__neg__'] = self._op_neg # boolean ops self._op_raw['And'] = self._op_and self._op_raw['Or'] = self._op_or self._op_raw['Xor'] = self._op_xor self._op_raw['Not'] = self._op_boolnot self._cache_objects = False
Example #11
Source File: lineroot.py From backtrader with GNU General Public License v3.0 | 5 votes |
def __add__(self, other): return self._operation(other, operator.__add__)
Example #12
Source File: lineroot.py From backtrader with GNU General Public License v3.0 | 5 votes |
def __radd__(self, other): return self._roperation(other, operator.__add__)
Example #13
Source File: Pbinop.py From supriya with MIT License | 5 votes |
def _string_to_operator(self): operators = { "+": operator.__add__, "-": operator.__sub__, "*": operator.__mul__, "**": operator.__pow__, "/": operator.__truediv__, "//": operator.__floordiv__, } return operators[self.operator] ### PUBLIC PROPERTIES ###
Example #14
Source File: backend_z3.py From claripy with BSD 2-Clause "Simplified" License | 4 votes |
def __init__(self, reuse_z3_solver=None, ast_cache_size=10000): Backend.__init__(self, solver_required=True) self._enable_simplification_cache = False self._hash_to_constraint = weakref.WeakValueDictionary() # Per-thread Z3 solver # This setting is treated as a global setting and is not supposed to be changed during runtime, unless you know # what you are doing. if reuse_z3_solver is None: reuse_z3_solver = True if os.environ.get('REUSE_Z3_SOLVER', "False").lower() in {"1", "true", "yes", "y"} \ else False self.reuse_z3_solver = reuse_z3_solver self._ast_cache_size = ast_cache_size # and the operations all_ops = backend_fp_operations | backend_operations if supports_fp else backend_operations all_ops |= backend_strings_operations - {'StrIsDigit'} for o in all_ops - {'BVV', 'BoolV', 'FPV', 'FPS', 'BitVec', 'StringV'}: self._op_raw[o] = getattr(self, '_op_raw_' + o) self._op_raw['Xor'] = self._op_raw_Xor self._op_raw['__ge__'] = self._op_raw_UGE self._op_raw['__gt__'] = self._op_raw_UGT self._op_raw['__le__'] = self._op_raw_ULE self._op_raw['__lt__'] = self._op_raw_ULT self._op_raw['Reverse'] = self._op_raw_Reverse self._op_raw['Identical'] = self._identical self._op_raw['fpToSBV'] = self._op_raw_fpToSBV self._op_raw['fpToUBV'] = self._op_raw_fpToUBV self._op_expr['BVS'] = self.BVS self._op_expr['BVV'] = self.BVV self._op_expr['FPV'] = self.FPV self._op_expr['FPS'] = self.FPS self._op_expr['BoolV'] = self.BoolV self._op_expr['BoolS'] = self.BoolS self._op_expr['StringV'] = self.StringV self._op_expr['StringS'] = self.StringS self._op_raw['__floordiv__'] = self._op_div self._op_raw['__mod__'] = self._op_mod # reduceable self._op_raw['__add__'] = self._op_add self._op_raw['__sub__'] = self._op_sub self._op_raw['__mul__'] = self._op_mul self._op_raw['__or__'] = self._op_or self._op_raw['__xor__'] = self._op_xor self._op_raw['__and__'] = self._op_and # XXX this is a HUGE HACK that should be removed whenever uninitialized gets moved to the # "proposed annotation backend" or wherever will prevent it from being part of the object # identity. also whenever the VSA attributes get the fuck out of BVS as well
Example #15
Source File: pygettext.py From HRTunerProxy with GNU General Public License v2.0 | 4 votes |
def write(self, fp): options = self.__options timestamp = time.strftime('%Y-%m-%d %H:%M+%Z') # The time stamp in the header doesn't have the same format as that # generated by xgettext... print >> fp, pot_header % {'time': timestamp, 'version': __version__} # Sort the entries. First sort each particular entry's keys, then # sort all the entries by their first item. reverse = {} for k, v in self.__messages.items(): keys = v.keys() keys.sort() reverse.setdefault(tuple(keys), []).append((k, v)) rkeys = reverse.keys() rkeys.sort() for rkey in rkeys: rentries = reverse[rkey] rentries.sort() for k, v in rentries: isdocstring = 0 # If the entry was gleaned out of a docstring, then add a # comment stating so. This is to aid translators who may wish # to skip translating some unimportant docstrings. if reduce(operator.__add__, v.values()): isdocstring = 1 # k is the message string, v is a dictionary-set of (filename, # lineno) tuples. We want to sort the entries in v first by # file name and then by line number. v = v.keys() v.sort() if not options.writelocations: pass # location comments are different b/w Solaris and GNU: elif options.locationstyle == options.SOLARIS: for filename, lineno in v: d = {'filename': filename, 'lineno': lineno} print >>fp, _( '# File: %(filename)s, line: %(lineno)d') % d elif options.locationstyle == options.GNU: # fit as many locations on one line, as long as the # resulting line length doesn't exceeds 'options.width' locline = '#:' for filename, lineno in v: d = {'filename': filename, 'lineno': lineno} s = _(' %(filename)s:%(lineno)d') % d if len(locline) + len(s) <= options.width: locline = locline + s else: print >> fp, locline locline = "#:" + s if len(locline) > 2: print >> fp, locline if isdocstring: print >> fp, '#, docstring' print >> fp, 'msgid', normalize(k) print >> fp, 'msgstr ""\n'
Example #16
Source File: pygettext.py From datafari with Apache License 2.0 | 4 votes |
def write(self, fp): options = self.__options timestamp = time.strftime('%Y-%m-%d %H:%M+%Z') # The time stamp in the header doesn't have the same format as that # generated by xgettext... print >> fp, pot_header % {'time': timestamp, 'version': __version__} # Sort the entries. First sort each particular entry's keys, then # sort all the entries by their first item. reverse = {} for k, v in self.__messages.items(): keys = v.keys() keys.sort() reverse.setdefault(tuple(keys), []).append((k, v)) rkeys = reverse.keys() rkeys.sort() for rkey in rkeys: rentries = reverse[rkey] rentries.sort() for k, v in rentries: isdocstring = 0 # If the entry was gleaned out of a docstring, then add a # comment stating so. This is to aid translators who may wish # to skip translating some unimportant docstrings. if reduce(operator.__add__, v.values()): isdocstring = 1 # k is the message string, v is a dictionary-set of (filename, # lineno) tuples. We want to sort the entries in v first by # file name and then by line number. v = v.keys() v.sort() if not options.writelocations: pass # location comments are different b/w Solaris and GNU: elif options.locationstyle == options.SOLARIS: for filename, lineno in v: d = {'filename': filename, 'lineno': lineno} print >>fp, _( '# File: %(filename)s, line: %(lineno)d') % d elif options.locationstyle == options.GNU: # fit as many locations on one line, as long as the # resulting line length doesn't exceeds 'options.width' locline = '#:' for filename, lineno in v: d = {'filename': filename, 'lineno': lineno} s = _(' %(filename)s:%(lineno)d') % d if len(locline) + len(s) <= options.width: locline = locline + s else: print >> fp, locline locline = "#:" + s if len(locline) > 2: print >> fp, locline if isdocstring: print >> fp, '#, docstring' print >> fp, 'msgid', normalize(k) print >> fp, 'msgstr ""\n'
Example #17
Source File: test_cgcheck.py From ironpython3 with Apache License 2.0 | 4 votes |
def test_main(level='full'): import sys import operator old_args = sys.argv sys.argv = ['checkonly'] # !!! Instead of a whitelist, we should have a blacklist so that any newly added # generators automatically get included in this tests generators = [ 'generate_alltypes', 'generate_calls', 'generate_casts', 'generate_dict_views', 'generate_dynsites', 'generate_encoding_aliases', 'generate_exceptions', 'generate_math', 'generate_ops', 'generate_reflected_calls', 'generate_set', 'generate_walker', 'generate_typecache', 'generate_dynamic_instructions', 'generate_comdispatch', # TODO: uncomment when we have whole Core sources in Snap/test # 'generate_exception_factory', ] if sys.implementation.name != "ironpython": generators.remove('generate_alltypes') generators.remove('generate_exceptions') generators.remove('generate_walker') generators.remove('generate_comdispatch') failures = 0 for gen in generators: print("Running", gen) g = __import__(gen) one = g.main() if isinstance(one, collections.Sequence): failures = functools.reduce( operator.__add__, map(lambda r: 0 if r else 1, one), failures ) else: print(" FAIL:", gen, "generator didn't return valid result") failures += 1 if failures > 0: print("FAIL:", failures, "generator" + ("s" if failures > 1 else "") + " failed") sys.exit(1) else: print("PASS") sys.argv = old_args
Example #18
Source File: pygettext.py From oss-ftp with MIT License | 4 votes |
def write(self, fp): options = self.__options timestamp = time.strftime('%Y-%m-%d %H:%M+%Z') # The time stamp in the header doesn't have the same format as that # generated by xgettext... print >> fp, pot_header % {'time': timestamp, 'version': __version__} # Sort the entries. First sort each particular entry's keys, then # sort all the entries by their first item. reverse = {} for k, v in self.__messages.items(): keys = v.keys() keys.sort() reverse.setdefault(tuple(keys), []).append((k, v)) rkeys = reverse.keys() rkeys.sort() for rkey in rkeys: rentries = reverse[rkey] rentries.sort() for k, v in rentries: isdocstring = 0 # If the entry was gleaned out of a docstring, then add a # comment stating so. This is to aid translators who may wish # to skip translating some unimportant docstrings. if reduce(operator.__add__, v.values()): isdocstring = 1 # k is the message string, v is a dictionary-set of (filename, # lineno) tuples. We want to sort the entries in v first by # file name and then by line number. v = v.keys() v.sort() if not options.writelocations: pass # location comments are different b/w Solaris and GNU: elif options.locationstyle == options.SOLARIS: for filename, lineno in v: d = {'filename': filename, 'lineno': lineno} print >>fp, _( '# File: %(filename)s, line: %(lineno)d') % d elif options.locationstyle == options.GNU: # fit as many locations on one line, as long as the # resulting line length doesn't exceeds 'options.width' locline = '#:' for filename, lineno in v: d = {'filename': filename, 'lineno': lineno} s = _(' %(filename)s:%(lineno)d') % d if len(locline) + len(s) <= options.width: locline = locline + s else: print >> fp, locline locline = "#:" + s if len(locline) > 2: print >> fp, locline if isdocstring: print >> fp, '#, docstring' print >> fp, 'msgid', normalize(k) print >> fp, 'msgstr ""\n'
Example #19
Source File: test_solve.py From pyx with GNU General Public License v2.0 | 4 votes |
def testMath(self): self.failUnlessEqual(str(-trafo([2, 3], "A")), str(trafo((-matrix([2, 3], "A_matrix"), -vector(2, "A_vector"))))) self.failUnlessEqual(str(trafo([2, 3], "A") + trafo([2, 3], "B")), str(trafo((matrix([2, 3], "A_matrix") + matrix([2, 3], "B_matrix"), vector(2, "A_vector") + vector(2, "B_vector"))))) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, 1, trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), 1) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, scalar(), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, scalar() + scalar(), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), scalar() + scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(2), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), vector(2)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(3), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), vector(3)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), matrix([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), trafo([2, 4])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), trafo([3, 3])) self.failUnlessEqual(str(trafo([2, 3], "A") - trafo([2, 3], "B")), str(trafo((matrix([2, 3], "A_matrix") - matrix([2, 3], "B_matrix"), vector(2, "A_vector") - vector(2, "B_vector"))))) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, 1, trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), 1) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, scalar(), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, scalar() + scalar(), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), scalar() + scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(2), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), vector(2)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(3), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), vector(3)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), matrix([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), trafo([2, 4])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), trafo([3, 3])) self.failUnlessEqual(str(trafo([2, 3], "A") * trafo([3, 4], "B")), str(trafo((matrix([2, 3], "A_matrix") * matrix([3, 4], "B_matrix"), vector(2, "A_vector") + matrix([2, 3], "A_matrix") * vector(3, "B_vector"))))) self.failUnlessEqual(str(2 * trafo([2, 3], "A")), str(trafo((2 * matrix([2, 3], "A_matrix"), 2 * vector(2, "A_vector"))))) self.failUnlessEqual(str(trafo([2, 3], "A") * 2), str(trafo((matrix([2, 3], "A_matrix") * 2, vector(2, "A_vector") * 2)))) self.failUnlessEqual(str(scalar() * trafo([2, 3], "A")), str(trafo((scalar() * matrix([2, 3], "A_matrix"), scalar() * vector(2, "A_vector"))))) self.failUnlessEqual(str(trafo([2, 3], "A") * scalar()), str(trafo((matrix([2, 3], "A_matrix") * scalar(), vector(2, "A_vector") * scalar())))) self.failUnlessEqual(str((scalar() + scalar()) * trafo([2, 3], "A")), str(trafo(((scalar() + scalar()) * matrix([2, 3], "A_matrix"), (scalar() + scalar()) * vector(2, "A_vector"))))) self.failUnlessEqual(str(trafo([2, 3], "A") * (scalar() + scalar())), str(trafo((matrix([2, 3], "A_matrix") * (scalar() + scalar()), vector(2, "A_vector") * (scalar() + scalar()))))) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, vector(2), trafo([2, 3])) self.failUnlessEqual(str(trafo([2, 3], "A") * vector(3, "a")), str(matrix([2, 3], "A_matrix") * vector(3, "a") + vector(2, "A_vector"))) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, trafo([2, 3]), vector(2)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, matrix([3, 2]), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, trafo([2, 3]), matrix([3, 2])) self.failUnlessEqual(str(trafo([2, 3], "A") / 2.0), str(trafo((matrix([2, 3], "A_matrix") / 2.0, vector(2, "A_vector") / 2.0)))) self.failUnlessEqual(str(trafo([2, 3], "A") / 2), str(trafo((matrix([2, 3], "A_matrix") / 2, vector(2, "A_vector") / 2)))) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar(), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar() + scalar(), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), scalar() + scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), vector(1)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), matrix([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), trafo([2, 3]))
Example #20
Source File: test_cgcheck.py From ironpython2 with Apache License 2.0 | 4 votes |
def test_main(level='full'): import sys import operator old_args = sys.argv sys.argv = ['checkonly'] # !!! Instead of a whitelist, we should have a blacklist so that any newly added # generators automatically get included in this tests generators = [ 'generate_alltypes', 'generate_calls', 'generate_casts', 'generate_dict_views', 'generate_dynsites', 'generate_exceptions', 'generate_math', 'generate_ops', 'generate_reflected_calls', 'generate_set', 'generate_walker', 'generate_typecache', 'generate_dynamic_instructions', 'generate_comdispatch', # TODO: uncomment when we have whole Core sources in Snap/test # 'generate_exception_factory', ] if sys.platform != "cli": generators.remove('generate_alltypes') generators.remove('generate_exceptions') generators.remove('generate_walker') generators.remove('generate_comdispatch') failures = 0 for gen in generators: print "Running", gen g = __import__(gen) one = g.main() if operator.isSequenceType(one): failures = reduce( operator.__add__, map(lambda r: 0 if r else 1, one), failures ) else: print " FAIL:", gen, "generator didn't return valid result" failures += 1 if failures > 0: print "FAIL:", failures, "generator" + ("s" if failures > 1 else "") + " failed" sys.exit(1) else: print "PASS" sys.argv = old_args