{"id":24557833,"url":"https://github.com/timclicks/choices","last_synced_at":"2025-04-19T03:15:19.217Z","repository":{"id":2563606,"uuid":"3542962","full_name":"timClicks/choices","owner":"timClicks","description":"for making random decisions","archived":false,"fork":false,"pushed_at":"2014-11-25T09:15:56.000Z","size":178,"stargazers_count":21,"open_issues_count":3,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T11:41:53.528Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/timClicks.png","metadata":{"files":{"readme":"README","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":"2012-02-25T06:46:02.000Z","updated_at":"2022-08-20T20:05:46.000Z","dependencies_parsed_at":"2022-08-28T23:03:31.824Z","dependency_job_id":null,"html_url":"https://github.com/timClicks/choices","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/timClicks%2Fchoices","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timClicks%2Fchoices/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timClicks%2Fchoices/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timClicks%2Fchoices/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timClicks","download_url":"https://codeload.github.com/timClicks/choices/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249598185,"owners_count":21297464,"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":"2025-01-23T05:29:39.045Z","updated_at":"2025-04-19T03:15:19.201Z","avatar_url":"https://github.com/timClicks.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"choices is designed to make it easy to make it easy to\nmake things happen in a probabilistic manner. Its main\nAPI is the Choice class.\n\nWhen called, a Choice object will return one of its keys\naccording to the proability it has been assigned.\n\nHere's an example:\n\n    \u003e\u003e\u003e mood = Choice({'happy': 0.3, 'neutral': 0.6, 'sad': 0.1})\n    \u003e\u003e\u003e mood()\n    'happy'\n    \u003e\u003e\u003e mood()\n    'neutral'\n    \u003e\u003e\u003e mood()\n    'neutral'\n\nCreating a Choice object\n========================\n\nThere are three main ways for creating a Choice object:\n\n- a dict of keys and probabilities\n- a list of (key, probability) pairs\n- a list of (key, sample_count) pairs\n\nEach of those options is explained below.\n\nA dict of keys and probabilities\n--------------------------------\n\nPerhaps the simplest way to create a Choice is to\nprovide the keys and associated probability wrapped\nup as a dict:\n\n    \u003e\u003e\u003e mood = Choice({'happy': 0.3, 'neutral': 0.6, 'sad': 0.1})\n    \u003e\u003e\u003e mood()\n    'happy'\n    \u003e\u003e\u003e mood()\n    'neutral'\n\nA list of (key, probability) pairs\n----------------------------------\n\nAs well as a dict assigning a probability to each key, you can\nsend in a list of (key,value) pairs as lists or tuples. This\nallows for lots of flexibility, including non-hashable\ntypes to be used. This means that you can provide objects such\nas functions to be used as keys.\n\nLet's say we wanted to create an NPC for a game and wanted\nsome scripted responses. Why not encode moods as functions\nand then pass them togetheer as a Choice.\n\n    \u003e\u003e\u003e def grumpy(news):\n    ... return ':/'\n    \u003e\u003e\u003e def happy(news):\n    ... return ':)'\n    \u003e\u003e\u003e react = Choice([(grumpy, 0.7), (happy, 0.3)])\n    \u003e\u003e\u003e reaction = react()\n    \u003e\u003e\u003e reaction(\"We're getting married!\")\n    ':/'\n\nA list of (key, sample_count) pairs\n-----------------------------------\n\nYou are not restricted to adding p to 1. If you only have samples,\nand wish to calculate a probablilty distribution, you can provide\nthose too:\n\n    \u003e\u003e\u003e birds_spotted = {'geese': 0, 'ducks': 12, 'sparrows': 4, 'other': 39}\n    \u003e\u003e\u003e birds = Choice(birds_spotted)\n    \u003e\u003e\u003e birds.distribution\n    [('geese', 0.0), ('sparrows', 0.07272727272727272), ('ducks', 0.21818181818181817), ('other', 0.7090909090909091)]\n\n\nUsage\n=====\n\nMaking decisions\n----------------\n\nTo return a new value, simply call the Choice object.\n\n    \u003e\u003e\u003e where_to_go = Choice({\"Paris\": 0.4, \"London\": 0.4, \"Copenhagen\": 0.1, \"Barcelona\": 0.1 })\n    \u003e\u003e\u003e where_to_go()\n    'Copenhagen'\n\nFinding probabilities\n---------------------\n\nYou can retrieve the distribution of how those values are applied\nby accessing the distribution attribute:\n\n    \u003e\u003e\u003e mood.distribution\n    [('happy', 0.3), ('neutral', 0.6), ('sad', 0.1)]\n    \u003e\u003e\u003e where_to_go.distribution\n    [('Paris': 0.4), ('London', 0.4), ('Copenhagen', 0.1), ('Barcelona', 0.1)]\n\nRetrieving the probability of a particular key is supported:\n\n    \u003e\u003e\u003e birds['geese']\n    0.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimclicks%2Fchoices","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimclicks%2Fchoices","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimclicks%2Fchoices/lists"}