def number_of_args(fn):
"""Return the number of positional arguments for a function, or None if the number is variable.
Looks inside any decorated functions."""
try:
if hasattr(fn, '__wrapped__'):
return number_of_args(fn.__wrapped__)
if any(p.kind == p.VAR_POSITIONAL for p in signature(fn).parameters.values()):
return None
else:
return sum(p.kind in (p.POSITIONAL_ONLY, p.POSITIONAL_OR_KEYWORD) for p in signature(fn).parameters.values())
except ValueError:
# signatures don't work for built-in operators, so check for a few explicitly
UNARY_OPS = [len, op.not_, op.truth, op.abs, op.index, op.inv, op.invert, op.neg, op.pos]
BINARY_OPS = [op.lt, op.le, op.gt, op.ge, op.eq, op.ne, op.is_, op.is_not, op.add, op.and_, op.floordiv, op.lshift, op.mod, op.mul, op.or_, op.pow, op.rshift, op.sub, op.truediv, op.xor, op.concat, op.contains, op.countOf, op.delitem, op.getitem, op.indexOf]
TERNARY_OPS = [op.setitem]
if fn in UNARY_OPS:
return 1
elif fn in BINARY_OPS:
return 2
elif fn in TERNARY_OPS:
return 3
else:
raise NotImplementedError("Bult-in operator {} not supported".format(fn))
python类concat()的实例源码
def get_default_operators():
""" generate a mapping of default operators allowed for evaluation """
return {
'u-': Func(1, operator.neg), # unary negation
'u%': Func(1, lambda a: a / Decimal(100)), # unary percentage
'&': Func(2, operator.concat),
'^': Func(2, operator.pow),
'+': Func(2, op_add),
'-': Func(2, operator.sub),
'/': Func(2, operator.truediv),
'*': Func(2, operator.mul),
'=': Func(2, operator.eq),
'<>': Func(2, lambda a, b: not operator.eq(a, b)),
'>': Func(2, operator.gt),
'<': Func(2, operator.lt),
'>=': Func(2, operator.ge),
'<=': Func(2, operator.le),
}
def feature_list(feature_dictionary):
"""Convert a feature dictionary to a sorted list.
Args: feature_dictionary (dict)
Returns: sorted list of feature names
"""
return sorted(
functools.reduce(
operator.concat,
(feature_dictionary[key] for key in feature_dictionary.keys())
)
)
def conj(head, tail):
'''
Prepend an element to a collection, returning a new copy
Exact behaviour will differ depending on the collection
'''
tail_type = type(tail)
return op.concat(tail_type([head]), tail)
def test_concat(self):
self.assertRaises(TypeError, operator.concat)
self.assertRaises(TypeError, operator.concat, None, None)
self.assertTrue(operator.concat('py', 'thon') == 'python')
self.assertTrue(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
self.assertTrue(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
self.assertTrue(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
self.assertRaises(TypeError, operator.concat, 13, 29)
def test_concat(self):
self.assertRaises(TypeError, operator.concat)
self.assertRaises(TypeError, operator.concat, None, None)
self.assertTrue(operator.concat('py', 'thon') == 'python')
self.assertTrue(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
self.assertTrue(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
self.assertTrue(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
self.assertRaises(TypeError, operator.concat, 13, 29)
def test_concat(self):
self.assertRaises(TypeError, operator.concat)
self.assertRaises(TypeError, operator.concat, None, None)
self.assertTrue(operator.concat('py', 'thon') == 'python')
self.assertTrue(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
self.assertTrue(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
self.assertTrue(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
self.assertRaises(TypeError, operator.concat, 13, 29)
def test_concat(self):
self.assertRaises(TypeError, operator.concat)
self.assertRaises(TypeError, operator.concat, None, None)
self.assertTrue(operator.concat('py', 'thon') == 'python')
self.assertTrue(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
self.assertTrue(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
self.assertTrue(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
self.assertRaises(TypeError, operator.concat, 13, 29)
def concat_deps(self, bn):
# read source
src = open(os.path.join(self.buildpath, bn), "r").read()
# update direct dependencies
deps = []
self.append_cfile_deps(src, deps)
# recurse through deps
# TODO detect cicular deps.
return reduce(operator.concat, map(self.concat_deps, deps), src)
def concat_deps(self, bn):
# read source
src = open(os.path.join(self.buildpath, bn), "r").read()
# update direct dependencies
deps = []
for l in src.splitlines():
res = includes_re.match(l)
if res is not None:
depfn = res.groups()[0]
if os.path.exists(os.path.join(self.buildpath, depfn)):
# print bn + " depends on "+depfn
deps.append(depfn)
# recurse through deps
# TODO detect cicular deps.
return reduce(operator.concat, map(self.concat_deps, deps), src)
def test_concat(self):
self.assertRaises(TypeError, operator.concat)
self.assertRaises(TypeError, operator.concat, None, None)
self.assertTrue(operator.concat('py', 'thon') == 'python')
self.assertTrue(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
self.assertTrue(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
self.assertTrue(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
self.assertRaises(TypeError, operator.concat, 13, 29)
def _fixed_table(table_element):
"""
Returns a new TableElement.
"""
assert isinstance(table_element, TableElement)
lines = tuple(common.lines(table_element.sub_elements))
fixed_lines = tuple(_fixed_line(l) if _line_length(l) > MAXIMUM_LINE_LENGTH else l for l in lines)
return TableElement(sub_elements=tuple(reduce(operator.concat, fixed_lines)))
def _unindent_table(table_element):
table_lines = tuple(common.lines(table_element.sub_elements))
unindented_lines = tuple(tuple(dropwhile(lambda e: isinstance(e, WhitespaceElement), line)) for line in table_lines)
return TableElement(reduce(operator.concat, unindented_lines))
def _sorted_table(table):
"""
Returns another TableElement where the table entries are sorted lexicographically by key.
"""
assert isinstance(table, TableElement)
# Discarding TokenElements with no tokens in them
table_elements = common.non_empty_elements(table.sub_elements)
lines = tuple(common.lines(table_elements))
sorted_lines = sorted(lines, key=_line_key)
sorted_elements = reduce(operator.concat, sorted_lines)
return TableElement(sorted_elements)
def test_concat(self):
self.assertRaises(TypeError, operator.concat)
self.assertRaises(TypeError, operator.concat, None, None)
self.assertTrue(operator.concat('py', 'thon') == 'python')
self.assertTrue(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
self.assertTrue(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
self.assertTrue(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
self.assertRaises(TypeError, operator.concat, 13, 29)
def bytearray_concat(*args):
"""
??????? ???????????????? ?????????? bytearray ? ????.
"""
return bytearray_cast(reduce(operator.concat, args))
def concat(l):
'''concatenate a list of lists'''
return reduce( operator.concat, l )
def multisplit(str, chars):
""" split str with any of chars"""
l = [str]
for c in chars:
l = concat(map(lambda x:x.split(c), l))
return l