{"id":16573548,"url":"https://github.com/v2e4lisp/_.py","last_synced_at":"2025-10-29T04:30:38.176Z","repository":{"id":9525804,"uuid":"11425731","full_name":"v2e4lisp/_.py","owner":"v2e4lisp","description":"underscore python and a wrapper for chaining","archived":false,"fork":false,"pushed_at":"2014-12-10T15:11:18.000Z","size":344,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-01T21:41:33.013Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/v2e4lisp.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-07-15T14:56:09.000Z","updated_at":"2017-01-31T15:26:22.000Z","dependencies_parsed_at":"2022-09-08T05:10:16.600Z","dependency_job_id":null,"html_url":"https://github.com/v2e4lisp/_.py","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/v2e4lisp%2F_.py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/v2e4lisp%2F_.py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/v2e4lisp%2F_.py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/v2e4lisp%2F_.py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/v2e4lisp","download_url":"https://codeload.github.com/v2e4lisp/_.py/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238768404,"owners_count":19527195,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T21:42:29.379Z","updated_at":"2025-10-29T04:30:37.854Z","avatar_url":"https://github.com/v2e4lisp.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"A python collection wrapper class\n=================================\n\n- implemented half of the underscore.js funtionality (collection, array, object)\n- all the built-in methods are still available on _ object.\n\nUsage \u0026 Example\n---------------\n### all(func)\ncheck if all items can pass the `func`(return `True`)\n`func` default is bool. `every` is an alias of `all`.\n\n**@param**  : `function(a) -\u003e bool`\n\n**@return** : `bool`\n\n```python\n\n_([1,2,3]).all(lambda x: x\u003e0)\n=\u003e True\n```\n\n### any(func)\ncheck if any item in self._ can pass the `func`(return `True`)\nfunc default is bool. `any` is an alias of `some`\n\n**@param**  : `function(a) -\u003e bool`\n\n**@return** : `bool`\n\n```python\n\n_([1,2,3]).some(lambda x: x\u003e2)\n=\u003e True\n```\n\n### but_last(n)\nreturn all but last n items. n default is 1.\n`take` and `initial` are its aliases.\n\n**@param**  : `int`\n\n**@return** : `_`\n\n```python\n\n_([1,2,3,4]).but_last()._\n=\u003e [1,2,3]\n```\n\n### ceil()\nmath.ceil\n\n**@param**  : none\n\n**@return** : _(float)\n\n```python\n\n_(1.2).ceil()._\n=\u003e 2.0\n```\n\n### chr()\nbuiltin chr\n\n**@param**  : none\n\n**@return** : _(str)\n\n```python\n\n_(65).chr()._\n'A'\n```\n\n### chunks(n)\ndivide the self._ into n-size list\n\n**@param**  : int\n\n**@return** : _([[a]])\n\n```python\n\n_([1,2,3,4]).chunks(3)._\n=\u003e [[1,2,3], [4]]\n```\n\n### clone()\nshallow copy\n\n**@param**  : `none`\n\n**@return** : `_`\n\n```python\n\n_a = _([1,2,[3]])\nb = _a.copy()\nb == _a._\n=\u003e True\n```\n\n### compact()\nit's equivalent to `reject(lambda x: bool(x))`\n\n**@param**  : none\n\n**@return** : _\n\n```python\n\n_(['', None, False, 0]).compact()._\n=\u003e []\n```\n\n### contains(item)\ncheck if item is part of self._\n\n**@param**  : `a`\n\n**@return** : `bool`\n\n```python\n\n_([1,2,3]).contains(1)\n=\u003e True\n_({'a': 1,'b': 2, 'c': 3}).contains({'a':1, 'c':3})\n=\u003e True\n```\n\n### copy(deep)\n(deep or shallow) copy `self._`. `deep` default is `False`\n\n**@param**  : `bool`\n\n**@return** : `_`\n\n```python\n\n_a = _([1,2,3])\nb = _a.copy()\nb == _a._\n=\u003e True\n\nb.append('hello')\nb == _a._\n=\u003e False\n```\n\n### count(item)\ncount a certain item\n\n**@param**  : `item`\n\n**@return** : `_(int)`\n\n```python\n\n_([1,2,3,2,1]).count(1)._\n=\u003e 2\n```\n\n### count_by(func)\ncount each element in each group. `func` default is bool\n\n**@param**  : `function(a) -\u003e b`\n\n**@return** : `_(dict)`\n\n```python\n\n_([1,2,3,4,5]).count_by(lambda x: x%2)._\n=\u003e {1: 3, 0: 2}\n```\n\n### deep_copy()\ndeep copy self._\n\n**@param**  : `none`\n\n**@return** : `_`\n\n```python\n\n_a = _([1,2,[3]])\nb = _a.copy()\nb == _a._\n=\u003e True\n\nb[-1].append('4')\nprint b\n=\u003e [1,2,[3,4]]\nprint _a._\n=\u003e [1,2,[3]]\n```\n\n### defaults(**kwargs)\nadd key and value to self._ only if key is not in self._ .\n\n**@param**  : *kwargs\n\n**@return** : _(dict)\n\n```python\n\n_({\"a\":1}).default({\"a\":2, \"b\": 3})._\n=\u003e {\"a\": 1, \"b\": 3}\n```\n\n### dict_keys(keys)\nmake a `dict` with keys as key and self._ as value\n\n**@param**  : a\n\n**@return** : _(dict)\n\n```python\n\n_([1,2,3]).dict_keys(['a', 'b', 'c'])._\n=\u003e {'a': 1, 'b': 2, 'c': 3}\n```\n\n### dict_values(values)\nmake a `dict` with `values` as value and `self._` as key\n\n**@param**  : a\n\n**@return** : _(dict)\n\n```python\n\n_(['a', 'k']).dict_values((1,2))._\n=\u003e {'a': 1, 'k': 2}\n```\n\n### diff(*lists)\nitems in self._ but not present in lists\nif self._ is a set, then the return _ object holds a set.\n`diff` is its alias.\n\n**@param**  : *lists\n\n**@return** : _\n\n```python\n\n_([1,2,2,3]).differece([2,3,4], [2,5])._\n=\u003e [1]\n\n# set\n_({1,2,3,4}).difference([1,2,6])._\n=\u003e set([3,4])\n```\n\n### difference(*lists)\nitems in self._ but not present in lists\nif self._ is a set, then the return _ object holds a set.\n`diff` is its alias.\n\n**@param**  : *lists\n\n**@return** : _\n\n```python\n\n_([1,2,2,3]).differece([2,3,4], [2,5])._\n=\u003e [1]\n\n# set\n_({1,2,3,4}).difference([1,2,6])._\n=\u003e set([3,4])\n```\n\n### down_to(n, fn)\niterate from `self._` to `n` pass the number to function `fn`.\n\n**@param**  : int\n\n**@param**  : function(int)\n\n**@return** : _(int)\n\n```python\n\nresult = []\n_(5).down_to(1, lambda x: result.append(x))\nprint result\n=\u003e [5,4,3,2]\n```\n\n### each(func)\napply the `func` to every item\n\n**type**: function -\u003e _\n\n**@param**  : `function(a)`\n\n**@return** : `_`\n\n```python\n\ndef printit(it): print(it)\n_([1,2,3]).each(printit)\n=\u003e 1\n=\u003e 2\n=\u003e 3\n```\n\n### even()\ncheck if the number is even\n\n**@param**  : none\n\n**@return** : bool\n\n```python\n\n_(2).even()\n=\u003e True\n```\n\n### every(func)\ncheck if all items can pass the `func`(return `True`)\n`func` default is bool. `every` is an alias of `all`.\n\n**@param**  : `function(a) -\u003e bool`\n\n**@return** : `bool`\n\n```python\n\n_([1,2,3]).all(lambda x: x\u003e0)\n=\u003e True\n```\n\n### filter(func)\nlooks through the self._ and collect the items that passed\n`func`(return true). the default `func` is `bool`.\n\n**@param**  : `function(a) -\u003e bool`\n\n**@return** : `_`\n\n```python\n\n_([1,2,3,4]).filter(lambda x: x % 2 == 0)._\n=\u003e [2,4]\n```\n\n### find_item(func)\nfind the first item when `func(item)` reutrn `True`.\nit return as soon as it finds such an item.\n\n**@param**  : `function(a) -\u003e bool`\n\n**@return** : `_(a) || None`\n\n```python\n\n_([1,2,2,4]).find_item(lambda x: x == 2)._\n=\u003e 2\n```\n\n### find_where(cond)\nfind the first item whoses (key, value) pair matches `cond`\n\n**@param**  : `dict`\n\n**@return** : `_(dict) || None`\n\n```python\n\n_([{\"a\": 1, \"b\":2}, {\"c\":1, \"d\":2}]).find_where({\"a\":1})._\n=\u003e {\"a\": 1, \"b\": 2}\n```\n\n### first()\nget the first value . the same as self._[0]\n\n**@param**  : `none`\n\n**@return** : `_(a)`\n\n```python\n\n_([1,2,3]).first._\n=\u003e 1\n```\n\n### flatten(deep)\nflatten a list, when `deep` is set to `True`,\nthis will recursively flatten the whole list.\n`deep` default is `False`\n\n**@param**  : bool\n\n**@return** : _\n\n```python\n\n_([1,2,[3], [[4]]]).flatten()._\n=\u003e [1,2,3,[4]]\n\n_([1,2,[3], [[4]]]).flatten(True)._\n=\u003e [1,2,3,4]\n```\n\n### floor()\nmath.floor\n\n**@param**  : none\n\n**@return** : _(float)\n\n```python\n\n_(1.2).floor()._\n=\u003e 1.0\n```\n\n### group_by(func)\ngroup items by function, return a dict which key is generated by\n`func` and value is a list.\n\n**@param**  : `function(a) -\u003e b`\n\n**@return** : `_(dict)`\n\n```python\n\n_([1,2,3,4]).group_by(lambda x: x%2)._\n=\u003e {1: [1,3], 0: [2,4]}\n```\n\n### initial(n)\nreturn all but last n items. n default is 1.\n`take` and `initial` are its aliases.\n\n**@param**  : `int`\n\n**@return** : `_`\n\n```python\n\n_([1,2,3,4]).but_last()._\n=\u003e [1,2,3]\n```\n\n### int()\nconvert to integer\n\n**@param**  : none\n\n**@return** : _(int)\n\n```python\n\n_(2.3).int()._\n2\n```\n\n### intersection(*lists)\nself._ and lists intersection.\nif self._ is a set, then the return _ object holds a set.\n\n**@param**  : *lists\n\n**@return** : _\n\n```python\n\n_([1,2,2,3]).intersection([2,3,4], [2,5])._\n=\u003e [2]\n\n# set\n_({1,2,3,4}).intersection([1,2,6])._\n=\u003e set([1,2])\n```\n\n### invert()\ninvert dict's key and value\n\n**@param**  : none\n\n**@return** : _(dict)\n\n```python\n\n_({\"k1\": \"v1\", \"k2\": \"v2\"}).invert()._\n{\"v1\": \"k1\", \"v2\": \"k2\"}\n```\n\n### invoke(method, *args, **kwargs)\ninvoke method with args on every item\n\n**@param**  : `function`\n\n**@param**  : `*args`\n\n**@param**  : `**kwargs`\n\n**@return** : `_`\n\n```python\n\n_([1,2,3], [3,4,5]).invoke(\"append\", 'hello')._\n=\u003e [[1,2,3,\"hello\"], [1,2,3,\"hello\"]]\n```\n\n### is_a(t)\ntest if self._ is a `t`.\n\n**@param**  :\n**type**\n\n**@return** : bool\n\n```python\n\n_([1,2,3,4]).is_a(list)\n=\u003e True\n```\n\n### join(sep)\nconcat a list of strings by sep which is string,\nand if sep is a list of string then concat the sep by self._\n\n**@param**  : [str] || str\n\n**@return** : _(str)\n\n```python\n\n_(['a', 'b']).join('/')._\n=\u003e 'a/b'\n\n_('/').join(['a', 'b'])._\n=\u003e 'a/b'\n```\n\n### last()\nget the last item . the same as self._[-1]\n\n**@param**  : `none`\n\n**@return** : `_(a)`\n\n```python\n\n_([1,2,3]).last._\n=\u003e 3\n```\n\n### last_index(item)\nreturn the last index of item in `self._`.\nsince it use the builtin `index` method, if no such item found\nit will raise ValueError.\n`last_index_of` is its alias.\n\n```python\n\n_([1,3,2,3]).last_index(3)._\n=\u003e 3\n```\n\n### last_index_of(item)\nreturn the last index of item in `self._`.\nsince it use the builtin `index` method, if no such item found\nit will raise ValueError.\n`last_index_of` is its alias.\n\n```python\n\n_([1,3,2,3]).last_index(3)._\n=\u003e 3\n```\n\n### map(func)\napply `func` to every item, and collect the result.\n\n**@param**  : `function(a) -\u003e b`\n\n**@return** : `_`\n\n```python\n\n_([1,2,3]).map(lambda x: x+1)._\n=\u003e [2,3,4]\n```\n\n### max(fn)\nget the max item using a key function `fn`\n`fn` default is `lambda x: x`\n\n**@param**  : `function(a) -\u003e b`\n\n**@return** : `_(a)`\n\n```python\n\n_([1,2,3,4]).max(lambda x: -x)._\n=\u003e -1\n```\n\n### min(fn)\nget the min item using a key function `fn`\n`fn` default is `lambda x: x`\n\n**@param**  : `function(a) -\u003e b`\n\n**@return** : `_(a)`\n\n```python\n\n_([1,2,3,4]).min(lambda x: -x)._\n=\u003e 4\n```\n\n### odd()\ncheck if the number is odd.\n\n**@param**  : none\n\n**@return** : bool\n\n```python\n\n_(2).odd()\n=\u003e False\n```\n\n### omit(*keys)\nget a new dict by filtering out keys from self._\n\n**@param**  : *keys\n\n**@return** : _(dict)\n\n```python\n\n_({\"a\": 1, \"b\": 2, \"c\": 2}).omit(\"b\", \"c\")._\n=\u003e {\"a\": 1}\n```\n\n### pairs()\nlist of lists containing two items.\nif self._ is dict then it will be key-value tuple.\n\n**@param**  : none\n\n**@return** : _([(a,b)])\n\n```python\n\n_([1,2,3,4,5]).pairs()._\n=\u003e [[1,2], [3,4], [5]]\n\n_({\"a\": 1, \"b\": 2}).pairs()._\n=\u003e [(\"a\", 1), (\"b\", 2)]\n```\n\n### pick(*keys)\nget a new dict by picking from `self._ ` by `keys`\n\n**@param**  : *keys\n\n**@return** : _(dict)\n\n```python\n\n_({\"a\": 1, \"b\": 2}).pick(\"a\")._\n=\u003e {\"a\": 1}\n```\n\n### pluck(key)\nextracting a list of property values from self._ which is a list of dict\n\n**@param**  : a\n\n**@return** : _([])\n\n```python\n\n_([{\"a\": 1, \"b\": 2}, {\"a\": 3, \"c\": 4}]).pluck(\"a\")._\n=\u003e [1,3]\n```\n\n### pred()\n\n**@param**  : none\n\n**@return** : _(int)\n\n```python\n\n_(2).pred()._\n=\u003e 1\n```\n\n### reduce(func, init)\nleft-fold the self._ by `func` with initial value set to `init`\n\n**@param**  : `function(t, s) -\u003e t`\n\n**@param**  : `init=t`\n\n**@return** : `_`\n\n```python\n\n_([1,2,3]).reduce(lambda t, x: t-x)._\n=\u003e -4\n```\n\n### reduce_right(func, init)\nright-fold the self._ by `func` with initial value set to `init`\n\n**@param**  : `function(t, s)`\n\n**@param**  : `init=t`\n\n**@return** : `_`\n\n```python\n\n_([1,2,3]).reduce_right(lambda t, x: t-x)._\n=\u003e 0\n```\n\n### reject(func)\ncollect items which dosen't pass the `func`(return `False`)\n`func` default is `bool`\n\n**@param**  : `function(a) -\u003e bool`\n\n**@return** : `_`\n\n```python\n\n_([1,2,3,4]).reject(lambda x: x%2 == 0)._\n=\u003e [1,3]\n```\n\n### rest(n)\nreturn all but first n items. n deafult is 1.\n\n**@param**  : int\n\n**@return** : _\n\n```python\n\n_([1,2,3,4]).rest()._\n=\u003e [2,3,4]\n```\n\n### shuffle()\n\n**@param**  : `none`\n\n**@return** : `_`\n\n```python\n\n_([1,2,3]).shuffle()._\n=\u003e ...\n```\n\n### size()\nreturn the size of the collection\n\n**@param**  : `none`\n\n**@return** : `_(int)`\n\n```python\n\n_([1,2,3]).size()._\n=\u003e 3\n```\n\n### some(func)\ncheck if any item in self._ can pass the `func`(return `True`)\nfunc default is bool. `any` is an alias of `some`\n\n**@param**  : `function(a) -\u003e bool`\n\n**@return** : `bool`\n\n```python\n\n_([1,2,3]).some(lambda x: x\u003e2)\n=\u003e True\n```\n\n### sorted(func)\nsort the self._\n\n**@param**  : `function(a) -\u003e k`\n\n**@return** : `_`\n\n```python\n\n_([3,2,1]).sorted(lambda x: x)._\n=\u003e [1,2,3]\n```\n\n### sorted_index(item)\nUses a binary search to determine the index at which the value should be\ninserted into the list in order to maintain the list's sorted order\nthe return index will be as large as possible. see the examples.\n\n**@param**  : a\n\n**@return** : _(int)\n\n```python\n\n_([1,2,3,4,5,6]).sorted_index(4)._\n=\u003e 4\n\n_([1,2,3,4,4,4,5,6]).sorted_index(4)._\n=\u003e 6\n```\n\n### succ()\n\n**@param**  : none\n\n**@return** : _(int)\n\n```python\n\n_(2).succ()._\n=\u003e 3\n```\n\n### take(n)\nreturn all but last n items. n default is 1.\n`take` and `initial` are its aliases.\n\n**@param**  : `int`\n\n**@return** : `_`\n\n```python\n\n_([1,2,3,4]).but_last()._\n=\u003e [1,2,3]\n```\n\n### times(fn)\ncall fn self._ times\n\n**@param**  : function()\n\n**@return** : _(number)\n\n```python\n\ndef p(): print('s')\n_(3).times(p)\n=\u003e s\n=\u003e s\n=\u003e s\n```\n\n### union(*lists)\nmerge `*lists` if there is any. and delete duplicated items.\nif self._ is a set, then the return _ object holds a set.\n\n**@param**  : *lists\n\n**@return** : _\n\n```python\n\n_([1,2,2,3]).union()._\n=\u003e [1,2,3]\n\n_([1,2,2,3]).union([2,3,4], [4,5])._\n=\u003e [1,5]\n\n# set\n_({1,2,3,4}).union([1,2,6])._\n=\u003e set([1,2,3,4,6])\n```\n\n### uniq()\nno duplicated items.\n\n**@param**  : none\n\n**@return** : _\n\n```python\n\n_([1,2,3,2]).uniq()._\n=\u003e [1,2,3]\n```\n\n### up_to(n, l)\niterate from `self._` to `n` pass the number to function `fn`.\n\n**@param**  : int\n\n**@param**  : function(int)\n\n**@return** : _(int)\n\n```python\n\nresult = []\n_(5).down_to(1, lambda x: result.append(x))\nprint result\n=\u003e [5,4,3,2]\n```\n\n### value()\nget the value out of the _ object\n\n**@param**  : `none`\n\n**@return** : `self._`\n\n```python\n\n_([1,2,3]).value()\n=\u003e [1,2,3]\n# which is the same as _([1,2,3])._\n```\n\n### where(cond)\nfind all items whose (key, value) pairs matches `cond`\n\n**@param**  : `dict`\n\n**@return** : `_([dict])`\n\n```python\n\n_([{\"a\": 1, \"b\":2}, {\"a\": 1, \"b\":1}, {\"c\":1, \"d\":2}]).find_where({\"a\":1})._\n=\u003e [{\"a\": 1, \"b\":2}, {\"a\": 1, \"b\":1}]\n```\n\n### without(*args)\nremove any item in `args` from `self._`\n\n**@param**  : *args\n\n**@return** : _\n\n```python\n\n_([1,2,2,3,4,1]).without(2,1)._\n=\u003e [3,4]\n```\n\n### zip(*lists)\nmerge self._ and lists in a way that each time pick one item from each of the lists\nand self._ , then make a tuple out of it.\n\n**@param**  : *lists\n\n**@return** : _\n\n```python\n\n_([1,2,3,4]).zip([1,2,3])._\n=\u003e [(1,1), (2,2), (3,3)]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fv2e4lisp%2F_.py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fv2e4lisp%2F_.py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fv2e4lisp%2F_.py/lists"}