{"id":16853712,"url":"https://github.com/abersheeran/cool","last_synced_at":"2025-05-07T10:35:57.956Z","repository":{"id":52725206,"uuid":"334599926","full_name":"abersheeran/cool","owner":"abersheeran","description":"Make Python code cooler. Less is more.","archived":false,"fork":false,"pushed_at":"2023-03-25T09:17:32.000Z","size":53,"stargazers_count":139,"open_issues_count":2,"forks_count":12,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-04T23:39:05.884Z","etag":null,"topics":["pipeline","python","redirect"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/abersheeran.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["https://donate.aber.sh/"]}},"created_at":"2021-01-31T07:37:31.000Z","updated_at":"2025-02-18T22:31:38.000Z","dependencies_parsed_at":"2024-10-26T21:15:41.800Z","dependency_job_id":"8b8eadb6-95c4-4ecc-8832-7bb8288d046d","html_url":"https://github.com/abersheeran/cool","commit_stats":{"total_commits":36,"total_committers":1,"mean_commits":36.0,"dds":0.0,"last_synced_commit":"6956ced641e9ad3e505bd4942fa0afdce388cd85"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abersheeran%2Fcool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abersheeran%2Fcool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abersheeran%2Fcool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abersheeran%2Fcool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/abersheeran","download_url":"https://codeload.github.com/abersheeran/cool/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252860532,"owners_count":21815533,"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":["pipeline","python","redirect"],"created_at":"2024-10-13T13:52:58.948Z","updated_at":"2025-05-07T10:35:57.933Z","avatar_url":"https://github.com/abersheeran.png","language":"Python","funding_links":["https://donate.aber.sh/"],"categories":["Python"],"sub_categories":[],"readme":"# Cool.py\n\nMake Python code cooler. 100% coverage. Use and enjoy this code!\n\n## Install\n\n```\npip install cool\n```\n\nOr fetch from github\n\n```\npip install git+https://github.com/abersheeran/cool@setup.py\n```\n\n## Usage\n\n### Pipe\n\n*Note: as fast as you didn't use F!*\n\nUse pipeline to pass data as a positional parameter to the next function.\n\n```python\nfrom cool import F\n\nassert range(10) | F(filter, lambda x: x % 2) | F(sum) == 25\n```\n\nOr you need to pass multiple parameters through the pipeline. Note that `FF` can only accept one parameter, and it must be an iterable object.\n\n```python\nfrom cool import FF\n\nassert (1, 2) | FF(lambda x, y: x + y) == 3\n```\n\nYou can use `...` as a placeholder. This is useful when you need to pass non-continuous parameters to create a partial function.\n\n```python\nfrom functools import reduce\nfrom cool import F\n\nassert range(10) | F(reduce, lambda x, y: x + y) == 45\nassert range(10) | F(reduce, lambda x, y: x + y, ..., 10) == 55\n\nsquare = F(pow, ..., 2)\nassert range(10) | F(map, square) | F(sum) == 285\n```\n\nThe `range(10) | F(reduce, lambda x, y: x + y, ..., 10)` is equivalent to `reduce(lambda x, y: x + y, range(10), 10)`.\n\n### Redirect\n\nJust like the redirection symbol in `Shell`, you can redirect the output to a specified file or `TextIO` object through `\u003e` or `\u003e\u003e`.\n\n```python\nfrom pathlib import PurePath\nfrom cool import R\n\n# Redirect output to specified filepath\nR(lambda : print(\"hello\")) \u003e PurePath(\"your-filepath\")\n\n# Append mode\nR(lambda : print(\"hello\")) \u003e\u003e PurePath(\"your-filepath\")\n```\n\nRedirect to opened file or other streams.\n\n```python\nfrom io import StringIO\nfrom cool import R\n\nwith open(\"filepath\", \"a+\", encoding=\"utf8\") as file:\n    R(lambda : print(\"hello\")) \u003e\u003e file\n\n\nout = StringIO(\"\")\nR(lambda : print(\"hello\")) \u003e out\nout.seek(0, 0)\nassert out.read() == \"hello\\n\"\n```\n\nMaybe you also want to block the output, just like `\u003e /dev/null`.\n\n```python\nfrom cool import R\n\nR(lambda : print(\"hello\")) \u003e None\n# Or\nR(lambda : print(\"hello\")) \u003e\u003e None\n```\n\nNote that after the calculation is over, `R` will faithfully return the return value of your function. Try the following example.\n\n```python\nfrom pathlib import PurePath\nfrom cool import F, R\n\n\ndef func(num):\n    return range(num) | F(map, lambda x: print(x) or x) | F(sum)\n\n\nresult = R(lambda : func(10)) \u003e PurePath(\"filepath\")\nassert result == 45\n```\n\n### Set Global\n\nMaybe you don't want to use `from cool import F` in every file of the entire project, you can use the following code to set it as a global function, just like `min`/`max`/`sum`.\n\n```python\nimport cool\n\ncool.set_global(cool.F, cool.FF)\n```\n\nMaybe you also want to expose `functools.reduce` to the world, just like `map`/`filter`.\n\n```python\nimport functools\nimport cool\n\ncool.set_global(cool.F, cool.FF, functools.reduce)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabersheeran%2Fcool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fabersheeran%2Fcool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabersheeran%2Fcool/lists"}