The PDist is designed to make it easy to make it easy to make things happen in a probablistic manner. Provide keys with an associated probability attached, and then call the resulting object when you need a new value.
>>> mood = PDist({'happy': 0.3, 'neutral': 0.6, 'sad': 0.1})
>>> mood()
'happy'
>>> mood()
'neutral'
You can retrieve the distribution of how those values are applied by accessing the PDist.distribution attribute:
>>> mood.distribution
[('happy', 0.3), ('neutral', 0.6), ('sad', 0.1)]
As well as providing a dict to assign probabilities, you can send in a list of lists/tuples. This allows for for non-hashable types to be used. This means that you provide objects such as functions to be used as keys:
>>> def grumpy(news):
... return ':/'
>>> def happy(news):
... return ':)'
>>> react = PDist([(grumpy, 0.7), (happy, 0.3)])
>>> reaction = react()
>>> reaction("We're getting married!")
':/'
You are not restricted to adding your input variables up to 1. For example, if you only have a tally chart, and wish to calculate a probability distribution from that sample, you can provide that too:
>>> spotted = {'geese': 0, 'ducks': 12, 'sparrows': 4, 'other': 39}
>>> bird_pdist = PDist(spotted)
>>> bird_pdist.distribution
[('geese', 0.0), ('sparrows', 0.07272727272727272), ('ducks', 0.21818181818181817), ('other', 0.7090909090909091)]
Retrieving the probability of a particular key is supported. However, the search strategy is very inefficient and probably shouldn’t be used outside of the interactive Python shell.
>>> bird_pdist['geese'] 0.0
Metadata
Release files for choices 0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| choices-0.1.tar.gz | 2.5 kB | Without provenance | Details for choices-0.1.tar.gz |
Release files / choices-0.1.tar.gz
| Download URL | choices-0.1.tar.gz |
|---|---|
| Size | 2.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ae673699f949b8e095c59a0a131b12abc59d409f22e2fb88de6bb15aab01fe7e
|
|
BLAKE2b-256 checksum How to use checksums |
21491ed382aec6fb4854fdd33e45489a190ffcbd056ca2a494a7aead85edced7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for choices 0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.