{"id":23092021,"url":"https://github.com/thiswillbeyourgithub/iterator_cacher","last_synced_at":"2025-08-16T09:30:48.111Z","repository":{"id":244405730,"uuid":"815146930","full_name":"thiswillbeyourgithub/iterator_cacher","owner":"thiswillbeyourgithub","description":"decorator to transparently cache each element of an iterable","archived":false,"fork":false,"pushed_at":"2024-09-26T10:06:54.000Z","size":57,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-12T07:21:17.756Z","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":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thiswillbeyourgithub.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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}},"created_at":"2024-06-14T13:00:41.000Z","updated_at":"2024-12-05T16:22:39.000Z","dependencies_parsed_at":"2024-07-19T17:25:36.794Z","dependency_job_id":"7dfef21f-1992-41b5-99e5-870eb9745efc","html_url":"https://github.com/thiswillbeyourgithub/iterator_cacher","commit_stats":null,"previous_names":["thiswillbeyourgithub/iterator_cacher"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiswillbeyourgithub%2Fiterator_cacher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiswillbeyourgithub%2Fiterator_cacher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiswillbeyourgithub%2Fiterator_cacher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiswillbeyourgithub%2Fiterator_cacher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thiswillbeyourgithub","download_url":"https://codeload.github.com/thiswillbeyourgithub/iterator_cacher/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230027903,"owners_count":18161836,"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-12-16T21:26:48.176Z","updated_at":"2024-12-16T21:26:50.637Z","avatar_url":"https://github.com/thiswillbeyourgithub.png","language":"Python","readme":"# iterator_cacher\n* Decorate a python function to cache all subsets of iterable inputs.\n\n# When should I use this?\n* Imagine you want to create LLM embeddings for 10 sentences in a single call. With most caching mechanism, if you now ask those 10 sentences with another sentence on top (so an iterable of length 11), the caching will think it know already 10 values. Same if you ask only 9 of the 10.\n\n# Getting started\n* ` python -m pip install iterator_cacher`\n* Import with `from iterator_cacher import IteratorCacher`\n* To use: don't use as decorator but instead manually decorate the function.\n* Example to cache embeddings\n\n``` python\nfrom iterator_cacher import IteratorCacher\nfrom pathlib import Path\nimport litellm\nfrom string import ascii_letters\nfrom joblib import Memory\nfrom functools import partial\n\nlt = ascii_letters[:list(ascii_letters).index(\"A\")]\n\ncache_location = Path(\"test_iterator\")\ncache_location.mkdir(parents=True, exist_ok=True)\ncache_location = Memory(cache_location, verbose=False)\ncached = IteratorCacher(\n    memory_object=cache_location,\n    iter_list=[\"input\"],\n    verbose=True,\n    res_to_list = lambda out: out.to_dict()[\"data\"],\n)(litellm.embedding)\nembedder = partial(cached, model=\"openai/text-embedding-3-small\")\n\ndef p():\n    input(\"Press any key to continue.\")\n\n\nthird = lt[:len(lt) // 3]\nhalf = lt[:len(lt) // 2]\nkwargs = {\"y\": 10, \"z\": 20}\n\n# cache half of letters\nprint(\"Expect 1 print of 1/3 of the alphabet\")\nout1 = embedder(input=list(third))\np()\n\nprint(\"Expect no print\")\nout2 = embedder(input=list(third))\np()\n\nprint(\"Expect 1 print for the letters not in 1/3 but in 1/2 of the alphabet\")\nout3 = embedder(input=list(half))\np()\n\nprint(\"Expect 1 print for the letters of the 2nd 1/2 of the alphabet\")\nout4 = embedder(input=list(lt))\np()\n\nprint(\"Expect no print\")\nout2 = embedder(input=list(lt + lt))\np()\n```\n\n# Note\n* This code is probably not final and will get updated as I encounter bugs.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthiswillbeyourgithub%2Fiterator_cacher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthiswillbeyourgithub%2Fiterator_cacher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthiswillbeyourgithub%2Fiterator_cacher/lists"}