• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python underscore._函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中src.underscore._函数的典型用法代码示例。如果您正苦于以下问题:Python _函数的具体用法?Python _怎么用?Python _使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了_函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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")
开发者ID:JacekPliszka,项目名称:underscore.py,代码行数:7,代码来源:test_collections.py


示例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")
开发者ID:jiaweihli,项目名称:underscore.py,代码行数:8,代码来源:test_collections.py


示例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")
开发者ID:JacekPliszka,项目名称:underscore.py,代码行数:8,代码来源:test_collections.py


示例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
开发者ID:vibster,项目名称:underscore.py,代码行数:8,代码来源:test_utility.py


示例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")
开发者ID:JacekPliszka,项目名称:underscore.py,代码行数:8,代码来源:test_collections.py


示例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")
开发者ID:jiaweihli,项目名称:underscore.py,代码行数:9,代码来源:test_collections.py


示例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")
开发者ID:jiaweihli,项目名称:underscore.py,代码行数:10,代码来源:test_collections.py


示例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")
开发者ID:jiaweihli,项目名称:underscore.py,代码行数:10,代码来源:test_collections.py


示例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')
开发者ID:jiaweihli,项目名称:underscore.py,代码行数:10,代码来源:test_collections.py


示例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")
开发者ID:JacekPliszka,项目名称:underscore.py,代码行数:14,代码来源:test_collections.py


示例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")
开发者ID:jiaweihli,项目名称:underscore.py,代码行数:15,代码来源:test_collections.py


示例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")
开发者ID:JacekPliszka,项目名称:underscore.py,代码行数:7,代码来源:test_objects.py


示例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")
开发者ID:serkanyersen,项目名称:underscore.py,代码行数:7,代码来源:test_utility.py


示例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')
开发者ID:JacekPliszka,项目名称:underscore.py,代码行数:8,代码来源:test_arrays.py


示例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')
开发者ID:JacekPliszka,项目名称:underscore.py,代码行数:10,代码来源:test_arrays.py


示例16: test_tap

    def test_tap(self):
        ns = self.Namespace()
        ns.intercepted = None

        def interceptor(obj):
            ns.intercepted = obj

        returned = _.tap(1, interceptor)
        self.assertEqual(ns.intercepted, 1, "passes tapped object to interceptor")
        self.assertEqual(returned, 1, "returns tapped object")

        returned = _([1, 2, 3]).chain().map(lambda n, *args: n * 2).max().tap(interceptor).value()
        self.assertTrue(returned == 6 and ns.intercepted == 6, 'can use tapped objects in a chain')
开发者ID:vibster,项目名称:underscore.py,代码行数:13,代码来源:test_objects.py


示例17: test_isEmpty

    def test_isEmpty(self):
        self.assertTrue(not _([1]).isEmpty(), '[1] is not empty')
        self.assertTrue(_.isEmpty([]), '[] is empty')
        self.assertTrue(not _.isEmpty({"one": 1}), '{one : 1} is not empty')
        self.assertTrue(_.isEmpty({}), '{} is empty')
        self.assertTrue(_.isEmpty(None), 'null is empty')
        self.assertTrue(_.isEmpty(), 'undefined is empty')
        self.assertTrue(_.isEmpty(''), 'the empty string is empty')
        self.assertTrue(not _.isEmpty('moe'), 'but other strings are not')

        obj = {"one": 1}
        obj.pop("one")
        self.assertTrue(_.isEmpty(obj), 'deleting all the keys from an object empties it')
        pass
开发者ID:vibster,项目名称:underscore.py,代码行数:14,代码来源:test_objects.py


示例18: test_chaining

 def test_chaining(self):
     array = range(1, 11)
     u = _(array).chain().filter(lambda x: x > 5).min()
     self.assertTrue(isinstance(u, _.underscore),
                     "object is not an instanse of underscore")
     self.assertEqual(6, u.value(), "value should have returned")
开发者ID:JacekPliszka,项目名称:underscore.py,代码行数:6,代码来源:test_structure.py


示例19: test_shuffle

 def test_shuffle(self):
     res = _([5, 10, 15, 4, 8]).shuffle()
     self.assertNotEqual([5, 10, 15, 4, 8], res,
                         "shuffled array was the same")
开发者ID:JacekPliszka,项目名称:underscore.py,代码行数:4,代码来源:test_collections.py


示例20: test_max

 def test_max(self):
     res = _([5, 10, 15, 4, 8]).max()
     self.assertEqual(15, res, "max did not work")
开发者ID:JacekPliszka,项目名称:underscore.py,代码行数:3,代码来源:test_collections.py



注:本文中的src.underscore._函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python util.workflow函数代码示例发布时间:2022-05-27
下一篇:
Python main.Edis类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap