pg.hyper.Choices

Accessible via pg.hyper.Choices.

class Choices(name=None, hints=None, num_choices=None, candidates=MISSING_VALUE, choices_distinct=True, choices_sorted=False, where=None)[source]

Bases: pg.hyper.HyperPrimitive

Categorical choices from a list of candidates.

Example:

# A single categorical choice:
v = pg.oneof([1, 2, 3])

# A multiple categorical choice as a list:
vs = pg.manyof(2, [1, 2, 3])

# A hierarchical categorical choice:
v2 = pg.oneof([
    'foo',
    'bar',
    pg.manyof(2, [1, 2, 3])
])

Attributes:

candidate_templates

Returns candidate templates.

is_leaf

Returns whether this is a leaf node.

Methods:

dna_spec([location])

Returns corresponding DNASpec.

encode(value)

Encode a list of values into DNA.

property candidate_templates[source]

Returns candidate templates.

dna_spec(location=None)[source]

Returns corresponding DNASpec.

Return type:

pg.geno.Choices

encode(value)[source]

Encode a list of values into DNA.

Example:

# DNA of an object containing a single OneOf.
# {'a': 1} => DNA(0)
{
   'a': one_of([1, 2])
}


# DNA of an object containing multiple OneOfs.
# {'b': 1, 'c': bar} => DNA([0, 1])
{
   'b': pg.oneof([1, 2]),
   'c': pg.oneof(['foo', 'bar'])
}

# DNA of an object containing conditional space.
# {'a': {'b': 1} => DNA(0, 0, 0)])
# {'a': {'b': [4, 7]} => DNA(1, [(0, 1), 2])
# {'a': {'b': 'bar'} => DNA(2)
{
   'a': {
      'b': pg.oneof([
        pg.oneof([
          pg.oneof([1, 2]),
          pg.oneof(3, 4)]),
          pg.manyof(2, [
            pg.oneof([4, 5]),
            6,
            7
          ]),
        ]),
        'bar',
      ])
   }
}
Return type:

pg.DNA

Parameters:

value – A list of value that can match choice candidates.

Returns:

Encoded DNA.

Raises:

ValueError if value cannot be encoded.

property is_leaf: bool[source]

Returns whether this is a leaf node.