{"id":22870690,"url":"https://github.com/muthuishere/python-streams","last_synced_at":"2025-05-05T22:15:08.449Z","repository":{"id":48720361,"uuid":"376446426","full_name":"muthuishere/python-streams","owner":"muthuishere","description":"A Library to support Writing concise functional code in python ","archived":false,"fork":false,"pushed_at":"2021-07-14T03:31:40.000Z","size":708,"stargazers_count":7,"open_issues_count":3,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-05T22:15:03.345Z","etag":null,"topics":["functional-programming","library","pip","python","python3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/muthuishere.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-06-13T05:16:40.000Z","updated_at":"2024-12-07T14:02:20.000Z","dependencies_parsed_at":"2022-09-02T17:33:55.256Z","dependency_job_id":null,"html_url":"https://github.com/muthuishere/python-streams","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/muthuishere%2Fpython-streams","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muthuishere%2Fpython-streams/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muthuishere%2Fpython-streams/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muthuishere%2Fpython-streams/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/muthuishere","download_url":"https://codeload.github.com/muthuishere/python-streams/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252584332,"owners_count":21771945,"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":["functional-programming","library","pip","python","python3"],"created_at":"2024-12-13T13:15:43.819Z","updated_at":"2025-05-05T22:15:08.423Z","avatar_url":"https://github.com/muthuishere.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# functional-streams\nWriting concise functional code in python\n\n![Converting to concise code](https://github.com/muthuishere/python-streams/blob/main/assets/pythonstreams.png?raw=true)\n\n\n\u003ca target=\"_blank\" href=\"https://www.youtube.com/watch?v=AcQcxh0VQv0\"\u003eDemo \u003c/a\u003e\n\n\n```python\n\n#To Fetch from a list of users\n#       Get their firstname , if their salary greater than 80000 and gender is male\n\n#Instead of writing like this\n\n\nlist(map(lambda user: user['first_name'],  \n         filter(lambda user:user['salary'] \u003e 80000, \n                filter(lambda product: product['gender'] == 'Male',\n                       users))))\n\n\n#Write this\nfrom streams.Stream import Stream\nfrom streams.operations.operators import item\n\n(Stream\n   .create(users)\n   .filter(item['salary'] \u003e 80000)\n   .filter(item['gender'] == 'Female')\n   .map(item['first_name'])\n   .asList())\n\n\n# You could have seen there is no lambdas involved in above code, for transformation\n# You are free to use lambdas or functions as well , something like below\n\n\n(Stream\n   .create(users)\n   .filter(lambda user:user['salary'] \u003e 80000)\n   .filter(lambda product: product['gender'] == 'Male')\n   .map(lambda user: user['first_name'])\n   .asList())\n\n\n#A concise way to write functional code in python\n\n```\n\n```python\nfrom streams.Stream import Stream\nfrom streams.operations.operators import item\nusers = [\n    {\n        \"id\": 1,\n        \"first_name\": \"Mandy\",\n        \"last_name\": \"Gowan\",\n        \"email\": \"mgowan0@aol.com\",\n        \"gender\": \"Female\",\n        \"loves\": ['Soccer','Cricket','Golf'],\n        \"salary\": 119885\n    },\n    {\n        \"id\": 2,\n        \"first_name\": \"Janessa\",\n        \"last_name\": \"Cotterell\",\n        \"email\": \"jcotterell1@aol.com\",\n        \"gender\": \"Female\",\n        \"loves\": ['Cricket'],\n        \"salary\": 107629\n    },\n    {\n        \"id\": 6,\n        \"first_name\": \"Jasen\",\n        \"last_name\": \"Franzini\",\n        \"email\": \"jfranzini5@aol.com\",\n        \"gender\": \"Male\",\n        \"loves\": ['Soccer','Golf'],\n        \"salary\": 78373\n    }\n]\n\n#Using Map Filter \nresults = (Stream\n           .create(users)\n           .filter(item['salary'] \u003e 80000)\n           .map(item['first_name'])\n           .asList())\n#['Mandy', 'Janessa']\n\n#Using flatMap Distinct \nresults = (Stream\n           .create(users)\n           .flatmap(item['loves'] )\n           .distinct()\n           .asList())\n#['Cricket', 'Golf', 'Soccer']\n\n#Using skip take \nresults = (Stream\n           .create(users)\n           .skip(1)\n           .take(1)\n           .map(item['first_name'])\n           .asList())\n#['Janessa']\n\n\n#Even you can peek results\nresults = (Stream\n           .create(users)\n           .peek(lambda data:print(\"User\",data))\n           .map(item['first_name'])\n           .asList())\n\n#also for peek with item.print or can use side effects inside\n(Stream\n   .create(users)\n   .peek(item.print)\n   .map(item['first_name'])\n   .asList())\n\n#Will list out all users\n\n\n#Also To find product within range of 5 elements\n(Stream\n   .create(range(5))\n   .map(item * 2)\n   .asList())\n#Result [0, 2, 4, 6, 8]\n\n\n```\n\n```text\nbabynames.csv\n\nId,Male name,Female name\n1,Liam,Olivia\n2,Noah,Emma\n```\n\n\n```python\n#From CSV to csv\nfrom streams.FileStream import FileStream\nfrom streams.operations.operators import item\n\n(FileStream.createFromCsv(full_path_of_input_csv)\n         .filter(item['Female name'].startswith(\"A\"))\n         .map(item['Female name'])\n         .peek(item.print)\n         .asCSV(full_path_of_output_csv))\n\n```\n\n```python\n#From text and to text\nfrom streams.FileStream import FileStream\n\n\n(FileStream.createFromText(full_path_of_input_text)\n         .filter(lambda value: value.startswith(\"A\"))\n         .peek(lambda val: print(val))         \n         .asTextFile(full_path_of_output_text))\n\n```\n\n\n## Additional Information\n#### Design\nMost of the functions underneath uses the same functions available in python (map uses map , filter uses filter etc..).\nOnly we have added wrapper to make the code concise\n\n\n#### Abstractions\nIf you need to use abstract items, use the same chaining and just invoke the stream when you are using it\n        as the generators used get corrupted by the very first expansion\nFor Example\n\n```python\n\nfrom streams.Stream import Stream\nfrom streams.operations.operators import item\n\nstream_of_users = (Stream\n                   .create(users)\n                   )\n\n# The below code might not work , as the genrators expire once you aggregate it\ntotal_users = (stream_of_users\n               .length())\n\nfirstname_of_users = (stream_of_users\n                      .map(lambda user: user['first_name'])\n                      .asList())\n\n# The above code should be rewritten as\ntotal_users = (stream_of_users\n               .stream()\n               .length())\n\nfirstname_of_users = (stream_of_users\n                      .stream()\n                      .map(lambda user: user['first_name'])\n                      .asList())\n\n# The stream will make use of copying the generators\n\n\n\n```\n\n#### Transducers\nIf you need to use transducers, create with Stream.transducer and connect with pipe whenever required\n\nFor Example\n\n```python\n\nskip_five_and_take_three_items = (Stream\n                                  .transducer()\n                                  .skip(5)\n                                  .take(3)\n                                  )\n\nskip_five_and_take_three_items_within_zero_to_hundred = (Stream\n                                                         .createFromText(range(100))\n                                                         .pipe(skip_five_and_take_three_items)\n                                                         .asList()\n                                                         )\n# Result [5, 6, 7]\n\nskip_five_and_take_three_items_within_700_to_800 = (Stream\n                                                    .createFromText(range(700, 800))\n                                                    .pipe(skip_five_and_take_three_items)\n                                                    .asList()\n                                                    )\n# Result [705, 706, 707]\n\n\n\n\n\n```\n### Known Constraints\nThis section will list down the constraints of library\n\n#### Single Operator with item\nThe item object will support only one operation, for more than one operations use lambda or refactor code\n\n```python\nfrom streams.Stream import Stream\nfrom streams.operations.operators import item\n\n(Stream\n   .create(range(5))\n    .map(item + 1)\n    .reduce(item.sum)\n    .asSingle())\n# Output 15\n\n\n(Stream\n   .create(range(5))\n    .map(item + 1)\n    .reduce(item.sum)\n    .asSingle())\n# Result 15\n\n(Stream\n   .create(range(10))\n    .filter(item.isodd)\n    .asList()\n    )\n\n#Result  [1, 3, 5, 7, 9]\n\n#All the above will work , as \n\n#The below will not work , as filter has two operators mod (%)  \u0026\u0026 Equal to (==)\n(Stream\n   .create(range(10))\n    .filter(item % 2 == 1)\n    .asList()\n    )\n\n#For these scenarios use lambda \n(Stream\n   .create(range(10))\n    .filter(lambda value: value % 2 == 1)\n    .asList()\n    )\n\n\n```\n\n\n#### Contributors\nThis is just a syntactic sugar, with no other third party software involved.\nEverything has been written with built-in modules, Because of very hard fights \nwith \u003ca href=\"https://github.com/yawpitch/\"\u003eyawpitch\u003c/a\u003e. I started taking performance,space complexity seriously.\nThanks for the extremely valuable suggestions. I would like to appreciate him for all his suggestions\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmuthuishere%2Fpython-streams","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmuthuishere%2Fpython-streams","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmuthuishere%2Fpython-streams/lists"}