PyGlove 言語リファレンス

Vertex AI ニューラル アーキテクチャ検索チュートリアル: 検索スペースを作成するを実行して PyGlove 言語について理解を深めてください。

PyGlove 型の制約

ユーザーが @pg.members を使用してクラスのメンバーを宣言するか、@pg.functor を使用してファンクターの引数を宣言するときに、値の仕様(pg.typing.Int() など)を指定できます。値の仕様を指定すると、作成時とメンバー割り当て時に値の検証が行われます。

pg.typing.Bool

# Declare a required Bool type.
pg.typing.Bool()
# Declare a Bool type with default value set to True.
pg.typing.Bool(default=True)

# Declare a Bool type that can accept None.
pg.typing.Bool().noneable()

pg.typing.Str

# Declare a required Str type.
pg.typing.Str()
# Declare a required Str type that matches a regular expression.
pg.typing.Str(regex='.*foo')

# Declare a Str type with default value set to 'x'.
pg.typing.Str(default='x')

# Declare a Str type that can accept None.
pg.typing.Str().noneable()

pg.typing.Int

# Declare a required Int type.
pg.typing.Int()
# Declare a required Int type with minimum and maximum value.
pg.typing.Int(min_value=1, max_value=10)

# Declare an Int type with default value set to 10.
pg.typing.Int(default=10)

# Declare an Int type that can accept None.
pg.typing.Int().noneable()

pg.typing.Float

# Declare a required Float type.
pg.typing.Float()
# Declare a required Float type with minimum and maximum value.
pg.typing.Float(min_value=0.0, max_value=1.0)

# Declare a Float type with default value set to 0.0.
pg.typing.Float(default=0.0)

# Declare a Float type that can accept None.
pg.typing.Float().noneable()

pg.typing.Enum

# Declare a required Enum type with default value set to 1.
pg.typing.Enum(1, [1, 'a', 'b'])

# Declare a Enum type that can accept None with default value set to 1.
pg.typing.Enum(1, [1, 'a', 'b']).noneable()

pg.typing.List

# Declare a required list of any type.
pg.typing.List(pg.typing.Any())

# Declare a required Int type (or None) list with at least two elements.
pg.typing.List(pg.typing.Int().noneable(), min_size=2)

# Declare a Float type list of fixed size (10) that can be None.
pg.typing.List(pg.typing.Float(), size=10).noneable()

# Declare a Str type list with a default value.
pg.typing.List(pg.typing.Str(), default=['foo'])

pg.typing.Tuple

# Declare a required Int type tuple of size 2.
pg.typing.Tuple([pg.typing.Int(), pg.typing.Int()])

# Declare a tuple of (Int, Str) types, which can also be None.
pg.typing.Tuple([pg.typing.Int(), pg.typing.Str()]).noneable()

# Declare an Int type tuple of size 2 with a default value.
pg.typing.Tuple([pg.typing.Int(), pg.typing.Int()], default=(1, 1))

pg.typing.Dict

# Declare a required dictionary of any key-value pairs.
pg.typing.Dict()

# Declare a dictionary that can accept None with field 'a' and 'b'.
pg.typing.Dict([
  ('a', pg.typing.Int()),
  ('b', pg.typing.Str()),
]).noneable()

# Declare a dictionary that can accept None with field 'a' and 'b'.
pg.typing.Dict([
  ('a', pg.typing.Int(default=1)),
  ('b', pg.typing.Str()),
]).noneable()

# Declare a dictionary with keys that match regular expression.
pg.typing.Dict([
  (pg.typing.StrKey('.*foo'), pg.typing.Int()),
])

pg.typing.Object

# Declare a required instance of class A.
pg.typing.Object(A)

# Declare an instance of class A that can accept None.
pg.typing.Object(B)

pg.typing.Union

# Declare a required union of Int and Str types.
pg.typing.Union([pg.typing.Int(), pg.typing.Str])

# Declare a union of Int and Str types that can accept None.
pg.typing.Union([pg.typing.Int(), pg.typing.Str]).noneable()

pg.typing.Any

# Declare a required value of any type (except None) with a default value.
pg.typing.Any(default=1)

# Declare a value of any type that can accept None.
pg.typing.Any().noneable()