Weighted Collection
A WeightedCollection assigns probability weights per elements and returns elements randomly using those weights.
For example: if "anakin" has a weight of 1, "constantine" a weight of 1, and "xenophon" a weight of 2, then "xenophon" will be randomly selected 50% of the time, "anakin" 25% of the time, and "xenophon" 25% of the time.
Usage
from weighted_collection.weighted_collection import WeightedCollection
w = WeightedCollection(obj_type=str)
w.add("anakin", 1)
w.add("constantine", 1)
w.add("xenophon", 2)
# 25% of the time, this will return "anakin",
# 25% of the time "constantine",
# 50% of the time "xenophon".
print(w.get())
Requirements
Python 3.6+
Installation
pip3 install weighted-collection
WeightedCollection API
WeightedCollection()
The constructor.
w = WeightedCollection() # Default constructor.
w = WeightedCollection(obj_type=str) # All elements in the collection must be strings.
w = WeightedCollection(random_seed=0)
w = WeightedCollection(obj_type=str, random_seed=0)
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
obj_type |
Type[T] |
The type of objects that can be added to the collection. | object |
|
random_seed |
int |
The random number generator seed. | 0 |
add(obj, weight)
Try to add an object to the collection.
- The object's type must be the class or a subclass of the
obj_typeparameter in the constructor. - The object must not already be in the collection.
- The weight must be > 0.
Return: True if the object was added.
w = WeightedCollection(obj_type=str)
w.add("anakin", 1) # Adds element "anakin" with a weight of 1. Returns True.
w.add("anakin", 3) # Returns False (object is already in the collecction).
w.add(33, 1) # Returns False (wrong type).
w.add("constantine", -2) # Returns False (weight must be > 0).
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
obj |
T (the value of the obj_type parameter in the constructor) |
The object. Must not already be in the WeightedCollection. | ||
weight |
int |
The probability weight. Must be > 0. |
add_many(objs)
Try to add many objects to the collection.
Return: A dictionary of each object and whether it was added to the collection.
w = WeightedCollection(obj_type=str)
result = w.add_many({"anakin": 1, "constantine": 1, 33: 0, "xenophon": -1})
print(result) # {"anakin": True, "constantine": True, 33: False, "xenophon": False}
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
objs |
Dict[T, int]( T is the value of the obj_type parameter in the constructor) |
A dictionary of objects. A dictionary of objects. Key = the object. Value = the probability weight. See add(obj, weight) for object and weight requirements. |
remove(obj)
Remove an object from the collection.
Return: True if the object was removed.
w = WeightedCollection(obj_type=str)
w.add("anakin", 1)
w.remove("anakin") # Returns True.
w.remove("constantine") # Returns False.
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
obj |
T (the value of the obj_type parameter in the constructor) |
The object in the collection. |
get()
Return: A randomly selected object using the probability weights per object.
w = WeightedCollection(obj_type=str)
w.add("anakin", 1)
w.add("constantine", 1)
w.add("xenophon", 2)
# 25% of the time, this will return "anakin",
# 25% of the time "constantine",
# 50% of the time "xenophon".
print(w.get())
Release files for weighted-collection 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 | |
|---|---|---|---|
| weighted_collection-1.tar.gz | 3.6 kB | Details |
Release files / weighted_collection-1.tar.gz
| Download URL | weighted_collection-1.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
51d3c9d98e0516848194febcaa89ba98af598a9532c04eed947daf434ab7384c
|
|
BLAKE2b-256 checksum How to use checksums |
77b51699f072746196ec08345de343892563cb287ab30351405ba2f42e014357
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5
|