本文整理汇总了Python中src.underscore._函数的典型用法代码示例。如果您正苦于以下问题:Python _函数的具体用法?Python _怎么用?Python _使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_reduce_right
def test_reduce_right(self):
res = _(["foo", "bar", "baz"]).reduceRight(
lambda sum, num, *args: sum + num)
self.assertEqual("bazbarfoo", res, "did not reducedRight correctly")
# alias
res = _(["foo", "bar", "baz"]).foldr(lambda sum, num, *args: sum + num)
self.assertEqual("bazbarfoo", res, "did not foldr correctly")
示例2: test_map_dict
def test_map_dict(self):
def mapTest(val, key, *args):
return val.upper()
map = _({"foo": "bar", "bar": "foo"}).map(mapTest)
self.assertEqual(["BAR", "FOO"], map, "map for dicts did not work")
# alias
map = _({"foo": "bar", "bar": "foo"}).collect(mapTest)
self.assertEqual(["BAR", "FOO"], map, "collect for dicts did not work")
示例3: test_filter
def test_filter(self):
res = _(["foo", "hello", "bar", "world"]
).filter(lambda x, *args: len(x) > 3)
self.assertEqual(["hello", "world"], res, "filter didn't work")
# alias
res = _(["foo", "hello", "bar", "world"]
).select(lambda x, *args: len(x) > 3)
self.assertEqual(["hello", "world"], res, "select didn't work")
示例4: test_times
def test_times(self):
vals = []
_.times(3, lambda i: vals.append(i))
self.assertEqual([0, 1, 2], vals, "is 0 indexed")
vals = []
_(3).times(lambda i: vals.append(i))
self.assertEqual([0, 1, 2], vals, "is 0 indexed")
pass
示例5: test_map_list
def test_map_list(self):
def mapTest(val, *args):
return val * 2
map = _([1, 2, 3, 4]).map(mapTest)
self.assertEqual([2, 4, 6, 8], map, "map for list did not work")
# alias
map = _([1, 2, 3, 4]).collect(mapTest)
self.assertEqual([2, 4, 6, 8], map, "collect for list did not work")
示例6: test_reduce
def test_reduce(self):
res = _([1, 2, 3, 4, 5, 6]).reduce(lambda sum, num, *args: sum + num, 0)
self.assertEqual(21, res, "did not reduced correctly")
# alias
res = _([1, 2, 3, 4, 5, 6]).foldl(lambda sum, num, *args: sum + num, 0)
self.assertEqual(21, res, "did not foldl correctly")
# alias
res = _([1, 2, 3, 4, 5, 6]).inject(lambda sum, num, *args: sum + num, 0)
self.assertEqual(21, res, "did not inject correctly")
示例7: test_each_list
def test_each_list(self):
def eachTest(val, *args):
self.eachList.append(val + 1)
_([1, 2, 3, 4]).each(eachTest)
self.assertEqual([2, 3, 4, 5], self.eachList, "each for lists did not work for all")
# test alias
self.eachList = []
_([1, 2, 3, 4]).forEach(eachTest)
self.assertEqual([2, 3, 4, 5], self.eachList, "forEach for lists did not work for all")
示例8: test_each_dict
def test_each_dict(self):
def eachTest(val, key, *args):
self.eachDict += (key + ":" + val + " ")
_({"foo": "bar", "bar": "foo"}).each(eachTest)
self.assertEqual("foo:bar bar:foo ", self.eachDict, "each for dicts did not work for all")
# alias
self.eachDict = ""
_({"foo": "bar", "bar": "foo"}).forEach(eachTest)
self.assertEqual("foo:bar bar:foo ", self.eachDict, "forEach for dicts did not work for all")
示例9: test_groupby
def test_groupby(self):
parity = _.groupBy([1, 2, 3, 4, 5, 6], lambda num, *args: num % 2)
self.assertTrue(0 in parity and 1 in parity, 'created a group for each value')
self.assertEqual(_(parity[0]).join(', '), '2, 4, 6', 'put each even number in the right group')
llist = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
grouped = _.groupBy(llist, lambda x, *args: len(x))
self.assertEqual(_(grouped[3]).join(' '), 'one two six ten')
self.assertEqual(_(grouped[4]).join(' '), 'four five nine')
self.assertEqual(_(grouped[5]).join(' '), 'three seven eight')
示例10: test_each_dict
def test_each_dict(self):
def eachTest(val, key, *args):
self.eachSet.add(val)
self.eachSet.add(key)
_({"foo": "bar", "fizz": "buzz"}).each(eachTest)
self.assertEqual({"foo", "bar", "fizz", "buzz"},
self.eachSet, "each for dicts did not work for all")
# alias
self.eachSet = set()
_({"foo": "bar", "fizz": "buzz"}).forEach(eachTest)
self.assertEqual({"foo", "bar", "fizz", "buzz"},
self.eachSet, "forEach for dicts did"
"not work for all")
示例11: test_sortBy
def test_sortBy(self):
res = _([{'age': '59', 'name': 'foo'},
{'age': '39', 'name': 'bar'},
{'age': '49', 'name': 'baz'}]).sortBy('age')
self.assertEqual([{'age': '39', 'name': 'bar'},
{'age': '49', 'name': 'baz'},
{'age': '59', 'name': 'foo'}], res, "filter by key did not work")
res = _([{'age': '59', 'name': 'foo'},
{'age': '39', 'name': 'bar'},
{'age': '49', 'name': 'baz'}]).sortBy(lambda x, y, *args: cmp(x, y))
self.assertEqual([{'age': '39', 'name': 'bar'}, {'age': '49', 'name': 'baz'}, {'age': '59', 'name': 'foo'}], res, "filter by lambda did not work")
res = _([50, 78, 30, 15, 90]).sortBy()
self.assertEqual([15, 30, 50, 78, 90], res, "filter list did not work")
示例12: test_invert
def test_invert(self):
obj = {"first": 'Moe', "second": 'Larry', "third": 'Curly'}
r = _(obj).chain().invert().keys().join(' ').value()
self.assertEqual(set(r), set('Larry Moe Curly'),
'can invert an object')
self.assertEqual(_.invert(_.invert(obj)), obj,
"two inverts gets you back where you started")
示例13: test_mixin
def test_mixin(self):
_.mixin({
"myUpper": lambda self: self.obj.upper(),
})
self.assertEqual('TEST', _.myUpper('test'), "mixed in a function to _")
self.assertEqual('TEST', _('test').myUpper(),
"mixed in a function to _ OOP")
示例14: test_zip
def test_zip(self):
names = ['moe', 'larry', 'curly']
ages = [30, 40, 50]
leaders = [True]
stooges = list(_(names).zip(ages, leaders))
self.assertEqual("[('moe', 30, True), ('larry', 40, None),"
" ('curly', 50, None)]", str(
stooges), 'zipped together arrays of different lengths')
示例15: test_intersection
def test_intersection(self):
stooges = ['moe', 'curly', 'larry'],
leaders = ['moe', 'groucho']
self.assertEqual(['moe'], _.intersection(stooges, leaders),
'can take the set intersection of two string arrays')
self.assertEqual(
[1, 2], _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]),
'can take the set intersection of two int arrays')
self.assertEqual(['moe'], _(stooges).intersection(leaders),
'can perform an OO-style intersection')